/* * File: json_array.c * Author: Arthur Brandao * * Created on 24 novembre 2018 */ #include #include #include #include "json.h" /* --- Fonctions privées parser --- */ /* --- 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 --- */ /* --- Fonctions publiques encoder --- */ void ini_array_encoder(JsonArray* this){ //Initialisation en mode encoder this->mode = JSON_ARRAY_ENCODER; this->encoder = malloc(sizeof(JsonArrayEncoder)); } boolean add_array_value(JsonArray* this, char* str){ //Verification if(!this->mode){ 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); return true; } boolean add_array_string(JsonArray* this, char* val){ //Verification if(!this->mode){ return false; } //Creation chaine int length = strlen(val) + 2 + 1; //val + 2 guillemet + \0 char* str = malloc(length * sizeof(char)); memset(str, 0, length); sprintf(str, "\"%s\"", val); //Ajout add_array_value(this, str); free(str); return true; } boolean add_array_number(JsonArray* this, double ndigit, int val){ //Verification if(!this->mode){ return false; } //Double en string char nombre[20]; memset(nombre, 0, 20); ftoa(val, nombre, ndigit); //Ajout add_array_value(this, nombre); return true; } boolean add_array_integer(JsonArray* this, int val){ //Verification if(!this->mode){ return false; } //Creation chaine int length = ceil(val/10.0) + 1; //val + \0 char* str = malloc(length * sizeof(char)); memset(str, 0, length); sprintf(str, "%d", val); //Ajout add_array_value(this, str); free(str); return true; } boolean add_array_boolean(JsonArray* this, boolean val){ //Verification if(!this->mode){ return false; } //On determine le boolean char bool[6]; if(val){ strcpy(bool, "true"); } else { strcpy(bool, "false"); } //Ajout add_array_value(this, bool); return true; } boolean add_array_array(JsonArray* this, JsonArray* val){ //Verification if(!this->mode){ return false; } //Ajout add_array_value(this, json_encode_array(val)); return true; } boolean add_array_object(JsonArray* this, JsonEncoder* val){ //Verification if(!this->mode){ return false; } //Ajout add_array_value(this, json_encode(val)); return true; } char* json_encode_array(JsonArray* this){ boolean first = true; //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[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; } /* --- Fonctions publiques --- */