added bool type parsing
This commit is contained in:
@@ -57,7 +57,7 @@ valgrind: $(bin_TESTS)
|
||||
done
|
||||
|
||||
bin_TESTS = \
|
||||
cdynarr_test
|
||||
jparser_test
|
||||
|
||||
noinst_PROGRAMS = \
|
||||
cfparser_test \
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
# Makefile.in generated by automake 1.16.3 from Makefile.am.
|
||||
# Makefile.in generated by automake 1.16.1 from Makefile.am.
|
||||
# @configure_input@
|
||||
|
||||
# Copyright (C) 1994-2020 Free Software Foundation, Inc.
|
||||
# Copyright (C) 1994-2018 Free Software Foundation, Inc.
|
||||
|
||||
# This Makefile.in is free software; the Free Software Foundation
|
||||
# gives unlimited permission to copy and/or distribute it,
|
||||
@@ -274,7 +274,6 @@ CC = @CC@
|
||||
CCDEPMODE = @CCDEPMODE@
|
||||
CFLAGS = @CFLAGS@
|
||||
CP = @CP@
|
||||
CPP = @CPP@
|
||||
CPPFLAGS = @CPPFLAGS@
|
||||
CYGPATH_W = @CYGPATH_W@
|
||||
DEFS = @DEFS@
|
||||
@@ -428,7 +427,7 @@ include_HEADERS = \
|
||||
|
||||
@HAVE_VALGRIND_TRUE@VALGRIND_OPT = --tool=memcheck -s
|
||||
bin_TESTS = \
|
||||
cdynarr_test
|
||||
jparser_test
|
||||
|
||||
|
||||
#TESTS = $(bin_TESTS)
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
#define JLEXCONT_BLOCKE 0x03
|
||||
#define JLEXCONT_SEPAR 0x04
|
||||
#define JLEXCONT_NUM 0x05
|
||||
#define JLEXCONT_UNKNOW 0x06
|
||||
#define JLEXCONT_RAWSTR 0x06
|
||||
#define JLEXCONT_END 0x99
|
||||
|
||||
#define JLEXTYPE_UNDEF 0x00
|
||||
@@ -158,12 +158,11 @@ int jlexer_gettoken(jlexer_t * lexer, char* token) {
|
||||
}
|
||||
case JLEXTYPE_CHAR:{
|
||||
lexer->tokpos = 0;
|
||||
char* prefix = "WTF? ";
|
||||
|
||||
char* prefix = "";
|
||||
strcpy(token, prefix);
|
||||
lexer->tokpos = +strlen(prefix);
|
||||
token[lexer->tokpos++] = lexer->letter;
|
||||
lexer->context = JLEXCONT_UNKNOW;
|
||||
lexer->context = JLEXCONT_RAWSTR;
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -201,7 +200,7 @@ int jlexer_gettoken(jlexer_t * lexer, char* token) {
|
||||
token[lexer->tokpos++] = lexer->letter;
|
||||
break;
|
||||
}
|
||||
case JLEXCONT_UNKNOW:{
|
||||
case JLEXCONT_RAWSTR:{
|
||||
int newcontext = lexer->context;
|
||||
|
||||
switch (type) {
|
||||
@@ -213,7 +212,7 @@ int jlexer_gettoken(jlexer_t * lexer, char* token) {
|
||||
token[lexer->tokpos++] = '\0';
|
||||
lexer->rewind = true;
|
||||
lexer->context = JLEXCONT_UNDEF;
|
||||
return JLEXTOK_UNKNOW;
|
||||
return JLEXTOK_RAWSTR;
|
||||
}
|
||||
}
|
||||
lexer->context = newcontext;
|
||||
|
||||
@@ -23,7 +23,7 @@ typedef struct {
|
||||
#define JLEXTOK_UNDEF 0x04
|
||||
#define JLEXTOK_WORD 0x05
|
||||
#define JLEXTOK_NUMB 0x06
|
||||
#define JLEXTOK_UNKNOW 0x07
|
||||
#define JLEXTOK_RAWSTR 0x07
|
||||
#define JLEXTOK_NEXT 0x08
|
||||
#define JLEXTOK_ARRB 0x10
|
||||
#define JLEXTOK_ARRE 0x11
|
||||
|
||||
@@ -41,7 +41,7 @@ int jparser_parse(jparser_t * parser) {
|
||||
char* key = "";
|
||||
|
||||
while ((type = jlexer_gettoken(lex, token)) != JLEXTOK_END) {
|
||||
//log_debug("pos %d tok 0x%02x: %s\n", pos, type, token);
|
||||
log_debug("pos %d tok 0x%02x: %s\n", pos, type, token);
|
||||
switch (pos) {
|
||||
// POS 0
|
||||
case 0:{
|
||||
@@ -103,29 +103,59 @@ int jparser_parse(jparser_t * parser) {
|
||||
pos = 1;
|
||||
continue;
|
||||
}
|
||||
if (type != JLEXTOK_WORD && type != JLEXTOK_NUMB) {
|
||||
log_error("Wrong value token: %s", token);
|
||||
return -1;
|
||||
if (type == JLEXTOK_WORD) {
|
||||
char* val = strcopy(token);
|
||||
jkval_t* kv = &(parser->kvalarr[parser->kvalsize]);
|
||||
kv->key = strcopy(key);
|
||||
//log_debug("Parser added key %s", kv->key);
|
||||
kv->type = JVALTYPE_STR;
|
||||
kv->str = val;
|
||||
free(key);
|
||||
parser->kvalsize++;
|
||||
pos++;
|
||||
break;
|
||||
}
|
||||
char* val = strcopy(token);
|
||||
|
||||
jkval_t* kv = &(parser->kvalarr[parser->kvalsize]);
|
||||
|
||||
kv->key = strcopy(key);
|
||||
//log_debug("Parser added key %s", kv->key);
|
||||
if (type == JLEXTOK_NUMB) {
|
||||
char* val = strcopy(token);
|
||||
jkval_t* kv = &(parser->kvalarr[parser->kvalsize]);
|
||||
kv->key = strcopy(key);
|
||||
//log_debug("Parser added key %s", kv->key);
|
||||
kv->type = JVALTYPE_NUM;
|
||||
char* eptr = NULL;
|
||||
kv->num = (int64_t)strtol(val, &eptr, 10);;
|
||||
free(val);
|
||||
} else {
|
||||
kv->type = JVALTYPE_STR;
|
||||
kv->str = val;
|
||||
free(key);
|
||||
parser->kvalsize++;
|
||||
pos++;
|
||||
break;
|
||||
}
|
||||
free(key);
|
||||
parser->kvalsize++;
|
||||
pos++;
|
||||
break;
|
||||
if (type == JLEXTOK_RAWSTR) {
|
||||
char* val = strcopy(token);
|
||||
bool isbool = false;
|
||||
bool bval = false;
|
||||
if (strcmp(val, "true") == 0) {
|
||||
isbool = true;
|
||||
bval = true;
|
||||
}
|
||||
if (strcmp(val, "false") == 0) {
|
||||
isbool = true;
|
||||
bval = false;
|
||||
}
|
||||
if (isbool) {
|
||||
jkval_t* kv = &(parser->kvalarr[parser->kvalsize]);
|
||||
kv->key = strcopy(key);
|
||||
log_debug("Parser added bool key %s = %d", kv->key, bval);
|
||||
kv->type = JVALTYPE_BOOL;
|
||||
kv->flag = bval;
|
||||
free(key);
|
||||
parser->kvalsize++;
|
||||
pos++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
log_error("Wrong value token: %s", token);
|
||||
return -1;
|
||||
|
||||
}
|
||||
// POS 4
|
||||
case 4:{
|
||||
@@ -168,6 +198,8 @@ int jparser_bind(jparser_t* parser, int type, char* key, void* ref) {
|
||||
*(char**)(ref) = strcopy(kv->str);
|
||||
} else if (kv->type == JVALTYPE_NUM) {
|
||||
*(int*)(ref) = kv->num;
|
||||
} else if (kv->type == JVALTYPE_BOOL) {
|
||||
*(bool*)(ref) = kv->num;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -22,8 +22,6 @@ int main(void) {
|
||||
|
||||
MASSERT(fd > 0);
|
||||
|
||||
|
||||
|
||||
rcache_t cache;
|
||||
jlexer_t lexer;
|
||||
jparser_t parser;
|
||||
@@ -38,20 +36,25 @@ int main(void) {
|
||||
}
|
||||
|
||||
int64_t id = 0;
|
||||
|
||||
if (jparser_bind(&parser, JVALTYPE_NUM, "id", (void *)&id) < 0) {
|
||||
log_error("cannot bind id variable\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
char* name = "";
|
||||
|
||||
if (jparser_bind(&parser, JVALTYPE_STR, "name", (void *)&name) < 0) {
|
||||
log_error("cannot bind name variable\n");
|
||||
}
|
||||
|
||||
bool exists = false;
|
||||
if (jparser_bind(&parser, JVALTYPE_BOOL, "exists", (void *)&exists) < 0) {
|
||||
log_error("cannot bind exists variable\n");
|
||||
}
|
||||
|
||||
|
||||
printf("id = %ld\n", id);
|
||||
printf("name = %s\n", name);
|
||||
printf("exists = %d\n", exists);
|
||||
|
||||
jparser_destroy(&parser);
|
||||
jlexer_destroy(&lexer);
|
||||
|
||||
@@ -1 +1 @@
|
||||
{"id":-123, "name" : "qwerty"}
|
||||
{"id":-123, "name" : "qwerty", "exists": true}
|
||||
|
||||
Reference in New Issue
Block a user