|
@@ -19,17 +19,17 @@
|
|
|
* @param JsonArrayEncoder* La structure pour encoder
|
|
|
* @param char* La chaine à mettre dans le noeud
|
|
|
*/
|
|
|
-void add_json_array_node(JsonArray* this, char* str){
|
|
|
+void add_json_array_node(JsonArray* this, char* str) {
|
|
|
//Création node
|
|
|
JsonNode* node;
|
|
|
- node = malloc(sizeof(JsonNode));
|
|
|
+ node = malloc(sizeof (JsonNode));
|
|
|
//Allocation node
|
|
|
int length = strlen(str) + 1;
|
|
|
- node->str = malloc(length * sizeof(char));
|
|
|
+ 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){
|
|
|
+ if (this->encoder->tab == NULL) {
|
|
|
this->encoder->tab = node;
|
|
|
node->prev = NULL;
|
|
|
} else {
|
|
@@ -44,7 +44,7 @@ void add_json_array_node(JsonArray* this, char* str){
|
|
|
* Supprimme un noeud
|
|
|
* @param JsonNode* Le noeud à supprimer
|
|
|
*/
|
|
|
-void delete_json_array_node(JsonNode* node){
|
|
|
+void delete_json_array_node(JsonNode* node) {
|
|
|
free(node->str);
|
|
|
}
|
|
|
|
|
@@ -52,15 +52,15 @@ void delete_json_array_node(JsonNode* node){
|
|
|
|
|
|
/* --- Fonctions publiques encoder --- */
|
|
|
|
|
|
-void ini_array_encoder(JsonArray* this){
|
|
|
+void ini_array_encoder(JsonArray* this) {
|
|
|
//Initialisation en mode encoder
|
|
|
this->mode = JSON_ARRAY_ENCODER;
|
|
|
- this->encoder = malloc(sizeof(JsonArrayEncoder));
|
|
|
+ this->encoder = malloc(sizeof (JsonArrayEncoder));
|
|
|
}
|
|
|
|
|
|
-boolean add_array_value(JsonArray* this, char* str){
|
|
|
+boolean add_array_value(JsonArray* this, char* str) {
|
|
|
//Verification
|
|
|
- if(!this->mode){
|
|
|
+ if (this->mode != JSON_ARRAY_ENCODER) {
|
|
|
return false;
|
|
|
}
|
|
|
//Ajoute la longueur de la chaine au total
|
|
@@ -70,14 +70,14 @@ boolean add_array_value(JsonArray* this, char* str){
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
-boolean add_array_string(JsonArray* this, char* val){
|
|
|
+boolean add_array_string(JsonArray* this, char* val) {
|
|
|
//Verification
|
|
|
- if(!this->mode){
|
|
|
+ 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));
|
|
|
+ char* str = malloc(length * sizeof (char));
|
|
|
memset(str, 0, length);
|
|
|
sprintf(str, "\"%s\"", val);
|
|
|
//Ajout
|
|
@@ -86,9 +86,9 @@ boolean add_array_string(JsonArray* this, char* val){
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
-boolean add_array_number(JsonArray* this, double ndigit, int val){
|
|
|
+boolean add_array_number(JsonArray* this, double ndigit, int val) {
|
|
|
//Verification
|
|
|
- if(!this->mode){
|
|
|
+ if (this->mode != JSON_ARRAY_ENCODER) {
|
|
|
return false;
|
|
|
}
|
|
|
//Double en string
|
|
@@ -100,14 +100,14 @@ boolean add_array_number(JsonArray* this, double ndigit, int val){
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
-boolean add_array_integer(JsonArray* this, int val){
|
|
|
+boolean add_array_integer(JsonArray* this, int val) {
|
|
|
//Verification
|
|
|
- if(!this->mode){
|
|
|
+ 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));
|
|
|
+ int length = ceil(val / 10.0) + 1; //val + \0
|
|
|
+ char* str = malloc(length * sizeof (char));
|
|
|
memset(str, 0, length);
|
|
|
sprintf(str, "%d", val);
|
|
|
//Ajout
|
|
@@ -116,14 +116,14 @@ boolean add_array_integer(JsonArray* this, int val){
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
-boolean add_array_boolean(JsonArray* this, boolean val){
|
|
|
+boolean add_array_boolean(JsonArray* this, boolean val) {
|
|
|
//Verification
|
|
|
- if(!this->mode){
|
|
|
+ if (this->mode != JSON_ARRAY_ENCODER) {
|
|
|
return false;
|
|
|
}
|
|
|
//On determine le boolean
|
|
|
char bool[6];
|
|
|
- if(val){
|
|
|
+ if (val) {
|
|
|
strcpy(bool, "true");
|
|
|
} else {
|
|
|
strcpy(bool, "false");
|
|
@@ -133,9 +133,9 @@ boolean add_array_boolean(JsonArray* this, boolean val){
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
-boolean add_array_array(JsonArray* this, JsonArray* val){
|
|
|
+boolean add_array_array(JsonArray* this, JsonArray* val) {
|
|
|
//Verification
|
|
|
- if(!this->mode){
|
|
|
+ if (this->mode != JSON_ARRAY_ENCODER) {
|
|
|
return false;
|
|
|
}
|
|
|
//Ajout
|
|
@@ -143,10 +143,9 @@ boolean add_array_array(JsonArray* this, JsonArray* val){
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-boolean add_array_object(JsonArray* this, JsonEncoder* val){
|
|
|
+boolean add_array_object(JsonArray* this, JsonEncoder* val) {
|
|
|
//Verification
|
|
|
- if(!this->mode){
|
|
|
+ if (this->mode != JSON_ARRAY_ENCODER) {
|
|
|
return false;
|
|
|
}
|
|
|
//Ajout
|
|
@@ -154,18 +153,22 @@ boolean add_array_object(JsonArray* this, JsonEncoder* val){
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
-char* json_encode_array(JsonArray* this){
|
|
|
+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 + []
|
|
|
+ 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){
|
|
|
+ while (node != NULL) {
|
|
|
+ if (first) {
|
|
|
sprintf(str, "%s%s", str, node->str);
|
|
|
first = false;
|
|
|
} else {
|
|
@@ -178,4 +181,23 @@ char* json_encode_array(JsonArray* this){
|
|
|
return str;
|
|
|
}
|
|
|
|
|
|
-/* --- Fonctions publiques --- */
|
|
|
+/* --- 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
|
|
|
+ }
|
|
|
+}
|