123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- /*
- * File: json_array.c
- * Author: Arthur Brandao
- *
- * Created on 24 novembre 2018
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #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 != 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);
- return true;
- }
- boolean add_array_string(JsonArray* this, char* val) {
- //Verification
- if (this->mode != JSON_ARRAY_ENCODER) {
- 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 != JSON_ARRAY_ENCODER) {
- 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 != JSON_ARRAY_ENCODER) {
- 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 != JSON_ARRAY_ENCODER) {
- 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 != JSON_ARRAY_ENCODER) {
- 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 != JSON_ARRAY_ENCODER) {
- return false;
- }
- //Ajout
- add_array_value(this, json_encode(val));
- return true;
- }
- char* json_encode_array(JsonArray* this) {
- //Verification
- if (this->mode != JSON_ARRAY_ENCODER) {
- return false;
- }
- 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 --- */
- void clean_json_array(JsonArray* this) {
- if (this->mode) {
- //Encoder
- JsonNode* node, * tmp;
- node = this->encoder->tab;
- while (node != NULL) {
- tmp = node->next;
- delete_json_array_node(node);
- free(node);
- node = tmp;
- }
- //Reset la structure
- free(this->encoder);
- this->mode = -1;
- } else {
- //Parser
- }
- }
|