Add function to traverse json tree and free all memory allocations
This commit is contained in:
		| @@ -170,12 +170,12 @@ | ||||
|       "-x", | ||||
|       "c", | ||||
|       "-o", | ||||
|       "/tmp/main-5fdd54.o", | ||||
|       "/tmp/main-4e7ed0.o", | ||||
|       "src/main.c" | ||||
|     ], | ||||
|     "directory": "/mnt/3A5CDF785CDF2CFF/Users/abdoo/dev/say_it_in_json", | ||||
|     "file": "/mnt/3A5CDF785CDF2CFF/Users/abdoo/dev/say_it_in_json/src/main.c", | ||||
|     "output": "/tmp/main-5fdd54.o" | ||||
|     "output": "/tmp/main-4e7ed0.o" | ||||
|   }, | ||||
|   { | ||||
|     "arguments": [ | ||||
| @@ -243,12 +243,12 @@ | ||||
|       "-x", | ||||
|       "c", | ||||
|       "-o", | ||||
|       "/tmp/dstring-157144.o", | ||||
|       "/tmp/dstring-762950.o", | ||||
|       "src/dstring/dstring.c" | ||||
|     ], | ||||
|     "directory": "/mnt/3A5CDF785CDF2CFF/Users/abdoo/dev/say_it_in_json", | ||||
|     "file": "/mnt/3A5CDF785CDF2CFF/Users/abdoo/dev/say_it_in_json/src/dstring/dstring.c", | ||||
|     "output": "/tmp/dstring-157144.o" | ||||
|     "output": "/tmp/dstring-762950.o" | ||||
|   }, | ||||
|   { | ||||
|     "arguments": [ | ||||
| @@ -316,12 +316,12 @@ | ||||
|       "-x", | ||||
|       "c", | ||||
|       "-o", | ||||
|       "/tmp/json_entities-f20100.o", | ||||
|       "/tmp/json_entities-5ce443.o", | ||||
|       "src/json_entities/json_entities.c" | ||||
|     ], | ||||
|     "directory": "/mnt/3A5CDF785CDF2CFF/Users/abdoo/dev/say_it_in_json", | ||||
|     "file": "/mnt/3A5CDF785CDF2CFF/Users/abdoo/dev/say_it_in_json/src/json_entities/json_entities.c", | ||||
|     "output": "/tmp/json_entities-f20100.o" | ||||
|     "output": "/tmp/json_entities-5ce443.o" | ||||
|   }, | ||||
|   { | ||||
|     "arguments": [ | ||||
| @@ -389,12 +389,12 @@ | ||||
|       "-x", | ||||
|       "c", | ||||
|       "-o", | ||||
|       "/tmp/lexer-eb6c2d.o", | ||||
|       "/tmp/lexer-6ba5bd.o", | ||||
|       "src/lexer/lexer.c" | ||||
|     ], | ||||
|     "directory": "/mnt/3A5CDF785CDF2CFF/Users/abdoo/dev/say_it_in_json", | ||||
|     "file": "/mnt/3A5CDF785CDF2CFF/Users/abdoo/dev/say_it_in_json/src/lexer/lexer.c", | ||||
|     "output": "/tmp/lexer-eb6c2d.o" | ||||
|     "output": "/tmp/lexer-6ba5bd.o" | ||||
|   }, | ||||
|   { | ||||
|     "arguments": [ | ||||
| @@ -462,11 +462,11 @@ | ||||
|       "-x", | ||||
|       "c", | ||||
|       "-o", | ||||
|       "/tmp/parser-73724a.o", | ||||
|       "/tmp/parser-95ad8d.o", | ||||
|       "src/parser/parser.c" | ||||
|     ], | ||||
|     "directory": "/mnt/3A5CDF785CDF2CFF/Users/abdoo/dev/say_it_in_json", | ||||
|     "file": "/mnt/3A5CDF785CDF2CFF/Users/abdoo/dev/say_it_in_json/src/parser/parser.c", | ||||
|     "output": "/tmp/parser-73724a.o" | ||||
|     "output": "/tmp/parser-95ad8d.o" | ||||
|   } | ||||
| ] | ||||
|   | ||||
| @@ -65,6 +65,7 @@ struct json_collection { | ||||
| }; | ||||
|  | ||||
| void print_json(const jentity_t *entity, u32 indent); | ||||
| void free_json(jentity_t **entity); | ||||
| jcoll_t *get_collection_from_entity(const jentity_t *entity); | ||||
| jentity_t *create_new_single_entity(const jval_t value, jentity_t *parent); | ||||
| jentity_t *create_new_pair_entity(dstr_t *key, const jval_t value, | ||||
|   | ||||
| @@ -110,6 +110,52 @@ void print_json(const jentity_t *entity, u32 indent) { | ||||
|   } | ||||
| } | ||||
|  | ||||
| void free_json(jentity_t **entity) { | ||||
|   if (!(*entity)) { | ||||
|     return; | ||||
|   } | ||||
|  | ||||
|   jentity_t *entt_ptr = *entity; | ||||
|  | ||||
|   dstr_t *key = NULL; | ||||
|   const jval_t *value = NULL; | ||||
|  | ||||
|   if (entt_ptr->type == JENTITY_SINGLE) { | ||||
|     value = &(entt_ptr->value); | ||||
|   } else { | ||||
|     key = entt_ptr->pair.key; | ||||
|     value = &(entt_ptr->pair.value); | ||||
|   } | ||||
|  | ||||
|   if (key) { | ||||
|     dstr_free(&(entt_ptr->pair.key)); | ||||
|   } | ||||
|  | ||||
|   switch (value->type) { | ||||
|   case JVAL_COLLECTION: | ||||
|     if (value->collection->begin) { | ||||
|       free_json(&(value->collection->begin)); | ||||
|     } | ||||
|  | ||||
|     free(value->collection); | ||||
|  | ||||
|     break; | ||||
|   case JVAL_STRING: | ||||
|     dstr_free(&(entt_ptr->pair.value.string)); | ||||
|  | ||||
|     break; | ||||
|   default: | ||||
|     break; | ||||
|   } | ||||
|  | ||||
|   if (entt_ptr->next) { | ||||
|     free_json(&(entt_ptr->next)); | ||||
|   } | ||||
|  | ||||
|   free(*entity); | ||||
|   *entity = NULL; | ||||
| } | ||||
|  | ||||
| jcoll_t *get_collection_from_entity(const jentity_t *entity) { | ||||
|   return entity->type == JENTITY_SINGLE ? entity->value.collection | ||||
|                                         : entity->pair.value.collection; | ||||
|   | ||||
| @@ -23,5 +23,7 @@ int main(int argc, char *argv[]) { | ||||
|  | ||||
|   print_json(root, 2); | ||||
|  | ||||
|   free_json(&(root)); | ||||
|  | ||||
|   return EXIT_SUCCESS; | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user