Skip to content

Commit

Permalink
Remove opts_used; replace with opts.property_overrides (#315)
Browse files Browse the repository at this point in the history
* replace opts_used with opts.property_overrides
* TO DO: fix bug-- see test-sheet-prop-cmd-opt
  • Loading branch information
liquidaty authored Nov 30, 2024
1 parent af96426 commit 7e22f10
Show file tree
Hide file tree
Showing 46 changed files with 214 additions and 266 deletions.
3 changes: 1 addition & 2 deletions app/2db.c
Original file line number Diff line number Diff line change
Expand Up @@ -637,9 +637,8 @@ static yajl_handle zsv_2db_yajl_handle(zsv_2db_handle data) {
}

int ZSV_MAIN_FUNC(ZSV_COMMAND)(int argc, const char *argv[], struct zsv_opts *zsv_opts,
struct zsv_prop_handler *custom_prop_handler, const char *opts_used) {
struct zsv_prop_handler *custom_prop_handler) {
(void)(zsv_opts);
(void)(opts_used);
(void)(custom_prop_handler);
FILE *f_in = NULL;
int err = 0;
Expand Down
33 changes: 17 additions & 16 deletions app/2json.c
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,9 @@ static int zsv_db2json(const char *input_filename, char **tname, jsonwriter_hand
return err;
}

int ZSV_MAIN_FUNC(ZSV_COMMAND)(int argc, const char *argv[], struct zsv_opts *opts,
struct zsv_prop_handler *custom_prop_handler, const char *opts_used) {
int ZSV_MAIN_FUNC(ZSV_COMMAND)(int argc, const char *argv[], struct zsv_opts *optsp,
struct zsv_prop_handler *custom_prop_handler) {
struct zsv_opts opts = *optsp;
struct zsv_2json_data data = {0};
data.headers_next = &data.headers;

Expand Down Expand Up @@ -291,9 +292,9 @@ int ZSV_MAIN_FUNC(ZSV_COMMAND)(int argc, const char *argv[], struct zsv_opts *op
} else if (!strcmp(argv[i], "--from-db")) {
if (++i >= argc)
fprintf(stderr, "%s option requires a filename value\n", argv[i - 1]), err = zsv_status_error;
else if (opts->stream)
else if (opts.stream)
fprintf(stderr, "Input file specified more than once\n"), err = zsv_status_error;
else if (!(opts->stream = fopen(argv[i], "rb")))
else if (!(opts.stream = fopen(argv[i], "rb")))
fprintf(stderr, "Unable to open for reading: %s\n", argv[i]), err = zsv_status_error;
else {
input_path = argv[i];
Expand All @@ -311,9 +312,9 @@ int ZSV_MAIN_FUNC(ZSV_COMMAND)(int argc, const char *argv[], struct zsv_opts *op
else if (!strcmp(argv[i], "--compact"))
data.compact = 1;
else {
if (opts->stream)
if (opts.stream)
fprintf(stderr, "Input file specified more than once\n"), err = zsv_status_error;
else if (!(opts->stream = fopen(argv[i], "rb")))
else if (!(opts.stream = fopen(argv[i], "rb")))
fprintf(stderr, "Unable to open for reading: %s\n", argv[i]), err = zsv_status_error;
else
input_path = argv[i];
Expand All @@ -327,14 +328,14 @@ int ZSV_MAIN_FUNC(ZSV_COMMAND)(int argc, const char *argv[], struct zsv_opts *op
fprintf(stderr, "--no-header cannot be used together with --object or --database\n"), err = zsv_status_error;
else if (data.no_empty && data.schema != ZSV_JSON_SCHEMA_OBJECT)
fprintf(stderr, "--no-empty can only be used with --object\n"), err = zsv_status_error;
else if (!opts->stream) {
else if (!opts.stream) {
if (data.from_db)
fprintf(stderr, "Database input specified, but no input file provided\n"), err = zsv_status_error;
else {
#ifdef NO_STDIN
fprintf(stderr, "Please specify an input file\n"), err = zsv_status_error;
#else
opts->stream = stdin;
opts.stream = stdin;
#endif
}
}
Expand All @@ -349,15 +350,15 @@ int ZSV_MAIN_FUNC(ZSV_COMMAND)(int argc, const char *argv[], struct zsv_opts *op
if (data.compact)
jsonwriter_set_option(data.jsw, jsonwriter_option_compact);
if (data.from_db) {
if (opts->stream != stdin) {
fclose(opts->stream);
opts->stream = NULL;
if (opts.stream != stdin) {
fclose(opts.stream);
opts.stream = NULL;
}
err = zsv_db2json(input_path, &data.db_tablename, data.jsw);
} else {
opts->row_handler = zsv_2json_row;
opts->ctx = &data;
if (zsv_new_with_properties(opts, custom_prop_handler, input_path, opts_used, &data.parser) == zsv_status_ok) {
opts.row_handler = zsv_2json_row;
opts.ctx = &data;
if (zsv_new_with_properties(&opts, custom_prop_handler, input_path, &data.parser) == zsv_status_ok) {
zsv_handle_ctrl_c_signal();
while (!data.err && !zsv_signal_interrupted && zsv_parse_more(data.parser) == zsv_status_ok)
;
Expand All @@ -372,8 +373,8 @@ int ZSV_MAIN_FUNC(ZSV_COMMAND)(int argc, const char *argv[], struct zsv_opts *op
}

zsv_2json_cleanup(&data);
if (opts->stream && opts->stream != stdin)
fclose(opts->stream);
if (opts.stream && opts.stream != stdin)
fclose(opts.stream);
if (out && out != stdout)
fclose(out);
return err;
Expand Down
23 changes: 12 additions & 11 deletions app/2tsv.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,9 @@ int zsv_2tsv_usage(int rc) {
return rc;
}

int ZSV_MAIN_FUNC(ZSV_COMMAND)(int argc, const char *argv[], struct zsv_opts *opts,
struct zsv_prop_handler *custom_prop_handler, const char *opts_used) {
int ZSV_MAIN_FUNC(ZSV_COMMAND)(int argc, const char *argv[], struct zsv_opts *optsp,
struct zsv_prop_handler *custom_prop_handler) {
struct zsv_opts opts = *optsp;
struct zsv_2tsv_data data = {0};
const char *input_path = NULL;
int err = 0;
Expand All @@ -177,9 +178,9 @@ int ZSV_MAIN_FUNC(ZSV_COMMAND)(int argc, const char *argv[], struct zsv_opts *op
else if (!(data.out.stream = fopen(argv[i], "wb")))
fprintf(stderr, "Unable to open for writing: %s\n", argv[i]), err = 1;
} else {
if (opts->stream)
if (opts.stream)
fprintf(stderr, "Input file specified more than once\n"), err = 1;
else if (!(opts->stream = fopen(argv[i], "rb")))
else if (!(opts.stream = fopen(argv[i], "rb")))
fprintf(stderr, "Unable to open for reading: %s\n", argv[i]), err = 1;
else
input_path = argv[i];
Expand All @@ -190,22 +191,22 @@ int ZSV_MAIN_FUNC(ZSV_COMMAND)(int argc, const char *argv[], struct zsv_opts *op
goto exit_2tsv;
}

if (!opts->stream) {
if (!opts.stream) {
#ifdef NO_STDIN
fprintf(stderr, "Please specify an input file\n");
err = 1;
goto exit_2tsv;
#else
opts->stream = stdin;
opts.stream = stdin;
#endif
}

if (!data.out.stream)
data.out.stream = stdout;

opts->row_handler = zsv_2tsv_row;
opts->ctx = &data;
if (zsv_new_with_properties(opts, custom_prop_handler, input_path, opts_used, &data.parser) == zsv_status_ok) {
opts.row_handler = zsv_2tsv_row;
opts.ctx = &data;
if (zsv_new_with_properties(&opts, custom_prop_handler, input_path, &data.parser) == zsv_status_ok) {
char output[ZSV_2TSV_BUFF_SIZE];
data.out.buff = output;

Expand All @@ -219,8 +220,8 @@ int ZSV_MAIN_FUNC(ZSV_COMMAND)(int argc, const char *argv[], struct zsv_opts *op
}

exit_2tsv:
if (opts->stream && opts->stream != stdin)
fclose(opts->stream);
if (opts.stream && opts.stream != stdin)
fclose(opts.stream);
if (data.out.stream && data.out.stream != stdout)
fclose(data.out.stream);
return err;
Expand Down
22 changes: 6 additions & 16 deletions app/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ zsvsheet_status zsvsheet_ext_prompt(struct zsvsheet_proc_context *ctx, char *buf
#include "cli_ini.c"

typedef int(cmd_main)(int argc, const char *argv[]);
typedef int(zsv_cmd)(int argc, const char *argv[], struct zsv_opts *opts, struct zsv_prop_handler *custom_prop_handler,
const char *opts_used);
typedef int(zsv_cmd)(int argc, const char *argv[], struct zsv_opts *opts, struct zsv_prop_handler *custom_prop_handler);
typedef int (*cmd_reserved)(void);

struct builtin_cmd {
Expand Down Expand Up @@ -152,17 +151,11 @@ struct zsv_execution_data {

int argc;
const char **argv;
char opts_used[ZSV_OPTS_SIZE_MAX];
void *custom_context; // user-defined
void *state; // program state relevant to the handler, e.g. cursor
// position in zsv sheet.
};

static const char *ext_opts_used(zsv_execution_context ctx) {
struct zsv_execution_data *d = ctx;
return d->opts_used;
}

static struct zsv_opts ext_parser_opts(zsv_execution_context ctx) {
(void)(ctx);
return zsv_get_default_opts();
Expand Down Expand Up @@ -389,7 +382,6 @@ static struct zsv_ext_callbacks *zsv_ext_callbacks_init(struct zsv_ext_callbacks

e->ext_parse_all = ext_parse_all;
e->ext_parser_opts = ext_parser_opts;
e->ext_opts_used = ext_opts_used;

e->ext_sqlite3_add_csv = zsv_sqlite3_add_csv;
e->ext_sqlite3_db_delete = zsv_sqlite3_db_delete;
Expand Down Expand Up @@ -474,11 +466,11 @@ static enum zsv_ext_status run_extension(int argc, const char *argv[], struct zs
struct zsv_execution_data ctx = {0};
if ((stat = execution_context_init(&ctx, argc, argv)) == zsv_ext_status_ok) {
struct zsv_opts opts;
zsv_args_to_opts(argc, argv, &ctx.argc, ctx.argv, &opts, ctx.opts_used);
zsv_args_to_opts(argc, argv, &ctx.argc, ctx.argv, &opts);
zsv_set_default_opts(opts);
// need a corresponding zsv_set_default_custom_prop_handler?

stat = cmd->main(&ctx, ctx.argc - 1, &ctx.argv[1], &opts, ctx.opts_used);
stat = cmd->main(&ctx, ctx.argc - 1, &ctx.argv[1], &opts);
}

if (stat != zsv_ext_status_ok)
Expand Down Expand Up @@ -549,9 +541,8 @@ int ZSV_CLI_MAIN(int argc, const char *argv[]) {
if (help_builtin->main)
return help_builtin->main(2, argv_tmp);
else if (help_builtin->cmd) {
char opts_used[ZSV_OPTS_SIZE_MAX] = {0};
struct zsv_opts opts = {0};
return help_builtin->cmd(2, argv_tmp, &opts, NULL, opts_used);
return help_builtin->cmd(2, argv_tmp, &opts, NULL);
} else
return fprintf(stderr, "Unexpected syntax!\n");
} else {
Expand All @@ -573,11 +564,10 @@ int ZSV_CLI_MAIN(int argc, const char *argv[]) {
if (builtin->main)
return builtin->main(argc - 1, argc > 1 ? &argv[1] : NULL);

char opts_used[ZSV_OPTS_SIZE_MAX];
struct zsv_opts opts;
enum zsv_status stat = zsv_args_to_opts(argc, argv, &argc, argv, &opts, opts_used);
enum zsv_status stat = zsv_args_to_opts(argc, argv, &argc, argv, &opts);
if (stat == zsv_status_ok)
return builtin->cmd(argc - 1, argc > 1 ? &argv[1] : NULL, &opts, NULL, opts_used);
return builtin->cmd(argc - 1, argc > 1 ? &argv[1] : NULL, &opts, NULL);
return stat;
}
}
Expand Down
3 changes: 1 addition & 2 deletions app/cli_ini.c
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,7 @@ static int parse_extensions_ini(struct cli_config *config, char err_if_not_found

static struct zsv_ext_command *ext_command_new(const char *id, const char *help,
enum zsv_ext_status (*extmain)(zsv_execution_context ctx, int argc,
const char *argv[], struct zsv_opts *,
const char *)) {
const char *argv[], struct zsv_opts *)) {
struct zsv_ext_command *cmd = calloc(1, sizeof(*cmd));
cmd->id = strdup(id ? id : "");
cmd->help = help ? strdup(help) : NULL;
Expand Down
12 changes: 5 additions & 7 deletions app/compare.c
Original file line number Diff line number Diff line change
Expand Up @@ -403,16 +403,15 @@ static unsigned zsv_compare_get_unsorted_colcount(struct zsv_compare_input *inpu
}

static enum zsv_compare_status input_init_unsorted(struct zsv_compare_data *data, struct zsv_compare_input *input,
struct zsv_opts *opts, struct zsv_prop_handler *custom_prop_handler,
const char *opts_used) {
(void)(opts_used);
struct zsv_opts *opts,
struct zsv_prop_handler *custom_prop_handler) {
if (!(input->stream = fopen(input->path, "rb"))) {
perror(input->path);
return zsv_compare_status_error;
}
struct zsv_opts these_opts = *opts;
these_opts.stream = input->stream;
enum zsv_status stat = zsv_new_with_properties(&these_opts, custom_prop_handler, input->path, NULL, &input->parser);
enum zsv_status stat = zsv_new_with_properties(&these_opts, custom_prop_handler, input->path, &input->parser);
if (stat != zsv_status_ok)
return zsv_compare_status_error;

Expand Down Expand Up @@ -646,9 +645,8 @@ static int compare_usage(void) {

// TO DO: consolidate w sql.c, move common code to utils/db.c
int ZSV_MAIN_FUNC(ZSV_COMMAND)(int argc, const char *argv[], struct zsv_opts *opts,
struct zsv_prop_handler *custom_prop_handler, const char *opts_used) {
struct zsv_prop_handler *custom_prop_handler) {
// See sql.c re passing options to sqlite3 when sorting is used
(void)(opts_used);
if (argc < 2 || !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
compare_usage();
return argc < 2 ? 1 : 0;
Expand Down Expand Up @@ -749,7 +747,7 @@ int ZSV_MAIN_FUNC(ZSV_COMMAND)(int argc, const char *argv[], struct zsv_opts *op
for (unsigned ix = 0; data->status == zsv_compare_status_ok && ix < input_count; ix++) {
struct zsv_compare_input *input = &data->inputs[ix];
input->path = input_filenames[ix];
data->status = data->input_init(data, input, opts, custom_prop_handler, opts_used);
data->status = data->input_init(data, input, opts, custom_prop_handler);
}
}

Expand Down
3 changes: 1 addition & 2 deletions app/compare_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,7 @@ struct zsv_compare_data {
struct zsv_cell (*get_column_name)(struct zsv_compare_input *input, unsigned ix);
unsigned (*get_column_count)(struct zsv_compare_input *input);
enum zsv_compare_status (*input_init)(struct zsv_compare_data *data, struct zsv_compare_input *input,
struct zsv_opts *opts, struct zsv_prop_handler *custom_prop_handler,
const char *opts_used);
struct zsv_opts *opts, struct zsv_prop_handler *custom_prop_handler);

sqlite3 *sort_db; // used when --sort option was specified

Expand Down
27 changes: 14 additions & 13 deletions app/compare_sort.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@

#include "external/sqlite3/sqlite3_csv_vtab-mem.h"

static int zsv_compare_sort_prep_table(struct zsv_compare_data *data, const char *fname, const char *opts_used,
char **err_msg, unsigned int table_ix) {
static int zsv_compare_sort_prep_table(struct zsv_compare_data *data, const char *fname, char **err_msg,
unsigned int table_ix) {
#define ZSV_COMPARE_MAX_TABLES 1000
char *sql = NULL;
if (table_ix > ZSV_COMPARE_MAX_TABLES)
return -1;

sql = sqlite3_mprintf("CREATE VIRTUAL TABLE data%i USING csv(filename=%Q,options_used=%Q)", table_ix, fname,
opts_used); //, max_columns);
sql = sqlite3_mprintf("CREATE VIRTUAL TABLE data%i USING csv(filename=%Q)", table_ix, fname);

if (!sql)
return -1;

Expand Down Expand Up @@ -40,12 +40,11 @@ static int zsv_compare_sort_stmt_prep(sqlite3 *db, sqlite3_stmt **stmtp, struct
}

static enum zsv_compare_status input_init_sorted(struct zsv_compare_data *data, struct zsv_compare_input *input,
struct zsv_opts *opts, struct zsv_prop_handler *custom_prop_handler,
const char *opts_used) {
struct zsv_opts *opts, struct zsv_prop_handler *custom_prop_handler) {
char *err_msg = NULL;
if (!sqlite3_zsv_list_add(input->path, opts, custom_prop_handler))
input->added = 1;
int rc = zsv_compare_sort_prep_table(data, input->path, opts_used, &err_msg, input->index);
int rc = zsv_compare_sort_prep_table(data, input->path, &err_msg, input->index);

if (err_msg) {
fprintf(stderr, "%s\n", err_msg);
Expand All @@ -65,10 +64,11 @@ static enum zsv_status zsv_compare_next_sorted_row(struct zsv_compare_input *inp
}

static struct zsv_cell zsv_compare_get_sorted_colname(struct zsv_compare_input *input, unsigned ix) {
struct zsv_cell c;
c.str = (unsigned char *)sqlite3_column_name(input->sort_stmt, (int)ix);
struct zsv_cell c = {
.str = (unsigned char *)sqlite3_column_name(input->sort_stmt, (int)ix),
.quoted = 1,
};
c.len = c.str ? strlen((const char *)c.str) : 0;
c.quoted = 1;
return c;
}

Expand All @@ -80,11 +80,12 @@ static unsigned zsv_compare_get_sorted_colcount(struct zsv_compare_input *input)
}

static struct zsv_cell zsv_compare_get_sorted_cell(struct zsv_compare_input *input, unsigned ix) {
struct zsv_cell c;
c.str = (unsigned char *)sqlite3_column_text(input->sort_stmt, (int)ix);
struct zsv_cell c = {
.str = (unsigned char *)sqlite3_column_text(input->sort_stmt, (int)ix),
.quoted = 1,
};
c.len = c.str ? sqlite3_column_bytes(input->sort_stmt, (int)ix) : 0;
if (c.len)
c.str = (unsigned char *)zsv_strtrim(c.str, &c.len);
c.quoted = 1;
return c;
}
4 changes: 2 additions & 2 deletions app/count-pull.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ static int count_usage(void) {
}

int ZSV_MAIN_FUNC(ZSV_COMMAND)(int argc, const char *argv[], struct zsv_opts *opts,
struct zsv_prop_handler *custom_prop_handler, const char *opts_used) {
struct zsv_prop_handler *custom_prop_handler) {
const char *input_path = NULL;
int err = 0;
for (int i = 1; !err && i < argc; i++) {
Expand Down Expand Up @@ -62,7 +62,7 @@ int ZSV_MAIN_FUNC(ZSV_COMMAND)(int argc, const char *argv[], struct zsv_opts *op

if (!err) {
zsv_parser parser;
if (zsv_new_with_properties(opts, custom_prop_handler, input_path, opts_used, &parser) != zsv_status_ok) {
if (zsv_new_with_properties(opts, custom_prop_handler, input_path, &parser) != zsv_status_ok) {
fprintf(stderr, "Unable to initialize parser\n");
err = 1;
} else {
Expand Down
Loading

0 comments on commit 7e22f10

Please sign in to comment.