rjp/include/rjp.h
2019-03-30 05:20:53 -07:00

183 lines
6.0 KiB
C

/**
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_H
#define RJP_H
#include <stddef.h> //size_t
#ifdef __cplusplus
extern "C"{
#endif
//type of data
typedef enum RJP_type{
json_object = 0,
json_string,
json_integer,
json_dfloat,
json_boolean,
json_array,
json_null
}RJP_type;
//Hold a C string and the length excluding NULL terminator
typedef struct RJP_string{
char* value;
size_t length;
}RJP_string;
//Forward declarations
struct RJP_object_member;
struct RJP_array_element;
//Represents a json object
typedef struct RJP_object{
struct RJP_object_member* members; //linked list of members
struct RJP_object_member* last; //final member of linked list
size_t num_members;
}RJP_object;
//Represents a json array
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_array;
//Represents json data
//hold any json data type
typedef struct RJP_value{
union{
long integer;
double dfloat;
char boolean;
struct RJP_object object;
struct RJP_string string;
struct RJP_array array;
};
struct RJP_value* parent; //pointer to parent (either an array or object or NULL)
enum RJP_type type; //flag to determine active member of union
}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_search_res;
//Convert C string consisting of json data into RJP's format
RJP_value* rjp_parse(const char* str);
//Initialize a root RJP_value to NULL
RJP_value* rjp_init_json(void);
//Initialize a root RJP_value to copy of value
RJP_value* rjp_init_json_as(RJP_value value);
//Free an RJP_value and all members/elements
void rjp_free_value(RJP_value* root);
//Deep copy RJP_value
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);
//Allocate heap space for rjp and initialize to 0.
void* rjp_calloc(size_t num, size_t nbytes);
//Resize heap allocated space for rjp
void* rjp_realloc(void* ptr, size_t 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);
//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);
//add an element to a json array
RJP_value* rjp_add_element(RJP_value* dest, RJP_value value);
//set existing value
void rjp_set_value(RJP_value* dest, RJP_value value);
//initialize a RJP_value to the requested type and value
RJP_value rjp_integer(long i);
RJP_value rjp_boolean(char b);
RJP_value rjp_dfloat(double d);
RJP_value rjp_string(char* c, size_t len);
RJP_value rjp_null(void);
RJP_value rjp_object(void);
RJP_value rjp_array(void);
//Access first member of a json object
RJP_value* rjp_get_member(const RJP_value* object);
//Access next member of a json object given the previous member
RJP_value* rjp_next_member(const RJP_value* member);
//Return number of members in the object
size_t 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(RJP_value* member);
//Search for an object member with given key
RJP_search_res rjp_search_member(const RJP_value* object, const char* search, size_t skip);
//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_search_res* dest, size_t skip);
//Search for multiple occurances of multiple keys. Returns pointer to list of matches.
//Returned pointer must be freed using rjp_free
RJP_search_res* rjp_search_members_multi(const RJP_value* object, size_t num, const char* const* searches, size_t* rsize, size_t skip);
//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);
//Return handle to parent of given value
RJP_value* rjp_value_parent(RJP_value* element);
//Return type of value
RJP_type rjp_value_type(RJP_value* value);
//value accessors
double rjp_value_dfloat(RJP_value* value);
int rjp_value_integer(RJP_value* value);
char rjp_value_boolean(RJP_value* value);
char* rjp_value_string(RJP_value* value);
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);
//Convert RJP_object to json string
size_t rjp_dump_object(RJP_value* root, char* dest);
//Convert RJP_array to json string
size_t rjp_dump_array(RJP_value* arr, char* dest);
//Convert root rjp value into json string
char* rjp_to_json(RJP_value* root);
#ifdef __cplusplus
}
#endif
#endif