Browse Source

:bug: Fin refonte encodeur json

Arthur Brandao 6 years ago
parent
commit
57b56acbdb
3 changed files with 135 additions and 134 deletions
  1. 3 9
      Serveur/json.h
  2. 126 122
      Serveur/json_array.c
  3. 6 3
      Serveur/json_encoder.c

+ 3 - 9
Serveur/json.h

@@ -51,9 +51,9 @@ typedef struct{
 }JsonArrayParser;
 
 typedef struct{
-    JsonNode* tab; //Liste des elements du tableau
-    JsonNode* last; //Dernier noeud ajouté
-    int length; //Taille de la chaine
+    char json[JSON_MAX_SIZE];
+    int max;
+    int pos;
 }JsonArrayEncoder;
 
 typedef struct{
@@ -62,12 +62,6 @@ typedef struct{
     JsonArrayEncoder* encoder;
 }JsonArray;
 
-struct JsonNode{
-    JsonNode* prev;
-    JsonNode* next;
-    char* str;
-};
-
 /* --- Fonctions ---- */
 
 //JsonParser

+ 126 - 122
Serveur/json_array.c

@@ -141,42 +141,6 @@ int parse_array_val(JsonArray* this, int index, char* json) {
     return ++compteur;
 }
 
-/* --- Fonctions privées encoder --- */
-
-/**
- * Ajout un noeud au JsonArrayEncoder
- * @param JsonArrayEncoder* La structure pour encoder 
- * @param char* La chaine à mettre dans le noeud
- */
-void add_json_array_node(JsonArray* this, char* str) {
-    //Création node
-    JsonNode* node;
-    node = malloc(sizeof (JsonNode));
-    //Allocation node
-    int length = strlen(str) + 1;
-    node->str = malloc(length * sizeof (char));
-    memset(node->str, 0, length);
-    strncpy(node->str, str, length - 1);
-    //Si 1er node
-    if (this->encoder->tab == NULL) {
-        this->encoder->tab = node;
-        node->prev = NULL;
-    } else {
-        node->prev = this->encoder->last;
-        node->prev->next = node;
-    }
-    this->encoder->last = node;
-    node->next = NULL;
-}
-
-/**
- * Supprimme un noeud
- * @param JsonNode* Le noeud à supprimer
- */
-void delete_json_array_node(JsonNode* node) {
-    free(node->str);
-}
-
 /* --- Fonctions publiques parser --- */
 
 void ini_array_parser(JsonArray* this) {
@@ -415,10 +379,12 @@ JsonParser* get_array_object(JsonArray* this, int index) {
 /* --- Fonctions publiques encoder --- */
 
 void ini_array_encoder(JsonArray* this) {
-    //Initialisation en mode encoder
     this->mode = JSON_ARRAY_ENCODER;
-    this->encoder = malloc(sizeof (JsonArrayEncoder));
-    this->encoder->tab = NULL;
+    this->encoder = malloc(sizeof(JsonEncoder));
+    this->encoder->max = JSON_MAX_SIZE;
+    this->encoder->pos = 1;
+    memset(this->encoder->json, 0, this->encoder->max);
+    this->encoder->json[0] = '[';
 }
 
 boolean add_array_value(JsonArray* this, char* str) {
@@ -426,10 +392,14 @@ boolean add_array_value(JsonArray* this, char* str) {
     if (this->mode != JSON_ARRAY_ENCODER) {
         return false;
     }
-    //Ajoute la longueur de la chaine au total
-    this->encoder->length += strlen(str) + 2; //Chaine + ", "
-    //Ajoute le noeud
-    add_json_array_node(this, str);
+    //Verif que l'on ne depasse pas (on retire 1 pour garder un \0 et on retire 1 autre pour pouvoir mettre ])
+    if(this->encoder->pos + strlen(str) >= this->encoder->max - 2){
+        return false;
+    }
+    //Ajoute
+    for(int i = 0; i < strlen(str); i++){
+        this->encoder->json[this->encoder->pos++] = str[i];
+    }
     return true;
 }
 
@@ -438,19 +408,22 @@ boolean add_array_string(JsonArray* this, char* val) {
     if (this->mode != JSON_ARRAY_ENCODER) {
         return false;
     }
-    //Creation chaine
-    int index = 0, length = strlen(val) + 2 + 1; //val + 2 guillemet + \0
-    char* str = malloc(length * sizeof (char));
-    memset(str, 0, length);
-    //Copie
-    str[index++] = '"';
-    for(int i = 0; i < strlen(val); i++, index++){
-        str[index] = val[i];
-    }
-    str[index++] = '"';
-    //Ajout
-    add_array_value(this, str);
-    free(str);
+    int length = strlen(val) + 2 + 2; //val + 2 guillemet + ", "
+    //Verif que l'on ne depasse pas
+    if(this->encoder->pos + length >= this->encoder->max - 2){
+        return false;
+    }
+    //Ajoute
+    if(this->encoder->pos != 1){
+        //Si pas 1er valeur on ajoute un separateur
+        this->encoder->json[this->encoder->pos++] = ',';
+        this->encoder->json[this->encoder->pos++] = ' ';
+    }
+    this->encoder->json[this->encoder->pos++] = '"';
+    for(int i = 0; i < strlen(val); i++){
+        this->encoder->json[this->encoder->pos++] = val[i];
+    }
+    this->encoder->json[this->encoder->pos++] = '"';
     return true;
 }
 
@@ -463,8 +436,21 @@ boolean add_array_number(JsonArray* this, double ndigit, int val) {
     char nombre[20];
     memset(nombre, 0, 20);
     ftoa(val, nombre, ndigit);
-    //Ajout
-    add_array_value(this, nombre);
+    //Creation chaine
+    int length = strlen(nombre) + 2; //val + ", "
+    //Verif que l'on ne depasse pas
+    if(this->encoder->pos + length >= this->encoder->max - 2){
+        return false;
+    }
+    //Ajoute
+    if(this->encoder->pos != 1){
+        //Si pas 1er valeur on ajoute un separateur
+        this->encoder->json[this->encoder->pos++] = ',';
+        this->encoder->json[this->encoder->pos++] = ' ';
+    }
+    for(int i = 0; i < strlen(nombre); i++){
+        this->encoder->json[this->encoder->pos++] = nombre[i];
+    }
     return true;
 }
 
@@ -473,14 +459,25 @@ boolean add_array_integer(JsonArray* this, int val) {
     if (this->mode != JSON_ARRAY_ENCODER) {
         return false;
     }
+    //Int en string
+    char nombre[20];
+    memset(nombre, 0, 20);
+    snprintf(nombre, 20, "%d", val);
     //Creation chaine
-    int length = ceil(val / 10.0) + 1; //val + \0
-    char* str = malloc(length * sizeof (char));
-    memset(str, 0, length);
-    snprintf(str, length, "%d", val);
-    //Ajout
-    add_array_value(this, str);
-    free(str);
+    int length = strlen(nombre) + 2; //val + ", "
+    //Verif que l'on ne depasse pas
+    if(this->encoder->pos + length >= this->encoder->max - 2){
+        return false;
+    }
+    //Ajoute
+    if(this->encoder->pos != 1){
+        //Si pas 1er valeur on ajoute un separateur
+        this->encoder->json[this->encoder->pos++] = ',';
+        this->encoder->json[this->encoder->pos++] = ' ';
+    }
+    for(int i = 0; i < strlen(nombre); i++){
+        this->encoder->json[this->encoder->pos++] = nombre[i];
+    }
     return true;
 }
 
@@ -491,95 +488,102 @@ boolean add_array_boolean(JsonArray* this, boolean val) {
     }
     //On determine le boolean
     char bool[6];
+    memset(bool, 0, 6);
     if (val) {
-        strcpy(bool, "true");
+        strncpy(bool, "true", 4);
     } else {
-        strcpy(bool, "false");
+        strncpy(bool, "false", 5);
+    }
+    //Creation chaine
+    int length = strlen(bool) + 2; //val + ", "
+    //Verif que l'on ne depasse pas
+    if(this->encoder->pos + length >= this->encoder->max - 2){
+        return false;
+    }
+    //Ajoute
+    if(this->encoder->pos != 1){
+        //Si pas 1er valeur on ajoute un separateur
+        this->encoder->json[this->encoder->pos++] = ',';
+        this->encoder->json[this->encoder->pos++] = ' ';
+    }
+    for(int i = 0; i < strlen(bool); i++){
+        this->encoder->json[this->encoder->pos++] = bool[i];
     }
-    //Ajout
-    add_array_value(this, bool);
     return true;
 }
 
 boolean add_array_array(JsonArray* this, JsonArray* val) {
-    char* json;
     //Verification
     if (this->mode != JSON_ARRAY_ENCODER) {
         return false;
     }
-    //Ajout
-    json = json_encode_array(val);
-    add_array_value(this, json);
+    //Recup json
+    char* json = json_encode_array(val);
+    //Creation chaine
+    int length = strlen(json) + 2; //val + ", "
+    //Verif que l'on ne depasse pas
+    if(this->encoder->pos + length >= this->encoder->max - 2){
+        return false;
+    }
+    //Ajoute
+    if(this->encoder->pos != 1){
+        //Si pas 1er valeur on ajoute un separateur
+        this->encoder->json[this->encoder->pos++] = ',';
+        this->encoder->json[this->encoder->pos++] = ' ';
+    }
+    for(int i = 0; i < strlen(json); i++){
+        this->encoder->json[this->encoder->pos++] = json[i];
+    }
+    //Nettoayge
     free(json);
     return true;
 }
 
 boolean add_array_object(JsonArray* this, JsonEncoder* val) {
-    char* json;
     //Verification
     if (this->mode != JSON_ARRAY_ENCODER) {
         return false;
     }
-    //Ajout
-    json = json_encode(val);
-    add_array_value(this, json);
+    //Recup json
+    char* json = json_encode(val);
+    //Creation chaine
+    int length = strlen(json) + 2; //val + ", "
+    //Verif que l'on ne depasse pas
+    if(this->encoder->pos + length >= this->encoder->max - 2){
+        return false;
+    }
+    //Ajoute
+    if(this->encoder->pos != 1){
+        //Si pas 1er valeur on ajoute un separateur
+        this->encoder->json[this->encoder->pos++] = ',';
+        this->encoder->json[this->encoder->pos++] = ' ';
+    }
+    for(int i = 0; i < strlen(json); i++){
+        this->encoder->json[this->encoder->pos++] = json[i];
+    }
+    //Nettoayge
     free(json);
     return true;
 }
 
 char* json_encode_array(JsonArray* this) {
-    //Verification
-    if (this->mode != JSON_ARRAY_ENCODER) {
-        return false;
-    }
-    boolean first = true;
-    int length, index = 0;
-    //Allocation chaine
-    char* str;
-    str = malloc((this->encoder->length + 2) * sizeof (char)); // La chaine + []
-    memset(str, 0, this->encoder->length + 2);
-    //Creation de la chaine
-    JsonNode* node;
-    node = this->encoder->tab;
-    str[index++] = '[';
-    while (node != NULL) {
-        if(!first){
-            str[index++] = ',';
-            str[index++] = ' ';
-        }
-        length = strlen(node->str);
-        for(int i = 0; i < length; i++, index++){
-            str[index] = node->str[i];
-        }
-        if(first){
-            first = false;
-        }
-        /*if (first) {
-            sprintf(str, "%s%s", str, node->str);
-            first = false;
-        } else {
-            sprintf(str, "%s, %s", str, node->str);
-        }*/
-        node = node->next;
-    }
-    str[index++] = ']';
-    //Retour
-    return str;
+    char* json;
+    //Ajoute } fin
+    this->encoder->json[this->encoder->pos] = ']';
+    //Creation chaine
+    json = malloc(sizeof(char) * (this->encoder->pos + 2));
+    memset(json, 0, this->encoder->pos + 2);
+    strncpy(json, this->encoder->json, this->encoder->pos + 1);
+    //Retourne
+    return json;
 }
 
 /* --- Fonctions publiques --- */
 
 void clean_json_array(JsonArray* this) {
     if (this->mode == JSON_ARRAY_ENCODER) {
-        JsonNode* node, * tmp;
-        node = this->encoder->tab;
-        while (node != NULL) {
-            tmp = node->next;
-            delete_json_array_node(node);
-            free(node);
-            node = tmp;
-        }
         free(this->encoder);
+        ini_array_encoder(this);
     } else {
         //Parser
         free(this->parser->type);

+ 6 - 3
Serveur/json_encoder.c

@@ -58,6 +58,7 @@ void add_string(JsonEncoder* this, char* key, char* val){
 void add_number(JsonEncoder* this, char* key, double val, int ndigit){
     //Double en string
     char nombre[20];
+    memset(nombre, 0, 20);
     ftoa(val, nombre, ndigit);
     //Creation chaine
     int length = strlen(key) + strlen(nombre) + 2 + 2 + 2; //clef + val + 2 guillemet + ": " + ", "
@@ -86,6 +87,7 @@ void add_number(JsonEncoder* this, char* key, double val, int ndigit){
 void add_integer(JsonEncoder* this, char* key, int val){
     //Double en string
     char nombre[20];
+    memset(nombre, 0, 20);
     snprintf(nombre, 20, "%d", val);
     //Creation chaine
     int length = strlen(key) + strlen(nombre) + 2 + 2 + 2; //clef + val + 2 guillemet + ": " + ", "
@@ -114,10 +116,11 @@ void add_integer(JsonEncoder* this, char* key, int val){
 void add_boolean(JsonEncoder* this, char* key, boolean val){
     //On determine le boolean
     char bool[6];
-    if(val){
-        strcpy(bool, "true");
+    memset(bool, 0, 6);
+    if (val) {
+        strncpy(bool, "true", 4);
     } else {
-        strcpy(bool, "false");
+        strncpy(bool, "false", 5);
     }
     //Creation chaine
     int length = strlen(key) + strlen(bool) + 2 + 2 + 2; //clef + val + 2 guillemet + ": " + ", "