From 0b4562a95a31d2968cc76a7c9036ba3d370f4100 Mon Sep 17 00:00:00 2001 From: rexy712 Date: Fri, 17 Jan 2020 13:02:53 -0800 Subject: [PATCH] Update rjp.h types --- include/rjp.h | 61 +++++++++++++++++++++----------------------------- include/tree.h | 18 +++++++++++++++ src/rjp.c | 26 ++++++++++----------- src/tree.c | 18 +++++++++++++++ 4 files changed, 75 insertions(+), 48 deletions(-) diff --git a/include/rjp.h b/include/rjp.h index da40178..b886d44 100644 --- a/include/rjp.h +++ b/include/rjp.h @@ -19,8 +19,6 @@ #ifndef RJP_H #define RJP_H -#include //size_t - #ifdef __cplusplus extern "C"{ #endif @@ -33,7 +31,11 @@ extern "C"{ #ifndef RJP_int #include -#define RJP_int int64_t +#define RJP_int int_fast64_t +#endif +#ifndef RJP_index +#include +#define RJP_index size_t #endif #ifndef RJP_float #include @@ -45,17 +47,6 @@ extern "C"{ #define RJP_FORMAT_PRETTY 1 //type of data -DEPRECATED("Use RJP_data_type instead") -typedef enum RJP_type{ - json_object = 0, - json_string, - json_integer, - json_dfloat, - json_boolean, - json_array, - json_null -}RJP_type; - typedef enum RJP_data_type{ rjp_json_object = 0, rjp_json_string, @@ -70,7 +61,7 @@ typedef enum RJP_data_type{ //Hold a C string and the length excluding NULL terminator typedef struct RJP_string{ char* value; - size_t length; + RJP_index length; }RJP_string; //Forward declarations @@ -89,7 +80,7 @@ typedef struct RJP_object{ typedef struct RJP_array{ struct RJP_array_element* elements; //linked list of elements struct RJP_array_element* last; //final member of linked list - size_t num_elements; + RJP_index num_elements; }RJP_array; //Represents json data @@ -110,8 +101,8 @@ typedef struct RJP_value{ //Result of an rjp search operation typedef struct RJP_search_res{ RJP_value* value; //pointer to match - size_t searchindex; //index in the search list - size_t objindex; //index in the searched object + RJP_index searchindex; //index in the search list + RJP_index objindex; //index in the searched object }RJP_search_res; //Convert C string consisting of json data into RJP's format @@ -129,18 +120,18 @@ void rjp_copy_value(RJP_value* dest, const RJP_value* src); //Allocate heap space for rjp to use. Use if the memory is to be freed by rjp -void* rjp_alloc(size_t nbytes); +void* rjp_alloc(RJP_index nbytes); //Allocate heap space for rjp and initialize to 0. -void* rjp_calloc(size_t num, size_t nbytes); +void* rjp_calloc(RJP_index num, RJP_index nbytes); //Resize heap allocated space for rjp -void* rjp_realloc(void* ptr, size_t nbytes); +void* rjp_realloc(void* ptr, RJP_index nbytes); //Free memory allocated by rjp_alloc or rjp_calloc void rjp_free(void* dest); //add a member to a json object, allocating a copy of the key -RJP_value* rjp_add_member(RJP_value* dest, const char* key, size_t keylen, RJP_value value); +RJP_value* rjp_add_member(RJP_value* dest, const char* key, RJP_index keylen, RJP_value value); //add a member to a json object without allocation. key must be allocated using rjp_alloc/rjp_calloc -RJP_value* rjp_add_member_no_alloc(RJP_value* dest, char* key, size_t keylen, RJP_value value); +RJP_value* rjp_add_member_no_alloc(RJP_value* dest, char* key, RJP_index keylen, RJP_value value); RJP_value* rjp_remove_member_by_key(RJP_value* obj, const char* key); RJP_value* rjp_remove_member(RJP_value* obj, RJP_value* member); @@ -148,9 +139,9 @@ RJP_value* rjp_remove_member(RJP_value* obj, RJP_value* member); RJP_value* rjp_add_element(RJP_value* dest, RJP_value value); //set existing object member's key -void rjp_set_key(RJP_value* dest, const char* key, size_t keylen); +void rjp_set_key(RJP_value* dest, const char* key, RJP_index keylen); //set existing object member's key without allocation. key must be allocated using rjp_alloc/rjp_calloc -void rjp_set_key_no_alloc(RJP_value* dest, char* key, size_t keylen); +void rjp_set_key_no_alloc(RJP_value* dest, char* key, RJP_index keylen); //set existing value void rjp_set_value(RJP_value* dest, RJP_value value); @@ -159,7 +150,7 @@ void rjp_set_value(RJP_value* dest, RJP_value value); RJP_value rjp_integer(RJP_int i); RJP_value rjp_boolean(char b); RJP_value rjp_dfloat(RJP_float d); -RJP_value rjp_string(char* c, size_t len); +RJP_value rjp_string(char* c, RJP_index len); RJP_value rjp_string_copy(const char* c); RJP_value rjp_null(void); RJP_value rjp_object(void); @@ -172,22 +163,22 @@ RJP_value* rjp_object_iterator_current(RJP_object_iterator* it); RJP_value* rjp_object_iterator_next(RJP_object_iterator* it); void rjp_free_object_iterator(RJP_object_iterator* it); //Return number of members in the object -size_t rjp_num_members(const RJP_value* object); +RJP_index rjp_num_members(const RJP_value* object); //Return the object member's key name char* rjp_member_name(const RJP_value* member); //Return the object member's key name length excluding null terminator -size_t rjp_member_name_length(const RJP_value* member); +RJP_index rjp_member_name_length(const RJP_value* member); //Search for an object member with given key RJP_value* rjp_search_member(const RJP_value* object, const char* search); //Search for first occurance of multiple keys. Returns first occurance of each. //Assumes dest is large enough to hold maximum num items. -size_t rjp_search_members(const RJP_value* object, size_t num, const char* const* searches, RJP_value** dest); +RJP_index rjp_search_members(const RJP_value* object, RJP_index num, const char* const* searches, RJP_value** dest); //Access first element of a json array RJP_value* rjp_get_element(const RJP_value* array); //Access next element of a json array given the previous element RJP_value* rjp_next_element(const RJP_value* element); //Return number of elements in the array -size_t rjp_num_elements(const RJP_value* array); +RJP_index rjp_num_elements(const RJP_value* array); //Return handle to parent of given value @@ -201,19 +192,19 @@ RJP_int rjp_value_integer(const RJP_value* value); char rjp_value_boolean(const RJP_value* value); char* rjp_value_string(RJP_value* value); const char* rjp_value_cstring(const RJP_value* value); -size_t rjp_value_string_length(const RJP_value* value); +RJP_index rjp_value_string_length(const RJP_value* value); //copy input string and add json escape sequences -size_t rjp_escape_strcpy(char* dest, const char* src); +RJP_index rjp_escape_strcpy(char* dest, const char* src); //length of string including escape sequences -size_t rjp_escape_strlen(const char* str); +RJP_index rjp_escape_strlen(const char* str); //Convert RJP_object to json string -size_t rjp_dump_object(const RJP_value* root, char* dest); +RJP_index rjp_dump_object(const RJP_value* root, char* dest); //Convert RJP_array to json string -size_t rjp_dump_array(const RJP_value* arr, char* dest); +RJP_index rjp_dump_array(const RJP_value* arr, char* dest); //Convert root rjp value into json string char* rjp_to_json(const RJP_value* root, int pretty); diff --git a/include/tree.h b/include/tree.h index 3944fb5..0257860 100644 --- a/include/tree.h +++ b/include/tree.h @@ -1,3 +1,21 @@ +/** + rjp + Copyright (C) 2018-2019 rexy712 + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + #ifndef RJP_TREE_H #define RJP_TREE_H diff --git a/src/rjp.c b/src/rjp.c index 07299bd..5b2e456 100644 --- a/src/rjp.c +++ b/src/rjp.c @@ -32,7 +32,7 @@ void irjp_add_element(RJP_array* j){ } } -RJP_value* rjp_add_member(RJP_value* dest, const char* key, size_t keylen, RJP_value value){ +RJP_value* rjp_add_member(RJP_value* dest, const char* key, RJP_index keylen, RJP_value value){ if(!keylen) keylen = strlen(key); ++dest->object.num_members; @@ -46,7 +46,7 @@ RJP_value* rjp_add_member(RJP_value* dest, const char* key, size_t keylen, RJP_v int status; return &(dest->object.root = irjp_tree_insert_value(dest->object.root, &mem, &status))->data.value; } -RJP_value* rjp_add_member_no_alloc(RJP_value* dest, char* key, size_t keylen, RJP_value value){ +RJP_value* rjp_add_member_no_alloc(RJP_value* dest, char* key, RJP_index keylen, RJP_value value){ if(!keylen) keylen = strlen(key); RJP_object_member mem; @@ -72,7 +72,7 @@ RJP_value* rjp_add_element(RJP_value* dest, RJP_value value){ dest->array.last->value.parent = dest; return &dest->array.last->value; } -void rjp_set_key(RJP_value* dest, const char* key, size_t keylen){ +void rjp_set_key(RJP_value* dest, const char* key, RJP_index keylen){ RJP_object_member* mem = (RJP_object_member*)dest; if(key){ if(!keylen){ @@ -86,7 +86,7 @@ void rjp_set_key(RJP_value* dest, const char* key, size_t keylen){ mem->name.value[keylen] = 0; mem->name.length = keylen; } -void rjp_set_key_no_alloc(RJP_value* dest, char* key, size_t keylen){ +void rjp_set_key_no_alloc(RJP_value* dest, char* key, RJP_index keylen){ RJP_object_member* mem = (RJP_object_member*)dest; if(key){ if(!keylen){ @@ -112,11 +112,11 @@ RJP_value rjp_boolean(char b){ RJP_value rjp_dfloat(double d){ return (RJP_value){.dfloat = d, .type = rjp_json_dfloat}; } -RJP_value rjp_string(char* c, size_t len){ +RJP_value rjp_string(char* c, RJP_index len){ return (RJP_value){.string = {.value = c, .length = len}, .type = rjp_json_string}; } RJP_value rjp_string_copy(const char* c){ - size_t esclen = rjp_escape_strlen(c); + RJP_index esclen = rjp_escape_strlen(c); char* tmp = rjp_alloc(esclen+1); rjp_escape_strcpy(tmp, c); return (RJP_value){.string = {.value = tmp, .length = esclen}, .type = rjp_json_string}; @@ -156,13 +156,13 @@ void rjp_free_object_iterator(RJP_object_iterator* it){ irjp_delete_object_iterator(it); rjp_free(it); } -size_t rjp_num_members(const RJP_value* object){ +RJP_index rjp_num_members(const RJP_value* object){ return object->object.num_members; } char* rjp_member_name(const RJP_value* member){ return ((RJP_object_member*)member)->name.value; } -size_t rjp_member_name_length(const RJP_value* member){ +RJP_index rjp_member_name_length(const RJP_value* member){ return ((RJP_object_member*)member)->name.length; } @@ -173,10 +173,10 @@ RJP_value* rjp_search_member(const RJP_value* object, const char* search){ return &n->data.value; } -size_t rjp_search_members(const RJP_value* object, size_t num, const char* const* searches, RJP_value** dest){ - size_t matches = 0; +RJP_index rjp_search_members(const RJP_value* object, RJP_index num, const char* const* searches, RJP_value** dest){ + RJP_index matches = 0; - for(size_t i = 0;i < num;++i){ + for(RJP_index i = 0;i < num;++i){ RJP_tree_node* n = irjp_tree_search_value(object->object.root, searches[i]); if(!n){ dest[i] = NULL; @@ -194,7 +194,7 @@ RJP_value* rjp_get_element(const RJP_value* array){ RJP_value* rjp_next_element(const RJP_value* element){ return (RJP_value*)(((RJP_array_element*)element)->next); } -size_t rjp_num_elements(const RJP_value* array){ +RJP_index rjp_num_elements(const RJP_value* array){ return array->array.num_elements; } @@ -219,7 +219,7 @@ char* rjp_value_string(RJP_value* value){ const char* rjp_value_cstring(const RJP_value* value){ return value->string.value; } -size_t rjp_value_string_length(const RJP_value* value){ +RJP_index rjp_value_string_length(const RJP_value* value){ return value->string.length; } diff --git a/src/tree.c b/src/tree.c index 03ff0ac..22d085d 100644 --- a/src/tree.c +++ b/src/tree.c @@ -1,3 +1,21 @@ +/** + rjp + Copyright (C) 2018-2019 rexy712 + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + #include "tree.h" #include