json_array.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 != JSON_ARRAY_ENCODER) {
  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 != JSON_ARRAY_ENCODER) {
  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 != JSON_ARRAY_ENCODER) {
  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 != JSON_ARRAY_ENCODER) {
  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 != JSON_ARRAY_ENCODER) {
  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 != JSON_ARRAY_ENCODER) {
  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 != JSON_ARRAY_ENCODER) {
  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. //Verification
  142. if (this->mode != JSON_ARRAY_ENCODER) {
  143. return false;
  144. }
  145. boolean first = true;
  146. //Allocation chaine
  147. char* str;
  148. str = malloc((this->encoder->length + 2) * sizeof (char)); // La chaine + []
  149. memset(str, 0, this->encoder->length + 2);
  150. //Creation de la chaine
  151. JsonNode* node;
  152. node = this->encoder->tab;
  153. str[0] = '[';
  154. while (node != NULL) {
  155. if (first) {
  156. sprintf(str, "%s%s", str, node->str);
  157. first = false;
  158. } else {
  159. sprintf(str, "%s, %s", str, node->str);
  160. }
  161. node = node->next;
  162. }
  163. sprintf(str, "%s]", str);
  164. //Retour
  165. return str;
  166. }
  167. /* --- Fonctions publiques --- */
  168. void clean_json_array(JsonArray* this) {
  169. if (this->mode) {
  170. //Encoder
  171. JsonNode* node, * tmp;
  172. node = this->encoder->tab;
  173. while (node != NULL) {
  174. tmp = node->next;
  175. delete_json_array_node(node);
  176. free(node);
  177. node = tmp;
  178. }
  179. //Reset la structure
  180. free(this->encoder);
  181. this->mode = -1;
  182. } else {
  183. //Parser
  184. }
  185. }