|
@@ -74,7 +74,6 @@ int parse_key(JsonParser* this, int index, char* json) {
|
|
return JSON_ERROR;
|
|
return JSON_ERROR;
|
|
}
|
|
}
|
|
//Setup les infos
|
|
//Setup les infos
|
|
- printf("Key[%d] : %d\n", index, compteur);
|
|
|
|
this->key[index] = json - compteur;
|
|
this->key[index] = json - compteur;
|
|
this->key_length[index] = compteur;
|
|
this->key_length[index] = compteur;
|
|
//Sinon retourne ok
|
|
//Sinon retourne ok
|
|
@@ -138,7 +137,6 @@ int parse_val(JsonParser* this, int index, char* json){
|
|
return JSON_ERROR;
|
|
return JSON_ERROR;
|
|
}
|
|
}
|
|
//Setup les infos
|
|
//Setup les infos
|
|
- printf("cpt : %d\n", compteur);
|
|
|
|
this->val[index] = json - compteur;
|
|
this->val[index] = json - compteur;
|
|
this->val_length[index] = compteur;
|
|
this->val_length[index] = compteur;
|
|
this->type[index] = JSON_NUMBER;
|
|
this->type[index] = JSON_NUMBER;
|
|
@@ -170,6 +168,18 @@ int parse_error(JsonParser* this){
|
|
return JSON_ERROR;
|
|
return JSON_ERROR;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+int search_index(JsonParser* this, char* key){
|
|
|
|
+ int length = strlen(key);
|
|
|
|
+ //Cherche la clef
|
|
|
|
+ for(int i = 0; i < this->elt; i++){
|
|
|
|
+ if(strncmp(this->key[i], key, length) == 0){
|
|
|
|
+ return i;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ //Si rien trouver
|
|
|
|
+ return JSON_ERROR;
|
|
|
|
+}
|
|
|
|
+
|
|
/* --- Fonctions publiques --- */
|
|
/* --- Fonctions publiques --- */
|
|
|
|
|
|
int json_parse(JsonParser* this, char* json) {
|
|
int json_parse(JsonParser* this, char* json) {
|
|
@@ -182,6 +192,7 @@ int json_parse(JsonParser* this, char* json) {
|
|
//Allocation de la taille des tableaux
|
|
//Allocation de la taille des tableaux
|
|
this->str = malloc(strlen(json) * sizeof (char));
|
|
this->str = malloc(strlen(json) * sizeof (char));
|
|
strcpy(this->str, json);
|
|
strcpy(this->str, json);
|
|
|
|
+ this->elt = compteur;
|
|
this->key = malloc(compteur * sizeof (char*));
|
|
this->key = malloc(compteur * sizeof (char*));
|
|
this->key_length = malloc(compteur * sizeof (int));
|
|
this->key_length = malloc(compteur * sizeof (int));
|
|
this->val = malloc(compteur * sizeof (char*));
|
|
this->val = malloc(compteur * sizeof (char*));
|
|
@@ -206,7 +217,6 @@ int json_parse(JsonParser* this, char* json) {
|
|
tmp++;
|
|
tmp++;
|
|
while(*tmp == ' ') { tmp++; }
|
|
while(*tmp == ' ') { tmp++; }
|
|
//Lecture valeur
|
|
//Lecture valeur
|
|
- printf("%s\n", tmp);
|
|
|
|
if((temp = parse_val(this, compteur, tmp)) == JSON_ERROR){ return parse_error(this); }
|
|
if((temp = parse_val(this, compteur, tmp)) == JSON_ERROR){ return parse_error(this); }
|
|
tmp += temp;
|
|
tmp += temp;
|
|
key = true;
|
|
key = true;
|
|
@@ -222,6 +232,38 @@ int json_parse(JsonParser* this, char* json) {
|
|
return JSON_OK;
|
|
return JSON_OK;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+int get_type(JsonParser* this, char* key){
|
|
|
|
+ //Recup index
|
|
|
|
+ int index;
|
|
|
|
+ if((index = search_index(this, key)) == JSON_ERROR){
|
|
|
|
+ return JSON_ERROR;
|
|
|
|
+ }
|
|
|
|
+ return this->type[index];
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+char* get(JsonParser* this, char* key){
|
|
|
|
+ //Recup index
|
|
|
|
+ int index;
|
|
|
|
+ if((index = search_index(this, key)) == JSON_ERROR){
|
|
|
|
+ return NULL;
|
|
|
|
+ }
|
|
|
|
+ return get_index(this, index);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+char* get_index(JsonParser* this, int index){
|
|
|
|
+ //Index incorrect
|
|
|
|
+ if(index < 0 || index >= this->elt){
|
|
|
|
+ return NULL;
|
|
|
|
+ }
|
|
|
|
+ //Creation string d'accueil
|
|
|
|
+ char* val;
|
|
|
|
+ val = malloc(this->val_length[index] * sizeof(char));
|
|
|
|
+ //Copie valeur
|
|
|
|
+ strncpy(val, this->val[index], this->val_length[index]);
|
|
|
|
+ //Retour
|
|
|
|
+ return val;
|
|
|
|
+}
|
|
|
|
+
|
|
void clean_json_parser(JsonParser* this) {
|
|
void clean_json_parser(JsonParser* this) {
|
|
free(this->key);
|
|
free(this->key);
|
|
free(this->key_length);
|
|
free(this->key_length);
|