added allocation and free functions

This commit is contained in:
rexy712
2018-12-01 10:11:20 -08:00
parent 1445d668a5
commit d68bea08f4
4 changed files with 16 additions and 6 deletions

View File

@@ -89,11 +89,14 @@ RJP_value* rjp_parse(const char* str);
//deallocate a json handle
void rjp_free_json(char* json);
void rjp_free(RJP_value* root);
void rjp_free_value(RJP_value* root);
//initialize an empty json handle (outputting to string would result in '{}')
RJP_value* rjp_init_json(void);
char* rjp_alloc(size_t nbytes);
void rjp_free(void* dest);
//add a member to a json object
RJP_value* rjp_add_member(RJP_value* dest, int alloc_key, char* key, size_t keylen, RJP_value value);
//add an element to a json array
@@ -132,6 +135,7 @@ size_t rjp_value_string_length(RJP_value* value);
//copy input string and add json escape sequences
size_t rjp_escape_strcpy(char* dest, const char* src);
//length of string including escape sequences
size_t rjp_escape_strlen(const char* str);

View File

@@ -193,11 +193,11 @@ static void _rjp__free_object_recurse(RJP_value* root){
free(m);
}
}
void rjp_free_json(char* json){
void rjp_free_value_json(char* json){
free(json);
}
//Same as recurse but also frees root node
void rjp_free(RJP_value* root){
void rjp_free_value(RJP_value* root){
if(!root)
return;
_rjp__free_object_recurse(root);
@@ -211,7 +211,7 @@ MAYBE_UNUSED static int _rjp__is_array_empty(RJP_value* curr){
}
#define syntax_error(msg, row, column)\
do{DIAG_PRINT(stderr, "Syntax error! %s (%i:%i)\n", msg, row, column);rjp_free(root);return NULL;}while(0)
do{DIAG_PRINT(stderr, "Syntax error! %s (%i:%i)\n", msg, row, column);rjp_free_value(root);return NULL;}while(0)
#define MAX_DEPTH 16
RJP_value* rjp_parse(const char* str){

View File

@@ -10,8 +10,14 @@ RJP_value* rjp_init_json(void){
ret->type = json_object;
return ret;
}
char* rjp_alloc(size_t nbytes){
return malloc(nbytes);
}
void rjp_free(void* data){
free(data);
}
RJP_value* rjp_add_member(RJP_value* dest, int alloc_key, char* key, size_t keylen, RJP_value value){
if(!keylen && alloc_key)
if(!keylen)
keylen = strlen(key);
if(alloc_key)
_rjp__add_member(&dest->object, key, keylen);

View File

@@ -15,7 +15,7 @@ char* _rjp__parse_string(RJP_value* root, const char* str, int* len, int* row, i
}else if(*(str+*len) == '\0'){
*len = 1;
fprintf(stderr, "Syntax error! %s (%i:%i)\n", "Unexpected EOF in string!", *row, *column);
rjp_free(root);
rjp_free_value(root);
return NULL;
}else if(*(str+*len) == '\n'){
++(*row);