json_array.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  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. int val_counter(char* json) {
  13. char* tmp = json + 1;
  14. int nbElt = 0;
  15. boolean empty = true;
  16. //Parours la chaine
  17. while (*tmp != ']' && *tmp) {
  18. //printf("%c\n", *tmp);
  19. fflush(stdout);
  20. //Si on croise autre chose que du vide
  21. if (empty && *tmp != ' ' && *tmp != ']') {
  22. empty = false;
  23. } //Si on croise un tableau ou un objet
  24. else if (*tmp == '[' || *tmp == '{') {
  25. //Saute
  26. int jump;
  27. if (*tmp == '{') {
  28. jump = skip_object(tmp);
  29. } else {
  30. jump = skip_array(tmp);
  31. }
  32. tmp += jump;
  33. //Si se termine mal
  34. if (!*tmp) {
  35. return JSON_ERROR;
  36. }
  37. } //On compte les ',' separatrice
  38. else if (*tmp == ',') {
  39. nbElt++;
  40. }
  41. tmp++;
  42. }
  43. //Si non vide
  44. if (!empty) {
  45. nbElt++;
  46. }
  47. //Retourne le nombre d'element trouvé
  48. return nbElt;
  49. }
  50. /**
  51. * Extraction d'une valeur
  52. * @param JsonParser* La structure pour sauvegarder l'extraction
  53. * @param int La position dans la structure
  54. * @param char* La chaine json
  55. * @return int JSON_ERROR ou le nombre de caractère de la valeur
  56. */
  57. int parse_array_val(JsonArray* this, int index, char* json) {
  58. //Declaration variable
  59. int compteur = 0;
  60. //Regarde le type de valeur
  61. switch (*json) {
  62. //String
  63. case '"':
  64. //Cherche le guillement de fin
  65. json++;
  66. while (*json && *json != '"') {
  67. json++;
  68. compteur++;
  69. }
  70. //Si json mal formé
  71. if (!*json) {
  72. return JSON_ERROR;
  73. }
  74. //Setup les infos
  75. this->parser->val[index] = json - compteur;
  76. this->parser->val_length[index] = compteur;
  77. this->parser->type[index] = JSON_STRING;
  78. break;
  79. //Boolean
  80. case 't':
  81. compteur = 4;
  82. this->parser->val[index] = json;
  83. this->parser->val_length[index] = compteur;
  84. this->parser->type[index] = JSON_BOOLEAN;
  85. break;
  86. case 'f':
  87. compteur = 5;
  88. this->parser->val[index] = json;
  89. this->parser->val_length[index] = compteur;
  90. this->parser->type[index] = JSON_BOOLEAN;
  91. break;
  92. //Nombre
  93. case '-':
  94. case '0':
  95. case '1':
  96. case '2':
  97. case '3':
  98. case '4':
  99. case '5':
  100. case '6':
  101. case '7':
  102. case '8':
  103. case '9':
  104. //Cherche espace de fin ou fin json ou suite json
  105. while (*json && *json != ' ' && *json != ']' && *json != ',') {
  106. json++;
  107. compteur++;
  108. }
  109. //Si json mal formé
  110. if (!*json) {
  111. return JSON_ERROR;
  112. }
  113. //Setup les infos
  114. this->parser->val[index] = json - compteur;
  115. this->parser->val_length[index] = compteur;
  116. this->parser->type[index] = JSON_NUMBER;
  117. break;
  118. //Tableau
  119. case '[':
  120. compteur = skip_array(json) + 1;
  121. this->parser->val[index] = json;
  122. this->parser->val_length[index] = compteur;
  123. this->parser->type[index] = JSON_ARRAY;
  124. break;
  125. //Objet
  126. case '{':
  127. compteur = skip_object(json) + 1;
  128. this->parser->val[index] = json;
  129. this->parser->val_length[index] = compteur;
  130. this->parser->type[index] = JSON_OBJECT;
  131. break;
  132. //Autre
  133. default:
  134. return JSON_ERROR;
  135. }
  136. //Retour
  137. return ++compteur;
  138. }
  139. /* --- Fonctions privées encoder --- */
  140. /**
  141. * Ajout un noeud au JsonArrayEncoder
  142. * @param JsonArrayEncoder* La structure pour encoder
  143. * @param char* La chaine à mettre dans le noeud
  144. */
  145. void add_json_array_node(JsonArray* this, char* str) {
  146. //Création node
  147. JsonNode* node;
  148. node = malloc(sizeof (JsonNode));
  149. //Allocation node
  150. int length = strlen(str) + 1;
  151. node->str = malloc(length * sizeof (char));
  152. memset(node->str, 0, length);
  153. strncpy(node->str, str, length - 1);
  154. //Si 1er node
  155. if (this->encoder->tab == NULL) {
  156. this->encoder->tab = node;
  157. node->prev = NULL;
  158. } else {
  159. node->prev = this->encoder->last;
  160. node->prev->next = node;
  161. }
  162. this->encoder->last = node;
  163. node->next = NULL;
  164. }
  165. /**
  166. * Supprimme un noeud
  167. * @param JsonNode* Le noeud à supprimer
  168. */
  169. void delete_json_array_node(JsonNode* node) {
  170. free(node->str);
  171. }
  172. /* --- Fonctions publiques parser --- */
  173. void ini_array_parser(JsonArray* this) {
  174. //Initialisation en mode encoder
  175. this->mode = JSON_ARRAY_PARSER;
  176. this->parser = malloc(sizeof (JsonArrayParser));
  177. }
  178. int json_parse_array(JsonArray* this, char* json) {
  179. int length, jump, index = 0, compteur = 1;
  180. //Verification
  181. if (this->mode != JSON_ARRAY_PARSER) {
  182. return JSON_ERROR;
  183. }
  184. if (json[0] != '[') {
  185. return JSON_ERROR;
  186. }
  187. //Compte le nombre d'éléments
  188. this->parser->length = val_counter(json);
  189. if (this->parser->length == JSON_ERROR) {
  190. return JSON_ERROR;
  191. }
  192. length = strlen(json) + 1;
  193. this->parser->str = malloc(sizeof (char) * length);
  194. memset(this->parser->str, 0, length);
  195. strncpy(this->parser->str, json, length - 1);
  196. //Si vide
  197. if (this->parser->length == 0) {
  198. return 0;
  199. }
  200. //Initialisation
  201. this->parser->val = malloc(sizeof (char*) * this->parser->length);
  202. this->parser->val_length = malloc(sizeof (int) * this->parser->length);
  203. this->parser->type = malloc(sizeof (int) * this->parser->length);
  204. //Sinon on parse les valeurs
  205. json++;
  206. while (*json != ']' && *json) {
  207. if (*json != ' ' && *json != ',') {
  208. jump = parse_array_val(this, index, this->parser->str + compteur);
  209. if (jump == JSON_ERROR) {
  210. clean_json_array(this);
  211. return JSON_ERROR;
  212. }
  213. json += jump;
  214. compteur += jump;
  215. index++;
  216. }
  217. json++;
  218. compteur++;
  219. }
  220. //Retourne le nombre d'element
  221. return this->parser->length;
  222. }
  223. int get_array_length(JsonArray* this) {
  224. //Verification
  225. if (this->mode != JSON_ARRAY_PARSER) {
  226. return JSON_ERROR;
  227. }
  228. //Retour
  229. return this->parser->length;
  230. }
  231. int get_array_type(JsonArray* this, int index) {
  232. //Verification
  233. if (this->mode != JSON_ARRAY_PARSER) {
  234. return JSON_ERROR;
  235. }
  236. if (index < 0 || index >= this->parser->length) {
  237. return JSON_ERROR;
  238. }
  239. //Retourne le type
  240. return this->parser->type[index];
  241. }
  242. char* get_array_index(JsonArray* this, int index) {
  243. //Verification
  244. if (this->mode != JSON_ARRAY_PARSER) {
  245. return NULL;
  246. }
  247. if (index < 0 || index >= this->parser->length) {
  248. return NULL;
  249. }
  250. //Creation string d'accueil
  251. char* val;
  252. val = malloc((this->parser->val_length[index] + 1) * sizeof (char)); //+1 pour \0
  253. memset(val, 0, this->parser->val_length[index] + 1);
  254. //Copie valeur
  255. strncpy(val, this->parser->val[index], this->parser->val_length[index]);
  256. //Retour
  257. return val;
  258. }
  259. char* get_array_value(JsonArray* this, int index) {
  260. //Verification
  261. if (this->mode != JSON_ARRAY_PARSER) {
  262. return NULL;
  263. }
  264. if (index < 0 || index >= this->parser->length) {
  265. return NULL;
  266. }
  267. //Retour
  268. return get_array_index(this, index);
  269. }
  270. char* get_array_string(JsonArray* this, int index) {
  271. //Verification
  272. if (this->mode != JSON_ARRAY_PARSER) {
  273. return NULL;
  274. }
  275. if (index < 0 || index >= this->parser->length) {
  276. return NULL;
  277. }
  278. //Retour
  279. if (this->parser->type[index] != JSON_STRING) {
  280. return NULL;
  281. }
  282. return get_array_index(this, index);
  283. }
  284. double get_array_number(JsonArray* this, int index) {
  285. //Verification
  286. if (this->mode != JSON_ARRAY_PARSER) {
  287. return JSON_ERROR;
  288. }
  289. if (index < 0 || index >= this->parser->length) {
  290. return JSON_ERROR;
  291. }
  292. //Retour
  293. if (this->parser->type[index] != JSON_NUMBER) {
  294. return JSON_ERROR;
  295. }
  296. char* val;
  297. val = get_array_index(this, index);
  298. double res;
  299. res = atof(val);
  300. free(val);
  301. return res;
  302. }
  303. int get_array_integer(JsonArray* this, int index) {
  304. //Verification
  305. if (this->mode != JSON_ARRAY_PARSER) {
  306. return JSON_ERROR;
  307. }
  308. if (index < 0 || index >= this->parser->length) {
  309. return JSON_ERROR;
  310. }
  311. //Retour
  312. if (this->parser->type[index] != JSON_NUMBER) {
  313. return JSON_ERROR;
  314. }
  315. char* val;
  316. val = get_array_index(this, index);
  317. int res;
  318. res = atoi(val);
  319. free(val);
  320. return res;
  321. }
  322. boolean get_array_boolean(JsonArray* this, int index) {
  323. //Verification
  324. if (this->mode != JSON_ARRAY_PARSER) {
  325. return false;
  326. }
  327. if (index < 0 || index >= this->parser->length) {
  328. return false;
  329. }
  330. //Verif type
  331. if (this->parser->type[index] != JSON_BOOLEAN) {
  332. return false;
  333. }
  334. //Recup valeur
  335. char* val;
  336. val = get_array_index(this, index);
  337. //Cast et retour
  338. if (val[0] == 't') {
  339. return true;
  340. }
  341. return false;
  342. }
  343. JsonArray* get_array_array(JsonArray* this, int index) {
  344. //Verification
  345. if (this->mode != JSON_ARRAY_PARSER) {
  346. return NULL;
  347. }
  348. if (index < 0 || index >= this->parser->length) {
  349. return NULL;
  350. }
  351. //Verif type
  352. if (this->parser->type[index] != JSON_ARRAY) {
  353. return NULL;
  354. }
  355. //Recup valeur
  356. char* val;
  357. val = get_array_index(this, index);
  358. //Parse
  359. JsonArray* json;
  360. json = malloc(sizeof (JsonArray));
  361. ini_array_parser(json);
  362. if (json_parse_array(json, val) == JSON_ERROR) {
  363. return NULL;
  364. }
  365. //Retour JSON utilisable
  366. free(val);
  367. return json;
  368. }
  369. JsonParser* get_array_object(JsonArray* this, int index) {
  370. //Verification
  371. if (this->mode != JSON_ARRAY_PARSER) {
  372. return NULL;
  373. }
  374. if (index < 0 || index >= this->parser->length) {
  375. return NULL;
  376. }
  377. //Verif type
  378. if (this->parser->type[index] != JSON_OBJECT) {
  379. return NULL;
  380. }
  381. //Recup valeur
  382. char* val;
  383. val = get_array_index(this, index);
  384. //Parse
  385. JsonParser* json;
  386. json = malloc(sizeof (JsonParser));
  387. if (json_parse(json, val) == JSON_ERROR) {
  388. return NULL;
  389. }
  390. //Retour JSON utilisable
  391. free(val);
  392. return json;
  393. }
  394. /* --- Fonctions publiques encoder --- */
  395. void ini_array_encoder(JsonArray* this) {
  396. //Initialisation en mode encoder
  397. this->mode = JSON_ARRAY_ENCODER;
  398. this->encoder = malloc(sizeof (JsonArrayEncoder));
  399. this->encoder->tab = NULL;
  400. }
  401. boolean add_array_value(JsonArray* this, char* str) {
  402. //Verification
  403. if (this->mode != JSON_ARRAY_ENCODER) {
  404. return false;
  405. }
  406. //Ajoute la longueur de la chaine au total
  407. this->encoder->length += strlen(str) + 2; //Chaine + ", "
  408. //Ajoute le noeud
  409. add_json_array_node(this, str);
  410. return true;
  411. }
  412. boolean add_array_string(JsonArray* this, char* val) {
  413. //Verification
  414. if (this->mode != JSON_ARRAY_ENCODER) {
  415. return false;
  416. }
  417. //Creation chaine
  418. int length = strlen(val) + 2 + 1; //val + 2 guillemet + \0
  419. char* str = malloc(length * sizeof (char));
  420. memset(str, 0, length);
  421. sprintf(str, "\"%s\"", val);
  422. //Ajout
  423. add_array_value(this, str);
  424. free(str);
  425. return true;
  426. }
  427. boolean add_array_number(JsonArray* this, double ndigit, int val) {
  428. //Verification
  429. if (this->mode != JSON_ARRAY_ENCODER) {
  430. return false;
  431. }
  432. //Double en string
  433. char nombre[20];
  434. memset(nombre, 0, 20);
  435. ftoa(val, nombre, ndigit);
  436. //Ajout
  437. add_array_value(this, nombre);
  438. return true;
  439. }
  440. boolean add_array_integer(JsonArray* this, int val) {
  441. //Verification
  442. if (this->mode != JSON_ARRAY_ENCODER) {
  443. return false;
  444. }
  445. //Creation chaine
  446. int length = ceil(val / 10.0) + 1; //val + \0
  447. char* str = malloc(length * sizeof (char));
  448. memset(str, 0, length);
  449. sprintf(str, "%d", val);
  450. //Ajout
  451. add_array_value(this, str);
  452. free(str);
  453. return true;
  454. }
  455. boolean add_array_boolean(JsonArray* this, boolean val) {
  456. //Verification
  457. if (this->mode != JSON_ARRAY_ENCODER) {
  458. return false;
  459. }
  460. //On determine le boolean
  461. char bool[6];
  462. if (val) {
  463. strcpy(bool, "true");
  464. } else {
  465. strcpy(bool, "false");
  466. }
  467. //Ajout
  468. add_array_value(this, bool);
  469. return true;
  470. }
  471. boolean add_array_array(JsonArray* this, JsonArray* val) {
  472. //Verification
  473. if (this->mode != JSON_ARRAY_ENCODER) {
  474. return false;
  475. }
  476. //Ajout
  477. add_array_value(this, json_encode_array(val));
  478. return true;
  479. }
  480. boolean add_array_object(JsonArray* this, JsonEncoder* val) {
  481. //Verification
  482. if (this->mode != JSON_ARRAY_ENCODER) {
  483. return false;
  484. }
  485. //Ajout
  486. add_array_value(this, json_encode(val));
  487. return true;
  488. }
  489. char* json_encode_array(JsonArray* this) {
  490. //Verification
  491. if (this->mode != JSON_ARRAY_ENCODER) {
  492. return false;
  493. }
  494. boolean first = true;
  495. //Allocation chaine
  496. char* str;
  497. str = malloc((this->encoder->length + 2) * sizeof (char)); // La chaine + []
  498. memset(str, 0, this->encoder->length + 2);
  499. //Creation de la chaine
  500. JsonNode* node;
  501. node = this->encoder->tab;
  502. str[0] = '[';
  503. while (node != NULL) {
  504. if (first) {
  505. sprintf(str, "%s%s", str, node->str);
  506. first = false;
  507. } else {
  508. sprintf(str, "%s, %s", str, node->str);
  509. }
  510. node = node->next;
  511. }
  512. sprintf(str, "%s]", str);
  513. //Retour
  514. return str;
  515. }
  516. /* --- Fonctions publiques --- */
  517. void clean_json_array(JsonArray* this) {
  518. if (this->mode == JSON_ARRAY_ENCODER) {
  519. JsonNode* node, * tmp;
  520. node = this->encoder->tab;
  521. while (node != NULL) {
  522. tmp = node->next;
  523. delete_json_array_node(node);
  524. free(node);
  525. node = tmp;
  526. }
  527. free(this->encoder);
  528. } else {
  529. //Parser
  530. free(this->parser->type);
  531. free(this->parser->val_length);
  532. free(this->parser->val);
  533. free(this->parser->str);
  534. free(this->parser);
  535. }
  536. this->mode = -1;
  537. }