From 9db4bd2f99dff430485710d303b7435e269e1edc Mon Sep 17 00:00:00 2001 From: liquidaty Date: Mon, 23 Dec 2024 01:28:35 -0800 Subject: [PATCH] add ZSV_EXTRAS guard to overwrite options (#355) * add missing ZSV_EXTRAS guards --- app/Makefile | 17 ++++++++++++++--- app/cli.c | 6 ++++++ app/echo.c | 20 ++++++++++++++++---- app/test/Makefile | 16 ++++++++++++---- app/utils/prop.c | 2 ++ src/zsv_internal.c | 2 ++ 6 files changed, 52 insertions(+), 11 deletions(-) diff --git a/app/Makefile b/app/Makefile index a4c3b312..e9630b76 100644 --- a/app/Makefile +++ b/app/Makefile @@ -161,7 +161,7 @@ CFLAGS+= -I${PREFIX}/include THIS_LIB_BASE=$(shell cd .. && pwd) INCLUDE_DIR=${THIS_LIB_BASE}/include BUILD_DIR=${THIS_LIB_BASE}/build/${BUILD_SUBDIR}/${CCBN} -UTILS1=writer file err signal mem clock arg dl string dirs prop cache jq os overwrite index overwrite_writer +UTILS1=writer file err signal mem clock arg dl string dirs prop cache jq os index ZSV_EXTRAS ?= @@ -175,6 +175,11 @@ else # not emcc CFLAGS+= ${CFLAGS_AVX} ${CFLAGS_SSE} LDFLAGS+=-lpthread # Linux explicitly requires endif + +ifeq ($(ZSV_EXTRAS),1) + UTILS1+= overwrite overwrite_writer +endif + UTILS=$(addprefix ${BUILD_DIR}/objs/utils/,$(addsuffix .o,${UTILS1})) ifeq ($(NO_THREADING),1) @@ -195,8 +200,14 @@ endif ZSV=$(BINDIR)/zsv${EXE} -SOURCES=echo paste count count-pull select select-pull 2tsv 2json serialize flatten pretty stack desc sql 2db compare prop rm mv jq overwrite -CLI_SOURCES=echo select desc count paste 2tsv pretty sql flatten 2json serialize stack 2db compare prop rm mv jq overwrite +SOURCES=echo paste count count-pull select select-pull 2tsv 2json serialize flatten pretty stack desc sql 2db compare prop rm mv jq +ifeq ($(ZSV_EXTRAS),1) + SOURCES+=overwrite +endif +CLI_SOURCES=echo select desc count paste 2tsv pretty sql flatten 2json serialize stack 2db compare prop rm mv jq +ifeq ($(ZSV_EXTRAS),1) + CLI_SOURCES+=overwrite +endif ifeq ($(ZSVSHEET_BUILD),1) SOURCES+=sheet diff --git a/app/cli.c b/app/cli.c index cc71173b..bcde4876 100644 --- a/app/cli.c +++ b/app/cli.c @@ -15,7 +15,9 @@ #include #include #include +#ifdef ZSV_EXTRAS #include +#endif #include #include #include "cli_internal.h" @@ -92,7 +94,9 @@ ZSV_MAIN_DECL(compare); ZSV_MAIN_DECL(sheet); #endif ZSV_MAIN_DECL(echo); +#ifdef ZSV_EXTRAS ZSV_MAIN_DECL(overwrite); +#endif ZSV_MAIN_NO_OPTIONS_DECL(prop); ZSV_MAIN_NO_OPTIONS_DECL(rm); ZSV_MAIN_NO_OPTIONS_DECL(mv); @@ -138,7 +142,9 @@ struct builtin_cmd builtin_cmds[] = { CLI_BUILTIN_COMMANDEXT(sheet), #endif CLI_BUILTIN_COMMAND(echo), +#ifdef ZSV_EXTRAS CLI_BUILTIN_COMMAND(overwrite), +#endif CLI_BUILTIN_NO_OPTIONS_COMMAND(prop), CLI_BUILTIN_NO_OPTIONS_COMMAND(rm), CLI_BUILTIN_NO_OPTIONS_COMMAND(mv), diff --git a/app/echo.c b/app/echo.c index e3cc1107..f1d83dfe 100644 --- a/app/echo.c +++ b/app/echo.c @@ -22,7 +22,9 @@ #include #include #include +#ifdef ZSV_EXTRAS #include +#endif struct zsv_echo_data { FILE *in; @@ -94,7 +96,11 @@ static void zsv_echo_row_skip_until(void *hook) { } const char *zsv_echo_usage_msg[] = { +#ifdef ZSV_EXTRAS APPNAME ": write tabular input to stdout with optional cell overwrites", +#else + APPNAME ": write tabular input to stdout", +#endif "", "Usage: " APPNAME " [options] ", "", @@ -105,6 +111,7 @@ const char *zsv_echo_usage_msg[] = { " --trim-columns : trim blank columns", " --contiguous : stop output upon scanning an entire row of blank values", " --skip-until : skip rows until the row where first column starts with the given value", +#ifdef ZSV_EXTRAS " --overwrite : overwrite cells using given source", "", "For --overwrite, the may be:", @@ -113,6 +120,7 @@ const char *zsv_echo_usage_msg[] = { "", "- /path/to/file.csv", " path to CSV file with columns row,col,val (in that order) and rows pre-sorted by row and column", +#endif NULL, }; @@ -143,8 +151,9 @@ int ZSV_MAIN_FUNC(ZSV_COMMAND)(int argc, const char *argv[], struct zsv_opts *op struct zsv_opts opts = *optsp; struct zsv_csv_writer_options writer_opts = zsv_writer_get_default_opts(); struct zsv_echo_data data = {0}; +#ifdef ZSV_EXTRAS struct zsv_overwrite_opts overwrite_opts = {0}; - +#endif int err = 0; for (int arg_i = 1; !err && arg_i < argc; arg_i++) { @@ -171,9 +180,11 @@ int ZSV_MAIN_FUNC(ZSV_COMMAND)(int argc, const char *argv[], struct zsv_opts *op data.skip_until_prefix = (unsigned char *)strdup(argv[arg_i]); data.skip_until_prefix_len = data.skip_until_prefix ? strlen((char *)data.skip_until_prefix) : 0; } - } else if (!strcmp(arg, "--overwrite")) +#ifdef ZSV_EXTRAS + } else if (!strcmp(arg, "--overwrite")) { overwrite_opts.src = zsv_next_arg(++arg_i, argc, argv, &err); - else if (!data.in) { +#endif + } else if (!data.in) { #ifndef NO_STDIN if (!strcmp(arg, "-")) data.in = stdin; @@ -261,6 +272,7 @@ int ZSV_MAIN_FUNC(ZSV_COMMAND)(int argc, const char *argv[], struct zsv_opts *op opts.ctx = &data; data.csv_writer = zsv_writer_new(&writer_opts); +#ifdef ZSV_EXTRAS if (overwrite_opts.src) { if (!(opts.overwrite.ctx = zsv_overwrite_context_new(&overwrite_opts))) { fprintf(stderr, "Out of memory!\n"); @@ -271,7 +283,7 @@ int ZSV_MAIN_FUNC(ZSV_COMMAND)(int argc, const char *argv[], struct zsv_opts *op opts.overwrite.close = zsv_overwrite_context_delete; } } - +#endif if (data.csv_writer && !err) { if (zsv_new_with_properties(&opts, custom_prop_handler, data.input_path, &data.parser) != zsv_status_ok) err = 1; diff --git a/app/test/Makefile b/app/test/Makefile index cf56d255..62cf3ed6 100644 --- a/app/test/Makefile +++ b/app/test/Makefile @@ -45,7 +45,10 @@ BUILD_DIR=${THIS_LIB_BASE}/build/${BUILD_SUBDIR}/${CCBN} export TMP_DIR=${THIS_LIB_BASE}/tmp TEST_DATA_DIR=${THIS_LIB_BASE}/data -SOURCES=echo count count-pull select select-pull sql 2json serialize flatten pretty desc stack 2db 2tsv jq compare overwrite +SOURCES=echo count count-pull select select-pull sql 2json serialize flatten pretty desc stack 2db 2tsv jq compare +ifneq ($(ZSV_EXTRAS),) + SOURCES+=overwrite +endif TARGETS=$(addprefix ${BUILD_DIR}/bin/zsv_,$(addsuffix ${EXE},${SOURCES})) TESTS=test-blank-leading-rows $(addprefix test-,${SOURCES}) test-rm test-mv test-2json-help test-paste @@ -180,11 +183,14 @@ test-echo-chars: ${BUILD_DIR}/bin/zsv_echo${EXE} @${CMP} ${TMP_DIR}/$@.out expected/$@.out && ${TEST_PASS} || ${TEST_FAIL} test-echo-overwrite: ${BUILD_DIR}/bin/zsv_echo${EXE} +ifneq ($(ZSV_EXTRAS),) @${TEST_INIT} @${PREFIX} $< ${TEST_DATA_DIR}/loans_1.csv --overwrite 'sqlite3://${TEST_DATA_DIR}/loans_1-overwrite.db?sql=select row,col,value from overwrites order by row,col' ${REDIRECT} ${TMP_DIR}/$@.out @${CMP} ${TMP_DIR}/$@.out expected/$@.out && ${TEST_PASS} || ${TEST_FAIL} +endif test-echo-overwrite-auto: ${BUILD_DIR}/bin/zsv_echo${EXE} +ifneq ($(ZSV_EXTRAS),) @${TEST_INIT} @rm -f ${TEST_DATA_DIR}/.zsv/data/loans_2.csv/overwrite.sqlite3 @cp -p ${TEST_DATA_DIR}/loans_1.csv ${TEST_DATA_DIR}/loans_2.csv @@ -196,11 +202,14 @@ test-echo-overwrite-auto: ${BUILD_DIR}/bin/zsv_echo${EXE} @${CMP} ${TMP_DIR}/$@-after-ignored.out expected/$@-after-ignored.out && ${TEST_PASS} || ${TEST_FAIL} @${PREFIX} $< ${TEST_DATA_DIR}/loans_2.csv --apply-overwrites ${REDIRECT} ${TMP_DIR}/$@-after-applied.out @${CMP} ${TMP_DIR}/$@-after-applied.out expected/$@-after-applied.out && ${TEST_PASS} || ${TEST_FAIL} +endif test-echo-overwrite-csv: ${BUILD_DIR}/bin/zsv_echo${EXE} ${TEST_DATA_DIR}/loans_1-overwrite.csv +ifneq ($(ZSV_EXTRAS),) @${TEST_INIT} @${PREFIX} $< ${TEST_DATA_DIR}/loans_1.csv --overwrite '${TEST_DATA_DIR}/loans_1-overwrite.csv' ${REDIRECT} ${TMP_DIR}/$@.out @${CMP} ${TMP_DIR}/$@.out expected/$@.out && ${TEST_PASS} || ${TEST_FAIL} +endif worldcitiespop_mil.csv: curl -LOk 'https://burntsushi.net/stuff/worldcitiespop_mil.csv' @@ -503,7 +512,7 @@ ${BUILD_DIR}/bin/zsv_%${EXE}: test-2db: test-%: ${BUILD_DIR}/bin/zsv_%${EXE} worldcitiespop_mil.csv ${BUILD_DIR}/bin/zsv_2json${EXE} ${BUILD_DIR}/bin/zsv_select${EXE} @${TEST_INIT} - @${BUILD_DIR}/bin/zsv_select${EXE} -L 25000 -N worldcitiespop_mil.csv | ${BUILD_DIR}/bin/zsv_2json${EXE} --database --index "country_ix on country" --unique-index "ux on [#]" > ${TMP_DIR}/$@.json + @${BUILD_DIR}/bin/zsv_select${EXE} -H 24999 -N worldcitiespop_mil.csv | ${BUILD_DIR}/bin/zsv_2json${EXE} --database --index "country_ix on country" --unique-index "ux on [#]" > ${TMP_DIR}/$@.json @(${PREFIX} $< ${ARGS-$*} -o ${TMP_DIR}/$@.db --table data --overwrite < ${TMP_DIR}/test-2db.json ${REDIRECT1} ${TMP_DIR}/$@.out) @${CMP} ${TMP_DIR}/$@.out expected/$@.out && ${TEST_PASS} || ${TEST_FAIL} @sqlite3 ${TMP_DIR}/$@.db .schema | sed 's/ IF NOT EXISTS//' | sed 's/"data"/data/g' > ${TMP_DIR}/$@.out2 @@ -543,8 +552,7 @@ test-2json: test-%: ${BUILD_DIR}/bin/zsv_%${EXE} ${BUILD_DIR}/bin/zsv_2db${EXE} @(${PREFIX} $< --object --no-empty < ${TEST_DATA_DIR}/quoted4.csv ${REDIRECT1} ${TMP_DIR}/$@.out6 && \ ${CMP} ${TMP_DIR}/$@.out6 expected/$@.out6 && ${TEST_PASS} || ${TEST_FAIL}) - @${BUILD_DIR}/bin/zsv_select${EXE} -L 2000 -N worldcitiespop_mil.csv | ${BUILD_DIR}/bin/zsv_2json${EXE} --database --index "country_ix on country" --unique-index "ux on [#]" | ${BUILD_DIR}/bin/zsv_2db${EXE} -o ${TMP_DIR}/$@.db --table data --overwrite && (${PREFIX} $< --from-db ${TMP_DIR}/$@.db ${REDIRECT1} ${TMP_DIR}/$@.out7 && ${CMP} ${TMP_DIR}/$@.out7 expected/$@.out7 && ${TEST_PASS} || ${TEST_FAIL}) - + @${BUILD_DIR}/bin/zsv_select${EXE} -H 1999 -N worldcitiespop_mil.csv | ${BUILD_DIR}/bin/zsv_2json${EXE} --database --index "country_ix on country" --unique-index "ux on [#]" | ${BUILD_DIR}/bin/zsv_2db${EXE} -o ${TMP_DIR}/$@.db --table data --overwrite && (${PREFIX} $< --from-db ${TMP_DIR}/$@.db ${REDIRECT1} ${TMP_DIR}/$@.out7 && ${CMP} ${TMP_DIR}/$@.out7 expected/$@.out7 && ${TEST_PASS} || ${TEST_FAIL}) # ajv validate --strict-tuples=false -s ${THIS_MAKEFILE_DIR}/../../docs/db.schema.json -d expected/$@.out7.json [suffix must be json] test-2json-help: test-%-help: ${BUILD_DIR}/bin/zsv_%${EXE} diff --git a/app/utils/prop.c b/app/utils/prop.c index 82ab3a34..7297bd3b 100644 --- a/app/utils/prop.c +++ b/app/utils/prop.c @@ -257,8 +257,10 @@ enum zsv_status zsv_new_with_properties(struct zsv_opts *opts, struct zsv_prop_h if (fp.stat != zsv_status_ok) return fp.stat; } +#ifdef ZSV_EXTRAS if (opts->overwrite_auto) zsv_overwrite_auto(opts, input_path); +#endif if ((*handle_out = zsv_new(opts))) return zsv_status_ok; return zsv_status_memory; diff --git a/src/zsv_internal.c b/src/zsv_internal.c index 3fa392a1..39c50711 100644 --- a/src/zsv_internal.c +++ b/src/zsv_internal.c @@ -591,9 +591,11 @@ static void set_callbacks(struct zsv_scanner *scanner) { scanner->opts.cell_handler = NULL; scanner->opts.ctx = scanner; } else { +#ifdef ZSV_EXTRAS if (scanner->overwrite.odata.have) scanner->get_cell = zsv_get_cell_with_overwrite; else +#endif scanner->get_cell = zsv_get_cell_1; scanner->data_row_count = 0; scanner->opts.row_handler = scanner->opts_orig.row_handler;