|  | @@ -6,20 +6,19 @@
 | 
											
												
													
														|  |   */
 |  |   */
 | 
											
												
													
														|  |  #include <stdio.h>
 |  |  #include <stdio.h>
 | 
											
												
													
														|  |  #include <stdlib.h>
 |  |  #include <stdlib.h>
 | 
											
												
													
														|  | 
 |  | +#include <math.h>
 | 
											
												
													
														|  |  #include "json.h"
 |  |  #include "json.h"
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |  /* --- Fonctions privée --- */
 |  |  /* --- Fonctions privée --- */
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  | -void add_node(JsonEncoder* this, char* key, char* val){
 |  | 
 | 
											
												
													
														|  | 
 |  | +void add_node(JsonEncoder* this, char* str){
 | 
											
												
													
														|  |      //Création node
 |  |      //Création node
 | 
											
												
													
														|  |      JsonNode* node;
 |  |      JsonNode* node;
 | 
											
												
													
														|  |      node = malloc(sizeof(JsonNode));
 |  |      node = malloc(sizeof(JsonNode));
 | 
											
												
													
														|  |      //Allocation node
 |  |      //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
 |  |      //Initialisation node
 | 
											
												
													
														|  | -    strcpy(node->key, key);
 |  | 
 | 
											
												
													
														|  | -    strcpy(node->val, val);
 |  | 
 | 
											
												
													
														|  | 
 |  | +    strcpy(node->str, str);
 | 
											
												
													
														|  |      //Si 1er node
 |  |      //Si 1er node
 | 
											
												
													
														|  |      if(this->head == NULL){
 |  |      if(this->head == NULL){
 | 
											
												
													
														|  |          this->head = node;
 |  |          this->head = node;
 | 
											
										
											
												
													
														|  | @@ -33,9 +32,7 @@ void add_node(JsonEncoder* this, char* key, char* val){
 | 
											
												
													
														|  |  }
 |  |  }
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |  void delete_node(JsonNode* node){
 |  |  void delete_node(JsonNode* node){
 | 
											
												
													
														|  | -    free(node->key);
 |  | 
 | 
											
												
													
														|  | -    free(node->val);
 |  | 
 | 
											
												
													
														|  | -    free(node);
 |  | 
 | 
											
												
													
														|  | 
 |  | +    free(node->str);
 | 
											
												
													
														|  |  }
 |  |  }
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |  /* --- Fonctions publique --- */
 |  |  /* --- Fonctions publique --- */
 | 
											
										
											
												
													
														|  | @@ -46,34 +43,107 @@ void ini_encoder(JsonEncoder* this){
 | 
											
												
													
														|  |      this->length = 0;
 |  |      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){
 |  |  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;
 | 
											
												
													
														|  |  }
 |  |  }
 |