|
@@ -5,6 +5,141 @@
|
|
|
* Created on 7 novembre 2018
|
|
|
*/
|
|
|
|
|
|
+#define _DEFAULT_SOURCE
|
|
|
+
|
|
|
#include <stdio.h>
|
|
|
#include <stdlib.h>
|
|
|
-#include "wildcard.h"
|
|
|
+#include <dirent.h>
|
|
|
+#include <sys/types.h>
|
|
|
+#include <sys/stat.h>
|
|
|
+#include <unistd.h>
|
|
|
+#include <fnmatch.h>
|
|
|
+#include <string.h>
|
|
|
+#include "wildcard.h"
|
|
|
+
|
|
|
+int wildcard_result(const char* seq) {
|
|
|
+ //Declaration variable
|
|
|
+ struct dirent **namelist;
|
|
|
+ int nbFile, nbRes = 0;
|
|
|
+ //Recup la liste des fichiers dans le dossier courant
|
|
|
+ nbFile = scandir(".", &namelist, 0, alphasort);
|
|
|
+ if (nbFile < 0) {
|
|
|
+ perror("scandir");
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ //Parcours chaque fichier pour compter le nombre de resultat
|
|
|
+ while (nbFile--) {
|
|
|
+ //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) {
|
|
|
+ //Regarde si le resultat est bien different de la wildcard
|
|
|
+ if (strcmp(seq, namelist[nbFile]->d_name) != 0) {
|
|
|
+ nbRes++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ free(namelist[nbFile]);
|
|
|
+ }
|
|
|
+ free(namelist);
|
|
|
+ //Retour
|
|
|
+ return nbRes;
|
|
|
+}
|
|
|
+
|
|
|
+//Sequence + taille du tableau result + tableau de retour vide
|
|
|
+int wildcard(const char* seq, int size, char** result) {
|
|
|
+ //Declaration variable
|
|
|
+ struct dirent **namelist;
|
|
|
+ int nbFile, nbRes = 0;
|
|
|
+ //Verification parametre
|
|
|
+ if (size < 1) {
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ //Recup la liste des fichiers dans le dossier courant
|
|
|
+ nbFile = scandir(".", &namelist, 0, alphasort);
|
|
|
+ if (nbFile < 0) {
|
|
|
+ perror("scandir");
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ //Parcours chaque fichier pour ajouter les resultats à result
|
|
|
+ int i = 0;
|
|
|
+ while (nbFile--) {
|
|
|
+ //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);
|
|
|
+ nbRes++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //Nettoyage
|
|
|
+ free(namelist[nbFile]);
|
|
|
+ //Si il n'y a plus de place dans le tableau on s'arrete
|
|
|
+ if (i == size) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ free(namelist);
|
|
|
+ //Retourne
|
|
|
+ return nbRes;
|
|
|
+}
|
|
|
+
|
|
|
+//La position de la valeur à remplacer, le tableau dans lequel il faut remplacer, sa taille, le tableau à insérer, sa taille, la nouvelle taille
|
|
|
+char** insert_array(int pos, char** array, int arraysize, char** insert, int insertsize, int* newsize) {
|
|
|
+ //Erreur parametre
|
|
|
+ if (pos < 0 || arraysize < 1 || insertsize < 1 || pos >= arraysize) {
|
|
|
+ if (newsize != NULL) {
|
|
|
+ *newsize = -1;
|
|
|
+ }
|
|
|
+ return NULL;
|
|
|
+ }
|
|
|
+ //Si une seul valeur à insérer
|
|
|
+ if (insertsize == 1) {
|
|
|
+ //Vide la chaine precedente
|
|
|
+ free(array[pos]);
|
|
|
+ //Remplace
|
|
|
+ array[pos] = malloc(strlen(insert[0]) * sizeof (char));
|
|
|
+ strcpy(array[pos], insert[0]);
|
|
|
+ //Nettoyage
|
|
|
+ free(insert[0]);
|
|
|
+ free(insert);
|
|
|
+ //Retourne le tableau
|
|
|
+ if (newsize != NULL) {
|
|
|
+ *newsize = arraysize;
|
|
|
+ }
|
|
|
+ return array;
|
|
|
+ }
|
|
|
+ //Sinon generation d'un nouveau tableau
|
|
|
+ char ** newarray;
|
|
|
+ int size = arraysize + insertsize - 1, i;
|
|
|
+ newarray = malloc(sizeof (char*) * size);
|
|
|
+ //Ajout des elements avant la postion
|
|
|
+ for (i = 0; i < pos; i++) {
|
|
|
+ newarray[i] = malloc(strlen(array[i]) * sizeof (char));
|
|
|
+ strcpy(newarray[i], array[i]);
|
|
|
+ free(array[i]);
|
|
|
+ }
|
|
|
+ //Ajout des nouveaux elements
|
|
|
+ for (int j = 0; j < insertsize; j++, i++) {
|
|
|
+ newarray[i] = malloc(strlen(insert[j]) * sizeof (char));
|
|
|
+ strcpy(newarray[i], insert[j]);
|
|
|
+ free(insert[j]);
|
|
|
+ }
|
|
|
+ //Ajout fin
|
|
|
+ for (int j = pos + 1; j < arraysize; j++, i++) {
|
|
|
+ newarray[i] = malloc(strlen(array[j]) * sizeof (char));
|
|
|
+ strcpy(newarray[i], array[j]);
|
|
|
+ free(array[j]);
|
|
|
+ }
|
|
|
+ //Nettoyage et changement tableau
|
|
|
+ free(array[pos]);
|
|
|
+ free(array);
|
|
|
+ free(insert);
|
|
|
+ //Indique la nouvelle taille
|
|
|
+ if (newsize != NULL) {
|
|
|
+ *newsize = size;
|
|
|
+ }
|
|
|
+ //Retourne le nombre d'element
|
|
|
+ return newarray;
|
|
|
+}
|