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

Opts used cleanup #298

Merged
merged 4 commits into from
Nov 20, 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
24 changes: 8 additions & 16 deletions app/sheet.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ struct zsvsheet_builtin_proc_state {
struct zsvsheet_display_info display_info;
char *find;
struct zsv_prop_handler *custom_prop_handler;
const char *opts_used;
};

static void get_subcommand(const char *prompt, char *buff, size_t buffsize, int footer_row) {
Expand Down Expand Up @@ -338,8 +337,8 @@ static zsvsheet_status zsvsheet_move_hor_end(struct zsvsheet_display_info *di, b
// zsvsheet_handle_find_next: return non-zero if a result was found
char zsvsheet_handle_find_next(struct zsvsheet_ui_buffer *uib, const char *needle, struct zsvsheet_opts *zsvsheet_opts,
size_t header_span, struct zsvsheet_display_dimensions *ddims, int *update_buffer,
struct zsv_prop_handler *custom_prop_handler, const char *opts_used) {
if (zsvsheet_find_next(uib, needle, zsvsheet_opts, header_span, custom_prop_handler, opts_used) > 0) {
struct zsv_prop_handler *custom_prop_handler) {
if (zsvsheet_find_next(uib, needle, zsvsheet_opts, header_span, custom_prop_handler) > 0) {
*update_buffer = zsvsheet_goto_input_raw_row(uib, zsvsheet_opts->found_rownum, header_span, ddims, (size_t)-1);
return 1;
}
Expand Down Expand Up @@ -370,7 +369,7 @@ static zsvsheet_status zsvsheet_find(struct zsvsheet_builtin_proc_state *state,

if (state->find) {
zsvsheet_handle_find_next(current_ui_buffer, state->find, &zsvsheet_opts, di->header_span, di->dimensions,
&di->update_buffer, state->custom_prop_handler, state->opts_used);
&di->update_buffer, state->custom_prop_handler);
}

out:
Expand All @@ -381,6 +380,7 @@ static zsvsheet_status zsvsheet_open_file_handler(struct zsvsheet_proc_context *
// TODO: should be PATH_MAX but that's going to be about a page and compiler
// might complain about stack being too large. Probably move to handler
// state or something.
// TODO: allow additional zsv options
char prompt_buffer[256] = {0};
struct zsvsheet_builtin_proc_state *state = (struct zsvsheet_builtin_proc_state *)ctx->subcommand_context;

Expand All @@ -394,7 +394,8 @@ static zsvsheet_status zsvsheet_open_file_handler(struct zsvsheet_proc_context *
if (*prompt_buffer == '\0')
goto no_input;

if ((err = zsvsheet_ui_buffer_open_file(prompt_buffer, NULL, NULL, state->custom_prop_handler, state->opts_used,
const char *opts_used = NULL;
if ((err = zsvsheet_ui_buffer_open_file(prompt_buffer, NULL, NULL, state->custom_prop_handler, opts_used,
di->ui_buffers.base, di->ui_buffers.current))) {
if (err > 0)
zsvsheet_priv_set_status(di->dimensions, 1, "%s: %s", prompt_buffer, strerror(err));
Expand Down Expand Up @@ -426,8 +427,7 @@ static zsvsheet_status zsvsheet_filter_handler(struct zsvsheet_proc_context *ctx
const char *data_filename = zsvsheet_buffer_data_filename(current_ui_buffer);
char is_filtered_file = !(data_filename == current_ui_buffer->filename);
struct zsv_opts *zsv_opts = is_filtered_file ? NULL : &current_ui_buffer->zsv_opts;
const char *opts_used = is_filtered_file ? NULL : state->opts_used;
if ((err = zsvsheet_ui_buffer_open_file(data_filename, zsv_opts, prompt_buffer, state->custom_prop_handler, opts_used,
if ((err = zsvsheet_ui_buffer_open_file(data_filename, zsv_opts, prompt_buffer, state->custom_prop_handler, NULL,
di->ui_buffers.base, di->ui_buffers.current))) {
if (err > 0)
zsvsheet_priv_set_status(di->dimensions, 1, "%s: %s", current_ui_buffer->filename, strerror(err));
Expand All @@ -439,13 +439,6 @@ static zsvsheet_status zsvsheet_filter_handler(struct zsvsheet_proc_context *ctx
}

struct zsvsheet_ui_buffer *new_ui_buffer = *state->display_info.ui_buffers.current;
/*
if (new_ui_buffer->dimensions.row_count < 2) {
zsvsheet_ui_buffer_pop(di->ui_buffers.base, di->ui_buffers.current, NULL);
zsvsheet_priv_set_status(di->dimensions, 1, "Not found: %s", prompt_buffer);
return zsvsheet_status_ignore;
}
*/
if (is_filtered_file) {
free(new_ui_buffer->filename);
new_ui_buffer->filename = strdup(current_ui_buffer->filename);
Expand Down Expand Up @@ -701,7 +694,6 @@ int ZSV_MAIN_FUNC(ZSV_COMMAND)(int argc, const char *argv[], struct zsv_opts *op
.display_info.header_span = header_span,
.find = NULL,
.custom_prop_handler = custom_prop_handler,
.opts_used = opts_used,
};

zsvsheet_status status;
Expand Down Expand Up @@ -736,7 +728,7 @@ int ZSV_MAIN_FUNC(ZSV_COMMAND)(int argc, const char *argv[], struct zsv_opts *op
if (handler_state.display_info.update_buffer && current_ui_buffer->filename) {
struct zsvsheet_opts zsvsheet_opts = {0};
if (read_data(&current_ui_buffer, NULL, current_ui_buffer->input_offset.row, current_ui_buffer->input_offset.col,
header_span, &zsvsheet_opts, custom_prop_handler, opts_used)) {
header_span, &zsvsheet_opts, custom_prop_handler)) {
zsvsheet_priv_set_status(&display_dims, 1, "Unexpected error!"); // to do: better error message
continue;
}
Expand Down
5 changes: 2 additions & 3 deletions app/sheet/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ int zsvsheet_ui_buffer_open_file(const char *filename, const struct zsv_opts *zs
uibopts.filename = filename;
if (zsv_optsp)
uibopts.zsv_opts = *zsv_optsp;
uibopts.opts_used = opts_used;
uibopts.buff_opts = &bopts;
struct zsvsheet_opts zsvsheet_opts = {0};
int err = 0;
struct zsvsheet_ui_buffer *tmp_ui_buffer = NULL;
uibopts.row_filter = row_filter;
if (!opts_used)
opts_used = "";
if ((err = read_data(&tmp_ui_buffer, &uibopts, 0, 0, 0, &zsvsheet_opts, custom_prop_handler, opts_used)) != 0 ||
if ((err = read_data(&tmp_ui_buffer, &uibopts, 0, 0, 0, &zsvsheet_opts, custom_prop_handler)) != 0 ||
!tmp_ui_buffer || !tmp_ui_buffer->buff_used_rows) {
zsvsheet_ui_buffer_delete(tmp_ui_buffer);
if (err)
Expand Down
4 changes: 2 additions & 2 deletions app/sheet/index.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ enum zsv_index_status build_memory_index(struct zsvsheet_index_opts *optsp) {
ix_zopts.row_handler = save_filtered_file_row_handler;

enum zsv_status zst =
zsv_new_with_properties(&ix_zopts, optsp->custom_prop_handler, optsp->filename, optsp->opts_used, &ixr.parser);
zsv_new_with_properties(&ix_zopts, optsp->custom_prop_handler, optsp->filename, NULL, &ixr.parser);
if (zst != zsv_status_ok)
goto out;

Expand All @@ -113,7 +113,7 @@ enum zsv_index_status build_memory_index(struct zsvsheet_index_opts *optsp) {
ix_zopts.row_handler = build_memory_index_row_handler;

enum zsv_status zst =
zsv_new_with_properties(&ix_zopts, optsp->custom_prop_handler, optsp->filename, optsp->opts_used, &ixr.parser);
zsv_new_with_properties(&ix_zopts, optsp->custom_prop_handler, optsp->filename, NULL, &ixr.parser);
if (zst != zsv_status_ok)
goto out;

Expand Down
1 change: 0 additions & 1 deletion app/sheet/index.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ struct zsvsheet_index_opts {
struct zsvsheet_ui_buffer *uib;
int *errp;
struct zsv_prop_handler *custom_prop_handler;
const char *opts_used;
};

enum zsv_index_status build_memory_index(struct zsvsheet_index_opts *optsp);
Expand Down
19 changes: 12 additions & 7 deletions app/sheet/read-data.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ static void *get_data_index(void *d);

static void get_data_index_async(struct zsvsheet_ui_buffer *uibuffp, const char *filename, struct zsv_opts *optsp,
const char *row_filter, struct zsv_prop_handler *custom_prop_handler,
const char *opts_used, pthread_mutex_t *mutexp) {
// const char *opts_used,
pthread_mutex_t *mutexp) {
struct zsvsheet_index_opts *ixopts = calloc(1, sizeof(*ixopts));
ixopts->mutexp = mutexp;
ixopts->filename = filename;
Expand All @@ -38,7 +39,7 @@ static void get_data_index_async(struct zsvsheet_ui_buffer *uibuffp, const char
ixopts->index = &uibuffp->index;
ixopts->index_ready = &uibuffp->index_ready;
ixopts->custom_prop_handler = custom_prop_handler;
ixopts->opts_used = opts_used;
// ixopts->opts_used = opts_used;
ixopts->uib = uibuffp;
ixopts->uib->ixopts = ixopts;
pthread_t thread;
Expand All @@ -49,13 +50,16 @@ static void get_data_index_async(struct zsvsheet_ui_buffer *uibuffp, const char
static int read_data(struct zsvsheet_ui_buffer **uibufferp, // a new zsvsheet_ui_buffer will be allocated
struct zsvsheet_ui_buffer_opts *uibopts, // if *uibufferp == NULL and uibopts != NULL
size_t start_row, size_t start_col, size_t header_span, struct zsvsheet_opts *zsvsheet_opts,
struct zsv_prop_handler *custom_prop_handler, const char *opts_used) {
struct zsv_prop_handler *custom_prop_handler) {
const char *filename = (uibufferp && *uibufferp) ? (*uibufferp)->filename : uibopts ? uibopts->filename : NULL;
struct zsv_opts opts = {0};
const char *opts_used = "";
if (uibufferp && *uibufferp)
opts = (*uibufferp)->zsv_opts;
else if (uibopts)
else if (uibopts) {
opts = uibopts->zsv_opts;
opts_used = uibopts->opts_used;
}
struct zsvsheet_ui_buffer *uibuff = uibufferp ? *uibufferp : NULL;
size_t remaining_rows_to_skip = start_row;
size_t remaining_header_to_skip = header_span;
Expand All @@ -77,6 +81,7 @@ static int read_data(struct zsvsheet_ui_buffer **uibufferp, // a new zsvsheet_
zsv_delete(parser);
return errno ? errno : -1;
}
// opts_used is no longer needed since opts will be updated

if (uibuff) {
pthread_mutex_lock(&uibuff->mutex);
Expand Down Expand Up @@ -216,7 +221,7 @@ static int read_data(struct zsvsheet_ui_buffer **uibufferp, // a new zsvsheet_
uibuff->index_started = 1;
if (original_row_num > 1 && (row_filter == NULL || rows_read > 0)) {
opts.stream = NULL;
get_data_index_async(uibuff, filename, &opts, row_filter, custom_prop_handler, opts_used, &uibuff->mutex);
get_data_index_async(uibuff, filename, &opts, row_filter, custom_prop_handler, /* opts_used, */ &uibuff->mutex);
asprintf(&ui_status, "(building index) ");
}
}
Expand Down Expand Up @@ -271,15 +276,15 @@ static void *get_data_index(void *gdi) {

static size_t zsvsheet_find_next(struct zsvsheet_ui_buffer *uib, const char *needle,
struct zsvsheet_opts *zsvsheet_opts, size_t header_span,
struct zsv_prop_handler *custom_prop_handler, const char *opts_used) {
struct zsv_prop_handler *custom_prop_handler) {
struct zsvsheet_rowcol *input_offset = &uib->input_offset;
struct zsvsheet_rowcol *buff_offset = &uib->buff_offset;
size_t cursor_row = uib->cursor_row;
zsvsheet_opts->find = needle;
zsvsheet_opts->found_rownum = 0;
// TO DO: check if it exists in current row, later column (and change 'cursor_row - 1' below to 'cursor_row')
read_data(&uib, NULL, input_offset->row + buff_offset->row + header_span + cursor_row - 1, 0, header_span,
zsvsheet_opts, custom_prop_handler, opts_used);
zsvsheet_opts, custom_prop_handler);
zsvsheet_opts->find = NULL;
return zsvsheet_opts->found_rownum;
}
1 change: 1 addition & 0 deletions app/sheet/ui_buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ struct zsvsheet_ui_buffer_opts {
const char *row_filter;
const char *filename;
struct zsv_opts zsv_opts; // options to use when opening this file
const char *opts_used;
char no_rownum_col_offset;
};

Expand Down
3 changes: 2 additions & 1 deletion app/utils/prop.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ enum zsv_status zsv_properties_parser_destroy(struct zsv_properties_parser *pars
* print a warning to stderr
*
* @param data_filepath required file path
* @param opts (optional) parser options to load
* @param opts (optional) parser options to load. will be updated to reflect
* what is actually used
* @param custom_prop_handler (optional) handler for custom properties
* @param cmd_opts_used (optional) cmd option codes to skip + warn if found
* @return zsv_status_ok on success
Expand Down