From c098eb31d870aafd96ef055d725f2e127043f6ab Mon Sep 17 00:00:00 2001 From: "C. McEnroe" Date: Sun, 1 Dec 2019 19:22:38 -0500 Subject: Implement database file search --- .gitignore | 3 +++ Makefile | 22 ++++++++++++++++++++ litterbox.c | 28 +++++++++++++++++++++++++ litterbox.h | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 121 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 litterbox.c create mode 100644 litterbox.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bea9ab7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.o +litterbox +tags diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..573290a --- /dev/null +++ b/Makefile @@ -0,0 +1,22 @@ +LIBS_PREFIX = /usr/local + +CFLAGS += -std=c11 -Wall -Wextra -Wpedantic +CFLAGS += ${LIBS_PREFIX:%=-I%/include} +LDFLAGS += ${LIBS_PREFIX:%=-L%/lib} +LDLIBS = -lsqlite3 + +BINS = litterbox + +-include config.mk + +dev: tags all + +all: ${BINS} + +${BINS:=.o}: litterbox.h + +tags: *.c *.h + ctags -w *.c *.h + +clean: + rm -f tags ${BINS} ${BINS:=.o} diff --git a/litterbox.c b/litterbox.c new file mode 100644 index 0000000..bcfdd8f --- /dev/null +++ b/litterbox.c @@ -0,0 +1,28 @@ +/* Copyright (C) 2019 C. McEnroe + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include +#include +#include +#include + +#include "litterbox.h" + +int main(void) { + sqlite3 *db = dbOpen(SQLITE_OPEN_READWRITE); + printf("%p\n", (void *)db); +} diff --git a/litterbox.h b/litterbox.h new file mode 100644 index 0000000..48d707c --- /dev/null +++ b/litterbox.h @@ -0,0 +1,68 @@ +/* Copyright (C) 2019 C. McEnroe + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define DATABASE_PATH "litterbox/database.sqlite" + +static inline sqlite3 *dbOpenPath(char *path, int flags) { + char *base = strrchr(path, '/'); + if (flags & SQLITE_OPEN_CREATE && base) { + *base = '\0'; + int error = mkdir(path, 0700); + if (error && errno != EEXIST) err(EX_CANTCREAT, "%s", path); + *base = '/'; + } + sqlite3 *db; + int error = sqlite3_open_v2(path, &db, flags, NULL); + if (!error) return db; + if (error == SQLITE_CANTOPEN) { + sqlite3_close(db); + return NULL; + } + errx(EX_NOINPUT, "%s: %s", path, sqlite3_errmsg(db)); +} + +static inline sqlite3 *dbOpen(int flags) { + const char *home = getenv("HOME"); + const char *dataHome = getenv("XDG_DATA_HOME"); + const char *dataDirs = getenv("XDG_DATA_DIRS"); + char path[PATH_MAX]; + if (dataHome) { + snprintf(path, sizeof(path), "%s/" DATABASE_PATH, dataHome); + } else { + snprintf(path, sizeof(path), "%s/.local/share/" DATABASE_PATH, home); + } + sqlite3 *db = dbOpenPath(path, flags); + if (db) return db; + while (dataDirs && *dataDirs) { + size_t len = strcspn(dataDirs, ":"); + snprintf(path, sizeof(path), "%.*s/" DATABASE_PATH, (int)len, dataDirs); + db = dbOpenPath(path, flags); + if (db) return db; + dataDirs += len; + if (*dataDirs) dataDirs++; + } + return NULL; +} -- cgit 1.4.1 colspan='3' class='logmsg'> 2012-02-01Add tabular.vimJune McEnroe 2012-02-01Update Vim-Tomorrow-ThemeJune McEnroe PS. Chris Kempson is a jerk. 2012-01-30Disable menu bar in GVimJune McEnroe 2012-01-30Set shiftwidth to 4 for LuaJune McEnroe 2012-01-29Don't show Syntastic errors automaticallyJune McEnroe 2012-01-28Update to Quicktask 1.1June McEnroe 2012-01-22Use space-test branch of quicktaskJune McEnroe 2012-01-22Enable syntax-based foldingJune McEnroe 2012-01-22Update quicktaskJune McEnroe 2012-01-22Revert "Add AutoClose"June McEnroe This reverts commit 2705f4b91a56caf4cf2b3b89b247580debd5d453. This fixes the delay when leaving insert mode 2012-01-22Revert "Disable powerline for now"June McEnroe This reverts commit 171b1aa0d1d445a1223d0d638f2798b780c3e1ce. 2012-01-22Revert "Add VCS repo directories to wildignore"June McEnroe This reverts commit f4cc36a0aca465eea866fa7131a19497d46e63f0. This fixes not being able to commit with vim-fugitive 2012-01-22Disable powerline for nowJune McEnroe 2012-01-22Update some pluginsJune McEnroe 2012-01-22Add VCS repo directories to wildignoreJune McEnroe 2012-01-22Map ,e and ,b to CtrlP file and buffer respectivelyJune McEnroe 2012-01-22Add vim-spaceJune McEnroe 2012-01-22Add AutoCloseJune McEnroe 2012-01-22Add binding for GundoJune McEnroe 2012-01-22Add GundoJune McEnroe 2012-01-22Add Jellybeans colorschemeJune McEnroe 2012-01-22Add syntasticJune McEnroe 2012-01-21Add PowerlineJune McEnroe 2012-01-21Add quicktaskJune McEnroe 2012-01-15Moved comments out of mapsJune McEnroe 2012-01-14Disable scrollbarsJune McEnroe