Update rjp.h types
This commit is contained in:
parent
b5fdb94fd3
commit
0b4562a95a
@ -19,8 +19,6 @@
|
|||||||
#ifndef RJP_H
|
#ifndef RJP_H
|
||||||
#define RJP_H
|
#define RJP_H
|
||||||
|
|
||||||
#include <stddef.h> //size_t
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C"{
|
extern "C"{
|
||||||
#endif
|
#endif
|
||||||
@ -33,7 +31,11 @@ extern "C"{
|
|||||||
|
|
||||||
#ifndef RJP_int
|
#ifndef RJP_int
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#define RJP_int int64_t
|
#define RJP_int int_fast64_t
|
||||||
|
#endif
|
||||||
|
#ifndef RJP_index
|
||||||
|
#include <stddef.h>
|
||||||
|
#define RJP_index size_t
|
||||||
#endif
|
#endif
|
||||||
#ifndef RJP_float
|
#ifndef RJP_float
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
@ -45,17 +47,6 @@ extern "C"{
|
|||||||
#define RJP_FORMAT_PRETTY 1
|
#define RJP_FORMAT_PRETTY 1
|
||||||
|
|
||||||
//type of data
|
//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{
|
typedef enum RJP_data_type{
|
||||||
rjp_json_object = 0,
|
rjp_json_object = 0,
|
||||||
rjp_json_string,
|
rjp_json_string,
|
||||||
@ -70,7 +61,7 @@ typedef enum RJP_data_type{
|
|||||||
//Hold a C string and the length excluding NULL terminator
|
//Hold a C string and the length excluding NULL terminator
|
||||||
typedef struct RJP_string{
|
typedef struct RJP_string{
|
||||||
char* value;
|
char* value;
|
||||||
size_t length;
|
RJP_index length;
|
||||||
}RJP_string;
|
}RJP_string;
|
||||||
|
|
||||||
//Forward declarations
|
//Forward declarations
|
||||||
@ -89,7 +80,7 @@ typedef struct RJP_object{
|
|||||||
typedef struct RJP_array{
|
typedef struct RJP_array{
|
||||||
struct RJP_array_element* elements; //linked list of elements
|
struct RJP_array_element* elements; //linked list of elements
|
||||||
struct RJP_array_element* last; //final member of linked list
|
struct RJP_array_element* last; //final member of linked list
|
||||||
size_t num_elements;
|
RJP_index num_elements;
|
||||||
}RJP_array;
|
}RJP_array;
|
||||||
|
|
||||||
//Represents json data
|
//Represents json data
|
||||||
@ -110,8 +101,8 @@ typedef struct RJP_value{
|
|||||||
//Result of an rjp search operation
|
//Result of an rjp search operation
|
||||||
typedef struct RJP_search_res{
|
typedef struct RJP_search_res{
|
||||||
RJP_value* value; //pointer to match
|
RJP_value* value; //pointer to match
|
||||||
size_t searchindex; //index in the search list
|
RJP_index searchindex; //index in the search list
|
||||||
size_t objindex; //index in the searched object
|
RJP_index objindex; //index in the searched object
|
||||||
}RJP_search_res;
|
}RJP_search_res;
|
||||||
|
|
||||||
//Convert C string consisting of json data into RJP's format
|
//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
|
//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.
|
//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
|
//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
|
//Free memory allocated by rjp_alloc or rjp_calloc
|
||||||
void rjp_free(void* dest);
|
void rjp_free(void* dest);
|
||||||
|
|
||||||
//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(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
|
//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_by_key(RJP_value* obj, const char* key);
|
||||||
RJP_value* rjp_remove_member(RJP_value* obj, RJP_value* member);
|
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);
|
RJP_value* rjp_add_element(RJP_value* dest, RJP_value value);
|
||||||
|
|
||||||
//set existing object member's key
|
//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
|
//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
|
//set existing value
|
||||||
void rjp_set_value(RJP_value* dest, RJP_value 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_integer(RJP_int i);
|
||||||
RJP_value rjp_boolean(char b);
|
RJP_value rjp_boolean(char b);
|
||||||
RJP_value rjp_dfloat(RJP_float d);
|
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_string_copy(const char* c);
|
||||||
RJP_value rjp_null(void);
|
RJP_value rjp_null(void);
|
||||||
RJP_value rjp_object(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);
|
RJP_value* rjp_object_iterator_next(RJP_object_iterator* it);
|
||||||
void rjp_free_object_iterator(RJP_object_iterator* it);
|
void rjp_free_object_iterator(RJP_object_iterator* it);
|
||||||
//Return number of members in the object
|
//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
|
//Return the object member's key name
|
||||||
char* rjp_member_name(const RJP_value* member);
|
char* rjp_member_name(const RJP_value* member);
|
||||||
//Return the object member's key name length excluding null terminator
|
//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
|
//Search for an object member with given key
|
||||||
RJP_value* rjp_search_member(const RJP_value* object, const char* search);
|
RJP_value* rjp_search_member(const RJP_value* object, const char* search);
|
||||||
//Search for first occurance of multiple keys. Returns first occurance of each.
|
//Search for first occurance of multiple keys. Returns first occurance of each.
|
||||||
//Assumes dest is large enough to hold maximum num items.
|
//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
|
//Access first element of a json array
|
||||||
RJP_value* rjp_get_element(const RJP_value* array);
|
RJP_value* rjp_get_element(const RJP_value* array);
|
||||||
//Access next element of a json array given the previous element
|
//Access next element of a json array given the previous element
|
||||||
RJP_value* rjp_next_element(const RJP_value* element);
|
RJP_value* rjp_next_element(const RJP_value* element);
|
||||||
//Return number of elements in the array
|
//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
|
//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_boolean(const RJP_value* value);
|
||||||
char* rjp_value_string(RJP_value* value);
|
char* rjp_value_string(RJP_value* value);
|
||||||
const char* rjp_value_cstring(const 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
|
//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
|
//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
|
//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
|
//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
|
//Convert root rjp value into json string
|
||||||
char* rjp_to_json(const RJP_value* root, int pretty);
|
char* rjp_to_json(const RJP_value* root, int pretty);
|
||||||
|
|
||||||
|
|||||||
@ -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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
#ifndef RJP_TREE_H
|
#ifndef RJP_TREE_H
|
||||||
#define RJP_TREE_H
|
#define RJP_TREE_H
|
||||||
|
|
||||||
|
|||||||
26
src/rjp.c
26
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)
|
if(!keylen)
|
||||||
keylen = strlen(key);
|
keylen = strlen(key);
|
||||||
++dest->object.num_members;
|
++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;
|
int status;
|
||||||
return &(dest->object.root = irjp_tree_insert_value(dest->object.root, &mem, &status))->data.value;
|
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)
|
if(!keylen)
|
||||||
keylen = strlen(key);
|
keylen = strlen(key);
|
||||||
RJP_object_member mem;
|
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;
|
dest->array.last->value.parent = dest;
|
||||||
return &dest->array.last->value;
|
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;
|
RJP_object_member* mem = (RJP_object_member*)dest;
|
||||||
if(key){
|
if(key){
|
||||||
if(!keylen){
|
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.value[keylen] = 0;
|
||||||
mem->name.length = keylen;
|
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;
|
RJP_object_member* mem = (RJP_object_member*)dest;
|
||||||
if(key){
|
if(key){
|
||||||
if(!keylen){
|
if(!keylen){
|
||||||
@ -112,11 +112,11 @@ RJP_value rjp_boolean(char b){
|
|||||||
RJP_value rjp_dfloat(double d){
|
RJP_value rjp_dfloat(double d){
|
||||||
return (RJP_value){.dfloat = d, .type = rjp_json_dfloat};
|
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};
|
return (RJP_value){.string = {.value = c, .length = len}, .type = rjp_json_string};
|
||||||
}
|
}
|
||||||
RJP_value rjp_string_copy(const char* c){
|
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);
|
char* tmp = rjp_alloc(esclen+1);
|
||||||
rjp_escape_strcpy(tmp, c);
|
rjp_escape_strcpy(tmp, c);
|
||||||
return (RJP_value){.string = {.value = tmp, .length = esclen}, .type = rjp_json_string};
|
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);
|
irjp_delete_object_iterator(it);
|
||||||
rjp_free(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;
|
return object->object.num_members;
|
||||||
}
|
}
|
||||||
char* rjp_member_name(const RJP_value* member){
|
char* rjp_member_name(const RJP_value* member){
|
||||||
return ((RJP_object_member*)member)->name.value;
|
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;
|
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;
|
return &n->data.value;
|
||||||
}
|
}
|
||||||
|
|
||||||
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){
|
||||||
size_t matches = 0;
|
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]);
|
RJP_tree_node* n = irjp_tree_search_value(object->object.root, searches[i]);
|
||||||
if(!n){
|
if(!n){
|
||||||
dest[i] = NULL;
|
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){
|
RJP_value* rjp_next_element(const RJP_value* element){
|
||||||
return (RJP_value*)(((RJP_array_element*)element)->next);
|
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;
|
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){
|
const char* rjp_value_cstring(const RJP_value* value){
|
||||||
return value->string.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;
|
return value->string.length;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
18
src/tree.c
18
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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
#include "tree.h"
|
#include "tree.h"
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user