Skip to content

Commit

Permalink
echo: add --skip-until and related test (#151)
Browse files Browse the repository at this point in the history
  • Loading branch information
liquidaty committed Feb 23, 2024
1 parent 3acfe72 commit 6e68a5a
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 8 deletions.
34 changes: 32 additions & 2 deletions app/echo.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ struct zsv_echo_data {
} sqlite3;
} o;

unsigned char *skip_until_prefix;
size_t skip_until_prefix_len;
unsigned char trim_white:1;
unsigned char _:7;
};
Expand Down Expand Up @@ -111,6 +113,17 @@ static void zsv_echo_row(void *hook) {
data->row_ix++;
}

static void zsv_echo_row_skip_until(void *hook) {
struct zsv_echo_data *data = hook;
struct zsv_cell cell = zsv_get_cell(data->parser, 0);
if(cell.len && cell.str && cell.len >= data->skip_until_prefix_len
&& (!data->skip_until_prefix_len || !zsv_strincmp(cell.str, data->skip_until_prefix_len,
data->skip_until_prefix, data->skip_until_prefix_len))) {
zsv_set_row_handler(data->parser, zsv_echo_row);
zsv_echo_row(hook);
}
}

const char *zsv_echo_usage_msg[] = {
APPNAME ": write tabular input to stdout with optional cell overwrites",
"",
Expand All @@ -119,6 +132,7 @@ const char *zsv_echo_usage_msg[] = {
"Options:",
" -b : output with BOM",
" --trim : trim whitespace",
" --skip-until <value>: ignore all leading rows until the first row whose first column starts with the given value ",
" --overwrite <source>: overwrite cells using given source. Source may be:",
" - sqlite3://<filename>[?sql=<query>]",
" ex: sqlite3://overwrites.db?sql=select row, column, value from overwrites order by row, column",
Expand All @@ -136,6 +150,7 @@ static int zsv_echo_usage() {
static void zsv_echo_cleanup(struct zsv_echo_data *data) {
zsv_writer_delete(data->csv_writer);
free(data->o.sqlite3.filename);
free(data->skip_until_prefix);
if(data->o.sqlite3.stmt)
sqlite3_finalize(data->o.sqlite3.stmt);
if(data->in && data->in != stdin)
Expand Down Expand Up @@ -216,7 +231,19 @@ int ZSV_MAIN_FUNC(ZSV_COMMAND)(int argc, const char *argv[], struct zsv_opts *op
writer_opts.with_bom = 1;
else if(!strcmp(arg, "--trim"))
data.trim_white = 1;
else if(!strcmp(arg, "--overwrite")) {
else if(!strcmp(arg, "--skip-until")) {
if(++arg_i >= argc) {
fprintf(stderr, "Option %s requires a value\n", arg);
err = 1;
} else if(!argv[arg_i][0]) {
fprintf(stderr, "--skip-until requires a non-empty value\n");
err = 1;
} else {
free(data.skip_until_prefix);
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")) {
if(arg_i+1 >= argc) {
fprintf(stderr, "Option %s requires a value\n", arg);
err = 1;
Expand Down Expand Up @@ -261,7 +288,10 @@ int ZSV_MAIN_FUNC(ZSV_COMMAND)(int argc, const char *argv[], struct zsv_opts *op
return 1;
}

opts->row_handler = zsv_echo_row;
if(data.skip_until_prefix)
opts->row_handler = zsv_echo_row_skip_until;
else
opts->row_handler = zsv_echo_row;
opts->stream = data.in;
opts->ctx = &data;
data.csv_writer = zsv_writer_new(&writer_opts);
Expand Down
3 changes: 1 addition & 2 deletions app/select.c
Original file line number Diff line number Diff line change
Expand Up @@ -557,8 +557,7 @@ const char *zsv_select_usage_msg[] = {
" -H,--head <n> : (head) only process the first n rows of data from all rows (including header) in the input",
" --no-header : do not output a header row",
" --prepend-header <value> : prepend each column header with the given text value",
" -s,--search <value> : only output rows with at least one cell containing"
" value",
" -s,--search <value> : only output rows with at least one cell containing value",
// to do: " -s,--search /<pattern>/modifiers: search on regex pattern; modifiers include 'g' (global) and 'i' (case-insensitive)",
" --sample-every <num of rows>: output a sample consisting of the first row, then every nth row",
" --sample-pct <percentage> : output a randomly-selected sample (32 bits of randomness) of n percent of the input rows",
Expand Down
9 changes: 7 additions & 2 deletions app/test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ test: ${TESTS}
test-prop:
EXE=${BUILD_DIR}/bin/zsv_prop${EXE} make -C prop test

test-echo : test-echo1 test-echo-overwrite test-echo-eol test-echo-overwrite-csv test-echo-chars test-echo-trim
test-echo : test-echo1 test-echo-overwrite test-echo-eol test-echo-overwrite-csv test-echo-chars test-echo-trim test-echo-skip-until

test-echo1: ${BUILD_DIR}/bin/zsv_echo${EXE}
@${TEST_INIT}
Expand All @@ -116,7 +116,12 @@ test-echo-eol-%: ${BUILD_DIR}/bin/zsv_echo${EXE}

test-echo-trim: ${BUILD_DIR}/bin/zsv_echo${EXE}
@${TEST_INIT}
@${PREFIX} $< ${TEST_DATA_DIR}/test/echo-trim.csv ${REDIRECT} ${TMP_DIR}/$@.out
@${PREFIX} $< --trim ${TEST_DATA_DIR}/test/echo-trim.csv ${REDIRECT} ${TMP_DIR}/$@.out
@${CMP} ${TMP_DIR}/$@.out expected/$@.out && ${TEST_PASS} || ${TEST_FAIL}

test-echo-skip-until: ${BUILD_DIR}/bin/zsv_echo${EXE}
@${TEST_INIT}
@${PREFIX} $< --skip-until ASF ${TEST_DATA_DIR}/test/echo-skip-until.csv ${REDIRECT} ${TMP_DIR}/$@.out
@${CMP} ${TMP_DIR}/$@.out expected/$@.out && ${TEST_PASS} || ${TEST_FAIL}

test-echo-chars: ${BUILD_DIR}/bin/zsv_echo${EXE}
Expand Down
4 changes: 4 additions & 0 deletions app/test/expected/test-echo-skip-until.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
asfdd,8,9
10,11,12
asfd,13,14
15,16,17
4 changes: 2 additions & 2 deletions app/test/expected/test-echo-trim.out
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
hi, there
how ,are, you,?
hi,there
how,are,you,?
6 changes: 6 additions & 0 deletions data/test/echo-skip-until.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
1,2,3
4,5,6
asfdd,8,9
10,11,12
asfd,13,14
15,16,17

0 comments on commit 6e68a5a

Please sign in to comment.