Skip to content

Commit

Permalink
zsv utils lib (#144)
Browse files Browse the repository at this point in the history
* add build cmds for zsvutil.a with writer, dirs (no jq)
  • Loading branch information
liquidaty authored Dec 12, 2023
1 parent d959919 commit 93d349e
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 46 deletions.
46 changes: 18 additions & 28 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ THIS_MAKEFILE_DIR:=$(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
THIS_DIR:=$(shell basename "${THIS_MAKEFILE_DIR}")
THIS_MAKEFILE:=$(lastword $(MAKEFILE_LIST))

THIS_MAKE=`basename ${MAKE}`

CONFIGFILE ?= config.mk
include ${CONFIGFILE}

Expand All @@ -23,59 +25,47 @@ help:
@echo "build and run as described in docs/extension.md"
@echo
@echo "To build, test and install zsvlib and zsv:"
@echo " ./configure && ${MAKE} test"
@echo " ./configure && ${THIS_MAKE} test"
@echo
@echo "To build and install zsvlib and zsv:"
@echo " ./configure && ${MAKE} install"
@echo " ./configure && ${THIS_MAKE} install"
@echo
@echo "To build and install only zsvlib:"
@echo " ./configure && ${MAKE} lib"
@echo " ./configure && ${THIS_MAKE} -C src install"
@echo
@echo "To build and install only zsv (i.e. install both, remove zsvlib):"
@echo " ./configure && ${MAKE} install && ${MAKE} uninstall-lib"
@echo " ./configure && ${THIS_MAKE} install && ${THIS_MAKE} -C src uninstall"
@echo
@echo "To save and build from a configuration without losing the current one,"
@echo "use the configuration option CONFIGFILE e.g.:"
@echo " ./configure --config-file=/path/to/config.custom"
@echo " ./configure && ${MAKE} -C src CONFIGFILE=/path/to/config.custom install"
@echo " ./configure && ${THIS_MAKE} -C src CONFIGFILE=/path/to/config.custom install"
@echo
@echo "To clean (remove temporary build objects) (after running configure):"
@echo " ${MAKE} clean"
@echo " ${THIS_MAKE} clean"
@echo
@echo "To uninstall libs and apps:"
@echo " ${MAKE} uninstall"
@echo " ${THIS_MAKE} uninstall"
@echo
@echo "To test:"
@echo " ${THIS_MAKE} test"
@echo
@echo "Additional make options available for the library or the apps by"
@echo " running ${MAKE} from the src or app directory"
@echo " running ${THIS_MAKE} from the src or app directory"
@echo
@echo "For more information, see README.md"

lib:
@${MAKE} -C src install CONFIGFILE=${CONFIGFILEPATH}

check test:
@${MAKE} -C app test CONFIGFILE=${CONFIGFILEPATH}
@${MAKE} -C examples/lib test CONFIGFILE=${CONFIGFILEPATH}

install:
@${MAKE} -C src install CONFIGFILE=${CONFIGFILEPATH}
@${MAKE} -C app install CONFIGFILE=${CONFIGFILEPATH}

all:
@${MAKE} -C src install CONFIGFILE=${CONFIGFILEPATH}
@${MAKE} -C app all CONFIGFILE=${CONFIGFILEPATH}
build install uninstall: % :
@${MAKE} -C src $* CONFIGFILE=${CONFIGFILEPATH}
@${MAKE} -C app $* CONFIGFILE=${CONFIGFILEPATH}

clean:
@${MAKE} -C app clean-all CONFIGFILE=${CONFIGFILEPATH}
@${MAKE} -C src clean CONFIGFILE=${CONFIGFILEPATH}
@${MAKE} -C app clean-all CONFIGFILE=${CONFIGFILEPATH}
@rm -rf ${THIS_MAKEFILE_DIR}/build

uninstall: uninstall-lib uninstall-app

uninstall-app:
${MAKE} -C app uninstall CONFIGFILE=${CONFIGFILEPATH}

uninstall-lib:
${MAKE} -C src uninstall CONFIGFILE=${CONFIGFILEPATH}

.PHONY: help install uninstall uninstall-app uninstall-lib check
.PHONY: help build install uninstall uninstall clean check test
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ that implements the expected

## Key highlights

* Available as BOTH a library and an application
* Available as BOTH a library and an application (coming soon: standalone zsvutil library for common helper functions such as csv writer)
* Open-source, permissively licensed
* Handles real-world CSV the same way that spreadsheet programs do (*including
edge cases*). Gracefully handles (and can "clean") real-world data that may be
Expand Down
4 changes: 1 addition & 3 deletions app/2db.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,7 @@ struct zsv_2db_data {
char *connection_string;

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

Expand Down Expand Up @@ -701,7 +699,7 @@ int ZSV_MAIN_FUNC(ZSV_COMMAND)(int argc, const char *argv[], struct zsv_opts *zs
" https://github.com/liquidaty/zsv/blob/main/app/schema/database-table.json",
"",
"For example:",
" ["
" [",
" {",
" \"columns\":[{\"name\":\"column 1\"}],",
" \"indexes\":{\"ix1\":{\"on\":\"[column 1]\",\"unique\":true}}",
Expand Down
34 changes: 31 additions & 3 deletions app/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -266,10 +266,31 @@ help:

install: ${ZSV}

uninstall-all: uninstall uninstall-util-lib

uninstall:
rm -rf ${ZSV}

build: all
ZSV_UTIL_A=${LIBDIR}/libzsvutil.a
${ZSV_UTIL_A}: ${BUILD_DIR}/objs/utils/util.a
@mkdir -p `dirname $@`
cp -p $< $@

UTIL_A_OBJ:=writer file dirs-no-jq os
UTIL_A_OBJ:=$(addprefix ${BUILD_DIR}/objs/utils/,$(addsuffix .o,${UTIL_A_OBJ}))

${BUILD_DIR}/objs/utils/util.a: ${UTIL_A_OBJ}
$(AR) rcv $@ $^ # $?
$(RANLIB) $@
$(AR) -t $@ # check it is there
@echo Built $@

uninstall-util-lib:
@rm -f ${ZSV_UTIL_A}

install-util-lib: ${ZSV_UTIL_A}

build: ${CLI}

all: ${TARGETS}

Expand Down Expand Up @@ -299,7 +320,7 @@ ${CLEANS}: clean-%:
rm -f ${STANDALONE_PFX}$*${EXE}
rm -f ${BUILD_DIR}/*/*$*.o

.PHONY: all install cli build build-% clean clean-% test test-% lib-% check
.PHONY: all install cli build build-% clean clean-% test test-% lib-% check install-util-lib uninstall-util-lib clean-util-lib

.SECONDARY: ${OBJECTS}

Expand All @@ -311,6 +332,10 @@ ${BUILD_DIR}/objs/utils/%.o : utils/%.c ${INCLUDE_DIR}/zsv/utils/%.h ${JQ_LIB}
@mkdir -p `dirname "$@"`
${CC} ${CFLAGS} -I${INCLUDE_DIR} -I${UTF8PROC_INCLUDE} -DINCLUDE_SRC -o $@ -c utils/$*.c ${MORE_SOURCE}

${BUILD_DIR}/objs/utils/dirs-no-jq.o : utils/dirs.c ${INCLUDE_DIR}/zsv/utils/dirs.h
@mkdir -p `dirname "$@"`
${CC} ${CFLAGS} -I${INCLUDE_DIR} -I${UTF8PROC_INCLUDE} -DINCLUDE_SRC -o $@ -c utils/dirs-no-jq.c ${MORE_SOURCE}

${BUILD_DIR}/objs/zsv_%.o: %.c
@mkdir -p `dirname "$@"`
${CC} ${CFLAGS} -I${INCLUDE_DIR} -I${UTF8PROC_INCLUDE} -c $< -o $@
Expand Down Expand Up @@ -418,11 +443,14 @@ test-standalone:
test-cli: ${CLI}
@${MAKE} -C test $@ QUIET=1 LEAKS=${LEAKS} CONFIGFILE=${CONFIGFILEPATH} DEBUG=${DEBUG} CLI=${CLI}

clean-all: clean clean-external clean-obj clean-lib
clean-all: clean clean-external clean-obj clean-lib clean-util-lib

clean-external:
@rm -rf ${JQ_SRC}

clean-util-lib:
@rm -f ${BUILD_DIR}/objs/utils/util.a

clean-lib:
@rm -rf ${BUILD_DIR}-external

Expand Down
2 changes: 2 additions & 0 deletions app/utils/dirs-no-jq.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#define ZSV_NO_JQ
#include "dirs.c"
6 changes: 3 additions & 3 deletions app/utils/writer.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ void zsv_writer_set_default_opts(struct zsv_csv_writer_options opts) {
zsv_csv_writer_default_opts = opts;
}

struct zsv_csv_writer_options zsv_writer_get_default_opts() {
struct zsv_csv_writer_options zsv_writer_get_default_opts(void) {
if(!zsv_writer_default_opts_initd) {
zsv_writer_default_opts_initd = 1;
zsv_csv_writer_default_opts.write = (size_t (*)(const void * restrict, size_t, size_t, void * restrict))fwrite;
Expand Down Expand Up @@ -238,10 +238,10 @@ enum zsv_writer_status zsv_writer_cell(zsv_csv_writer w, char new_row,

if(VERY_UNLIKELY(w->cell_prepend && *w->cell_prepend)) {
char *tmp = NULL;
asprintf(&tmp, "%s%.*s", w->cell_prepend, (int)len, s);
asprintf(&tmp, "%s%.*s", w->cell_prepend, (int)len, s ? s : (const unsigned char *)"");
if(!tmp)
return zsv_writer_status_error; // zsv_writer_status_memory;
s = (const char *)tmp;
s = (const unsigned char *)tmp;
len = len + strlen(w->cell_prepend);
enum zsv_writer_status stat = zsv_writer_cell_aux(w, s, len, 1);
free(tmp);
Expand Down
2 changes: 2 additions & 0 deletions app/zsv_main.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#define ZSV_MAIN_FUNC1(x) zsv_ ## x ## _main
#define ZSV_MAIN_NO_OPTIONS_FUNC1(x) zsv_ ## x ## _main_no_options

struct zsv_opts;

/* macros for commands that use common zsv parsing */
#define ZSV_MAIN_FUNC(x) ZSV_MAIN_FUNC1(x)
#define ZSV_MAIN_DECL(x) int ZSV_MAIN_FUNC(x)(int argc, const char *argv[], struct zsv_opts *opts, struct zsv_prop_handler *custom_prop_handler, const char *opts_used)
Expand Down
6 changes: 6 additions & 0 deletions include/zsv/utils/dirs.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ size_t zsv_get_executable_path(char* buff, size_t buffsize);
*/
int zsv_dir_exists(const char *path);

/**
* mkdir that works with UNC paths on Win
* if fail, use zsv_perror() instead of perror()
*/
int zsv_mkdir(const char *path);

/**
* Make a directory, as well as any intermediate dirs
* return zero on success
Expand Down
2 changes: 1 addition & 1 deletion include/zsv/utils/writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ struct zsv_csv_writer_options {
};

void zsv_writer_set_default_opts(struct zsv_csv_writer_options opts);
struct zsv_csv_writer_options zsv_writer_get_default_opts();
struct zsv_csv_writer_options zsv_writer_get_default_opts(void);

enum zsv_writer_status {
zsv_writer_status_ok = 0,
Expand Down
15 changes: 8 additions & 7 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,14 @@ endif


help:
@echo "${MAKE} [CONFIGFILE=config.mk] [NO_UTF8_CHECK=1] [VERBOSE=1] [LIBDIR=${LIBDIR}] [INCLUDEDIR=${INCLUDEDIR}] [LIB_SUFFIX=]"
@echo "Make options:"
@echo " `basename ${MAKE}` build|install|uninstall|clean"
@echo
@echo "Optional ake variables:"
@echo " [CONFIGFILE=config.mk] [NO_UTF8_CHECK=1] [VERBOSE=1] [LIBDIR=${LIBDIR}] [INCLUDEDIR=${INCLUDEDIR}] [LIB_SUFFIX=]"
@echo

lib: ${LIBZSV}

all: lib
build: ${LIBZSV}

${LIBZSV}: ${ZSV_OBJ}
@mkdir -p `dirname "$@"`
Expand All @@ -126,13 +129,11 @@ ${LIBZSV}: ${ZSV_OBJ}
$(AR) -t $@ # check it is there
@echo Built $@


install: ${LIBZSV_INSTALL}
@mkdir -p $(INCLUDEDIR)
@cp -pR ../include/* $(INCLUDEDIR)
@echo "include files copied to $(INCLUDEDIR)"


${LIBZSV_INSTALL}: ${LIBZSV}
@mkdir -p `dirname "$@"`
@cp -p ${LIBZSV} "$@"
Expand All @@ -145,7 +146,7 @@ uninstall:
clean:
rm -rf ${BUILD_DIR}/objs ${LIBZSV}

.PHONY: all install clean lib ${LIBZSV_INSTALL}
.PHONY: build install uninstall clean ${LIBZSV_INSTALL}

${BUILD_DIR}/objs/zsv.o: zsv.c zsv_internal.c
@mkdir -p `dirname "$@"`
Expand Down

0 comments on commit 93d349e

Please sign in to comment.