Explorar o código

:bug: Debug wildcard

Loquicom %!s(int64=6) %!d(string=hai) anos
pai
achega
c6b58bf38c
Modificáronse 2 ficheiros con 71 adicións e 20 borrados
  1. 69 18
      wildcard.c
  2. 2 2
      wildcard.h

+ 69 - 18
wildcard.c

@@ -18,12 +18,45 @@
 #include "error.h"
 #include "wildcard.h"
 
-int wildcard_result(const char* seq) {
+/* --- Fonctions privées --- */
+
+/**
+ * Determine la position de separation entre le chemin et la wildcard
+ * @param char* La chaine
+ * @return int Position dans la chaine
+ */
+int wilcard_begin(const char* seq) {
+    int pos = 0, compteur = 1;
+    while (*seq) {
+        if (*seq == '/') {
+            pos = compteur;
+        }
+        seq++;
+        compteur++;
+    }
+    return pos;
+}
+
+/* --- Fonctions publiques --- */
+int wildcard_result(char* seq) {
     //Declaration variable
     struct dirent **namelist;
-    int nbFile, nbRes = 0;
+    int nbFile, nbRes = 0, wcpos;
+    char* wc, *path;
+    //Trouve la wildcard
+    wcpos = wilcard_begin(seq);
+    if (!wcpos) {
+        path = malloc(sizeof (char) * 2);
+        strcpy(path, ".");
+        wc = seq;
+    } else {
+        path = malloc(sizeof (char) * wcpos);
+        memset(path, 0, wcpos);
+        strncpy(path, seq, wcpos - 1); //Pour ne pas prendre le / final
+        wc = seq + wcpos;
+    }
     //Recup la liste des fichiers dans le dossier courant
-    nbFile = scandir(".", &namelist, 0, alphasort);
+    nbFile = scandir(path, &namelist, 0, alphasort);
     if (nbFile < 0) {
         addperror("Erreur scandir()");
         return ERR;
@@ -33,9 +66,9 @@ int wildcard_result(const char* seq) {
         //Si c'est bien un fichier (et non un dossier)
         if (namelist[nbFile]->d_type == DT_REG) {
             //Test par rapport au wildcard
-            if (fnmatch(seq, namelist[nbFile]->d_name, 0) == 0) {
+            if (fnmatch(wc, namelist[nbFile]->d_name, 0) == 0) {
                 //Regarde si le resultat est bien different de la wildcard
-                if (strcmp(seq, namelist[nbFile]->d_name) != 0) {
+                if (strcmp(wc, namelist[nbFile]->d_name) != 0) {
                     nbRes++;
                 }
             }
@@ -47,16 +80,29 @@ int wildcard_result(const char* seq) {
     return nbRes;
 }
 
-int wildcard(const char* seq, int size, char** result) {
+int wildcard(char* seq, int size, char** result) {
     //Declaration variable
     struct dirent **namelist;
-    int nbFile, nbRes = 0;
+    int nbFile, nbRes = 0, wcpos;
+    char* wc, *path;
     //Verification parametre
     if (size < 1) {
-        return ERR;
+        return -1;
+    }
+    //Trouve la wildcard
+    wcpos = wilcard_begin(seq);
+    if (!wcpos) {
+        path = malloc(sizeof (char) * 2);
+        strcpy(path, ".");
+        wc = seq;
+    } else {
+        path = malloc(sizeof (char) * wcpos);
+        memset(path, 0, wcpos);
+        strncpy(path, seq, wcpos - 1);
+        wc = seq + wcpos;
     }
     //Recup la liste des fichiers dans le dossier courant
-    nbFile = scandir(".", &namelist, 0, alphasort);
+    nbFile = scandir(path, &namelist, 0, alphasort);
     if (nbFile < 0) {
         addperror("Erreur scandir()");
         return ERR;
@@ -67,9 +113,17 @@ int wildcard(const char* seq, int size, char** result) {
         //Si c'est bien un fichier (et non un dossier)
         if (namelist[nbFile]->d_type == DT_REG) {
             //Test par rapport au wildcard
-            if (fnmatch(seq, namelist[nbFile]->d_name, 0) == 0) {
-                result[i] = malloc(strlen(namelist[nbFile]->d_name) * sizeof (char));
-                strcpy(result[i++], namelist[nbFile]->d_name);
+            if (fnmatch(wc, namelist[nbFile]->d_name, 0) == 0) {
+                //Si il y a un chemin
+                if (wcpos) {
+                    result[i] = malloc(sizeof (char) * (strlen(path) + strlen(namelist[nbFile]->d_name) + 2));
+                    memset(result[i], 0, strlen(path) + strlen(namelist[nbFile]->d_name) + 2);
+                    sprintf(result[i++], "%s/%s", path, namelist[nbFile]->d_name);
+                } else {
+                    result[i] = malloc(sizeof (char) * (strlen(namelist[nbFile]->d_name) + 1));
+                    memset(result[i], 0, strlen(namelist[nbFile]->d_name) + 1);
+                    sprintf(result[i++], "%s", namelist[nbFile]->d_name);
+                }
                 nbRes++;
             }
         }
@@ -118,8 +172,7 @@ char** insert_array(int pos, char** array, int arraysize, char** insert, int ins
         //Si l'element est null
         if (array[i] == NULL) {
             newarray[i] = NULL;
-        }            
-        //Sinon on le copie
+        }            //Sinon on le copie
         else {
             newarray[i] = malloc(strlen(array[i]) * sizeof (char));
             strcpy(newarray[i], array[i]);
@@ -131,8 +184,7 @@ char** insert_array(int pos, char** array, int arraysize, char** insert, int ins
         //Si l'element est null
         if (insert[j] == NULL) {
             newarray[i] = NULL;
-        }            
-        //Sinon on le copie
+        }            //Sinon on le copie
         else {
             newarray[i] = malloc(strlen(insert[j]) * sizeof (char));
             strcpy(newarray[i], insert[j]);
@@ -144,8 +196,7 @@ char** insert_array(int pos, char** array, int arraysize, char** insert, int ins
         //Si l'element est null
         if (array[j] == NULL) {
             newarray[i] = NULL;
-        }            
-        //Sinon on le copie
+        }            //Sinon on le copie
         else {
             newarray[i] = malloc(strlen(array[j]) * sizeof (char));
             strcpy(newarray[i], array[j]);

+ 2 - 2
wildcard.h

@@ -19,7 +19,7 @@
  * @return int Le nombre de fichiers correspondant (0 si aucun ou si le nom du
  * fichier est identique à la sequence)
  */
-int wildcard_result(const char*);
+int wildcard_result(char*);
 
 /**
  * Recupere les fichiers correspondant à une sequence
@@ -29,7 +29,7 @@ int wildcard_result(const char*);
  * @return int Le nombre d'élement mis dans le tableau (ne depasse jamais la
  * capacité indiqué en paramètre)
  */
-int wildcard(const char*, int, char**);
+int wildcard(char*, int, char**);
 
 /**
  * Insert un tableau à la place d'une valeur d'un autre tableau