Pārlūkot izejas kodu

Suppr fichier fonctions globales json

Arthur Brandao 6 gadi atpakaļ
vecāks
revīzija
405f4996f8
3 mainītis faili ar 97 papildinājumiem un 55 dzēšanām
  1. 0 38
      Serveur/json.c
  2. 61 17
      Serveur/json.h
  3. 36 0
      Serveur/json_encoder.c

+ 0 - 38
Serveur/json.c

@@ -1,38 +0,0 @@
-/* 
- * File:   json.c
- * Author: Arthur Brandao
- *
- * Created on 24 novembre 2018
- */
-
-#include <stdio.h>
-#include <stdlib.h>
-#include "json.h"
-
-/* --- Fonctions publiques --- */
-
-void add_json_node(JsonEncoder* 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->head == NULL){
-        this->head = node;
-        node->prev = NULL;
-    } else {
-        node->prev = this->tail;
-        node->prev->next = node;
-    }
-    this->tail = node;
-    node->next = NULL;
-}
-
-
-void delete_json_node(JsonNode* node){
-    free(node->str);
-}

+ 61 - 17
Serveur/json.h

@@ -19,6 +19,8 @@
 #define JSON_BOOLEAN 3
 #define JSON_ARRAY 4
 #define JSON_OBJECT 5
+#define JSON_ARRAY_PARSER 0
+#define JSON_ARRAY_ENCODER 1
 
 /* --- Structure --- */
 typedef struct JsonNode JsonNode;
@@ -65,21 +67,6 @@ struct JsonNode{
 
 /* --- Fonctions ---- */
 
-//Utilitaire
-
-/**
- * Ajout un noeud au JsonEncoder
- * @param JsonEncoder* La structure pour encoder 
- * @param char* La chaine à mettre dans le noeud
- */
-void add_json_node(JsonEncoder*, char*);
-
-/**
- * Supprimme un noeud
- * @param JsonNode* Le noeud à supprimer
- */
-void delete_json_node(JsonNode*);
-
 //JsonParser
 
 /**
@@ -263,9 +250,66 @@ char* json_encode(JsonEncoder*);
  */
 void clean_json_encoder(JsonEncoder*);
 
-//JsonArray
+//JsonArrayParser
 int json_parse_array(JsonArray*, char*);
-void ini_array_encoder(JsonArray*, int);
+
+//JsonArrayEncoder
+boolean ini_array_encoder(JsonArray*, int);
+/**
+ * Ajoute une valeur au JSON
+ * @param JsonEncoder* La structure pour encoder
+ * @param char* La chaine à ajouter (sous la forme "key": val)
+ */
+void add_array_value(JsonArray*, char*);
+
+/**
+ * Ajoute un string au JSON
+ * @param JsonEncoder* La structure pour encoder
+ * @param char* La clef pour acceder à la valeur
+ * @param char* La valeur
+ */
+void add_array_string(JsonArray*, char*, char*);
+
+/**
+ * Ajoute un nombre au JSON
+ * @param JsonEncoder* La structure pour encoder
+ * @param char* La clef pour acceder à la valeur
+ * @param double La valeur
+ * @param int Le nombre de chiffre après la virgule
+ */
+void add_array_number(JsonArray*, char*, double, int);
+
+/**
+ * Ajoute un entier au JSON
+ * @param JsonEncoder* La structure pour encoder
+ * @param char* La clef pour acceder à la valeur
+ * @param int La valeur
+ */
+void add_iarray_nteger(JsonArray*, char*, int);
+
+/**
+ * Ajoute un boolean au JSON
+ * @param JsonEncoder* La structure pour encoder
+ * @param char* La clef pour acceder à la valeur
+ * @param boolean La valeur
+ */
+void add_array_boolean(JsonArray*, char*, boolean);
+
+/**
+ * Ajoute un tableau au JSON
+ * @param JsonEncoder* La structure pour encoder
+ * @param char* La clef pour acceder à la valeur
+ * @param char* La valeur
+ */
+void add_array_array(JsonArray*, char*, JsonArray*);
+
+/**
+ * Ajoute un objet JSON au JSON
+ * @param JsonEncoder* La structure pour encoder
+ * @param char* La clef pour acceder à la valeur
+ * @param JsonEncoder La valeur
+ */
+void add_array_object(JsonArray*, char*, JsonEncoder);
 char* json_encode_array(JsonArray*);
 
 #endif /* JSON_H */

+ 36 - 0
Serveur/json_encoder.c

@@ -9,6 +9,42 @@
 #include <math.h>
 #include "json.h"
 
+/* --- Fonctions privée --- */
+
+/**
+ * Ajout un noeud au JsonEncoder
+ * @param JsonEncoder* La structure pour encoder 
+ * @param char* La chaine à mettre dans le noeud
+ */
+void add_json_node(JsonEncoder* 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->head == NULL){
+        this->head = node;
+        node->prev = NULL;
+    } else {
+        node->prev = this->tail;
+        node->prev->next = node;
+    }
+    this->tail = node;
+    node->next = NULL;
+}
+
+/**
+ * Supprimme un noeud
+ * @param JsonNode* Le noeud à supprimer
+ */
+void delete_json_node(JsonNode* node){
+    free(node->str);
+}
+
 /* --- Fonctions publique --- */
 
 void ini_encoder(JsonEncoder* this){