瀏覽代碼

:sparkles: Encodeur fonctionelle

Arthur Brandao 6 年之前
父節點
當前提交
8070495c7c
共有 6 個文件被更改,包括 346 次插入39 次删除
  1. 10 10
      Serveur/json.h
  2. 92 22
      Serveur/json_encoder.c
  3. 36 2
      Serveur/main.c
  4. 6 5
      Serveur/makefile
  5. 180 0
      Serveur/str.c
  6. 22 0
      Serveur/str.h

+ 10 - 10
Serveur/json.h

@@ -8,7 +8,7 @@
 #define JSON_H
 
 /* --- Include --- */
-#include <string.h>
+#include "str.h"
 #include "boolean.h"
 
 /* --- Constante --- */
@@ -21,7 +21,7 @@
 #define JSON_OBJECT 5
 
 /* --- Structure --- */
-typedef JsonNode JsonNode;
+typedef struct JsonNode JsonNode;
 
 typedef struct{
     char* str; //La chaine de carac json
@@ -42,8 +42,7 @@ typedef struct{
 struct JsonNode{
     JsonNode* prev;
     JsonNode* next;
-    char* key;
-    char* val;
+    char* str;
 };
 
 /* --- Fonctions ---- */
@@ -148,12 +147,13 @@ void clean_json_parser(JsonParser*);
 //Json Encoder
 
 void ini_encoder(JsonEncoder*);
-int add_value(JsonEncoder*, char*, char*);
-int add_number(JsonEncoder*, char*, double);
-int add_integer(JsonEncoder*, char*, int);
-int add_boolean(JsonEncoder*, char*, boolean);
-int add_array(JsonEncoder*, char*, char*);
-int add_object(JsonEncoder*, char*, JsonEncoder);
+void add_value(JsonEncoder*, char*);
+void add_string(JsonEncoder*, char*, char*);
+void add_number(JsonEncoder*, char*, double, int);
+void add_integer(JsonEncoder*, char*, int);
+void add_boolean(JsonEncoder*, char*, boolean);
+void add_array(JsonEncoder*, char*, char*);
+void add_object(JsonEncoder*, char*, JsonEncoder);
 char* json_encode(JsonEncoder*);
 void clean_json_encoder(JsonEncoder*);
 

+ 92 - 22
Serveur/json_encoder.c

@@ -6,20 +6,19 @@
  */
 #include <stdio.h>
 #include <stdlib.h>
+#include <math.h>
 #include "json.h"
 
 /* --- Fonctions privée --- */
 
-void add_node(JsonEncoder* this, char* key, char* val){
+void add_node(JsonEncoder* this, char* str){
     //Création node
     JsonNode* node;
     node = malloc(sizeof(JsonNode));
     //Allocation node
-    node->key = malloc(strlen(key) * sizeof(char));
-    node->val = malloc(strlen(val) * sizeof(char));
+    node->str = malloc(strlen(str) * sizeof(char));
     //Initialisation node
-    strcpy(node->key, key);
-    strcpy(node->val, val);
+    strcpy(node->str, str);
     //Si 1er node
     if(this->head == NULL){
         this->head = node;
@@ -33,9 +32,7 @@ void add_node(JsonEncoder* this, char* key, char* val){
 }
 
 void delete_node(JsonNode* node){
-    free(node->key);
-    free(node->val);
-    free(node);
+    free(node->str);
 }
 
 /* --- Fonctions publique --- */
@@ -46,34 +43,107 @@ void ini_encoder(JsonEncoder* this){
     this->length = 0;
 }
 
-int add_value(JsonEncoder* this, char* key, char* val){
-    
+void add_value(JsonEncoder* this, char* str){
+    //Ajoute la longueur de la chaine au total
+    this->length += strlen(str) + 2; //Chaine + ", "
+    //Ajoute le noeud
+    add_node(this, str);
 }
 
-int add_number(JsonEncoder*, double){
-    
+void add_string(JsonEncoder* this, char* key, char* val){
+    //Creation chaine
+    char* str;
+    str = malloc((strlen(key) + strlen(val) + 4 + 2 + 1) * sizeof(char)); //clef + val + 4 guillemet + ": " + \0
+    sprintf(str, "\"%s\": \"%s\"", key, val);
+    //Ajout
+    add_value(this, str);
+    free(str);
 }
 
-int add_integer(JsonEncoder*, int){
-    
+void add_number(JsonEncoder* this, char* key, double val, int ndigit){
+    //Double en string
+    char nombre[20];
+    ftoa(val, nombre, ndigit);
+    //Creation chaine
+    char* str;
+    str = malloc((strlen(key) + strlen(nombre) + 2 + 2 + 1) * sizeof(char)); //clef + val + 2 guillemets + ": " + \0
+    sprintf(str, "\"%s\": %s", key, nombre);
+    //Ajout
+    add_value(this, str);
+    free(str);
 }
 
-int add_boolean(JsonEncoder*, boolean){
-    
+void add_integer(JsonEncoder* this, char* key, int val){
+    //Creation chaine
+    char* str;
+    str = malloc((strlen(key) + ceil(val/10.0) + 2 + 2 + 1) * sizeof(char)); //clef + val + 2 guillemets + ": " + \0
+    sprintf(str, "\"%s\": %d", key, val);
+    //Ajout
+    add_value(this, str);
+    free(str);
 }
 
-int add_array(JsonEncoder*, char*){
-    
+void add_boolean(JsonEncoder* this, char* key, boolean val){
+    //On determine le boolean
+    char bool[6];
+    if(val){
+        strcpy(bool, "true");
+    } else {
+        strcpy(bool, "false");
+    }
+    //Creation chaine
+    char* str;
+    str = malloc((strlen(key) + strlen(bool) + 2 + 2 + 1) * sizeof(char)); //clef + val + 2 guillemets + ": " + \0
+    sprintf(str, "\"%s\": %s", key, bool);
+    //Ajout
+    add_value(this, str);
+    free(str);
 }
 
-int add_object(JsonEncoder*, JsonEncoder){
-    
+void add_array(JsonEncoder* this, char* key, char* val){
+    add_string(this, key, val); //Pas de gestion special
 }
 
-char* json_encode(JsonEncoder*){
+void add_object(JsonEncoder* this, char* key, JsonEncoder val){
     
 }
 
+char* json_encode(JsonEncoder* this){
+    boolean first = true;
+    //Allocation chaine
+    char* str;
+    printf("Length : %d\n", this->length);
+    str = malloc((this->length + 2) * sizeof(char)); // La chaine + {}
+    //Creation de la chaine
+    JsonNode* node;
+    node = this->head;
+    str[0] = '{';
+    while(node != NULL){
+        if(first){
+            sprintf(str, "%s%s", str, node->str);
+            first = false;
+        } else {
+            sprintf(str, "%s, %s", str, node->str);
+        }    
+        node = node->next;
+    }
+    sprintf(str, "%s}", str);
+    //Retour
+    return str;
+}
+
 void clean_json_encoder(JsonEncoder* this){
-    
+    //Parcours les noeuds et les supprimes
+    JsonNode* node, * tmp;
+    node = this->head;
+    while(node != NULL){
+        tmp = node->next;
+        delete_node(node);
+        free(node);
+        node = tmp;
+    }
+    //Reset la structure
+    this->head = NULL;
+    this->tail = NULL;
+    this->length = 0;
 }

+ 36 - 2
Serveur/main.c

@@ -2,7 +2,7 @@
 #include <stdlib.h>
 #include "json.h"
 
-int main(){
+int parse(){
     char str[200];
     char* key = NULL;
     char* val = NULL;
@@ -18,7 +18,7 @@ int main(){
         printf("%s : %s\n", key, val);
     }
     //Recup un nombre
-    printf("Double : %f %f | Int : %d %d\n", get_number(&json, "age"), get_number(&json, "nb"), get_integer(&json, "age"), get_integer(&json, "nb"));
+    printf("Double : %f %.2f | Int : %d %d\n", get_number(&json, "age"), get_number(&json, "nb"), get_integer(&json, "age"), get_integer(&json, "nb"));
     //Recup boolean
     printf("Bool : %d %d\n", get_boolean(&json, "test"), get_boolean(&json, "tab"));
     //Recup obj
@@ -35,4 +35,38 @@ int main(){
     free(val);
     clean_json_parser(&json);
     return 0;
+}
+
+int encode(){
+    //Encode
+    JsonEncoder json;
+    ini_encoder(&json);
+    add_string(&json, "name", "robert");
+    add_number(&json, "nb", 25.698, 2);
+    add_integer(&json, "int", 84);
+    add_string(&json, "aze", "rty");
+    add_boolean(&json, "bool", false);
+    add_value(&json, "\"test\": \"aze\nrty\"");
+    char * str;
+    str = json_encode(&json);
+    printf("%s\n", str);
+    clean_json_encoder(&json);
+    
+    //Decode
+    JsonParser parser;
+    char* key, *val;
+    json_parse(&parser, str);
+    //Affiche toutes les clefs : valeurs
+    for(int i = 0; i < parser.elt; i++){
+        key = key_index(&parser, i);
+        val = get_index(&parser, i);
+        printf("%s : %s\n", key, val);
+    }
+    
+    return 0;
+}
+
+int main(){
+    //return parse();
+    return encode();
 }

+ 6 - 5
Serveur/makefile

@@ -3,7 +3,7 @@
 #
 
 EXEC = main
-OBJETS = json_parser.o json_encoder.o
+OBJETS = str.o json_parser.o json_encoder.o
 NOM_PROJET = Porjet Reseau
 
 #
@@ -27,7 +27,7 @@ CC = gcc
 CCFLAGS_STD = -Wall -O3 -Werror -ansi -pedantic -std=c11
 CCFLAGS_DEBUG = -D _DEBUG_
 CCFLAGS = $(CCFLAGS_STD)
-CCLIBS = -lncurses
+CCLIBS = -lncurses -lm
 
 #
 # REGLES
@@ -88,6 +88,7 @@ archive: clean
 	@echo "Termine."
 
 # DEPENDANCES
-json_parser.o: json_parser.c json.h boolean.h
-json_encoder.o: json_encoder.c json.h boolean.h
-main.o: main.c json.h boolean.h
+str.o: str.c str.h
+json_parser.o: json_parser.c json.h str.h boolean.h
+json_encoder.o: json_encoder.c json.h str.h boolean.h
+main.o: main.c json.h str.h boolean.h

+ 180 - 0
Serveur/str.c

@@ -0,0 +1,180 @@
+/* 
+ * File:   str.c
+ * Author: Arthur Brandao
+ *
+ * Created on 28 octobre 2018
+ */
+#define _POSIX_C_SOURCE 200112L
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <math.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*));
+    //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++;
+            }
+        }
+    }
+    //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){
+        return str;
+    }
+    //Sinon creation nouvelle chaine
+    res = malloc(sizeof(char) * (strlen(str) - cmpt));
+    for(int i = cmpt; i < strlen(str); i++){
+        res[i] = str[cmpt++];
+    }
+    //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){
+        return str;
+    }
+    cmpt++;
+    //Sinon creation nouvelle chaine
+    res = malloc(sizeof(char) * (cmpt + 1));
+    for(int i = 0; i < cmpt; i++){
+        res[i] = str[i];
+    }
+    //Retour nouvelle chaine
+    return res;
+}
+
+// reverses a string 'str' of length 'len' 
+void reverse(char *str, int len) 
+{ 
+    int i=0, j=len-1, temp; 
+    while (i<j) 
+    { 
+        temp = str[i]; 
+        str[i] = str[j]; 
+        str[j] = temp; 
+        i++; j--; 
+    } 
+} 
+
+ // Converts a given integer x to string str[].  d is the number 
+ // of digits required in output. If d is more than the number 
+ // of digits in x, then 0s are added at the beginning. 
+int intToStr(int x, char str[], int d) 
+{ 
+    int i = 0; 
+    while (x) 
+    { 
+        str[i++] = (x%10) + '0'; 
+        x = x/10; 
+    } 
+  
+    // If number of digits required is more, then 
+    // add 0s at the beginning 
+    while (i < d) 
+        str[i++] = '0'; 
+  
+    reverse(str, i); 
+    str[i] = '\0'; 
+    return i; 
+} 
+
+// Converts a floating point number to string. 
+void ftoa(float n, char *res, int afterpoint) 
+{ 
+    // Extract integer part 
+    int ipart = (int)n; 
+  
+    // Extract floating part 
+    float fpart = n - (float)ipart; 
+  
+    // convert integer part to string 
+    int i = intToStr(ipart, res, 0); 
+  
+    // check for display option after point 
+    if (afterpoint != 0) 
+    { 
+        res[i] = '.';  // add dot 
+  
+        // Get the value of fraction part upto given no. 
+        // of points after dot. The third parameter is needed 
+        // to handle cases like 233.007 
+        fpart = fpart * pow(10, afterpoint); 
+  
+        intToStr((int)fpart, res + i + 1, afterpoint); 
+    } 
+} 

+ 22 - 0
Serveur/str.h

@@ -0,0 +1,22 @@
+/* 
+ * File:   str.h
+ * Author: Arthur Brandao
+ *
+ * Created on 28 octobre 2018
+ */
+#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);
+void reverse(char*, int);
+int intToStr(int, char*, int);
+void ftoa(float, char*, int) ;
+
+#endif /* STR_H */
+