From a56994fbaea532e2d2314aeeb917c30031c29f08 Mon Sep 17 00:00:00 2001 From: yuzu-eva Date: Fri, 11 Apr 2025 17:18:52 +0200 Subject: re-organized project structure (no src folder) --- .gitignore | 2 +- Makefile | 44 ++++++++++-------- dbhandling.c | 138 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ dbhandling.h | 15 ++++++ main.c | 110 ++++++++++++++++++++++++++++++++++++++++++++ src/dbhandling.c | 138 ------------------------------------------------------- src/dbhandling.h | 15 ------ src/main.c | 110 -------------------------------------------- 8 files changed, 289 insertions(+), 283 deletions(-) create mode 100644 dbhandling.c create mode 100644 dbhandling.h create mode 100644 main.c delete mode 100644 src/dbhandling.c delete mode 100644 src/dbhandling.h delete mode 100644 src/main.c diff --git a/.gitignore b/.gitignore index bef63d7..27c573e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ -src/old_main.c +old_main.c *.csv *.db diff --git a/Makefile b/Makefile index 5a0a267..d42e755 100644 --- a/Makefile +++ b/Makefile @@ -1,31 +1,37 @@ -SHELL=/bin/sh -CC=gcc +SHELL = /bin/sh +CC = gcc -SRCDIR=./src -PREFIX=/usr/local -BINDIR=/bin +PREFIX = /usr/local +BINDIR = /bin -INSTALL_PROGRAM=install -INSTALL_DATA=install -m 644 +INSTALL_PROGRAM = install +INSTALL_DATA = install -m 644 -CFLAGS=-Wall -Wextra -LIBS=-lsqlite3 -BIN=myal +CFLAGS = -Wall -Wextra +LIBS = -lsqlite3 +SRC = main.c dbhandling.c +OBJ = ${SRC:.c=.o} +BIN = myal -all: $(BIN) +all: options ${BIN} -myal: $(SRCDIR)/main.c dbhandling - $(CC) $(CFLAGS) $(LIBS) $(SRCDIR)/main.c dbhandling.o -o myal +options: + @echo myal build options: + @echo "CFLAGS = ${CFLAGS}" + @echo "CC = ${CC}" -dbhandling: $(SRCDIR)/dbhandling.c - $(CC) $(CFLAGS) $(LIBS) -c $(SRCDIR)/dbhandling.c +.c.o: + ${CC} -c ${CFLAGS} ${SRC} + +myal: .c.o + ${CC} ${CFLAGS} ${LIBS} ${OBJ} -o myal install: - $(INSTALL_PROGRAM) myal $(PREFIX)$(BINDIR)/myal + ${INSTALL_PROGRAM} myal ${PREFIX}${BINDIR}/myal clean: - $(RM) myal - $(RM) dbhandling.o + ${RM} myal + ${RM} ${OBJ} uninstall: - $(RM) /usr/local/bin/myal + ${RM} /usr/local/bin/myal diff --git a/dbhandling.c b/dbhandling.c new file mode 100644 index 0000000..778c147 --- /dev/null +++ b/dbhandling.c @@ -0,0 +1,138 @@ +#include "dbhandling.h" + +void exit_with_error(sqlite3 *db, const char *msg) +{ + fprintf(stderr, "%s: %s\n", msg, sqlite3_errmsg(db)); + sqlite3_close(db); + exit(69); +} + +void select_from_table(sqlite3 *db, char *tblName, char *qp) +{ + char *sql; + int rc; + int found = 0; + + const unsigned char *name, *value, *status; + const char *type; + char *query_param = malloc(strlen(qp) + 2); + int id; + + + if (!strcmp(tblName, "anime")) { + type = "Episode"; + sql = "SELECT * FROM anime WHERE NAME LIKE ?1 ORDER BY ID;"; + } else if (!strcmp(tblName, "manga")) { + type = "Chapter"; + sql = "SELECT * FROM manga WHERE NAME LIKE ?1 ORDER BY ID;"; + } else { + fprintf(stderr, "table does not exist...\n"); + sqlite3_close(db); + exit(69); + } + + sqlite3_stmt *stmt; + snprintf(query_param, strlen(qp) + 2, "%s%%", qp); + + rc = sqlite3_prepare_v2(db, sql, -1, &stmt, 0); + if (rc != SQLITE_OK) { + exit_with_error(db, "failure fetching data: "); + } + + sqlite3_bind_text(stmt, 1, query_param, -1, SQLITE_TRANSIENT); + + while(sqlite3_step(stmt) == SQLITE_ROW) { + id = sqlite3_column_int(stmt, 0); + name = sqlite3_column_text(stmt, 1); + value = sqlite3_column_text(stmt, 2); + status = sqlite3_column_text(stmt, 3); + printf("%03d: %s, %s %s, %s\n", id, name, type, value, status); + found = 1; + } + + if (!found) { + printf("no entry found...\n"); + } + + sqlite3_finalize(stmt); + free(query_param); +} + +void update_entry(sqlite3 *db, char *tblName, char *qp, char *value, char *status) +{ + char *sql; + int rc; + int res; + + sqlite3_stmt *stmt; + + if (!strcmp(tblName, "anime")) { + if (status == NULL) { + sql = "UPDATE anime SET EPISODE=?1 WHERE NAME=?2;"; + } else { + sql = "UPDATE anime SET EPISODE=?1, STATUS=?2 WHERE NAME=?3;"; + } + } else if (!strcmp(tblName, "manga")) { + if (status == NULL) { + sql = "UPDATE manga SET CHAPTER=?1 WHERE NAME=?2;"; + } else { + sql = "UPDATE manga SET CHAPTER=?1, STATUS=?2 WHERE NAME=?3;"; + } + } else { + fprintf(stderr, "table does not exist...\n"); + sqlite3_close(db); + exit(69); + } + + rc = sqlite3_prepare_v2(db, sql, -1, &stmt, 0); + if (rc != SQLITE_OK) { + exit_with_error(db, "failure fetching data: "); + } + + if (status == NULL) { + sqlite3_bind_text(stmt, 1, value, -1, SQLITE_STATIC); + sqlite3_bind_text(stmt, 2, qp, -1, SQLITE_STATIC); + } else { + sqlite3_bind_text(stmt, 1, value, -1, SQLITE_STATIC); + sqlite3_bind_text(stmt, 2, status, -1, SQLITE_STATIC); + sqlite3_bind_text(stmt, 3, qp, -1, SQLITE_STATIC); + } + + rc = sqlite3_step(stmt); + res = sqlite3_changes(db); + if (!res) { + printf("no entry found...\n"); + } + sqlite3_finalize(stmt); +} + +void add_entry(sqlite3 *db, char *tblName, char *name, char *value, char *status) +{ + char *sql; + int rc; + + sqlite3_stmt *stmt; + + if (!strcmp(tblName, "anime")) { + sql = "INSERT INTO anime (NAME, EPISODE, STATUS) VALUES (?1, ?2, ?3);"; + } else if (!strcmp(tblName, "manga")) { + sql = "INSERT INTO manga (NAME, CHAPTER, STATUS) VALUES (?1, ?2, ?3);"; + } else { + fprintf(stderr, "table does not exist...\n"); + sqlite3_close(db); + exit(69); + } + + rc = sqlite3_prepare_v2(db, sql, -1, &stmt, 0); + if (rc != SQLITE_OK) { + exit_with_error(db, "failure fetching data: "); + } + + sqlite3_bind_text(stmt, 1, name, -1, SQLITE_STATIC); + sqlite3_bind_text(stmt, 2, value, -1, SQLITE_STATIC); + sqlite3_bind_text(stmt, 3, status, -1, SQLITE_STATIC); + + rc = sqlite3_step(stmt); + printf("Entry %s added to %s!\n", name, tblName); + sqlite3_finalize(stmt); +} diff --git a/dbhandling.h b/dbhandling.h new file mode 100644 index 0000000..4f09852 --- /dev/null +++ b/dbhandling.h @@ -0,0 +1,15 @@ +#ifndef _DBHANDLING_ +#define _DBHANDLING_ + +#include +#include +#include +#include +#include + +void exit_with_error(sqlite3 *db, const char *msg); +void select_from_table(sqlite3 *db, char *tblName, char *qp); +void update_entry(sqlite3 *db, char *tblName, char *qp, char *value, char *status); +void add_entry(sqlite3 *db, char *tblName, char *name, char *value, char *status); + +#endif diff --git a/main.c b/main.c new file mode 100644 index 0000000..b38733b --- /dev/null +++ b/main.c @@ -0,0 +1,110 @@ +#include + +#include "dbhandling.h" + +const uint8_t PATH_MAX = 64; +const char *filepath = ".local/share/sqlite"; +const char *filename = "library.db"; + +typedef enum { + GET, + SET, + ADD, +} mode_e; + +static const struct { + mode_e val; + const char *str; +} conversion [] = { + {GET, "get"}, + {SET, "set"}, + {ADD, "add"}, +}; + +mode_e str2enum(const char *str) +{ + for (size_t i = 0; i < sizeof(conversion) / sizeof(conversion[0]); ++i) { + if (!strcmp(str, conversion[i].str)) + return conversion[i].val; + } + return -1; +} + +void print_help(void) +{ + printf("\n"); + printf("usage: myal MODE TARGET NAME [EPISODE|CHAPTER] \n"); + printf("possible modes are: get|set|add \n"); + printf("possible targets are: anime|manga \n"); + printf("EXAMPLES: myal get anime %% | Prints all anime \n"); + printf(" myal set manga Murcielago 10 | Set chapter of " \ + "Murcielago to 10\n"); + printf("mode get is fuzzy; set and add have to match exactly \n"); + printf("\n"); +} + +int main(int argc, char **argv) +{ + if (argc < 4) { + fprintf(stderr, "missing argument...\n"); + print_help(); + exit(69); + } + + mode_e mode; + char *target, *name, *value, *status; + mode = str2enum(argv[1]); + target = argv[2]; + name = argv[3]; + + char fullpath[PATH_MAX]; + snprintf(fullpath, PATH_MAX, "%s/%s/%s", getenv("HOME"), filepath, filename); + + sqlite3 *db; + int rc; + + rc = sqlite3_open(fullpath, &db); + + if (rc) { + exit_with_error(db, "Can't open database: "); + } + + switch (mode) { + case GET: + select_from_table(db, target, name); + break; + case SET: + if (argc < 5) { + fprintf(stderr, "missing argument...\n"); + sqlite3_close(db); + exit(69); + } + if (argc == 5) { + value = argv[4]; + update_entry(db, target, name, value, NULL); + } else if (argc == 6) { + value = argv[4]; + status = argv[5]; + update_entry(db, target, name, value, status); + } + break; + case ADD: + if (argc < 6) { + fprintf(stderr, "missing argument...\n"); + sqlite3_close(db); + exit(69); + } + value = argv[4]; + status = argv[5]; + add_entry(db, target, name, value, status); + break; + default: + fprintf(stderr, "unknown option...\n"); + print_help(); + sqlite3_close(db); + exit(69); + } + + sqlite3_close(db); + return 0; +} diff --git a/src/dbhandling.c b/src/dbhandling.c deleted file mode 100644 index 778c147..0000000 --- a/src/dbhandling.c +++ /dev/null @@ -1,138 +0,0 @@ -#include "dbhandling.h" - -void exit_with_error(sqlite3 *db, const char *msg) -{ - fprintf(stderr, "%s: %s\n", msg, sqlite3_errmsg(db)); - sqlite3_close(db); - exit(69); -} - -void select_from_table(sqlite3 *db, char *tblName, char *qp) -{ - char *sql; - int rc; - int found = 0; - - const unsigned char *name, *value, *status; - const char *type; - char *query_param = malloc(strlen(qp) + 2); - int id; - - - if (!strcmp(tblName, "anime")) { - type = "Episode"; - sql = "SELECT * FROM anime WHERE NAME LIKE ?1 ORDER BY ID;"; - } else if (!strcmp(tblName, "manga")) { - type = "Chapter"; - sql = "SELECT * FROM manga WHERE NAME LIKE ?1 ORDER BY ID;"; - } else { - fprintf(stderr, "table does not exist...\n"); - sqlite3_close(db); - exit(69); - } - - sqlite3_stmt *stmt; - snprintf(query_param, strlen(qp) + 2, "%s%%", qp); - - rc = sqlite3_prepare_v2(db, sql, -1, &stmt, 0); - if (rc != SQLITE_OK) { - exit_with_error(db, "failure fetching data: "); - } - - sqlite3_bind_text(stmt, 1, query_param, -1, SQLITE_TRANSIENT); - - while(sqlite3_step(stmt) == SQLITE_ROW) { - id = sqlite3_column_int(stmt, 0); - name = sqlite3_column_text(stmt, 1); - value = sqlite3_column_text(stmt, 2); - status = sqlite3_column_text(stmt, 3); - printf("%03d: %s, %s %s, %s\n", id, name, type, value, status); - found = 1; - } - - if (!found) { - printf("no entry found...\n"); - } - - sqlite3_finalize(stmt); - free(query_param); -} - -void update_entry(sqlite3 *db, char *tblName, char *qp, char *value, char *status) -{ - char *sql; - int rc; - int res; - - sqlite3_stmt *stmt; - - if (!strcmp(tblName, "anime")) { - if (status == NULL) { - sql = "UPDATE anime SET EPISODE=?1 WHERE NAME=?2;"; - } else { - sql = "UPDATE anime SET EPISODE=?1, STATUS=?2 WHERE NAME=?3;"; - } - } else if (!strcmp(tblName, "manga")) { - if (status == NULL) { - sql = "UPDATE manga SET CHAPTER=?1 WHERE NAME=?2;"; - } else { - sql = "UPDATE manga SET CHAPTER=?1, STATUS=?2 WHERE NAME=?3;"; - } - } else { - fprintf(stderr, "table does not exist...\n"); - sqlite3_close(db); - exit(69); - } - - rc = sqlite3_prepare_v2(db, sql, -1, &stmt, 0); - if (rc != SQLITE_OK) { - exit_with_error(db, "failure fetching data: "); - } - - if (status == NULL) { - sqlite3_bind_text(stmt, 1, value, -1, SQLITE_STATIC); - sqlite3_bind_text(stmt, 2, qp, -1, SQLITE_STATIC); - } else { - sqlite3_bind_text(stmt, 1, value, -1, SQLITE_STATIC); - sqlite3_bind_text(stmt, 2, status, -1, SQLITE_STATIC); - sqlite3_bind_text(stmt, 3, qp, -1, SQLITE_STATIC); - } - - rc = sqlite3_step(stmt); - res = sqlite3_changes(db); - if (!res) { - printf("no entry found...\n"); - } - sqlite3_finalize(stmt); -} - -void add_entry(sqlite3 *db, char *tblName, char *name, char *value, char *status) -{ - char *sql; - int rc; - - sqlite3_stmt *stmt; - - if (!strcmp(tblName, "anime")) { - sql = "INSERT INTO anime (NAME, EPISODE, STATUS) VALUES (?1, ?2, ?3);"; - } else if (!strcmp(tblName, "manga")) { - sql = "INSERT INTO manga (NAME, CHAPTER, STATUS) VALUES (?1, ?2, ?3);"; - } else { - fprintf(stderr, "table does not exist...\n"); - sqlite3_close(db); - exit(69); - } - - rc = sqlite3_prepare_v2(db, sql, -1, &stmt, 0); - if (rc != SQLITE_OK) { - exit_with_error(db, "failure fetching data: "); - } - - sqlite3_bind_text(stmt, 1, name, -1, SQLITE_STATIC); - sqlite3_bind_text(stmt, 2, value, -1, SQLITE_STATIC); - sqlite3_bind_text(stmt, 3, status, -1, SQLITE_STATIC); - - rc = sqlite3_step(stmt); - printf("Entry %s added to %s!\n", name, tblName); - sqlite3_finalize(stmt); -} diff --git a/src/dbhandling.h b/src/dbhandling.h deleted file mode 100644 index 4f09852..0000000 --- a/src/dbhandling.h +++ /dev/null @@ -1,15 +0,0 @@ -#ifndef _DBHANDLING_ -#define _DBHANDLING_ - -#include -#include -#include -#include -#include - -void exit_with_error(sqlite3 *db, const char *msg); -void select_from_table(sqlite3 *db, char *tblName, char *qp); -void update_entry(sqlite3 *db, char *tblName, char *qp, char *value, char *status); -void add_entry(sqlite3 *db, char *tblName, char *name, char *value, char *status); - -#endif diff --git a/src/main.c b/src/main.c deleted file mode 100644 index b38733b..0000000 --- a/src/main.c +++ /dev/null @@ -1,110 +0,0 @@ -#include - -#include "dbhandling.h" - -const uint8_t PATH_MAX = 64; -const char *filepath = ".local/share/sqlite"; -const char *filename = "library.db"; - -typedef enum { - GET, - SET, - ADD, -} mode_e; - -static const struct { - mode_e val; - const char *str; -} conversion [] = { - {GET, "get"}, - {SET, "set"}, - {ADD, "add"}, -}; - -mode_e str2enum(const char *str) -{ - for (size_t i = 0; i < sizeof(conversion) / sizeof(conversion[0]); ++i) { - if (!strcmp(str, conversion[i].str)) - return conversion[i].val; - } - return -1; -} - -void print_help(void) -{ - printf("\n"); - printf("usage: myal MODE TARGET NAME [EPISODE|CHAPTER] \n"); - printf("possible modes are: get|set|add \n"); - printf("possible targets are: anime|manga \n"); - printf("EXAMPLES: myal get anime %% | Prints all anime \n"); - printf(" myal set manga Murcielago 10 | Set chapter of " \ - "Murcielago to 10\n"); - printf("mode get is fuzzy; set and add have to match exactly \n"); - printf("\n"); -} - -int main(int argc, char **argv) -{ - if (argc < 4) { - fprintf(stderr, "missing argument...\n"); - print_help(); - exit(69); - } - - mode_e mode; - char *target, *name, *value, *status; - mode = str2enum(argv[1]); - target = argv[2]; - name = argv[3]; - - char fullpath[PATH_MAX]; - snprintf(fullpath, PATH_MAX, "%s/%s/%s", getenv("HOME"), filepath, filename); - - sqlite3 *db; - int rc; - - rc = sqlite3_open(fullpath, &db); - - if (rc) { - exit_with_error(db, "Can't open database: "); - } - - switch (mode) { - case GET: - select_from_table(db, target, name); - break; - case SET: - if (argc < 5) { - fprintf(stderr, "missing argument...\n"); - sqlite3_close(db); - exit(69); - } - if (argc == 5) { - value = argv[4]; - update_entry(db, target, name, value, NULL); - } else if (argc == 6) { - value = argv[4]; - status = argv[5]; - update_entry(db, target, name, value, status); - } - break; - case ADD: - if (argc < 6) { - fprintf(stderr, "missing argument...\n"); - sqlite3_close(db); - exit(69); - } - value = argv[4]; - status = argv[5]; - add_entry(db, target, name, value, status); - break; - default: - fprintf(stderr, "unknown option...\n"); - print_help(); - sqlite3_close(db); - exit(69); - } - - sqlite3_close(db); - return 0; -} -- cgit v1.2.3