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

Fix cppcheck issues in overwrite.c #279

Merged
merged 3 commits into from
Nov 10, 2024
Merged
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
80 changes: 28 additions & 52 deletions app/overwrite.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ static int zsv_overwrites_init(struct zsv_overwrite_ctx *ctx, struct zsv_overwri
}

sqlite3_stmt *query = NULL;
int ret = 0;

/*
if ((ret = sqlite3_initialize()) != SQLITE_OK) {
Expand All @@ -114,38 +113,38 @@ static int zsv_overwrites_init(struct zsv_overwrite_ctx *ctx, struct zsv_overwri
}
*/

if ((ret = sqlite3_open_v2(overwrites_fn, &ctx->sqlite3.db, SQLITE_OPEN_READONLY, NULL)) != SQLITE_OK || args->add ||
if (sqlite3_open_v2(overwrites_fn, &ctx->sqlite3.db, SQLITE_OPEN_READONLY, NULL) != SQLITE_OK || args->add ||
args->clear) {
sqlite3_close(ctx->sqlite3.db);
if ((ret = sqlite3_open_v2(overwrites_fn, &ctx->sqlite3.db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL)) !=
if (sqlite3_open_v2(overwrites_fn, &ctx->sqlite3.db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL) !=
SQLITE_OK) {
err = 1;
fprintf(stderr, "Failed to open conn: %d, %s\n", ret, sqlite3_errmsg(ctx->sqlite3.db));
fprintf(stderr, "Failed to open conn: %s\n", sqlite3_errmsg(ctx->sqlite3.db));
return err;
}

if ((ret = sqlite3_prepare_v2(ctx->sqlite3.db,
"CREATE TABLE IF NOT EXISTS overwrites ( row integer, column integer, value string, "
"timestamp varchar(25), author varchar(25) );",
-1, &query, NULL)) == SQLITE_OK) {
if ((ret = sqlite3_step(query)) != SQLITE_DONE) {
if (sqlite3_prepare_v2(ctx->sqlite3.db,
"CREATE TABLE IF NOT EXISTS overwrites ( row integer, column integer, value string, "
"timestamp varchar(25), author varchar(25) );",
-1, &query, NULL) == SQLITE_OK) {
if (sqlite3_step(query) != SQLITE_DONE) {
err = 1;
fprintf(stderr, "Failed to step: %d, %s\n", ret, sqlite3_errmsg(ctx->sqlite3.db));
fprintf(stderr, "Failed to step: %s\n", sqlite3_errmsg(ctx->sqlite3.db));
return err;
}
} else {
err = 1;
fprintf(stderr, "Failed to prepare: %d, %s\n", ret, sqlite3_errmsg(ctx->sqlite3.db));
fprintf(stderr, "Failed to prepare: %s\n", sqlite3_errmsg(ctx->sqlite3.db));
}

if (query)
sqlite3_finalize(query);

if ((ret = sqlite3_prepare_v2(ctx->sqlite3.db, "CREATE UNIQUE INDEX overwrites_uix ON overwrites (row, column)", -1,
&query, NULL)) == SQLITE_OK) {
if ((ret = sqlite3_step(query)) != SQLITE_DONE) {
if (sqlite3_prepare_v2(ctx->sqlite3.db, "CREATE UNIQUE INDEX overwrites_uix ON overwrites (row, column)", -1,
&query, NULL) == SQLITE_OK) {
if (sqlite3_step(query) != SQLITE_DONE) {
err = 1;
fprintf(stderr, "Failed to step: %d, %s\n", ret, sqlite3_errmsg(ctx->sqlite3.db));
fprintf(stderr, "Failed to step: %s\n", sqlite3_errmsg(ctx->sqlite3.db));
return err;
}
if (query)
Expand All @@ -162,53 +161,29 @@ static int zsv_overwrites_init(struct zsv_overwrite_ctx *ctx, struct zsv_overwri
static int zsv_overwrites_clear(struct zsv_overwrite_ctx *ctx) {
int err = 0;
sqlite3_stmt *query = NULL;
int ret;
if ((ret = sqlite3_prepare_v2(ctx->sqlite3.db, "DELETE FROM overwrites", -1, &query, NULL)) == SQLITE_OK) {
if ((ret = sqlite3_step(query)) != SQLITE_DONE) {
if (sqlite3_prepare_v2(ctx->sqlite3.db, "DELETE FROM overwrites", -1, &query, NULL) == SQLITE_OK) {
if (sqlite3_step(query) != SQLITE_DONE) {
err = 1;
fprintf(stderr, "Failed to step: %d, %s\n", ret, sqlite3_errmsg(ctx->sqlite3.db));
fprintf(stderr, "Failed to step: %s\n", sqlite3_errmsg(ctx->sqlite3.db));
return err;
}
} else {
err = 1;
fprintf(stderr, "Could not prepare: %d, %s\n", ret, sqlite3_errmsg(ctx->sqlite3.db));
fprintf(stderr, "Could not prepare: %s\n", sqlite3_errmsg(ctx->sqlite3.db));
}
if (query)
sqlite3_finalize(query);
return err;
}

static int zsv_overwrite_check_for_value(struct zsv_overwrite_ctx *ctx, struct zsv_overwrite_data *overwrite) {
sqlite3_stmt *query = NULL;
int ret = 0;
int err = 0;
if ((ret = sqlite3_prepare_v2(ctx->sqlite3.db, "SELECT 1 FROM overwrites WHERE row = ? AND column = ?", -1, &query,
NULL)) != SQLITE_OK) {
err = 1;
fprintf(stderr, "Failed to prepare: %d, %s\n", ret, sqlite3_errmsg(ctx->sqlite3.db));
return err;
}
sqlite3_bind_int64(query, 1, overwrite->row_ix);
sqlite3_bind_int64(query, 2, overwrite->col_ix);
if ((ret = sqlite3_step(query)) == SQLITE_ROW) // Value exists
err = 1;
else // Value does not exist
err = 0;

if (query)
sqlite3_finalize(query);
return err;
}

static int zsv_overwrites_insert(struct zsv_overwrite_ctx *ctx, struct zsv_overwrite_data *overwrite,
struct zsv_overwrite_args *args) {
const struct zsv_overwrite_args *args) {
int err = 0;
sqlite3_stmt *query = NULL;
int ret;

if ((ret = sqlite3_prepare_v2(ctx->sqlite3.db,
"INSERT INTO overwrites (row, column, value, timestamp, author) VALUES (?, ?, ?, ?, ?)",
-1, &query, NULL)) == SQLITE_OK) {
if (sqlite3_prepare_v2(ctx->sqlite3.db,
"INSERT INTO overwrites (row, column, value, timestamp, author) VALUES (?, ?, ?, ?, ?)", -1,
&query, NULL) == SQLITE_OK) {
sqlite3_bind_int64(query, 1, overwrite->row_ix);
sqlite3_bind_int64(query, 2, overwrite->col_ix);
sqlite3_bind_text(query, 3, (const char *)overwrite->val.str, -1, SQLITE_STATIC);
Expand All @@ -217,15 +192,15 @@ static int zsv_overwrites_insert(struct zsv_overwrite_ctx *ctx, struct zsv_overw
else
sqlite3_bind_null(query, 4);
sqlite3_bind_text(query, 5, "", -1, SQLITE_STATIC); // author
if ((ret = sqlite3_step(query)) != SQLITE_DONE) {
if (sqlite3_step(query) != SQLITE_DONE) {
err = 1;
fprintf(stderr, "Value already exists at row %zu and column %zu, use --force to force insert\n",
overwrite->row_ix, overwrite->col_ix);
return err;
}
} else {
err = 1;
fprintf(stderr, "Failed to prepare: %d, %s\n", ret, sqlite3_errmsg(ctx->sqlite3.db));
fprintf(stderr, "Failed to prepare: %s\n", sqlite3_errmsg(ctx->sqlite3.db));
}

if (query)
Expand Down Expand Up @@ -271,11 +246,12 @@ static char *row_col_to_a1(size_t col, size_t row) {
return result;
}

static int show_all_overwrites(struct zsv_overwrite_ctx *ctx, struct zsv_overwrite_args *args, zsv_csv_writer writer) {
static int show_all_overwrites(struct zsv_overwrite_ctx *ctx, const struct zsv_overwrite_args *args,
zsv_csv_writer writer) {
int err = 0;
sqlite3_stmt *stmt;
int ret;
if ((ret = sqlite3_prepare_v2(ctx->sqlite3.db, "SELECT * FROM overwrites", -1, &stmt, NULL) != SQLITE_OK)) {
if (sqlite3_prepare_v2(ctx->sqlite3.db, "SELECT * FROM overwrites", -1, &stmt, NULL) != SQLITE_OK) {
err = 1;
fprintf(stderr, "Failed to prepare statement: %s\n", sqlite3_errmsg(ctx->sqlite3.db));
return err;
Expand Down Expand Up @@ -379,7 +355,7 @@ static int zsv_overwrite_parse_pos(struct zsv_overwrite_data *overwrite, const 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) {
int err = 0;
if (argc < 3 || (argc > 1 && (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")))) {
if (argc < 3 || !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
zsv_overwrite_usage();
return err;
}
Expand Down