From 8061236e4e4eaf169e74b0d266d3bd13fbf375f1 Mon Sep 17 00:00:00 2001 From: Moritz Lipp Date: Mon, 10 Oct 2011 23:46:50 +0200 Subject: [PATCH] Added structure for plain database --- Makefile | 15 ++++++++++++++- config.mk | 8 ++++++-- database-sqlite.c | 48 +++++++++++++++++++++++++++++++++++------------ database.h | 19 ++++++++++++------- zathura.c | 4 +--- 5 files changed, 69 insertions(+), 25 deletions(-) diff --git a/Makefile b/Makefile index d057347..92c89cc 100644 --- a/Makefile +++ b/Makefile @@ -4,10 +4,20 @@ include config.mk include common.mk PROJECT = zathura -SOURCE = $(shell find . -iname "*.c") +SOURCE = $(shell find . -iname "*.c" -a ! -iname "database-*") OBJECTS = $(patsubst %.c, %.o, $(SOURCE)) DOBJECTS = $(patsubst %.c, %.do, $(SOURCE)) +ifeq (${DATABASE}, sqlite) +INCS += $(SQLITE_INC) +LIBS += $(SQLITE_LIB) +SOURCE += database-sqlite.c +else +ifeq (${DATABASE}, plain) +SOURCE += database-plain.c +endif +endif + all: options ${PROJECT} options: @@ -27,6 +37,9 @@ options: @mkdir -p .depend $(QUIET)${CC} -c ${CFLAGS} ${DFLAGS} -o $@ $< -MMD -MF .depend/$@.dep +# force recompilation of database.o if DATABASE has changed +database.o: database-${DATABASE}.o + ${OBJECTS}: config.mk ${DOBJECTS}: config.mk diff --git a/config.mk b/config.mk index cc3301b..00e8f62 100644 --- a/config.mk +++ b/config.mk @@ -24,8 +24,8 @@ SQLITE_LIB ?= $(shell pkg-config --libs sqlite3) DL_LIB ?= -ldl -INCS = ${GIRARA_INC} ${GTK_INC} $(SQLITE_INC) -LIBS = ${GIRARA_LIB} ${GTK_LIB} $(SQLITE_LIB) $(DL_LIB) -lpthread -lm +INCS = ${GIRARA_INC} ${GTK_INC} +LIBS = ${GIRARA_LIB} ${GTK_LIB} $(DL_LIB) -lpthread -lm # flags CFLAGS += -std=c99 -pedantic -Wall -Wno-format-zero-length -Wextra $(INCS) -DZATHURA_PLUGINDIR=\"${PLUGINDIR}\" @@ -44,3 +44,7 @@ SFLAGS ?= -s # set to something != 0 if you want verbose build output VERBOSE ?= 0 + +# database +# possible values are sqlite and plain +DATABASE ?= sqlite diff --git a/database-sqlite.c b/database-sqlite.c index eba12ef..fa76be3 100644 --- a/database-sqlite.c +++ b/database-sqlite.c @@ -6,15 +6,25 @@ #include "database.h" +#define DATABASE "bookmarks.sqlite" + struct zathura_database_s { sqlite3* session; }; zathura_database_t* -zathura_db_init(const char* path) +zathura_db_init(const char* dir) { + char* path = g_build_filename(dir, DATABASE, NULL); + if (path == NULL) { + goto error_ret; + } + zathura_database_t* db = g_malloc0(sizeof(zathura_database_t)); + if (db == NULL) { + goto error_free; + } /* create bookmarks database */ static const char SQL_BOOKMARK_INIT[] = @@ -33,23 +43,30 @@ zathura_db_init(const char* path) if (sqlite3_open(path, &(db->session)) != SQLITE_OK) { girara_error("Could not open database: %s\n", path); - zathura_db_free(db); - return NULL; + goto error_free; } if (sqlite3_exec(db->session, SQL_BOOKMARK_INIT, NULL, 0, NULL) != SQLITE_OK) { girara_error("Failed to initialize database: %s\n", path); - zathura_db_free(db); - return NULL; + goto error_free; } if (sqlite3_exec(db->session, SQL_FILEINFO_INIT, NULL, 0, NULL) != SQLITE_OK) { girara_error("Failed to initialize database: %s\n", path); - zathura_db_free(db); - return NULL; + goto error_free; } return db; + +error_free: + + zathura_db_free(db); + +error_ret: + + g_free(path); + + return NULL; } void @@ -59,7 +76,10 @@ zathura_db_free(zathura_database_t* db) return; } - sqlite3_close(db->session); + if (db->session != NULL) { + sqlite3_close(db->session); + } + g_free(db); } @@ -87,7 +107,8 @@ prepare_statement(sqlite3* session, const char* statement) } bool -zathura_db_add_bookmark(zathura_database_t* db, const char* file, zathura_bookmark_t* bookmark) +zathura_db_add_bookmark(zathura_database_t* db, const char* file, + zathura_bookmark_t* bookmark) { g_return_val_if_fail(db && file && bookmark, false); @@ -113,7 +134,8 @@ zathura_db_add_bookmark(zathura_database_t* db, const char* file, zathura_bookma } bool -zathura_db_remove_bookmark(zathura_database_t* db, const char* file, const char* id) +zathura_db_remove_bookmark(zathura_database_t* db, const char* file, const char* + id) { g_return_val_if_fail(db && file && id, false); @@ -175,7 +197,8 @@ zathura_db_load_bookmarks(zathura_database_t* db, const char* file) } bool -zathura_db_set_fileinfo(zathura_database_t* db, const char* file, unsigned int page, int offset, float scale) +zathura_db_set_fileinfo(zathura_database_t* db, const char* file, unsigned int + page, int offset, float scale) { g_return_val_if_fail(db && file, false); @@ -202,7 +225,8 @@ zathura_db_set_fileinfo(zathura_database_t* db, const char* file, unsigned int p } bool -zathura_db_get_fileinfo(zathura_database_t* db, const char* file, unsigned int* page, int* offset, float* scale) +zathura_db_get_fileinfo(zathura_database_t* db, const char* file, unsigned int* + page, int* offset, float* scale) { g_return_val_if_fail(db && file && page && offset && scale, false); diff --git a/database.h b/database.h index 1659662..44ee74d 100644 --- a/database.h +++ b/database.h @@ -11,10 +11,10 @@ /** * Initialize database system. - * @param path Path to the database file. + * @param path Path to the directory where the database file should be located. * @return A valid zathura_database_t instance or NULL on failure */ -zathura_database_t* zathura_db_init(const char* path); +zathura_database_t* zathura_db_init(const char* dir); /** * Free database instance. @@ -29,7 +29,8 @@ void zathura_db_free(zathura_database_t* db); * @param bookmark The bookmark instance. * @return true on success, false otherwise */ -bool zathura_db_add_bookmark(zathura_database_t* db, const char* file, zathura_bookmark_t* bookmark); +bool zathura_db_add_bookmark(zathura_database_t* db, const char* file, + zathura_bookmark_t* bookmark); /** * Add or update bookmark in the database. @@ -38,7 +39,8 @@ bool zathura_db_add_bookmark(zathura_database_t* db, const char* file, zathura_b * @param bookmark The bookmark instance. * @return true on success, false otherwise */ -bool zathura_db_remove_bookmark(zathura_database_t* db, const char* file, const char* id); +bool zathura_db_remove_bookmark(zathura_database_t* db, const char* file, const + char* id); /** * Loads all bookmarks from the database belonging to a specific file. @@ -46,7 +48,8 @@ bool zathura_db_remove_bookmark(zathura_database_t* db, const char* file, const * @param file The file for which the bookmarks should be loaded. * @return List of zathura_bookmark_t* or NULL on failure. */ -girara_list_t* zathura_db_load_bookmarks(zathura_database_t* db, const char* file); +girara_list_t* zathura_db_load_bookmarks(zathura_database_t* db, const char* + file); /** * Set file info (last site, ...) in the database. @@ -57,7 +60,8 @@ girara_list_t* zathura_db_load_bookmarks(zathura_database_t* db, const char* fil * @param scale The last scale. * @return true on success, false otherwise. */ -bool zathura_db_set_fileinfo(zathura_database_t* db, const char* file, unsigned int page, int offset, float scale); +bool zathura_db_set_fileinfo(zathura_database_t* db, const char* file, unsigned + int page, int offset, float scale); /* Get file info (last site, ...) from the database. * @param db The database instance @@ -67,6 +71,7 @@ bool zathura_db_set_fileinfo(zathura_database_t* db, const char* file, unsigned * @param scale The last scale. * @return true on success, false otherwise. */ -bool zathura_db_get_fileinfo(zathura_database_t* db, const char* file, unsigned int* page, int* offset, float* scale); +bool zathura_db_get_fileinfo(zathura_database_t* db, const char* file, unsigned + int* page, int* offset, float* scale); #endif // DATABASE_H diff --git a/zathura.c b/zathura.c index 0f85f38..afab9a7 100644 --- a/zathura.c +++ b/zathura.c @@ -205,12 +205,10 @@ zathura_init(int argc, char* argv[]) } /* database */ - char* database_path = g_build_filename(zathura->config.data_dir, "bookmarks.sqlite", NULL); - zathura->database = zathura_db_init(database_path); + zathura->database = zathura_db_init(zathura->config.data_dir); if (zathura->database == NULL) { girara_error("Unable to initialize database. Bookmarks won't be available."); } - g_free(database_path); /* bookmarks */ zathura->bookmarks.bookmarks = girara_list_new();