aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main.c62
1 files changed, 37 insertions, 25 deletions
diff --git a/src/main.c b/src/main.c
index 8501ec2..d1a2ee8 100644
--- a/src/main.c
+++ b/src/main.c
@@ -68,6 +68,30 @@ static void assert_db_exists(void)
static void assert_no_single_quote(const char *str)
{
assert(strchr(str, '\'') == NULL);
+ assert(strchr(str, ';') == NULL);
+}
+
+static int table_next_id_callback(void *_id, int, char **cols, char **)
+{
+ int *id = _id;
+ assert(cols);
+ assert(cols[0]);
+ *id = atoi(cols[0]);
+ return 0;
+}
+
+static int table_next_id(sqlite3 *db, const char *table)
+{
+ int last_id = -1;
+ int rc;
+ char sql[128];
+ memset(sql, 0, sizeof(sql));
+ snprintf(sql, sizeof(sql)-1, "SELECT MAX(id) FROM %s;", table);
+ rc = sqlite3_exec(db, sql, table_next_id_callback, &last_id, NULL);
+ sqlite3_check(rc, db);
+ assert(last_id >= 0);
+ printf("%s: debug: last if for table \"%s\" is %d\n", __func__, table, last_id);
+ return last_id + 1;
}
static void ftag_init(int, char **)
@@ -114,7 +138,7 @@ static void ftag_list_table(const char *table, const char *col)
static void canonicalize(char *out, const char *in)
{
int i;
- for (i = 0; i < strlen(in)-1; i++) {
+ for (i = 0; i < strlen(in); i++) {
if (in[i] >= 'A' && in[i] <= 'Z')
out[i] = in[i] - 'A' + 'a';
else if (in[i] == ' ')
@@ -126,7 +150,7 @@ static void canonicalize(char *out, const char *in)
}
static void ftag_add_one_file(sqlite3 *db,
- int *last_id,
+ int *next_id,
const char *file)
{
char sql[2048];
@@ -153,6 +177,7 @@ static void ftag_add_one_file(sqlite3 *db,
exit(EXIT_FAILURE);
}
assert_no_single_quote(full_name);
+ full_name[read_len-1] = '\0';
line_len = read_len+1;
canonical_name = malloc(line_len);
@@ -169,6 +194,8 @@ static void ftag_add_one_file(sqlite3 *db,
}
if (canonical_name[0] == '\n')
canonicalize(canonical_name, full_name);
+ else
+ canonical_name[read_len-1] = '\0';
assert_no_single_quote(canonical_name);
printf("Enter the description.\n");
@@ -180,6 +207,8 @@ static void ftag_add_one_file(sqlite3 *db,
}
if (!description)
description = strdup("");
+ else
+ description[read_len-1] = '\0';
assert_no_single_quote(description);
line_len = 32;
@@ -200,10 +229,10 @@ static void ftag_add_one_file(sqlite3 *db,
memset(sql, 0, sizeof(sql));
snprintf(sql, sizeof(sql)-1,
"INSERT INTO files VALUES(%d, '%s', '%s', '%s', '%s')",
- *last_id, canonical_name, full_name, description, date);
+ *next_id, canonical_name, full_name, description, date);
rc = sqlite3_exec(db, sql, NULL, NULL, NULL);
sqlite3_check(rc, db);
- (*last_id)++;
+ (*next_id)++;
char new_path[512];
memset(new_path, 0, sizeof(new_path));
@@ -232,7 +261,6 @@ static void ftag_add_one_file(sqlite3 *db,
exit(EXIT_FAILURE);
}
- free(now_tm);
close(in_fd);
close(out_fd);
free(full_name);
@@ -249,12 +277,11 @@ static void ftag_file_add(int argc, char **argv)
}
sqlite3 *db = NULL;
int rc;
- int last_id;
rc = sqlite3_open(DATABASE_PATH, &db);
sqlite3_check(rc, db);
- /* TODO: set last_id properly */
+ int next_id = table_next_id(db, "files");
for (int i = 0; i < argc; i++)
- ftag_add_one_file(db, &last_id, argv[i]);
+ ftag_add_one_file(db, &next_id, argv[i]);
sqlite3_close(db);
}
@@ -310,15 +337,6 @@ static int ftag_tag_check(void *, int, char **cols, char **)
return 1;
}
-static int ftag_tag_last_id(void *_id, int, char **cols, char **)
-{
- int *id = _id;
- assert(cols);
- assert(cols[0]);
- *id = atoi(cols[0]);
- return 0;
-}
-
static void ftag_tag_add(int argc, char **argv)
{
if (argc != 2) {
@@ -344,20 +362,14 @@ static void ftag_tag_add(int argc, char **argv)
rc = sqlite3_exec(db, sql, ftag_tag_check, NULL, NULL);
sqlite3_check(rc, db);
- int last_id = -1;
- memset(sql, 0, sizeof(sql));
- snprintf(sql, sizeof(sql)-1, "SELECT MAX(id) FROM tags;");
- rc = sqlite3_exec(db, sql, ftag_tag_last_id, &last_id, NULL);
- sqlite3_check(rc, db);
-
+ int next_id = table_next_id(db, "tags");
const char *new_tag_desc = argv[1];
assert(strlen(new_tag_desc) <= 600);
assert_no_single_quote(new_tag_desc);
- last_id++;
memset(sql, 0, sizeof(sql));
snprintf(sql, sizeof(sql)-1,
"INSERT INTO tags VALUES(%d, '%s', '%s');",
- last_id, new_tag_name, new_tag_desc);
+ next_id, new_tag_name, new_tag_desc);
rc = sqlite3_exec(db, sql, NULL, NULL, NULL);
sqlite3_check(rc, db);
sqlite3_close(db);