json_array.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * File: json_array.c
  3. * Author: Arthur Brandao
  4. *
  5. * Created on 24 novembre 2018
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <math.h>
  10. #include "json.h"
  11. /* --- Fonctions privées parser --- */
  12. /* --- Fonctions privées encoder --- */
  13. /**
  14. * Ajout un noeud au JsonArrayEncoder
  15. * @param JsonArrayEncoder* La structure pour encoder
  16. * @param char* La chaine à mettre dans le noeud
  17. */
  18. void add_json_array_node(JsonArray* this, char* str){
  19. //Création node
  20. JsonNode* node;
  21. node = malloc(sizeof(JsonNode));
  22. //Allocation node
  23. int length = strlen(str) + 1;
  24. node->str = malloc(length * sizeof(char));
  25. memset(node->str, 0, length);
  26. strncpy(node->str, str, length - 1);
  27. //Si 1er node
  28. if(this->encoder->tab == NULL){
  29. this->encoder->tab = node;
  30. node->prev = NULL;
  31. } else {
  32. node->prev = this->encoder->last;
  33. node->prev->next = node;
  34. }
  35. this->encoder->last = node;
  36. node->next = NULL;
  37. }
  38. /**
  39. * Supprimme un noeud
  40. * @param JsonNode* Le noeud à supprimer
  41. */
  42. void delete_json_array_node(JsonNode* node){
  43. free(node->str);
  44. }
  45. /* --- Fonctions publiques parser --- */
  46. /* --- Fonctions publiques encoder --- */
  47. void ini_array_encoder(JsonArray* this){
  48. //Initialisation en mode encoder
  49. this->mode = JSON_ARRAY_ENCODER;
  50. this->encoder = malloc(sizeof(JsonArrayEncoder));
  51. }
  52. boolean add_array_value(JsonArray* this, char* str){
  53. //Verification
  54. if(!this->mode){
  55. return false;
  56. }
  57. //Ajoute la longueur de la chaine au total
  58. this->encoder->length += strlen(str) + 2; //Chaine + ", "
  59. //Ajoute le noeud
  60. add_json_array_node(this, str);
  61. return true;
  62. }
  63. boolean add_array_string(JsonArray* this, char* val){
  64. //Verification
  65. if(!this->mode){
  66. return false;
  67. }
  68. //Creation chaine
  69. int length = strlen(val) + 2 + 1; //val + 2 guillemet + \0
  70. char* str = malloc(length * sizeof(char));
  71. memset(str, 0, length);
  72. sprintf(str, "\"%s\"", val);
  73. //Ajout
  74. add_array_value(this, str);
  75. free(str);
  76. return true;
  77. }
  78. boolean add_array_number(JsonArray* this, double ndigit, int val){
  79. //Verification
  80. if(!this->mode){
  81. return false;
  82. }
  83. //Double en string
  84. char nombre[20];
  85. memset(nombre, 0, 20);
  86. ftoa(val, nombre, ndigit);
  87. //Ajout
  88. add_array_value(this, nombre);
  89. return true;
  90. }
  91. boolean add_array_integer(JsonArray* this, int val){
  92. //Verification
  93. if(!this->mode){
  94. return false;
  95. }
  96. //Creation chaine
  97. int length = ceil(val/10.0) + 1; //val + \0
  98. char* str = malloc(length * sizeof(char));
  99. memset(str, 0, length);
  100. sprintf(str, "%d", val);
  101. //Ajout
  102. add_array_value(this, str);
  103. free(str);
  104. return true;
  105. }
  106. boolean add_array_boolean(JsonArray* this, boolean val){
  107. //Verification
  108. if(!this->mode){
  109. return false;
  110. }
  111. //On determine le boolean
  112. char bool[6];
  113. if(val){
  114. strcpy(bool, "true");
  115. } else {
  116. strcpy(bool, "false");
  117. }
  118. //Ajout
  119. add_array_value(this, bool);
  120. return true;
  121. }
  122. boolean add_array_array(JsonArray* this, JsonArray* val){
  123. //Verification
  124. if(!this->mode){
  125. return false;
  126. }
  127. //Ajout
  128. add_array_value(this, json_encode_array(val));
  129. return true;
  130. }
  131. boolean add_array_object(JsonArray* this, JsonEncoder* val){
  132. //Verification
  133. if(!this->mode){
  134. return false;
  135. }
  136. //Ajout
  137. add_array_value(this, json_encode(val));
  138. return true;
  139. }
  140. char* json_encode_array(JsonArray* this){
  141. boolean first = true;
  142. //Allocation chaine
  143. char* str;
  144. str = malloc((this->encoder->length + 2) * sizeof(char)); // La chaine + []
  145. memset(str, 0, this->encoder->length + 2);
  146. //Creation de la chaine
  147. JsonNode* node;
  148. node = this->encoder->tab;
  149. str[0] = '[';
  150. while(node != NULL){
  151. if(first){
  152. sprintf(str, "%s%s", str, node->str);
  153. first = false;
  154. } else {
  155. sprintf(str, "%s, %s", str, node->str);
  156. }
  157. node = node->next;
  158. }
  159. sprintf(str, "%s]", str);
  160. //Retour
  161. return str;
  162. }
  163. /* --- Fonctions publiques --- */