aboutsummaryrefslogtreecommitdiff
path: root/booki.c
diff options
context:
space:
mode:
authorBen Winston2024-05-23 21:31:36 -0400
committerBen Winston2024-05-23 21:38:50 -0400
commitdbe56773532d8adaf547af91899978990984e853 (patch)
treed12e7d2d1bbfb41ba88757375ab0de8fd5fd1f2b /booki.c
parent25dc82dd9d2d5e6283dad0bae4a32916c5a233d2 (diff)
initial commit of edit functionality
Diffstat (limited to 'booki.c')
-rw-r--r--booki.c118
1 files changed, 95 insertions, 23 deletions
diff --git a/booki.c b/booki.c
index dbe6392..a2a6084 100644
--- a/booki.c
+++ b/booki.c
@@ -8,6 +8,7 @@
#include <unistd.h>
#define BOOKI_FILE_REL ".local/share/booki/books.toml"
+#define EDIT_FILE "tmp.toml"
#define MAX_SEARCH_OPTS 5
#define MAX_BOOKS 5000
@@ -303,11 +304,13 @@ char* load_file(char* filename) {
static struct option search_options[] = {
{"show", no_argument, 0, 's'},
+ {"edit", no_argument, 0, 'e'},
{0, 0, 0, 0} // marks the end of the array
};
struct search_opt {
int show;
+ int edit;
int count;
char* opts[MAX_SEARCH_OPTS];
char* args[MAX_SEARCH_OPTS];
@@ -319,6 +322,7 @@ struct search_opt parse_search_options(int argc, char* argv[]) {
struct search_opt opt_out;
int count = 0;
int show = false;
+ int edit = false;
// opt options
int opt;
@@ -331,6 +335,9 @@ struct search_opt parse_search_options(int argc, char* argv[]) {
case 's':
show = true;
break;
+ case 'e':
+ edit = true;
+ break;
case '?':
// optind points at the argument (one past the option)
opt_out.opts[count] = argv[optind-1] + 2; // '--example' -> 'example'
@@ -346,6 +353,7 @@ struct search_opt parse_search_options(int argc, char* argv[]) {
// set the count/show values
opt_out.count = count;
opt_out.show = show;
+ opt_out.edit = edit;
return opt_out;
}
@@ -381,6 +389,86 @@ void print_book(struct Book book, bool all_fields) {
}
}
+void write_book(struct Book book, FILE *output) {
+ fwrite("[[books]]\n", 1, 10, output);
+ char str[100];
+ int size;
+ if (book.id) {
+ size = sprintf(str, "id = %d\n", book.id);
+ fwrite(str, 1, size, output);
+ }
+ if (book.isbn.ptr) {
+ size = sprintf(str, "isbn = \"%.*s\"\n", book.isbn.len, book.isbn.ptr);
+ fwrite(str, 1, size, output);
+ }
+ if (book.title.ptr) {
+ size = sprintf(str, "title = \"%.*s\"\n", book.title.len, book.title.ptr);
+ fwrite(str, 1, size, output);
+ }
+ if (book.author.ptr) {
+ size = sprintf(str, "author = \"%.*s\"\n", book.author.len, book.author.ptr);
+ fwrite(str, 1, size, output);
+ }
+ if (book.pages) {
+ size = sprintf(str, "pages = %d\n", book.pages);
+ fwrite(str, 1, size, output);
+ }
+ if (book.published) {
+ size = sprintf(str, "published = %d\n", book.published);
+ fwrite(str, 1, size, output);
+ }
+ if (book.language.ptr) {
+ size = sprintf(str, "language = \"%.*s\"\n", book.language.len, book.language.ptr);
+ fwrite(str, 1, size, output);
+ }
+ if (book.translator.ptr) {
+ size = sprintf(str, "translator = \"%.*s\"\n", book.translator.len, book.translator.ptr);
+ fwrite(str, 1, size, output);
+ }
+
+ int on_count = 0;
+ int str_offset = 0;
+ struct es tmp = book.on[on_count];
+ if (tmp.len != 0) {
+ // got at least one book
+ str_offset += sprintf(str, "on = [ ");
+ while (tmp.len != 0) {
+ if (on_count != 0)
+ str_offset += sprintf(str + str_offset, ", ");
+
+ str_offset += sprintf(str + str_offset, "\"%.*s\"", tmp.len, tmp.ptr);
+ on_count++;
+ tmp = book.on[on_count];
+ }
+ str_offset += sprintf(str + str_offset, " ]\n");
+ fwrite(str, 1, str_offset, output);
+ }
+
+ fwrite("\n", 1, 1, output); // trailing newline between books
+}
+
+void open(char* filepath) {
+ char* editor = getenv("EDITOR");
+ if (!editor)
+ editor = "nano";
+
+ pid_t pid;
+ int status;
+ switch ((pid = fork())) {
+ case -1:
+ printf("fork has failed!\n");
+ break;
+ case 0:
+ execlp(editor, editor, filepath, NULL);
+ printf("child failed :(\n");
+ break;
+ default:
+ // wait for the child to finish
+ pid = wait(&status);
+ break;
+ }
+}
+
char** search(int argc, char* argv[], char* booki_file) {
struct search_opt search_opts = parse_search_options(argc, argv);
bool print = false;
@@ -392,6 +480,8 @@ char** search(int argc, char* argv[], char* booki_file) {
return NULL;
}
+ FILE *output = fopen(EDIT_FILE, "w");
+
// book loop
struct Book books[MAX_BOOKS];
int book_count = 0;
@@ -439,36 +529,18 @@ char** search(int argc, char* argv[], char* booki_file) {
if (print) {
print_book(*book, search_opts.show);
+ write_book(*book, output);
book_count++;
}
}
free(data);
+ fclose(output);
- return argv + optind;
-}
-
-
-void open(char* filepath) {
- char* editor = getenv("EDITOR");
- if (!editor)
- editor = "nano";
+ if (search_opts.edit)
+ open(EDIT_FILE);
- pid_t pid;
- int status;
- switch ((pid = fork())) {
- case -1:
- printf("fork has failed!\n");
- break;
- case 0:
- execlp(editor, editor, filepath, NULL);
- printf("child failed :(\n");
- break;
- default:
- // wait for the child to finish
- pid = wait(&status);
- break;
- }
+ return argv + optind;
}
void help(bool err) {