Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ZSV_EXTRAS guard to overwrite options #355

Merged
merged 3 commits into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions app/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?=

Expand All @@ -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)
Expand All @@ -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
Expand Down
6 changes: 6 additions & 0 deletions app/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
#include <zsv/utils/string.h>
#include <zsv/utils/dirs.h>
#include <zsv/utils/signal.h>
#ifdef ZSV_EXTRAS
#include <zsv/utils/overwrite.h>
#endif
#include <zsv.h>
#include <zsv/ext.h>
#include "cli_internal.h"
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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),
Expand Down
20 changes: 16 additions & 4 deletions app/echo.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
#include <zsv/utils/string.h>
#include <zsv/utils/mem.h>
#include <zsv/utils/arg.h>
#ifdef ZSV_EXTRAS
#include <zsv/utils/overwrite.h>
#endif

struct zsv_echo_data {
FILE *in;
Expand Down Expand Up @@ -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] <filename>",
"",
Expand All @@ -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 <value> : skip rows until the row where first column starts with the given value",
#ifdef ZSV_EXTRAS
" --overwrite <source> : overwrite cells using given source",
"",
"For --overwrite, the <source> may be:",
Expand All @@ -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,
};

Expand Down Expand Up @@ -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++) {
Expand All @@ -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;
Expand Down Expand Up @@ -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");
Expand All @@ -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;
Expand Down
16 changes: 12 additions & 4 deletions app/test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -180,11 +183,14 @@ test-echo-chars: ${BUILD_DIR}/bin/zsv_echo${EXE}
@${CMP} ${TMP_DIR}/[email protected] expected/[email protected] && ${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}/[email protected]
@${CMP} ${TMP_DIR}/[email protected] expected/[email protected] && ${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
Expand All @@ -196,11 +202,14 @@ test-echo-overwrite-auto: ${BUILD_DIR}/bin/zsv_echo${EXE}
@${CMP} ${TMP_DIR}/[email protected] expected/[email protected] && ${TEST_PASS} || ${TEST_FAIL}
@${PREFIX} $< ${TEST_DATA_DIR}/loans_2.csv --apply-overwrites ${REDIRECT} ${TMP_DIR}/[email protected]
@${CMP} ${TMP_DIR}/[email protected] expected/[email protected] && ${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}/[email protected]
@${CMP} ${TMP_DIR}/[email protected] expected/[email protected] && ${TEST_PASS} || ${TEST_FAIL}
endif

worldcitiespop_mil.csv:
curl -LOk 'https://burntsushi.net/stuff/worldcitiespop_mil.csv'
Expand Down Expand Up @@ -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}/[email protected]
@${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}/[email protected]
@(${PREFIX} $< ${ARGS-$*} -o ${TMP_DIR}/[email protected] --table data --overwrite < ${TMP_DIR}/test-2db.json ${REDIRECT1} ${TMP_DIR}/[email protected])
@${CMP} ${TMP_DIR}/[email protected] expected/[email protected] && ${TEST_PASS} || ${TEST_FAIL}
@sqlite3 ${TMP_DIR}/[email protected] .schema | sed 's/ IF NOT EXISTS//' | sed 's/"data"/data/g' > ${TMP_DIR}/[email protected]
Expand Down Expand Up @@ -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}/[email protected] && \
${CMP} ${TMP_DIR}/[email protected] expected/[email protected] && ${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}/[email protected] --table data --overwrite && (${PREFIX} $< --from-db ${TMP_DIR}/[email protected] ${REDIRECT1} ${TMP_DIR}/[email protected] && ${CMP} ${TMP_DIR}/[email protected] expected/[email protected] && ${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}/[email protected] --table data --overwrite && (${PREFIX} $< --from-db ${TMP_DIR}/[email protected] ${REDIRECT1} ${TMP_DIR}/[email protected] && ${CMP} ${TMP_DIR}/[email protected] expected/[email protected] && ${TEST_PASS} || ${TEST_FAIL})
# ajv validate --strict-tuples=false -s ${THIS_MAKEFILE_DIR}/../../docs/db.schema.json -d expected/[email protected] [suffix must be json]

test-2json-help: test-%-help: ${BUILD_DIR}/bin/zsv_%${EXE}
Expand Down
2 changes: 2 additions & 0 deletions app/utils/prop.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/zsv_internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down