diff --git a/include/rjp_internal.h b/include/rjp_internal.h index 6283fc8..5a6022b 100644 --- a/include/rjp_internal.h +++ b/include/rjp_internal.h @@ -46,8 +46,8 @@ typedef struct RJP_object_member{ struct RJP_object_member* next; }RJP_object_member; -void _rjp__add_element(RJP_array* j); -void _rjp__add_member(RJP_object* j, const char* str, size_t len); -void _rjp__add_member_no_alloc(RJP_object* j, char* str, size_t len); +void irjp_add_element(RJP_array* j); +void irjp_add_member(RJP_object* j, const char* str, size_t len); +void irjp_add_member_no_alloc(RJP_object* j, char* str, size_t len); #endif diff --git a/include/strings.h b/include/strings.h index 69dd6a9..46178a2 100644 --- a/include/strings.h +++ b/include/strings.h @@ -22,11 +22,11 @@ #include "rjp.h" #include //size_t -int _rjp__is_whitespace(char c); -char* _rjp__parse_string(RJP_value* root, const char* str, int* inclen, int* len, int* row, int* column); -size_t _rjp__array_strlen(const RJP_value* arr); -size_t _rjp__object_strlen(const RJP_value* root); -size_t _rjp__value_strlen(const RJP_value* root); -void _rjp__strcpy(RJP_string* dest, const RJP_string* src); +int irjp_is_whitespace(char c); +char* irjp_parse_string(RJP_value* root, const char* str, int* inclen, int* len, int* row, int* column); +size_t irjp_array_strlen(const RJP_value* arr); +size_t irjp_object_strlen(const RJP_value* root); +size_t irjp_value_strlen(const RJP_value* root); +void irjp_strcpy(RJP_string* dest, const RJP_string* src); #endif diff --git a/src/input.c b/src/input.c index 17f0248..5f37e21 100644 --- a/src/input.c +++ b/src/input.c @@ -37,7 +37,7 @@ typedef enum json_search_target{ json_target_none }json_search_target; -static RJP_value* _rjp__add_value(RJP_value* curr, RJP_value new_val){ +static RJP_value* irjp_add_value(RJP_value* curr, RJP_value new_val){ new_val.parent = curr; if(!curr){ curr = rjp_calloc(1, sizeof(RJP_value)); @@ -45,7 +45,7 @@ static RJP_value* _rjp__add_value(RJP_value* curr, RJP_value new_val){ return curr; } if(curr->type == json_array){ - _rjp__add_element(&curr->array); + irjp_add_element(&curr->array); curr->array.last->value = new_val; return &curr->array.last->value; } @@ -79,7 +79,7 @@ typedef struct RJP_parse_state{ int* target; }RJP_parse_state; -void _rjp__init_parse_state(RJP_parse_state* state){ +void irjp_init_parse_state(RJP_parse_state* state){ state->root = NULL; state->curr = NULL; state->row = state->column = 0; @@ -95,7 +95,7 @@ static void syntax_error(const char* msg, RJP_parse_state* state){ } //Return number of characters handled while processing comment -int _rjp__handle_comment(const char* str, RJP_parse_state* state){ +int irjp_handle_comment(const char* str, RJP_parse_state* state){ char c = *str; if(state->in_line_comment){ if(c == '\n') @@ -116,7 +116,7 @@ int _rjp__handle_comment(const char* str, RJP_parse_state* state){ } return 0; } -int _rjp__handle_key(const char* str, RJP_parse_state* state){ +int irjp_handle_key(const char* str, RJP_parse_state* state){ char c = *str; //start of key if(c == '"'){ @@ -127,13 +127,13 @@ int _rjp__handle_key(const char* str, RJP_parse_state* state){ int keylen; int inclen; - char* new_string = _rjp__parse_string(state->root, str+1, &inclen, &keylen, &state->row, &state->column); + char* new_string = irjp_parse_string(state->root, str+1, &inclen, &keylen, &state->row, &state->column); if(!new_string){ if(!keylen) syntax_error("Cannot have empty key name!", state); return -1; } - _rjp__add_member_no_alloc(&(state->curr->object), new_string, keylen); + irjp_add_member_no_alloc(&(state->curr->object), new_string, keylen); *state->target = json_target_colon; return inclen+2; //end of this object (object is empty) @@ -144,26 +144,26 @@ int _rjp__handle_key(const char* str, RJP_parse_state* state){ return 1; //unrecognized character - }else if(!_rjp__is_whitespace(c)){ + }else if(!irjp_is_whitespace(c)){ syntax_error("Unexpected character, expected '\"'!", state); return -1; } return 1; } -int _rjp__handle_colon(const char* str, RJP_parse_state* state){ +int irjp_handle_colon(const char* str, RJP_parse_state* state){ char c = *str; //colon after a key if(c == ':'){ *state->target = json_target_value; //unrecognized character - }else if(!_rjp__is_whitespace(c)){ + }else if(!irjp_is_whitespace(c)){ syntax_error( "Unexpected character, expected ':'!", state); return -1; } return 1; } -int _rjp__handle_comma(const char* str, RJP_parse_state* state){ +int irjp_handle_comma(const char* str, RJP_parse_state* state){ char c = *str; //comma separating keys in an object or values in an array if(c == ','){ @@ -182,23 +182,23 @@ int _rjp__handle_comma(const char* str, RJP_parse_state* state){ }else if(c == ']' && state->curr->type == json_array){ state->curr = state->curr->parent; //unrecognized character - }else if(!_rjp__is_whitespace(c)){ + }else if(!irjp_is_whitespace(c)){ syntax_error("Unexpected character, expected ','!", state); return -1; } return 1; } -int _rjp__handle_value(const char* str, RJP_parse_state* state){ +int irjp_handle_value(const char* str, RJP_parse_state* state){ //object char c = *str; if(c == '{'){ if(!state->root){ - state->root = _rjp__add_value(NULL, rjp_object()); + state->root = irjp_add_value(NULL, rjp_object()); state->curr = state->root; *state->target = json_target_key; }else{ - state->curr = _rjp__add_value(state->curr, rjp_object()); + state->curr = irjp_add_value(state->curr, rjp_object()); *state->target = json_target_comma; ++state->target; *state->target = json_target_key; @@ -207,11 +207,11 @@ int _rjp__handle_value(const char* str, RJP_parse_state* state){ } else if(c == '['){ if(!state->root){ - state->root = _rjp__add_value(NULL, rjp_array()); + state->root = irjp_add_value(NULL, rjp_array()); state->curr = state->root; }else{ - state->curr = _rjp__add_value(state->curr, rjp_array()); + state->curr = irjp_add_value(state->curr, rjp_array()); } return 1; } @@ -223,7 +223,7 @@ int _rjp__handle_value(const char* str, RJP_parse_state* state){ //strings else if(c == '"'){ int vallen, inclen; - char* new_string = _rjp__parse_string(state->root, str+1, &inclen, &vallen, &state->row, &state->column); + char* new_string = irjp_parse_string(state->root, str+1, &inclen, &vallen, &state->row, &state->column); if(!new_string){ if(vallen == 0){ new_string = rjp_calloc(1, 1); @@ -231,7 +231,7 @@ int _rjp__handle_value(const char* str, RJP_parse_state* state){ return -1; } } - _rjp__add_value(state->curr, rjp_string(new_string, vallen)); + irjp_add_value(state->curr, rjp_string(new_string, vallen)); *state->target = json_target_comma; return inclen+2; } @@ -263,15 +263,15 @@ int _rjp__handle_value(const char* str, RJP_parse_state* state){ } if(floating){ if(!state->root){ - state->root = state->curr = _rjp__add_value(NULL, rjp_dfloat(strtod(str, NULL))); + state->root = state->curr = irjp_add_value(NULL, rjp_dfloat(strtod(str, NULL))); }else{ - _rjp__add_value(state->curr, rjp_dfloat(strtod(str, NULL))); + irjp_add_value(state->curr, rjp_dfloat(strtod(str, NULL))); } }else{ if(!state->root){ - state->root = state->curr = _rjp__add_value(NULL, rjp_integer(strtoll(str, NULL, 10))); + state->root = state->curr = irjp_add_value(NULL, rjp_integer(strtoll(str, NULL, 10))); }else{ - _rjp__add_value(state->curr, rjp_integer(strtoll(str, NULL, 10))); + irjp_add_value(state->curr, rjp_integer(strtoll(str, NULL, 10))); } } state->column += numlen; @@ -281,36 +281,36 @@ int _rjp__handle_value(const char* str, RJP_parse_state* state){ else if(!strncmp(str, "true", 4)){ if(!state->curr){ *state->target = json_target_none; - state->root = state->curr = _rjp__add_value(state->curr, rjp_boolean(1)); + state->root = state->curr = irjp_add_value(state->curr, rjp_boolean(1)); }else{ *state->target = json_target_comma; - _rjp__add_value(state->curr, rjp_boolean(1)); + irjp_add_value(state->curr, rjp_boolean(1)); } state->column += 3; return 4; }else if(!strncmp(str, "false", 5)){ if(!state->curr){ *state->target = json_target_none; - state->root = state->curr = _rjp__add_value(state->curr, rjp_boolean(0)); + state->root = state->curr = irjp_add_value(state->curr, rjp_boolean(0)); }else{ *state->target = json_target_comma; - _rjp__add_value(state->curr, rjp_boolean(0)); + irjp_add_value(state->curr, rjp_boolean(0)); } state->column += 4; return 5; }else if(!strncmp(str, "null", 4)){ if(!state->curr){ *state->target = json_target_none; - state->root = state->curr = _rjp__add_value(state->curr, rjp_null()); + state->root = state->curr = irjp_add_value(state->curr, rjp_null()); }else{ *state->target = json_target_comma; - _rjp__add_value(state->curr, rjp_null()); + irjp_add_value(state->curr, rjp_null()); } state->column += 3; return 4; } //unrecognized character - else if(!_rjp__is_whitespace(c)){ + else if(!irjp_is_whitespace(c)){ syntax_error("Unexpected character!", state); return -1; } @@ -319,7 +319,7 @@ int _rjp__handle_value(const char* str, RJP_parse_state* state){ RJP_value* rjp_parse(const char* str){ RJP_parse_state state; - _rjp__init_parse_state(&state); + irjp_init_parse_state(&state); //initially search for the root object *state.target = json_target_value; @@ -336,25 +336,25 @@ RJP_value* rjp_parse(const char* str){ ++state.column; } - if((inc = _rjp__handle_comment(str, &state))){ + if((inc = irjp_handle_comment(str, &state))){ continue; } switch(*state.target){ case json_target_key: - inc = _rjp__handle_key(str, &state); + inc = irjp_handle_key(str, &state); break; case json_target_colon: - inc = _rjp__handle_colon(str, &state); + inc = irjp_handle_colon(str, &state); break; case json_target_comma: - inc = _rjp__handle_comma(str, &state); + inc = irjp_handle_comma(str, &state); break; case json_target_value: - inc = _rjp__handle_value(str, &state); + inc = irjp_handle_value(str, &state); break; case json_target_none: - if(!_rjp__is_whitespace(*str)){ + if(!irjp_is_whitespace(*str)){ syntax_error("Unexpected character!", &state); return NULL; } diff --git a/src/memory.c b/src/memory.c index 597197f..228d282 100644 --- a/src/memory.c +++ b/src/memory.c @@ -34,7 +34,7 @@ void rjp_free(void* data){ free(data); } -static void _rjp__copy_object(RJP_value* dest, const RJP_value* src){ +static void irjp_copy_object(RJP_value* dest, const RJP_value* src){ dest->object.members = dest->object.last = 0; for(RJP_value* curr = rjp_get_member(src);curr;curr = rjp_next_member(curr)){ RJP_value copy_mem; @@ -42,7 +42,7 @@ static void _rjp__copy_object(RJP_value* dest, const RJP_value* src){ rjp_add_member(dest, rjp_member_name(curr), rjp_member_name_length(curr), copy_mem); } } -static void _rjp__copy_array(RJP_value* dest, const RJP_value* src){ +static void irjp_copy_array(RJP_value* dest, const RJP_value* src){ dest->array.elements = dest->array.last = 0; for(RJP_value* curr = rjp_get_element(src);curr;curr = rjp_next_element(curr)){ RJP_value copy_mem; @@ -56,13 +56,13 @@ void rjp_copy_value(RJP_value* dest, const RJP_value* src){ switch(src->type){ case json_object: - _rjp__copy_object(dest, src); + irjp_copy_object(dest, src); break; case json_array: - _rjp__copy_array(dest, src); + irjp_copy_array(dest, src); break; case json_string: - _rjp__strcpy(&dest->string, &src->string); + irjp_strcpy(&dest->string, &src->string); break; case json_integer: dest->integer = src->integer; @@ -78,15 +78,15 @@ void rjp_copy_value(RJP_value* dest, const RJP_value* src){ }; } -static void _rjp__free_object_recurse(RJP_value* root); -static void _rjp__free_array(RJP_value* root){ +static void irjp_free_object_recurse(RJP_value* root); +static void irjp_free_array(RJP_value* root){ RJP_array_element* arr = root->array.elements; for(RJP_array_element* i = arr;i != NULL;i = arr){ arr = arr->next; if(i->value.type == json_object){ - _rjp__free_object_recurse(&i->value); + irjp_free_object_recurse(&i->value); }else if(i->value.type == json_array){ - _rjp__free_array(&i->value); + irjp_free_array(&i->value); }else if(i->value.type == json_string){ free(i->value.string.value); } @@ -94,16 +94,16 @@ static void _rjp__free_array(RJP_value* root){ } } //Recursively free JSON objects -static void _rjp__free_object_recurse(RJP_value* root){ +static void irjp_free_object_recurse(RJP_value* root){ RJP_object_member* next; for(RJP_object_member* m = root->object.members;m;m = next){ next = m->next; if(m->value.type == json_object) - _rjp__free_object_recurse(&m->value); + irjp_free_object_recurse(&m->value); else if(m->value.type == json_string) free(m->value.string.value); else if(m->value.type == json_array) - _rjp__free_array(&m->value); + irjp_free_array(&m->value); if(m->name.value) free(m->name.value); free(m); @@ -116,9 +116,9 @@ void rjp_free_value(RJP_value* root){ return; if((root->type) == json_object) - _rjp__free_object_recurse(root); + irjp_free_object_recurse(root); else if((root->type) == json_array) - _rjp__free_array(root); + irjp_free_array(root); free(root); } diff --git a/src/output.c b/src/output.c index 4294ce2..16784f6 100644 --- a/src/output.c +++ b/src/output.c @@ -36,7 +36,7 @@ RJP_value* rjp_init_json_as(RJP_value value){ return ret; } -static size_t _rjp__write_value(char* dest, const RJP_value* val){ +static size_t irjp_write_value(char* dest, const RJP_value* val){ size_t ret; switch(val->type){ case json_integer: @@ -73,7 +73,7 @@ size_t rjp_dump_array(const RJP_value* arr, char* dest){ size_t pos = 1; sprintf(dest, "["); for(;element_list;element_list = element_list->next){ - pos += _rjp__write_value(dest+pos, &element_list->value); + pos += irjp_write_value(dest+pos, &element_list->value); if(element_list->next) pos += sprintf(dest+pos, ","); @@ -92,7 +92,7 @@ size_t rjp_dump_object(const RJP_value* root, char* dest){ for(;member_list;member_list = member_list->next){ pos += sprintf(dest+pos, "\"%s\":", member_list->name.value); - pos += _rjp__write_value(dest+pos, &member_list->value); + pos += irjp_write_value(dest+pos, &member_list->value); if(member_list->next) pos += sprintf(dest+pos, ","); @@ -106,13 +106,13 @@ size_t rjp_dump_object(const RJP_value* root, char* dest){ char* rjp_to_json(const RJP_value* root){ if(!root) return NULL; - size_t len = _rjp__value_strlen(root); + size_t len = irjp_value_strlen(root); if(!len) return NULL; char* tmp = rjp_alloc(len + 1); tmp[len] = 0; - _rjp__write_value(tmp, root); + irjp_write_value(tmp, root); return tmp; } diff --git a/src/rjp.c b/src/rjp.c index 8b49f19..79ef18f 100644 --- a/src/rjp.c +++ b/src/rjp.c @@ -20,7 +20,7 @@ #include //strncpy, strlen -void _rjp__add_element(RJP_array* j){ +void irjp_add_element(RJP_array* j){ ++j->num_elements; if(!j->elements){ j->elements = rjp_calloc(1, sizeof(RJP_array_element)); @@ -31,7 +31,7 @@ void _rjp__add_element(RJP_array* j){ } } //create member of the object as a linked list member and assign a name with name allocation -void _rjp__add_member(RJP_object* j, const char* str, size_t len){ +void irjp_add_member(RJP_object* j, const char* str, size_t len){ ++j->num_members; if(!j->members){ j->members = rjp_calloc(1, sizeof(RJP_object_member)); @@ -45,7 +45,7 @@ void _rjp__add_member(RJP_object* j, const char* str, size_t len){ j->last->name.value[len] = 0; j->last->name.length = len; } -void _rjp__add_member_no_alloc(RJP_object* j, char* str, size_t len){ +void irjp_add_member_no_alloc(RJP_object* j, char* str, size_t len){ ++j->num_members; if(!j->members){ j->members = rjp_calloc(1, sizeof(RJP_object_member)); @@ -61,7 +61,7 @@ void _rjp__add_member_no_alloc(RJP_object* j, char* str, size_t len){ RJP_value* rjp_add_member(RJP_value* dest, const char* key, size_t keylen, RJP_value value){ if(!keylen) keylen = strlen(key); - _rjp__add_member(&dest->object, key, keylen); + irjp_add_member(&dest->object, key, keylen); dest->object.last->value = value; dest->object.last->value.parent = dest; return &dest->object.last->value; @@ -69,13 +69,13 @@ RJP_value* rjp_add_member(RJP_value* dest, const char* key, size_t keylen, RJP_v RJP_value* rjp_add_member_no_alloc(RJP_value* dest, char* key, size_t keylen, RJP_value value){ if(!keylen) keylen = strlen(key); - _rjp__add_member_no_alloc(&dest->object, key, keylen); + irjp_add_member_no_alloc(&dest->object, key, keylen); dest->object.last->value = value; dest->object.last->value.parent = dest; return &dest->object.last->value; } RJP_value* rjp_add_element(RJP_value* dest, RJP_value value){ - _rjp__add_element(&dest->array); + irjp_add_element(&dest->array); dest->array.last->value = value; dest->array.last->value.parent = dest; return &dest->array.last->value; diff --git a/src/strings.c b/src/strings.c index 40a718c..4ec82e5 100644 --- a/src/strings.c +++ b/src/strings.c @@ -28,7 +28,7 @@ #include //strcpy //Determine if the character is valid whitespace -int _rjp__is_whitespace(char c){ +int irjp_is_whitespace(char c){ return c == ' ' || c == '\n' || c == '\r' || c == '\t'; } @@ -151,7 +151,7 @@ static uint32_t u8_to_codepoint(char* u){ } //Convert escape sequences in strings -char* _rjp__parse_string(RJP_value* root, const char* str, int* inclen, int* len, int* row, int* column){ +char* irjp_parse_string(RJP_value* root, const char* str, int* inclen, int* len, int* row, int* column){ char* new_string; ++(*column); //account for starting quotation mark int oldpos = 0; @@ -271,7 +271,7 @@ size_t rjp_escape_strcpy(char* dest, const char* src){ dest[j] = 0; return j; } -void _rjp__strcpy(RJP_string* dest, const RJP_string* src){ +void irjp_strcpy(RJP_string* dest, const RJP_string* src){ dest->value = rjp_alloc(src->length + 1); strcpy(dest->value, src->value); dest->value[src->length] = 0; @@ -297,7 +297,7 @@ size_t rjp_escape_strlen(const char* str){ } return count; } -size_t _rjp__array_strlen(const RJP_value* arr){ +size_t irjp_array_strlen(const RJP_value* arr){ size_t count = 2; //[] const RJP_array* array = &arr->array; const RJP_array_element* element_list = array->elements; @@ -319,10 +319,10 @@ size_t _rjp__array_strlen(const RJP_value* arr){ count += element_list->value.string.length; break; case json_array: - count += _rjp__array_strlen(&element_list->value); + count += irjp_array_strlen(&element_list->value); break; case json_object: - count += _rjp__object_strlen(&element_list->value); + count += irjp_object_strlen(&element_list->value); break; }; if(element_list->next) @@ -333,7 +333,7 @@ size_t _rjp__array_strlen(const RJP_value* arr){ return count; } -size_t _rjp__object_strlen(const RJP_value* root){ +size_t irjp_object_strlen(const RJP_value* root){ size_t count = 2; //{} const RJP_object* root_obj = &root->object; const RJP_object_member* member_list = root_obj->members; @@ -341,7 +341,7 @@ size_t _rjp__object_strlen(const RJP_value* root){ if(member_list->name.length == 0 || member_list->name.value[0] == 0) return 0; count += member_list->name.length + 3; //"": - count += _rjp__value_strlen(&member_list->value); + count += irjp_value_strlen(&member_list->value); if(member_list->next) ++count; //, else @@ -350,7 +350,7 @@ size_t _rjp__object_strlen(const RJP_value* root){ return count; } -size_t _rjp__value_strlen(const RJP_value* root){ +size_t irjp_value_strlen(const RJP_value* root){ switch(root->type){ case json_integer: return snprintf(NULL, 0, "%" PRId64, root->integer); @@ -363,9 +363,9 @@ size_t _rjp__value_strlen(const RJP_value* root){ case json_string: return rjp_escape_strlen(root->string.value) + 2; //""; case json_array: - return _rjp__array_strlen(root); + return irjp_array_strlen(root); case json_object: - return _rjp__object_strlen(root); + return irjp_object_strlen(root); default: return 0; };