diff options
| -rw-r--r-- | .gitignore | 5 | ||||
| -rw-r--r-- | Makefile | 10 | ||||
| -rw-r--r-- | src/dbhandling.c (renamed from main.c) | 107 | ||||
| -rw-r--r-- | src/dbhandling.h | 15 | ||||
| -rw-r--r-- | src/main.c | 110 |
5 files changed, 149 insertions, 98 deletions
@@ -1,4 +1,7 @@ +src/old_main.c + *.csv *.db -old_main.c +*.o + myal @@ -1,7 +1,7 @@ SHELL=/bin/sh CC=gcc -SRCDIR=. +SRCDIR=./src PREFIX=/usr/local BINDIR=/bin @@ -14,14 +14,18 @@ BIN=myal all: $(BIN) -myal: $(SRCDIR)/main.c - $(CC) $(CFLAGS) $(LIBS) $(SRCDIR)/main.c -o myal +myal: $(SRCDIR)/main.c dbhandling + $(CC) $(CFLAGS) $(LIBS) $(SRCDIR)/main.c dbhandling.o -o myal + +dbhandling: $(SRCDIR)/dbhandling.c + $(CC) $(CFLAGS) $(LIBS) -c $(SRCDIR)/dbhandling.c install: $(INSTALL_PROGRAM) myal $(PREFIX)$(BINDIR)/myal clean: $(RM) myal + $(RM) dbhandling.o uninstall: $(RM) /usr/local/bin/myal diff --git a/main.c b/src/dbhandling.c index 526270c..778c147 100644 --- a/main.c +++ b/src/dbhandling.c @@ -1,36 +1,4 @@ -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <unistd.h> -#include <sqlite3.h> -#include <stdint.h> - -const uint8_t PATH_MAX = 64; -const char *filepath = ".local/share/sqlite"; -const char *filename = "library.db"; - - -static int callback(void *NotUsed, int argc, char **argv, char **azColName) -{ - int i; - for(i = 0; i < argc; i++) { - printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL"); - } - printf("\n"); - return 0; -} - -void print_help(void) -{ - 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"); -} +#include "dbhandling.h" void exit_with_error(sqlite3 *db, const char *msg) { @@ -43,6 +11,7 @@ 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; @@ -76,9 +45,13 @@ void select_from_table(sqlite3 *db, char *tblName, char *qp) id = sqlite3_column_int(stmt, 0); name = sqlite3_column_text(stmt, 1); value = sqlite3_column_text(stmt, 2); - 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); @@ -89,6 +62,7 @@ void update_entry(sqlite3 *db, char *tblName, char *qp, char *value, char *statu { char *sql; int rc; + int res; sqlite3_stmt *stmt; @@ -125,6 +99,10 @@ void update_entry(sqlite3 *db, char *tblName, char *qp, char *value, char *statu } rc = sqlite3_step(stmt); + res = sqlite3_changes(db); + if (!res) { + printf("no entry found...\n"); + } sqlite3_finalize(stmt); } @@ -155,65 +133,6 @@ void add_entry(sqlite3 *db, char *tblName, char *name, char *value, char *status 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); } - -int main(int argc, char **argv) -{ - if (argc < 4) { - fprintf(stderr, "missing argument...\n"); - print_help(); - exit(69); - } - char *mode, *target, *name, *value, *status; - mode = 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: "); - } - - if (!strcmp(mode, "get")) { - select_from_table(db, target, name); - } else if (!strcmp(mode, "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); - } - } else if (!strcmp(mode, "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); - } else { - fprintf(stderr, "unknown option...\n"); - print_help(); - sqlite3_close(db); - exit(69); - } - - sqlite3_close(db); - return 0; -} diff --git a/src/dbhandling.h b/src/dbhandling.h new file mode 100644 index 0000000..4f09852 --- /dev/null +++ b/src/dbhandling.h @@ -0,0 +1,15 @@ +#ifndef _DBHANDLING_ +#define _DBHANDLING_ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <sqlite3.h> + +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 new file mode 100644 index 0000000..b38733b --- /dev/null +++ b/src/main.c @@ -0,0 +1,110 @@ +#include <stdint.h> + +#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; +} |
