Skip to content

Commit

Permalink
prop command: add options for list copy export import clean
Browse files Browse the repository at this point in the history
* added features and tests
  • Loading branch information
liquidaty authored Mar 18, 2023
1 parent 392269b commit 67f21a7
Show file tree
Hide file tree
Showing 13 changed files with 1,112 additions and 135 deletions.
40 changes: 20 additions & 20 deletions app/2db.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ struct zsv_2db_data {
char *connection_string;

struct {
yajl_handle handle;
// yajl_handle handle;
struct yajl_helper_parse_state st;
yajl_callbacks callbacks;
// yajl_callbacks callbacks;
yajl_status yajl_stat;
enum zsv_2db_state state;

Expand Down Expand Up @@ -153,10 +153,9 @@ static void zsv_2db_delete(zsv_2db_handle data) {

free(data->json_parser.row_values);


yajl_helper_parse_state_free(&data->json_parser.st);
if(data->json_parser.handle)
yajl_free(data->json_parser.handle);
// if(data->json_parser.handle)
// yajl_free(data->json_parser.handle);

free(data);
}
Expand All @@ -165,11 +164,11 @@ static int zsv_2db_json_parse_err(struct zsv_2db_data *data,
unsigned char *last_parsed_buff,
size_t last_parsed_buff_len
) {
unsigned char *str = yajl_get_error(data->json_parser.handle, 1,
unsigned char *str = yajl_get_error(data->json_parser.st.yajl, 1,
last_parsed_buff, last_parsed_buff_len);
if(str) {
fprintf(stderr, "Error parsing JSON: %s", (const char *)str);
yajl_free_error(data->json_parser.handle, str);
yajl_free_error(data->json_parser.st.yajl, str);
}
return 1;
}
Expand Down Expand Up @@ -626,18 +625,17 @@ static zsv_2db_handle zsv_2db_new(struct zsv_2db_options *opts) {
sqlite3_exec(data->db, "PRAGMA journal_mode = OFF", NULL, NULL, NULL);

// parse the input and create & populate the database table
yajl_helper_parse_state_init(&data->json_parser.st, 32,
json_start_map, json_end_map, json_map_key,
json_start_array, json_end_array,
json_process_value,
data);
yajl_helper_callbacks_init(&data->json_parser.callbacks, 1);

data->json_parser.handle = yajl_alloc(&data->json_parser.callbacks, NULL,
&data->json_parser.st);
if(!data->json_parser.handle) {
if(yajl_helper_parse_state_init(&data->json_parser.st, 32,
json_start_map, json_end_map, json_map_key,
json_start_array, json_end_array,
json_process_value,
data) != yajl_status_ok) {
fprintf(stderr, "Unable to get yajl parser\n");
err = 1;
} else {
// yajl_helper_callbacks_init(&data->json_parser.callbacks, 32);
// data->json_parser.handle = st->yajl; // yajl_alloc(&data->json_parser.callbacks, NULL,
// &data->json_parser.st);
}
}
}
Expand Down Expand Up @@ -683,7 +681,7 @@ static int zsv_2db_finish(zsv_2db_handle data) {

// exportable
static yajl_handle zsv_2db_yajl_handle(zsv_2db_handle data) {
return data->json_parser.handle;
return data->json_parser.st.yajl;
}

int ZSV_MAIN_FUNC(ZSV_COMMAND)(int argc, const char *argv[], struct zsv_opts *zsv_opts, const char *opts_used) {
Expand Down Expand Up @@ -770,12 +768,14 @@ int ZSV_MAIN_FUNC(ZSV_COMMAND)(int argc, const char *argv[], struct zsv_opts *zs
break;
yajl_status stat = yajl_parse(zsv_2db_yajl_handle(data), buff, bytes_read);
if(stat != yajl_status_ok)
err = zsv_2db_json_parse_err(data, buff, bytes_read);
// err = zsv_2db_json_parse_err(data, buff, bytes_read);
err = yajl_helper_print_err(data->json_parser.st.yajl, buff, bytes_read);
}

if(!err) {
if(yajl_complete_parse(zsv_2db_yajl_handle(data)) != yajl_status_ok)
err = zsv_2db_json_parse_err(data, buff, bytes_read);
// err = zsv_2db_json_parse_err(data, buff, bytes_read);
err = yajl_helper_print_err(data->json_parser.st.yajl, buff, bytes_read);
else if(zsv_2db_err(data) || zsv_2db_finish(data))
err = 1;
}
Expand Down
7 changes: 7 additions & 0 deletions app/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ ifneq ($(ZSV_CACHE_PREFIX),)
CFLAGS+= -DZSV_CACHE_PREFIX='${ZSV_CACHE_PREFIX}'
endif

ifneq ($(ZSV_IS_PROP_FILE_HANDLER),)
CFLAGS+=-DZSV_IS_PROP_FILE_HANDLER=${ZSV_IS_PROP_FILE_HANDLER}
endif
ifneq ($(ZSV_IS_PROP_FILE_DEPTH),)
CFLAGS+=-DZSV_IS_PROP_FILE_DEPTH=${ZSV_IS_PROP_FILE_DEPTH}
endif

DEBUG=0
WIN=
ifeq ($(WIN),)
Expand Down
52 changes: 40 additions & 12 deletions app/external/yajl_helper/yajl_helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,24 @@ char json_value_truthy(struct json_value *value) {
return 0;
}

/**
* Print any error from the yajl parser
* Returns non-zero
*/
int yajl_helper_print_err(yajl_handle yajl,
unsigned char *last_parsed_buff,
size_t last_parsed_buff_len
) {
unsigned char *str = yajl_get_error(yajl, 1,
last_parsed_buff, last_parsed_buff_len);
if(str) {
fprintf(stderr, "Error parsing JSON: %s", (const char *)str);
yajl_free_error(yajl, str);
}
return 1;
}


const char *yajl_helper_get_map_key(struct yajl_helper_parse_state *st, unsigned int offset) {
if(YAJL_HELPER_LEVEL(st) >= offset + 1) {
unsigned int level = st->level - offset;
Expand All @@ -176,12 +194,9 @@ const char *yajl_helper_get_map_key(struct yajl_helper_parse_state *st, unsigned
return NULL;
}

char yajl_helper_got_path(struct yajl_helper_parse_state *st, unsigned int level, const char *path) {
if(YAJL_HELPER_LEVEL(st) != level)
return 0;

unsigned int this_level = st->level_offset + 1;
for(; *path && this_level <= st->level; path++) {
static char yajl_helper_got_path_aux(struct yajl_helper_parse_state *st, unsigned int level, const char *path) {
for(unsigned i = 1; *path && i <= level; path++, i++) {
unsigned this_level = st->level_offset + i;
switch(*path) {
case '{':
case '[':
Expand All @@ -199,8 +214,6 @@ char yajl_helper_got_path(struct yajl_helper_parse_state *st, unsigned int level
path += len;
}
}

this_level++;
break;
default: // map key start
return 0;
Expand All @@ -209,6 +222,18 @@ char yajl_helper_got_path(struct yajl_helper_parse_state *st, unsigned int level
return 1;
}

char yajl_helper_got_path(struct yajl_helper_parse_state *st, unsigned int level, const char *path) {
if(YAJL_HELPER_LEVEL(st) != level)
return 0;
return yajl_helper_got_path_aux(st, level, path);
}

char yajl_helper_got_path_prefix(struct yajl_helper_parse_state *st, unsigned int level, const char *path) {
if(YAJL_HELPER_LEVEL(st) < level)
return 0;
return yajl_helper_got_path_aux(st, level, path);
}

char yajl_helper_path_is(struct yajl_helper_parse_state *st, const char *path) {
unsigned int level = 0;
for(int i = 0; path[i]; i++)
Expand Down Expand Up @@ -318,10 +343,13 @@ static int yajl_helper_map_key(void *ctx, const unsigned char *stringVal, size_t

static inline int process_value(struct yajl_helper_parse_state *st,
void *ctx, struct json_value *v) {
int rc = st->value(ctx, v);
if(st->level && strchr("{[", st->stack[st->level-1]) && st->level <= st->max_level)
st->item_ind[st->level-1]++;
return rc;
if(st->value) {
int rc = st->value(ctx, v);
if(st->level && strchr("{[", st->stack[st->level-1]) && st->level <= st->max_level)
st->item_ind[st->level-1]++;
return rc;
}
return 1;
}

static int yajl_helper_number_str(void * ctx, const char * numberVal,
Expand Down
16 changes: 16 additions & 0 deletions app/external/yajl_helper/yajl_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,14 @@ unsigned char *json_str_dup_if_len(struct json_value *value);
// malloc() a new string and return that. returns buff, if buff was written to, else new malloc'd mem
unsigned char *json_str_dup_if_len_buff(struct json_value *value, unsigned char *buff, size_t bufflen);

/*
* yajl_helper_got_path() and yajl_helper_got_path_prefix() are the same except that the former
* requires that the current level is equal to the level argument, and the latter only requires
* that the current level is greater than or equal to the level argument
*/
char yajl_helper_got_path(struct yajl_helper_parse_state *st, unsigned int level, const char *path);
char yajl_helper_got_path_prefix(struct yajl_helper_parse_state *st, unsigned int level, const char *path);

char yajl_helper_path_is(struct yajl_helper_parse_state *st, const char *path);

const char *yajl_helper_get_map_key(struct yajl_helper_parse_state *st, unsigned int offset);
Expand Down Expand Up @@ -158,4 +165,13 @@ void int_list_free(struct int_list *e);
*/
void yajl_helper_dump_path(struct yajl_helper_parse_state *st, FILE *out);

/**
* Print any error from the yajl parser
* Returns non-zero
*/
int yajl_helper_print_err(yajl_handle yajl,
unsigned char *last_parsed_buff,
size_t last_parsed_buff_len
);

#endif // ifdef YAJL_HELPER_H
Loading

0 comments on commit 67f21a7

Please sign in to comment.