6 Commits
v0.5 ... v0.6.2

Author SHA1 Message Date
rexy712
50c7a055ec Updated version 2019-03-30 05:22:11 -07:00
rexy712
6771d00c6e Updated copyright notice 2019-03-30 05:20:53 -07:00
rexy712
71661cd9ad Made rjp_search_members take a 'const char* const*' for search terms 2019-03-30 04:37:06 -07:00
rexy712
46b7b56852 Fix rjp_string containing incorrect length 2019-02-21 10:49:21 -08:00
rexy712
fd7d9988b8 Fixed search return value being in an inconsistent state 2019-02-13 15:12:14 -08:00
rexy712
280e24a8e7 Rethought searching after trying to use it. Should be more useful now with the added return information 2019-02-08 15:51:11 -08:00
10 changed files with 57 additions and 45 deletions

View File

@@ -4,8 +4,8 @@ include(GNUInstallDirs)
cmake_minimum_required(VERSION 3.0.2)
project(rjp)
set(rjp_VERSION_MAJOR 0)
set(rjp_VERSION_MINOR 5)
set(rjp_VERSION_REVISION 0)
set(rjp_VERSION_MINOR 6)
set(rjp_VERSION_REVISION 2)
set(INCLUDE_PATH ${CMAKE_SOURCE_DIR}/include)
configure_file(
"${INCLUDE_PATH}/config.h.in"

View File

@@ -1,6 +1,6 @@
/**
rjp
Copyright (C) 2018 rexy712
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

View File

@@ -1,6 +1,6 @@
/**
rjp
Copyright (C) 2018 rexy712
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
@@ -75,6 +75,13 @@ typedef struct RJP_value{
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);
@@ -127,13 +134,13 @@ 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_value* rjp_search_member(const RJP_value* object, const char* search, size_t* index, size_t skip);
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** searches, RJP_value** dest, size_t skip);
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_value** rjp_search_members_multi(const RJP_value* object, size_t num, const char** searches, size_t* rsize, size_t skip);
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

View File

@@ -1,6 +1,6 @@
/**
rjp
Copyright (C) 2018 rexy712
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

View File

@@ -1,6 +1,6 @@
/**
rjp
Copyright (C) 2018 rexy712
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
@@ -23,7 +23,7 @@
#include <stddef.h> //size_t
int _rjp__is_whitespace(char c);
char* _rjp__parse_string(RJP_value* root, const char* str, int* len, int* row, int* column);
char* _rjp__parse_string(RJP_value* root, const char* str, int* inclen, int* len, int* row, int* column);
size_t _rjp__array_strlen(RJP_value* arr);
size_t _rjp__object_strlen(RJP_value* root);
size_t _rjp__value_strlen(RJP_value* root);

View File

@@ -1,6 +1,6 @@
/**
rjp
Copyright (C) 2018 rexy712
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
@@ -104,14 +104,15 @@ RJP_value* rjp_parse(const char* str){
syntax_error("Key found outside of object definition!", row, column);
int keylen;
char* new_string = _rjp__parse_string(root, ++str, &keylen, &row, &column);
int inclen;
char* new_string = _rjp__parse_string(root, ++str, &inclen, &keylen, &row, &column);
if(!new_string){
if(!keylen)
syntax_error("Cannot have empty key name!", row, column);
return NULL;
}
_rjp__add_member_no_alloc(&curr->object, new_string, keylen);
str += keylen;
str += inclen;
*top = json_colon;
//end of this object (object is empty)
}else if(c == '}'){
@@ -183,9 +184,9 @@ RJP_value* rjp_parse(const char* str){
}
//strings
else if(c == '"'){
int vallen;
int vallen, inclen;
++str;
char* new_string = _rjp__parse_string(root, str, &vallen, &row, &column);
char* new_string = _rjp__parse_string(root, str, &inclen, &vallen, &row, &column);
if(!new_string){
if(vallen == 0){
new_string = rjp_calloc(1, 1);
@@ -194,7 +195,7 @@ RJP_value* rjp_parse(const char* str){
}
}
_rjp__add_value(curr, rjp_string(new_string, vallen));
str += vallen;
str += inclen;
*top = json_comma;
}
//numbers

View File

@@ -1,6 +1,6 @@
/**
rjp
Copyright (C) 2018 rexy712
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

View File

@@ -1,6 +1,6 @@
/**
rjp
Copyright (C) 2018 rexy712
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

View File

@@ -1,6 +1,6 @@
/**
rjp
Copyright (C) 2018 rexy712
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
@@ -124,64 +124,67 @@ char* rjp_member_name(const RJP_value* member){
size_t rjp_member_name_length(RJP_value* member){
return ((RJP_object_member*)member)->name.length;
}
RJP_value* rjp_search_member(const RJP_value* object, const char* search, size_t* index, size_t skip){
RJP_search_res rjp_search_member(const RJP_value* object, const char* search, size_t skip){
RJP_value* member = rjp_get_member(object);
size_t i = 0;
for(;i < skip && member;member = rjp_next_member(member),++i);
for(;member;member = rjp_next_member(member),++i){
if(!strcmp(((RJP_object_member*)member)->name.value, search)){
if(index){
*index = i;
}
return member;
return (RJP_search_res){member, 0, i};
}
}
return NULL;
return (RJP_search_res){0};
}
size_t rjp_search_members(const RJP_value* object, size_t num, const char** searches, RJP_value** dest, size_t skip){
size_t rjp_search_members(const RJP_value* object, size_t num, const char* const* searches, RJP_search_res* dest, size_t skip){
const char** tmp = rjp_alloc(num*sizeof(char*));
for(size_t i = 0;i < num;++i){
tmp[i] = searches[i];
dest[i] = NULL;
dest[i] = (RJP_search_res){0};
}
RJP_value* member = rjp_get_member(object);
for(size_t i = 0;i < skip && member;member = rjp_next_member(member));
size_t objindex = 0;
for(;objindex < skip && member;member = rjp_next_member(member),++objindex);
size_t matches = 0;
for(;member;member = rjp_next_member(member)){
for(;member;member = rjp_next_member(member),++objindex){
for(size_t i = 0;i < num;++i){
if(searches[i] == NULL)
if(tmp[i] == NULL)
continue;
if(!strcmp(((RJP_object_member*)member)->name.value, searches[i])){
dest[i] = member;
searches[i] = NULL;
if(!strcmp(((RJP_object_member*)member)->name.value, tmp[i])){
dest[i].value = member;
dest[i].searchindex = i;
dest[i].objindex = objindex;
tmp[i] = NULL;
++matches;
break;
}
}
}
for(size_t i = 0;i < num;++i){
searches[i] = tmp[i];
}
rjp_free(tmp);
return matches;
}
RJP_value** rjp_search_members_multi(const RJP_value* object, size_t num, const char** searches, size_t* rsize, size_t skip){
RJP_value** ret = rjp_alloc(num*sizeof(RJP_value*));
RJP_search_res* rjp_search_members_multi(const RJP_value* object, size_t num, const char* const* searches, size_t* rsize, size_t skip){
RJP_search_res* ret = rjp_alloc(num*sizeof(RJP_search_res));
size_t ret_size = num;
size_t j = 0;
RJP_value* member = rjp_get_member(object);
for(size_t i = 0;i < skip && member;member = rjp_next_member(member));
size_t objindex = 0;
for(;objindex < skip && member;member = rjp_next_member(member),++objindex);
for(;member;member = rjp_next_member(member)){
for(;member;member = rjp_next_member(member),++objindex){
for(size_t i = 0;i < num;++i){
if(!strcmp(((RJP_object_member*)member)->name.value, searches[i])){
ret[j++] = member;
ret[j].value = member;
ret[j].searchindex = i;
ret[j].objindex = objindex;
++j;
if(j == ret_size){
ret_size *= 2;
ret = rjp_realloc(ret, ret_size*sizeof(RJP_value*));
ret = rjp_realloc(ret, ret_size*sizeof(RJP_search_res));
}
break;
}

View File

@@ -1,6 +1,6 @@
/**
rjp
Copyright (C) 2018 rexy712
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
@@ -148,7 +148,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* len, int* row, int* column){
char* _rjp__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;
@@ -173,7 +173,8 @@ char* _rjp__parse_string(RJP_value* root, const char* str, int* len, int* row, i
*column = 0;
}
}
*len = oldpos;
*inclen = oldpos;
*len = newpos;
if(newpos == 0){
return NULL;
}