Browse Source

Ajout fichier traitement string

Loquicom 6 năm trước cách đây
mục cha
commit
a3625122f4
3 tập tin đã thay đổi với 134 bổ sung2 xóa
  1. 4 2
      makefile
  2. 117 0
      str.c
  3. 13 0
      str.h

+ 4 - 2
makefile

@@ -3,7 +3,7 @@
 #
 
 EXEC = mysh
-OBJETS =
+OBJETS = str.o parser.o
 NOM_PROJET = mini-shell
 
 #
@@ -88,4 +88,6 @@ archive: clean
 	@echo "Termine."
 
 # DEPENDANCES
-mysh.o: mysh.c
+str.o: str.c str.h
+parser.o: parser.c parser.h constante.h str.h
+mysh.o: mysh.c parser.h constante.h str.h

+ 117 - 0
str.c

@@ -0,0 +1,117 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "str.h"
+
+char** str_split(char* str, const char delim, int* length) {
+    char** res;
+    char* signet, * tmp = str;
+    char last = 0;
+    int compteur = 0, nb = 1, taille, count = 0;
+    //Compte le nombre d'element
+    while (*tmp) {
+        if (*tmp == delim && last != delim) {
+            nb++;
+        }
+        last = *tmp;
+        tmp++;
+    }
+    //Creation du tableau
+    res = malloc(sizeof (char*) * (nb + 1));
+    //Decoupage
+    tmp = str;
+    while (*tmp) {
+        //Si c'est le dernier mot
+        if (compteur == nb - 1) {
+            //Ajoute tous ce qui reste
+            res[compteur] = malloc(sizeof (char) * (strlen(str) - count));
+            int i = 0;
+            while(*tmp){
+                res[compteur][i++] = *tmp;
+                tmp++;
+            }
+        } else {
+            //Recup la taille du mot
+            signet = tmp;
+            taille = 0;
+            while (*tmp != delim) {
+                taille++;
+                tmp++;
+                count++;
+            }
+            //Creation du mot
+            res[compteur] = malloc(sizeof (char) * taille);
+            //Ajout du mot
+            for (int i = 0; i < taille; i++) {
+                res[compteur][i] = *signet;
+                signet++;
+            }
+            compteur++;
+            //Passe les delimiteurs consecutif
+            while (*tmp == delim) {
+                tmp++;
+                count++;
+            }
+        }
+    }
+    //Ajoute NULL à la fin
+    res[nb + 1] = NULL;
+    //Retour nombre de mot et tableau
+    *length = nb;
+    return res;
+}
+
+char* trim(char* str){
+    return ltrim(rtrim(str, ' '), ' ');
+}
+
+char* mtrim(char* str, char mask){
+    return ltrim(rtrim(str, mask), mask);
+}
+
+char* ltrim(char* str, char mask){
+    //Variable
+    int cmpt = 0;
+    char* res;
+    //Compte le nombre d'espace
+    while(str[cmpt] == mask){
+        cmpt++;
+    }
+    //Si aucun espace au debut
+    if(cmpt == 0){
+        res = malloc(sizeof(char) * strlen(str));
+        strcpy(res, str);
+        return res;
+    }
+    //Sinon creation nouvelle chaine
+    res = malloc(sizeof(char) * (strlen(str) - cmpt + 1));
+    for(int i = 0, j = cmpt; i < (strlen(str) - cmpt); i++, j++){
+        res[i] = str[j];
+    }
+    //Retour nouvelle chaine
+    return res;
+}
+
+char* rtrim(char* str, char mask){
+    //Variable
+    int cmpt = strlen(str) - 1;
+    char* res;
+    //Compte le nombre d'espace
+    while(str[cmpt] == mask){
+        cmpt--;
+    }
+    //Si aucun espace au debut
+    if(cmpt == strlen(str) - 1){
+        res = malloc(sizeof(char) * strlen(str));
+        strcpy(res, str);
+        return res;
+    }
+    cmpt++;
+    //Sinon creation nouvelle chaine
+    res = malloc(sizeof(char) * (cmpt + 2));
+    for(int i = 0; i < cmpt; i++){
+        res[i] = str[i];
+    }
+    //Retour nouvelle chaine
+    return res;
+}

+ 13 - 0
str.h

@@ -0,0 +1,13 @@
+#ifndef STR_H
+#define STR_H
+
+#include <string.h>
+
+char** str_split(char*, const char, int*);
+char* trim(char*);
+char* mtrim(char*, char);
+char* ltrim(char*, char);
+char* rtrim(char*, char);
+
+#endif /* STR_H */
+