Browse Source

Ajout gestion expression reguliere

Loquicom 6 years ago
parent
commit
318399aa73
3 changed files with 101 additions and 2 deletions
  1. 66 0
      expreg.c
  2. 32 0
      expreg.h
  3. 3 2
      makefile

+ 66 - 0
expreg.c

@@ -0,0 +1,66 @@
+/* 
+ * File:   expreg.c
+ * Author: Arthur Brandao
+ *
+ * Created on 21 décembre 2018
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "expreg.h"
+
+/* --- Fonctions publiques --- */
+boolean ini_expreg(expreg* er, char* str, char* regex) {
+    //Setup regex
+    if (regcomp(&er->regex, regex, REG_EXTENDED) != 0) {
+        return false;
+    }
+    //Copie la chaine
+    int length = strlen(str);
+    er->str = malloc(sizeof (char) * (length + 1));
+    memset(er->str, 0, length + 1);
+    strncpy(er->str, str, length);
+    //Setup structure
+    er->pos = er->str;
+    er->nmatch = er->regex.re_nsub;
+    er->pmatch = malloc(sizeof (regmatch_t) * er->nmatch);
+    return true;
+}
+
+char* get_match_expreg(expreg* er, int* deb, int* fin) {
+    if (regexec(&er->regex, er->pos, er->nmatch, er->pmatch, 0) != 0) {
+        if(deb != NULL){
+        *deb = -1;
+        }
+        if(fin != NULL){
+            *fin = -1;
+        }
+        return NULL;
+    }
+    //Recup info regex
+    int start = er->pmatch[0].rm_so;
+    int end = er->pmatch[0].rm_eo;
+    int length = end - start;
+    //Indique les positions
+    if(deb != NULL){
+        *deb = start;
+    }
+    if(fin != NULL){
+        *fin = end;
+    }
+    //Recup la chaine
+    char* str;
+    str = malloc(sizeof(char) * (length + 1));
+    memset(str, 0, length + 1);
+    strncpy(str, &er->pos[start], length);
+    //On avance dans la chaine
+    er->pos += end;
+    return str;
+}
+
+void clean_expreg(expreg* er) {
+    regfree(&er->regex);
+    free(er->str);
+    free(er->pmatch);
+}

+ 32 - 0
expreg.h

@@ -0,0 +1,32 @@
+/* 
+ * File:   expreg.h
+ * Author: Arthur Brandao
+ *
+ * Created on 21 décembre 2018
+ */
+
+#ifndef EXPREG_H
+#define EXPREG_H
+
+/* --- Include --- */
+#include <regex.h>
+#include "constante.h"
+
+/* --- Structure --- */
+typedef struct{
+    char* str; //La chaine à vérifier
+    char* pos; //Position actuel dans la chaine à verifier
+    regex_t regex;
+    size_t nmatch;
+    regmatch_t* pmatch;
+}expreg;
+
+/* --- Fonctions --- */
+boolean ini_expreg(expreg*, char*, char*);
+
+char* get_match_expreg(expreg*, int*, int*);
+
+void clean_expreg(expreg*);
+
+#endif /* EXPREG_H */
+

+ 3 - 2
makefile

@@ -3,7 +3,7 @@
 #
 
 EXEC = mysh
-OBJETS = error.o str.o parser.o wildcard.o command.o execute.o sem.o shm.o subdiv.o ipc.o
+OBJETS = error.o str.o parser.o wildcard.o command.o execute.o sem.o shm.o subdiv.o ipc.o expreg.o
 NOM_PROJET = mini-shell
 
 #
@@ -107,7 +107,8 @@ command.o: command.c error.h str.h parser.h constante.h mysh.h execute.h \
 execute.o: execute.c error.h execute.h constante.h
 sem.o: sem.c error.h sem.h constante.h
 shm.o: shm.c error.h shm.h constante.h
-subdiv.o: subdiv.c subdiv.h
+subdiv.o: subdiv.c subdiv.h constante.h
 ipc.o: ipc.c ipc.h constante.h sem.h shm.h subdiv.h
+expreg.o: expreg.c expreg.h constante.h
 mysh.o: mysh.c error.h str.h parser.h constante.h command.h execute.h \
  ipc.h sem.h shm.h subdiv.h mysh.h