at work
This commit is contained in:
@@ -19,7 +19,9 @@ libcworker_la_SOURCES = \
|
|||||||
logger.c \
|
logger.c \
|
||||||
massert.c \
|
massert.c \
|
||||||
rcache.c \
|
rcache.c \
|
||||||
jblock.c
|
jblock.c \
|
||||||
|
cdynarr.c \
|
||||||
|
cstring.c
|
||||||
|
|
||||||
include_HEADERS = \
|
include_HEADERS = \
|
||||||
cflexer.h \
|
cflexer.h \
|
||||||
@@ -31,8 +33,9 @@ include_HEADERS = \
|
|||||||
logger.h \
|
logger.h \
|
||||||
massert.h \
|
massert.h \
|
||||||
rcache.h \
|
rcache.h \
|
||||||
jblock.h
|
jblock.h \
|
||||||
|
cdynarr.h \
|
||||||
|
cstring.h
|
||||||
|
|
||||||
if HAVE_VALGRIND
|
if HAVE_VALGRIND
|
||||||
VALGRIND_OPT = --tool=memcheck
|
VALGRIND_OPT = --tool=memcheck
|
||||||
@@ -56,7 +59,7 @@ valgrind: $(bin_TESTS)
|
|||||||
common_SOURCES = libcworker.la
|
common_SOURCES = libcworker.la
|
||||||
|
|
||||||
bin_TESTS = \
|
bin_TESTS = \
|
||||||
jblock_test
|
cstring_test
|
||||||
|
|
||||||
noinst_PROGRAMS = \
|
noinst_PROGRAMS = \
|
||||||
cfparser_test \
|
cfparser_test \
|
||||||
@@ -68,7 +71,9 @@ noinst_PROGRAMS = \
|
|||||||
jlexer_test \
|
jlexer_test \
|
||||||
jparser_test \
|
jparser_test \
|
||||||
rcache_test \
|
rcache_test \
|
||||||
jblock_test
|
jblock_test \
|
||||||
|
cdynarr_test \
|
||||||
|
cstring_test
|
||||||
|
|
||||||
#TESTS = $(bin_TESTS)
|
#TESTS = $(bin_TESTS)
|
||||||
|
|
||||||
@@ -80,7 +85,8 @@ jlexer_test_SOURCES = jlexer_test.c $(common_SOURCES)
|
|||||||
jparser_test_SOURCES = jparser_test.c $(common_SOURCES)
|
jparser_test_SOURCES = jparser_test.c $(common_SOURCES)
|
||||||
rcache_test_SOURCES = rcache_test.c $(common_SOURCES)
|
rcache_test_SOURCES = rcache_test.c $(common_SOURCES)
|
||||||
jblock_test_SOURCES = jblock_test.c $(common_SOURCES)
|
jblock_test_SOURCES = jblock_test.c $(common_SOURCES)
|
||||||
|
cdynarr_test_SOURCES = cdynarr_test.c $(common_SOURCES)
|
||||||
|
cstring_test_SOURCES = cstring_test.c $(common_SOURCES)
|
||||||
|
|
||||||
cflexer_test_LDADD = libcworker.la
|
cflexer_test_LDADD = libcworker.la
|
||||||
cfparser_test_LDADD = libcworker.la
|
cfparser_test_LDADD = libcworker.la
|
||||||
@@ -90,7 +96,8 @@ jblock_test_LDADD = libcworker.la
|
|||||||
jlexer_test_LDADD = libcworker.la
|
jlexer_test_LDADD = libcworker.la
|
||||||
jparser_test_LDADD = libcworker.la
|
jparser_test_LDADD = libcworker.la
|
||||||
rcache_test_LDADD = libcworker.la
|
rcache_test_LDADD = libcworker.la
|
||||||
|
cdynarr_test_LDADD = libcworker.la
|
||||||
|
cstring_test_LDADD = libcworker.la
|
||||||
|
|
||||||
|
|
||||||
clean-local:
|
clean-local:
|
||||||
|
|||||||
@@ -94,7 +94,8 @@ noinst_PROGRAMS = cfparser_test$(EXEEXT) clparser_test$(EXEEXT) \
|
|||||||
cflexer_test$(EXEEXT) cfparser_test$(EXEEXT) \
|
cflexer_test$(EXEEXT) cfparser_test$(EXEEXT) \
|
||||||
cllexer_test$(EXEEXT) clparser_test$(EXEEXT) \
|
cllexer_test$(EXEEXT) clparser_test$(EXEEXT) \
|
||||||
jlexer_test$(EXEEXT) jparser_test$(EXEEXT) \
|
jlexer_test$(EXEEXT) jparser_test$(EXEEXT) \
|
||||||
rcache_test$(EXEEXT) jblock_test$(EXEEXT)
|
rcache_test$(EXEEXT) jblock_test$(EXEEXT) \
|
||||||
|
cdynarr_test$(EXEEXT) cstring_test$(EXEEXT)
|
||||||
subdir = clib
|
subdir = clib
|
||||||
ACLOCAL_M4 = $(top_srcdir)/aclocal.m4
|
ACLOCAL_M4 = $(top_srcdir)/aclocal.m4
|
||||||
am__aclocal_m4_deps = $(top_srcdir)/m4/libtool.m4 \
|
am__aclocal_m4_deps = $(top_srcdir)/m4/libtool.m4 \
|
||||||
@@ -114,13 +115,16 @@ LTLIBRARIES = $(noinst_LTLIBRARIES)
|
|||||||
libcworker_la_LIBADD =
|
libcworker_la_LIBADD =
|
||||||
am_libcworker_la_OBJECTS = cflexer.lo cfparser.lo cllexer.lo \
|
am_libcworker_la_OBJECTS = cflexer.lo cfparser.lo cllexer.lo \
|
||||||
clparser.lo jlexer.lo jparser.lo logger.lo massert.lo \
|
clparser.lo jlexer.lo jparser.lo logger.lo massert.lo \
|
||||||
rcache.lo jblock.lo
|
rcache.lo jblock.lo cdynarr.lo cstring.lo
|
||||||
libcworker_la_OBJECTS = $(am_libcworker_la_OBJECTS)
|
libcworker_la_OBJECTS = $(am_libcworker_la_OBJECTS)
|
||||||
AM_V_lt = $(am__v_lt_@AM_V@)
|
AM_V_lt = $(am__v_lt_@AM_V@)
|
||||||
am__v_lt_ = $(am__v_lt_@AM_DEFAULT_V@)
|
am__v_lt_ = $(am__v_lt_@AM_DEFAULT_V@)
|
||||||
am__v_lt_0 = --silent
|
am__v_lt_0 = --silent
|
||||||
am__v_lt_1 =
|
am__v_lt_1 =
|
||||||
am__objects_1 =
|
am__objects_1 =
|
||||||
|
am_cdynarr_test_OBJECTS = cdynarr_test.$(OBJEXT) $(am__objects_1)
|
||||||
|
cdynarr_test_OBJECTS = $(am_cdynarr_test_OBJECTS)
|
||||||
|
cdynarr_test_DEPENDENCIES = libcworker.la
|
||||||
am_cflexer_test_OBJECTS = cflexer_test.$(OBJEXT) $(am__objects_1)
|
am_cflexer_test_OBJECTS = cflexer_test.$(OBJEXT) $(am__objects_1)
|
||||||
cflexer_test_OBJECTS = $(am_cflexer_test_OBJECTS)
|
cflexer_test_OBJECTS = $(am_cflexer_test_OBJECTS)
|
||||||
cflexer_test_DEPENDENCIES = libcworker.la
|
cflexer_test_DEPENDENCIES = libcworker.la
|
||||||
@@ -133,6 +137,9 @@ cllexer_test_DEPENDENCIES = libcworker.la
|
|||||||
am_clparser_test_OBJECTS = clparser_test.$(OBJEXT) $(am__objects_1)
|
am_clparser_test_OBJECTS = clparser_test.$(OBJEXT) $(am__objects_1)
|
||||||
clparser_test_OBJECTS = $(am_clparser_test_OBJECTS)
|
clparser_test_OBJECTS = $(am_clparser_test_OBJECTS)
|
||||||
clparser_test_DEPENDENCIES = libcworker.la
|
clparser_test_DEPENDENCIES = libcworker.la
|
||||||
|
am_cstring_test_OBJECTS = cstring_test.$(OBJEXT) $(am__objects_1)
|
||||||
|
cstring_test_OBJECTS = $(am_cstring_test_OBJECTS)
|
||||||
|
cstring_test_DEPENDENCIES = libcworker.la
|
||||||
am_jblock_test_OBJECTS = jblock_test.$(OBJEXT) $(am__objects_1)
|
am_jblock_test_OBJECTS = jblock_test.$(OBJEXT) $(am__objects_1)
|
||||||
jblock_test_OBJECTS = $(am_jblock_test_OBJECTS)
|
jblock_test_OBJECTS = $(am_jblock_test_OBJECTS)
|
||||||
jblock_test_DEPENDENCIES = libcworker.la
|
jblock_test_DEPENDENCIES = libcworker.la
|
||||||
@@ -178,14 +185,16 @@ AM_V_CCLD = $(am__v_CCLD_@AM_V@)
|
|||||||
am__v_CCLD_ = $(am__v_CCLD_@AM_DEFAULT_V@)
|
am__v_CCLD_ = $(am__v_CCLD_@AM_DEFAULT_V@)
|
||||||
am__v_CCLD_0 = @echo " CCLD " $@;
|
am__v_CCLD_0 = @echo " CCLD " $@;
|
||||||
am__v_CCLD_1 =
|
am__v_CCLD_1 =
|
||||||
SOURCES = $(libcworker_la_SOURCES) $(cflexer_test_SOURCES) \
|
SOURCES = $(libcworker_la_SOURCES) $(cdynarr_test_SOURCES) \
|
||||||
$(cfparser_test_SOURCES) $(cllexer_test_SOURCES) \
|
$(cflexer_test_SOURCES) $(cfparser_test_SOURCES) \
|
||||||
$(clparser_test_SOURCES) $(jblock_test_SOURCES) \
|
$(cllexer_test_SOURCES) $(clparser_test_SOURCES) \
|
||||||
|
$(cstring_test_SOURCES) $(jblock_test_SOURCES) \
|
||||||
$(jlexer_test_SOURCES) $(jparser_test_SOURCES) \
|
$(jlexer_test_SOURCES) $(jparser_test_SOURCES) \
|
||||||
$(rcache_test_SOURCES)
|
$(rcache_test_SOURCES)
|
||||||
DIST_SOURCES = $(libcworker_la_SOURCES) $(cflexer_test_SOURCES) \
|
DIST_SOURCES = $(libcworker_la_SOURCES) $(cdynarr_test_SOURCES) \
|
||||||
$(cfparser_test_SOURCES) $(cllexer_test_SOURCES) \
|
$(cflexer_test_SOURCES) $(cfparser_test_SOURCES) \
|
||||||
$(clparser_test_SOURCES) $(jblock_test_SOURCES) \
|
$(cllexer_test_SOURCES) $(clparser_test_SOURCES) \
|
||||||
|
$(cstring_test_SOURCES) $(jblock_test_SOURCES) \
|
||||||
$(jlexer_test_SOURCES) $(jparser_test_SOURCES) \
|
$(jlexer_test_SOURCES) $(jparser_test_SOURCES) \
|
||||||
$(rcache_test_SOURCES)
|
$(rcache_test_SOURCES)
|
||||||
am__can_run_installinfo = \
|
am__can_run_installinfo = \
|
||||||
@@ -392,7 +401,9 @@ libcworker_la_SOURCES = \
|
|||||||
logger.c \
|
logger.c \
|
||||||
massert.c \
|
massert.c \
|
||||||
rcache.c \
|
rcache.c \
|
||||||
jblock.c
|
jblock.c \
|
||||||
|
cdynarr.c \
|
||||||
|
cstring.c
|
||||||
|
|
||||||
include_HEADERS = \
|
include_HEADERS = \
|
||||||
cflexer.h \
|
cflexer.h \
|
||||||
@@ -404,12 +415,14 @@ include_HEADERS = \
|
|||||||
logger.h \
|
logger.h \
|
||||||
massert.h \
|
massert.h \
|
||||||
rcache.h \
|
rcache.h \
|
||||||
jblock.h
|
jblock.h \
|
||||||
|
cdynarr.h \
|
||||||
|
cstring.h
|
||||||
|
|
||||||
@HAVE_VALGRIND_TRUE@VALGRIND_OPT = --tool=memcheck
|
@HAVE_VALGRIND_TRUE@VALGRIND_OPT = --tool=memcheck
|
||||||
common_SOURCES = libcworker.la
|
common_SOURCES = libcworker.la
|
||||||
bin_TESTS = \
|
bin_TESTS = \
|
||||||
jblock_test
|
cstring_test
|
||||||
|
|
||||||
|
|
||||||
#TESTS = $(bin_TESTS)
|
#TESTS = $(bin_TESTS)
|
||||||
@@ -421,6 +434,8 @@ jlexer_test_SOURCES = jlexer_test.c $(common_SOURCES)
|
|||||||
jparser_test_SOURCES = jparser_test.c $(common_SOURCES)
|
jparser_test_SOURCES = jparser_test.c $(common_SOURCES)
|
||||||
rcache_test_SOURCES = rcache_test.c $(common_SOURCES)
|
rcache_test_SOURCES = rcache_test.c $(common_SOURCES)
|
||||||
jblock_test_SOURCES = jblock_test.c $(common_SOURCES)
|
jblock_test_SOURCES = jblock_test.c $(common_SOURCES)
|
||||||
|
cdynarr_test_SOURCES = cdynarr_test.c $(common_SOURCES)
|
||||||
|
cstring_test_SOURCES = cstring_test.c $(common_SOURCES)
|
||||||
cflexer_test_LDADD = libcworker.la
|
cflexer_test_LDADD = libcworker.la
|
||||||
cfparser_test_LDADD = libcworker.la
|
cfparser_test_LDADD = libcworker.la
|
||||||
cllexer_test_LDADD = libcworker.la
|
cllexer_test_LDADD = libcworker.la
|
||||||
@@ -429,6 +444,8 @@ jblock_test_LDADD = libcworker.la
|
|||||||
jlexer_test_LDADD = libcworker.la
|
jlexer_test_LDADD = libcworker.la
|
||||||
jparser_test_LDADD = libcworker.la
|
jparser_test_LDADD = libcworker.la
|
||||||
rcache_test_LDADD = libcworker.la
|
rcache_test_LDADD = libcworker.la
|
||||||
|
cdynarr_test_LDADD = libcworker.la
|
||||||
|
cstring_test_LDADD = libcworker.la
|
||||||
all: all-am
|
all: all-am
|
||||||
|
|
||||||
.SUFFIXES:
|
.SUFFIXES:
|
||||||
@@ -486,6 +503,10 @@ clean-noinstLTLIBRARIES:
|
|||||||
libcworker.la: $(libcworker_la_OBJECTS) $(libcworker_la_DEPENDENCIES) $(EXTRA_libcworker_la_DEPENDENCIES)
|
libcworker.la: $(libcworker_la_OBJECTS) $(libcworker_la_DEPENDENCIES) $(EXTRA_libcworker_la_DEPENDENCIES)
|
||||||
$(AM_V_CCLD)$(LINK) $(libcworker_la_OBJECTS) $(libcworker_la_LIBADD) $(LIBS)
|
$(AM_V_CCLD)$(LINK) $(libcworker_la_OBJECTS) $(libcworker_la_LIBADD) $(LIBS)
|
||||||
|
|
||||||
|
cdynarr_test$(EXEEXT): $(cdynarr_test_OBJECTS) $(cdynarr_test_DEPENDENCIES) $(EXTRA_cdynarr_test_DEPENDENCIES)
|
||||||
|
@rm -f cdynarr_test$(EXEEXT)
|
||||||
|
$(AM_V_CCLD)$(LINK) $(cdynarr_test_OBJECTS) $(cdynarr_test_LDADD) $(LIBS)
|
||||||
|
|
||||||
cflexer_test$(EXEEXT): $(cflexer_test_OBJECTS) $(cflexer_test_DEPENDENCIES) $(EXTRA_cflexer_test_DEPENDENCIES)
|
cflexer_test$(EXEEXT): $(cflexer_test_OBJECTS) $(cflexer_test_DEPENDENCIES) $(EXTRA_cflexer_test_DEPENDENCIES)
|
||||||
@rm -f cflexer_test$(EXEEXT)
|
@rm -f cflexer_test$(EXEEXT)
|
||||||
$(AM_V_CCLD)$(LINK) $(cflexer_test_OBJECTS) $(cflexer_test_LDADD) $(LIBS)
|
$(AM_V_CCLD)$(LINK) $(cflexer_test_OBJECTS) $(cflexer_test_LDADD) $(LIBS)
|
||||||
@@ -502,6 +523,10 @@ clparser_test$(EXEEXT): $(clparser_test_OBJECTS) $(clparser_test_DEPENDENCIES) $
|
|||||||
@rm -f clparser_test$(EXEEXT)
|
@rm -f clparser_test$(EXEEXT)
|
||||||
$(AM_V_CCLD)$(LINK) $(clparser_test_OBJECTS) $(clparser_test_LDADD) $(LIBS)
|
$(AM_V_CCLD)$(LINK) $(clparser_test_OBJECTS) $(clparser_test_LDADD) $(LIBS)
|
||||||
|
|
||||||
|
cstring_test$(EXEEXT): $(cstring_test_OBJECTS) $(cstring_test_DEPENDENCIES) $(EXTRA_cstring_test_DEPENDENCIES)
|
||||||
|
@rm -f cstring_test$(EXEEXT)
|
||||||
|
$(AM_V_CCLD)$(LINK) $(cstring_test_OBJECTS) $(cstring_test_LDADD) $(LIBS)
|
||||||
|
|
||||||
jblock_test$(EXEEXT): $(jblock_test_OBJECTS) $(jblock_test_DEPENDENCIES) $(EXTRA_jblock_test_DEPENDENCIES)
|
jblock_test$(EXEEXT): $(jblock_test_OBJECTS) $(jblock_test_DEPENDENCIES) $(EXTRA_jblock_test_DEPENDENCIES)
|
||||||
@rm -f jblock_test$(EXEEXT)
|
@rm -f jblock_test$(EXEEXT)
|
||||||
$(AM_V_CCLD)$(LINK) $(jblock_test_OBJECTS) $(jblock_test_LDADD) $(LIBS)
|
$(AM_V_CCLD)$(LINK) $(jblock_test_OBJECTS) $(jblock_test_LDADD) $(LIBS)
|
||||||
|
|||||||
@@ -11,7 +11,7 @@
|
|||||||
#include <logger.h>
|
#include <logger.h>
|
||||||
#include <clparser.h>
|
#include <clparser.h>
|
||||||
|
|
||||||
#define INIT_BSIZE 64
|
#define INIT_BSIZE 128
|
||||||
|
|
||||||
|
|
||||||
static char* strcopy(char* src) {
|
static char* strcopy(char* src) {
|
||||||
|
|||||||
172
clib/jblock.c
172
clib/jblock.c
@@ -4,17 +4,17 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <jblock.h>
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include <cstring.h>
|
||||||
|
#include <cdynarr.h>
|
||||||
|
#include <jblock.h>
|
||||||
|
|
||||||
#define INIT_CAPA 64
|
#define INIT_CAPA 64
|
||||||
|
|
||||||
static void* jstring_init(jstring_t* str);
|
|
||||||
static void* jstring_append(jstring_t* str, char* add);
|
|
||||||
static char* jstring_getref(jstring_t* str);
|
|
||||||
static char* strcopy(char* src);
|
static char* strcopy(char* src);
|
||||||
|
|
||||||
void jblock_init(jblock_t* jb) {
|
void jblock_init(jblock_t* jb) {
|
||||||
@@ -106,9 +106,9 @@ int jblock_addstr(jblock_t* jb, char* key, char* val) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void jblock_outjson(jblock_t* jb, char** res) {
|
void jblock_outjson(jblock_t* jb, char** res) {
|
||||||
jstring_t jstr;
|
cstring_t jstr;
|
||||||
jstring_init(&jstr);
|
cstring_init(&jstr);
|
||||||
jstring_append(&jstr, "{");
|
cstring_append(&jstr, "{");
|
||||||
for (int i = 0; i < jb->kvsize; i++) {
|
for (int i = 0; i < jb->kvsize; i++) {
|
||||||
switch (jb->kvarr[i].type) {
|
switch (jb->kvarr[i].type) {
|
||||||
case JKVTYPE_STR:
|
case JKVTYPE_STR:
|
||||||
@@ -117,7 +117,7 @@ void jblock_outjson(jblock_t* jb, char** res) {
|
|||||||
case JKVTYPE_INT: {
|
case JKVTYPE_INT: {
|
||||||
char* buffer = NULL;
|
char* buffer = NULL;
|
||||||
asprintf(&buffer, "\"%s\":", jb->kvarr[i].key);
|
asprintf(&buffer, "\"%s\":", jb->kvarr[i].key);
|
||||||
jstring_append(&jstr, buffer);
|
cstring_append(&jstr, buffer);
|
||||||
free(buffer);
|
free(buffer);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -129,39 +129,39 @@ void jblock_outjson(jblock_t* jb, char** res) {
|
|||||||
case JKVTYPE_INT: {
|
case JKVTYPE_INT: {
|
||||||
char* buffer = NULL;
|
char* buffer = NULL;
|
||||||
asprintf(&buffer, "%ld", jb->kvarr[i].num);
|
asprintf(&buffer, "%ld", jb->kvarr[i].num);
|
||||||
jstring_append(&jstr, buffer);
|
cstring_append(&jstr, buffer);
|
||||||
free(buffer);
|
free(buffer);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case JKVTYPE_FLT: {
|
case JKVTYPE_FLT: {
|
||||||
char* buffer = NULL;
|
char* buffer = NULL;
|
||||||
asprintf(&buffer, "%e", jb->kvarr[i].flt);
|
asprintf(&buffer, "%e", jb->kvarr[i].flt);
|
||||||
jstring_append(&jstr, buffer);
|
cstring_append(&jstr, buffer);
|
||||||
free(buffer);
|
free(buffer);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case JKVTYPE_BOOL: {
|
case JKVTYPE_BOOL: {
|
||||||
if (jb->kvarr[i].flag) {
|
if (jb->kvarr[i].flag) {
|
||||||
jstring_append(&jstr, "true");
|
cstring_append(&jstr, "true");
|
||||||
} else {
|
} else {
|
||||||
jstring_append(&jstr, "false");
|
cstring_append(&jstr, "false");
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case JKVTYPE_STR: {
|
case JKVTYPE_STR: {
|
||||||
char* buffer = NULL;
|
char* buffer = NULL;
|
||||||
asprintf(&buffer, "\"%s\"", jb->kvarr[i].str);
|
asprintf(&buffer, "\"%s\"", jb->kvarr[i].str);
|
||||||
jstring_append(&jstr, buffer);
|
cstring_append(&jstr, buffer);
|
||||||
free(buffer);
|
free(buffer);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (i < jb->kvsize - 1) {
|
if (i < jb->kvsize - 1) {
|
||||||
jstring_append(&jstr, ",");
|
cstring_append(&jstr, ",");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
jstring_append(&jstr, "}");
|
cstring_append(&jstr, "}");
|
||||||
*res = jstring_getref(&jstr);
|
*res = cstring_getref(&jstr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -205,146 +205,6 @@ void jblock_free(jblock_t* jb) {
|
|||||||
free(jb);
|
free(jb);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* String container */
|
|
||||||
static void* jstring_init(jstring_t* str) {
|
|
||||||
str->data = malloc(INIT_CAPA + 1);
|
|
||||||
if (str->data == NULL) return NULL;
|
|
||||||
memset(str->data, '\0', INIT_CAPA + 1);
|
|
||||||
str->capa = INIT_CAPA;
|
|
||||||
str->size = 0;
|
|
||||||
return str->data;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void* jstring_append(jstring_t* str, char* add) {
|
|
||||||
size_t addsize = strlen(add);
|
|
||||||
size_t newsize = str->size + addsize;
|
|
||||||
if (newsize > str->capa) {
|
|
||||||
char* newstr = realloc(str->data, newsize + 1);
|
|
||||||
if (newstr == NULL) return NULL;
|
|
||||||
}
|
|
||||||
strcpy(&(str->data[str->size]), add);
|
|
||||||
str->data[newsize + 1] = '\0';
|
|
||||||
str->capa = newsize;
|
|
||||||
str->size = newsize;
|
|
||||||
return str->data;
|
|
||||||
}
|
|
||||||
|
|
||||||
static char* jstring_getref(jstring_t* str) {
|
|
||||||
return str->data;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Integer array */
|
|
||||||
jintarr_t* new_jintarr(void) {
|
|
||||||
jintarr_t* arr = malloc(sizeof(jintarr_t));
|
|
||||||
if (arr == NULL) return NULL;
|
|
||||||
if (jintarr_init(arr) == NULL) {
|
|
||||||
jintarr_free(arr);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
return arr;
|
|
||||||
}
|
|
||||||
|
|
||||||
void* jintarr_init(jintarr_t* array) {
|
|
||||||
array->data = malloc(INIT_CAPA * sizeof(int64_t));
|
|
||||||
if (array->data == NULL) return NULL;
|
|
||||||
memset(array->data, 0, INIT_CAPA);
|
|
||||||
array->capa = INIT_CAPA;
|
|
||||||
array->size = 0;
|
|
||||||
return array->data;
|
|
||||||
}
|
|
||||||
|
|
||||||
void* jintarr_append(jintarr_t* array, int64_t add) {
|
|
||||||
if (array->size + 1 > array->capa) {
|
|
||||||
size_t newcapa = (array->capa * 10) / 6 ;
|
|
||||||
int64_t* newarray = realloc(array->data, newcapa);
|
|
||||||
if (newarray == NULL) return NULL;
|
|
||||||
array->capa = newcapa;
|
|
||||||
array->data = newarray;
|
|
||||||
}
|
|
||||||
array->data[array->size++] = add;
|
|
||||||
return array->data;
|
|
||||||
}
|
|
||||||
|
|
||||||
int64_t* jintarr_getref(jintarr_t* array) {
|
|
||||||
return array->data;
|
|
||||||
}
|
|
||||||
|
|
||||||
void jintarr_destroy(jintarr_t* array) {
|
|
||||||
if (array == NULL) return;
|
|
||||||
free(array->data);
|
|
||||||
}
|
|
||||||
|
|
||||||
void jintarr_free(jintarr_t* array) {
|
|
||||||
jintarr_destroy(array);
|
|
||||||
free(array->data);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/* Float array */
|
|
||||||
void* jfltarr_init(jfltarr_t* array) {
|
|
||||||
array->data = malloc(INIT_CAPA * sizeof(double));
|
|
||||||
if (array->data == NULL) return NULL;
|
|
||||||
memset(array->data, 0, INIT_CAPA);
|
|
||||||
array->capa = INIT_CAPA;
|
|
||||||
array->size = 0;
|
|
||||||
return array->data;
|
|
||||||
}
|
|
||||||
|
|
||||||
void* jfltarr_append(jfltarr_t* array, double add) {
|
|
||||||
if (array->size + 1 > array->capa) {
|
|
||||||
size_t newcapa = (array->capa * 10) / 6 ;
|
|
||||||
double* newarray = realloc(array->data, newcapa);
|
|
||||||
if (newarray == NULL) return NULL;
|
|
||||||
array->capa = newcapa;
|
|
||||||
array->data = newarray;
|
|
||||||
}
|
|
||||||
array->data[array->size++] = add;
|
|
||||||
return array->data;
|
|
||||||
}
|
|
||||||
|
|
||||||
double* jfltarr_getref(jfltarr_t* array) {
|
|
||||||
return array->data;
|
|
||||||
}
|
|
||||||
|
|
||||||
void jfltarr_destroy(jfltarr_t* array) {
|
|
||||||
if (array == NULL) return;
|
|
||||||
free(array->data);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/* Bool array */
|
|
||||||
void* jboolarr_init(jboolarr_t* array) {
|
|
||||||
array->data = malloc(INIT_CAPA * sizeof(bool));
|
|
||||||
if (array->data == NULL) return NULL;
|
|
||||||
memset(array->data, 0, INIT_CAPA);
|
|
||||||
array->capa = INIT_CAPA;
|
|
||||||
array->size = 0;
|
|
||||||
return array->data;
|
|
||||||
}
|
|
||||||
|
|
||||||
void* jboolarr_append(jboolarr_t* array, bool add) {
|
|
||||||
if (array->size + 1 > array->capa) {
|
|
||||||
size_t newcapa = (array->capa * 10) / 6 ;
|
|
||||||
bool* newarray = realloc(array->data, newcapa);
|
|
||||||
if (newarray == NULL) return NULL;
|
|
||||||
array->capa = newcapa;
|
|
||||||
array->data = newarray;
|
|
||||||
}
|
|
||||||
array->data[array->size++] = add;
|
|
||||||
return array->data;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool* jboolarr_getref(jboolarr_t* array) {
|
|
||||||
return array->data;
|
|
||||||
}
|
|
||||||
|
|
||||||
void jboolarr_destroy(jboolarr_t* array) {
|
|
||||||
if (array == NULL) return;
|
|
||||||
free(array->data);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/* Tools */
|
/* Tools */
|
||||||
static char* strcopy(char* src) {
|
static char* strcopy(char* src) {
|
||||||
size_t srcsize = strlen(src) + 1;
|
size_t srcsize = strlen(src) + 1;
|
||||||
|
|||||||
@@ -5,31 +5,8 @@
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
#include <cdynarr.h>
|
||||||
typedef struct {
|
#include <cstring.h>
|
||||||
char* data;
|
|
||||||
int capa;
|
|
||||||
int size;
|
|
||||||
} jstring_t;
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
int64_t* data;
|
|
||||||
int capa;
|
|
||||||
int size;
|
|
||||||
} jintarr_t;
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
bool* data;
|
|
||||||
int capa;
|
|
||||||
int size;
|
|
||||||
} jboolarr_t;
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
double* data;
|
|
||||||
int capa;
|
|
||||||
int size;
|
|
||||||
} jfltarr_t;
|
|
||||||
|
|
||||||
|
|
||||||
typedef struct jblock jblock_t;
|
typedef struct jblock jblock_t;
|
||||||
|
|
||||||
@@ -41,9 +18,9 @@ typedef struct {
|
|||||||
bool flag;
|
bool flag;
|
||||||
char* str;
|
char* str;
|
||||||
double flt;
|
double flt;
|
||||||
jintarr_t numarr;
|
cintarr_t numarr;
|
||||||
jfltarr_t fltarr;
|
cfltarr_t fltarr;
|
||||||
jboolarr_t flagarr;
|
cboolarr_t flagarr;
|
||||||
jblock_t* blk;
|
jblock_t* blk;
|
||||||
jblock_t* blkarr;
|
jblock_t* blkarr;
|
||||||
char** strarr;
|
char** strarr;
|
||||||
@@ -85,26 +62,5 @@ int jblock_addfloat(jblock_t* block, char* key, double val);
|
|||||||
int jblock_addstr(jblock_t* block, char* key, char* val);
|
int jblock_addstr(jblock_t* block, char* key, char* val);
|
||||||
int jblock_addbool(jblock_t* block, char* key, bool val);
|
int jblock_addbool(jblock_t* block, char* key, bool val);
|
||||||
|
|
||||||
jintarr_t* new_jintarr(void);
|
|
||||||
void* jintarr_init(jintarr_t* array);
|
|
||||||
void* jintarr_append(jintarr_t* array, int64_t add);
|
|
||||||
int64_t* jintarr_getref(jintarr_t* array);
|
|
||||||
void jintarr_destroy(jintarr_t* array);
|
|
||||||
void jintarr_free(jintarr_t* array);
|
|
||||||
|
|
||||||
jfltarr_t* new_jfltarr(void);
|
|
||||||
void* jfltarr_init(jfltarr_t* array);
|
|
||||||
void* jfltarr_append(jfltarr_t* array, double add);
|
|
||||||
double* jfltarr_getref(jfltarr_t* array);
|
|
||||||
void jfltarr_destroy(jfltarr_t* array);
|
|
||||||
void jfltarr_free(jfltarr_t* array);
|
|
||||||
|
|
||||||
jboolarr_t* new_jboolarr(void);
|
|
||||||
void* jboolarr_init(jboolarr_t* array);
|
|
||||||
void* jboolarr_append(jboolarr_t* array, bool add);
|
|
||||||
bool* jboolarr_getref(jboolarr_t* array);
|
|
||||||
void jboolarr_destroy(jboolarr_t* array);
|
|
||||||
void jboolarr_free(jboolarr_t* array);
|
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -30,30 +30,30 @@ void test01(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void test02(void) {
|
void test02(void) {
|
||||||
jintarr_t* arr = new_jintarr();
|
cintarr_t* arr = new_cintarr();
|
||||||
jintarr_init(arr);
|
cintarr_init(arr);
|
||||||
|
|
||||||
for (int64_t i = 0; i < 1024 + 1; i++) {
|
for (int64_t i = 0; i < 1024 + 1; i++) {
|
||||||
jintarr_append(arr, i);
|
cintarr_append(arr, i);
|
||||||
}
|
}
|
||||||
printf("size: %d capa: %d\n", arr->size, arr->capa);
|
printf("size: %d capa: %d\n", arr->size, arr->capa);
|
||||||
printf("last: %ld\n", arr->data[arr->size - 1]);
|
printf("last: %ld\n", arr->data[arr->size - 1]);
|
||||||
|
|
||||||
jintarr_free(arr);
|
cintarr_free(arr);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void test03(void) {
|
void test03(void) {
|
||||||
jfltarr_t arr;
|
cfltarr_t arr;
|
||||||
jfltarr_init(&arr);
|
cfltarr_init(&arr);
|
||||||
|
|
||||||
for (int64_t i = 0; i < 1024 + 1; i++) {
|
for (int64_t i = 0; i < 1024 + 1; i++) {
|
||||||
jfltarr_append(&arr, i);
|
cfltarr_append(&arr, i);
|
||||||
}
|
}
|
||||||
printf("size: %d capa: %d\n", arr.size, arr.capa);
|
printf("size: %d capa: %d\n", arr.size, arr.capa);
|
||||||
printf("last: %f\n", arr.data[arr.size - 1]);
|
printf("last: %f\n", arr.data[arr.size - 1]);
|
||||||
|
|
||||||
jfltarr_destroy(&arr);
|
cfltarr_destroy(&arr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -12,10 +12,10 @@
|
|||||||
|
|
||||||
|
|
||||||
#define CLOCK_SOURCE CLOCK_MONOTONIC
|
#define CLOCK_SOURCE CLOCK_MONOTONIC
|
||||||
#define MAX_TS_LEN 256
|
#define MAX_TS_LEN 256 * 2
|
||||||
|
|
||||||
|
|
||||||
static pthread_mutex_t logmutex = NULL;
|
static pthread_mutex_t logmutex;
|
||||||
static pthread_mutexattr_t logmutex_attrs;
|
static pthread_mutexattr_t logmutex_attrs;
|
||||||
|
|
||||||
void log_init(void) {
|
void log_init(void) {
|
||||||
|
|||||||
76
cworker.c
76
cworker.c
@@ -94,7 +94,7 @@ static int cworker_readopts(const cworker_t* worker, char** argv, int argc) {
|
|||||||
log_error("Args parsing error");
|
log_error("Args parsing error");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
clparser_destroy(&parser);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -269,52 +269,54 @@ int cworker_run(const cworker_t* worker) {
|
|||||||
int cworker_handler(const cworker_t* worker, int socket) {
|
int cworker_handler(const cworker_t* worker, int socket) {
|
||||||
int err = 0;
|
int err = 0;
|
||||||
|
|
||||||
rcache_t cache;
|
//rcache_t cache;
|
||||||
jlexer_t lexer;
|
//jlexer_t lexer;
|
||||||
jparser_t parser;
|
//jparser_t parser;
|
||||||
|
|
||||||
rcache_init(&cache, socket);
|
//rcache_init(&cache, socket);
|
||||||
jlexer_init(&lexer, &cache);
|
//jlexer_init(&lexer, &cache);
|
||||||
jparser_init(&parser, &lexer);
|
//jparser_init(&parser, &lexer);
|
||||||
|
|
||||||
if (jparser_parse(&parser) < 0) {
|
//if (jparser_parse(&parser) < 0) {
|
||||||
log_error("Cannot parse json");
|
//log_error("Cannot parse json");
|
||||||
err = -1;
|
//err = -1;
|
||||||
goto exit;
|
//goto exit;
|
||||||
}
|
//}
|
||||||
|
|
||||||
int64_t id = 0;
|
//int64_t id = 0;
|
||||||
char* name = "none";
|
//char* name = "none";
|
||||||
|
|
||||||
if (jparser_bind(&parser, JVALTYPE_NUM, "id", (void *)&id) < 0) {
|
//if (jparser_bind(&parser, JVALTYPE_NUM, "id", (void *)&id) < 0) {
|
||||||
log_error("Cannot bind id");
|
//log_error("Cannot bind id");
|
||||||
}
|
//}
|
||||||
if (jparser_bind(&parser, JVALTYPE_STR, "name", (void *)&name) < 0) {
|
//if (jparser_bind(&parser, JVALTYPE_STR, "name", (void *)&name) < 0) {
|
||||||
log_error("Cannot bind name");
|
//log_error("Cannot bind name");
|
||||||
}
|
//}
|
||||||
|
|
||||||
|
|
||||||
char* msg = NULL;
|
//char* msg = NULL;
|
||||||
asprintf(&msg, "hello, %s!", name);
|
//asprintf(&msg, "hello, %s!", name);
|
||||||
|
|
||||||
jblock_t jb;
|
//jblock_t jb;
|
||||||
jblock_init(&jb);
|
//jblock_init(&jb);
|
||||||
jblock_addstr(&jb, "message", msg);
|
//jblock_addstr(&jb, "message", msg);
|
||||||
jblock_addbool(&jb, "error", false);
|
//jblock_addbool(&jb, "error", false);
|
||||||
|
|
||||||
char* jsonstr = NULL;
|
//char* jsonstr = NULL;
|
||||||
jblock_outjson(&jb, &jsonstr);
|
//jblock_outjson(&jb, &jsonstr);
|
||||||
jblock_destroy(&jb);
|
//jblock_destroy(&jb);
|
||||||
|
|
||||||
write(socket, jsonstr, strlen(jsonstr));
|
//write(socket, jsonstr, strlen(jsonstr));
|
||||||
free(jsonstr);
|
//free(jsonstr);
|
||||||
free(name);
|
//free(name);
|
||||||
free(msg);
|
//free(msg);
|
||||||
|
|
||||||
exit:
|
//exit:
|
||||||
jparser_destroy(&parser);
|
//jparser_destroy(&parser);
|
||||||
jlexer_destroy(&lexer);
|
//jlexer_destroy(&lexer);
|
||||||
rcache_destroy(&cache);
|
//rcache_destroy(&cache);
|
||||||
|
|
||||||
|
close(socket);
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user