Fix small output bugs. Add ability to add member to object/array in one function call.

This commit is contained in:
rexy712 2020-03-23 17:28:12 -07:00
parent 968e74b182
commit 330b3956e5
7 changed files with 41 additions and 13 deletions

View File

@ -182,9 +182,11 @@ RJP_data_type rjp_value_type(const RJP_value* value);
/***************** OBJECT OPERATIONS *******************/ /***************** OBJECT OPERATIONS *******************/
//add a member to a json object, allocating a copy of the key //add a member to a json object, allocating a copy of the key
RJP_value* rjp_add_member_key_copy(RJP_value* dest, const char* key, RJP_index keylen); RJP_value* rjp_new_member_key_copy(RJP_value* dest, const char* key, RJP_index keylen);
//add a member to a json object without allocation. key must be allocated using rjp_alloc/rjp_calloc //add a member to a json object without allocation. key must be allocated using rjp_alloc/rjp_calloc
RJP_value* rjp_add_member(RJP_value* dest, char* key, RJP_index keylen); RJP_value* rjp_new_member(RJP_value* dest, char* key, RJP_index keylen);
RJP_value* rjp_add_member_key_copy(RJP_value* dest, const char* key, RJP_index keylen, RJP_value* src);
RJP_value* rjp_add_member(RJP_value* dest, char* key, RJP_index keylen, RJP_value* src);
//Remove member without freeing //Remove member without freeing
RJP_value* rjp_remove_member_by_key(RJP_value* obj, const char* key); RJP_value* rjp_remove_member_by_key(RJP_value* obj, const char* key);
@ -218,7 +220,8 @@ RJP_value* rjp_object_iterator_peek(const RJP_object_iterator* it);
/***************** ARRAY OPERATIONS *******************/ /***************** ARRAY OPERATIONS *******************/
//add an element to a json array //add an element to a json array
RJP_value* rjp_add_element(RJP_value* dest); RJP_value* rjp_new_element(RJP_value* dest);
RJP_value* rjp_add_element(RJP_value* dest, RJP_value* src);
RJP_value* rjp_remove_element(RJP_value* arr, RJP_value* elem); RJP_value* rjp_remove_element(RJP_value* arr, RJP_value* elem);
void rjp_free_element(RJP_value* arr, RJP_value* elem); void rjp_free_element(RJP_value* arr, RJP_value* elem);

View File

@ -37,7 +37,7 @@ namespace rjp{
rjp_set_array(m_value); rjp_set_array(m_value);
} }
value array::add(void){ value array::add(void){
RJP_value* newelem = rjp_add_element(m_value); RJP_value* newelem = rjp_new_element(m_value);
return create_unmanaged(newelem); return create_unmanaged(newelem);
} }

View File

@ -36,7 +36,7 @@ namespace rjp{
rjp_set_object(m_value); rjp_set_object(m_value);
} }
member object::add(const rexy::string_base& key){ member object::add(const rexy::string_base& key){
RJP_value* newmemb = rjp_add_member_key_copy(m_value, key.get(), key.length()); RJP_value* newmemb = rjp_new_member_key_copy(m_value, key.get(), key.length());
return create_unmanaged(newmemb); return create_unmanaged(newmemb);
} }

View File

@ -76,6 +76,7 @@ RJP_value* rjp_copy_value(RJP_value* dest, const RJP_value* src){
} }
RJP_value* rjp_move_value(RJP_value* dest, RJP_value* src){ RJP_value* rjp_move_value(RJP_value* dest, RJP_value* src){
*dest = *src; *dest = *src;
src->type = rjp_json_null;
return dest; return dest;
} }
@ -109,7 +110,7 @@ void rjp_free_value(RJP_value* root){
if(!root) if(!root)
return; return;
irjp_delete_value(root); irjp_delete_value(root);
free(root); rjp_free(root);
} }
/* VALUE SETTING */ /* VALUE SETTING */

View File

@ -27,7 +27,7 @@ void irjp_copy_array(RJP_value* dest, const RJP_value* src){
dest->array.elements = dest->array.last = NULL; dest->array.elements = dest->array.last = NULL;
for(RJP_array_element* curr = src->array.elements;curr;curr = curr->next){ for(RJP_array_element* curr = src->array.elements;curr;curr = curr->next){
RJP_value* copy_mem; RJP_value* copy_mem;
copy_mem = rjp_add_element(dest); copy_mem = rjp_new_element(dest);
rjp_copy_value(copy_mem, &curr->value); rjp_copy_value(copy_mem, &curr->value);
} }
} }
@ -42,11 +42,19 @@ void irjp_add_element(RJP_array* j){
j->last->prev = j->last; j->last->prev = j->last;
} }
} }
RJP_value* rjp_add_element(RJP_value* dest){ RJP_value* rjp_new_element(RJP_value* dest){
irjp_add_element(&dest->array); irjp_add_element(&dest->array);
dest->array.last->value.parent = dest; dest->array.last->value.parent = dest;
return &dest->array.last->value; return &dest->array.last->value;
} }
RJP_value* rjp_add_element(RJP_value* dest, RJP_value* src){
RJP_value* newelm = rjp_new_element(dest);
if(!newelm)
return NULL;
rjp_move_value(newelm, src);
rjp_free(src);
return newelm;
}
RJP_value* rjp_remove_element(RJP_value* dest, RJP_value* value){ RJP_value* rjp_remove_element(RJP_value* dest, RJP_value* value){
if(value->parent != dest) if(value->parent != dest)
return NULL; return NULL;

View File

@ -46,7 +46,7 @@ void irjp_delete_object(RJP_value* obj){
default: break; default: break;
}; };
} }
RJP_value* rjp_add_member(RJP_value* dest, char* key, RJP_index keylen){ RJP_value* rjp_new_member(RJP_value* dest, char* key, RJP_index keylen){
switch(dest->type){ switch(dest->type){
case rjp_json_object: case rjp_json_object:
return irjp_add_unordered_member(dest, key, keylen); return irjp_add_unordered_member(dest, key, keylen);
@ -56,13 +56,29 @@ RJP_value* rjp_add_member(RJP_value* dest, char* key, RJP_index keylen){
}; };
return NULL; return NULL;
} }
RJP_value* rjp_add_member_key_copy(RJP_value* dest, const char* key, RJP_index keylen){ RJP_value* rjp_new_member_key_copy(RJP_value* dest, const char* key, RJP_index keylen){
if(!keylen) if(!keylen)
keylen = rjp_escape_strlen(key); keylen = rjp_escape_strlen(key);
char* newkey = rjp_alloc(keylen+1); char* newkey = rjp_alloc(keylen+1);
rjp_escape_strcpy(newkey, key); rjp_escape_strcpy(newkey, key);
newkey[keylen] = 0; newkey[keylen] = 0;
return rjp_add_member(dest, newkey, keylen); return rjp_new_member(dest, newkey, keylen);
}
RJP_value* rjp_add_member(RJP_value* dest, char* key, RJP_index keylen, RJP_value* src){
RJP_value* newval = rjp_new_member(dest, key, keylen);
if(!newval)
return NULL;
rjp_move_value(newval, src);
rjp_free(src);
return newval;
}
RJP_value* rjp_add_member_key_copy(RJP_value* dest, const char* key, RJP_index keylen, RJP_value* src){
RJP_value* newval = rjp_new_member_key_copy(dest, key, keylen);
if(!newval)
return NULL;
rjp_move_value(newval, src);
rjp_free(src);
return newval;
} }
RJP_value* rjp_remove_member_by_key(RJP_value* obj, const char* key){ RJP_value* rjp_remove_member_by_key(RJP_value* obj, const char* key){
RJP_value* member = rjp_search_member(obj, key); RJP_value* member = rjp_search_member(obj, key);

View File

@ -142,7 +142,7 @@ static int irjp_init_value(RJP_value* newval, RJP_lex_category cat, RJP_parse_st
return 0; return 0;
} }
static RJP_value* irjp_add_value_to_array(RJP_lex_category cat, RJP_parse_state* state){ static RJP_value* irjp_add_value_to_array(RJP_lex_category cat, RJP_parse_state* state){
state->lastadded = rjp_add_element(state->curr); state->lastadded = rjp_new_element(state->curr);
if(irjp_init_value(state->lastadded, cat, state)) if(irjp_init_value(state->lastadded, cat, state))
return NULL; return NULL;
return state->lastadded; return state->lastadded;
@ -150,7 +150,7 @@ static RJP_value* irjp_add_value_to_array(RJP_lex_category cat, RJP_parse_state*
static RJP_value* irjp_add_value_to_object(RJP_parse_state* state, const char* key, RJP_index keylen){ static RJP_value* irjp_add_value_to_object(RJP_parse_state* state, const char* key, RJP_index keylen){
RJP_index newlen; RJP_index newlen;
char* newkey = irjp_convert_string(key, keylen, &newlen); char* newkey = irjp_convert_string(key, keylen, &newlen);
return (state->lastadded = rjp_add_member(state->curr, newkey, newlen)); return (state->lastadded = rjp_new_member(state->curr, newkey, newlen));
} }