diff options
| author | Ben Winston | 2024-05-27 18:44:50 -0400 |
|---|---|---|
| committer | Ben Winston | 2024-05-27 18:44:50 -0400 |
| commit | 741e0061fe48257cf00c66db95eda1388dc3ac30 (patch) | |
| tree | 93d0c9d70fb21f3fdec347ec768599cec7944db3 | |
| parent | e3a9f25b0a103b22b5b314887a34d7317865b960 (diff) | |
edit support
| -rw-r--r-- | booki.c | 39 |
1 files changed, 29 insertions, 10 deletions
@@ -7,7 +7,8 @@ #include <sys/wait.h> #include <unistd.h> -#define EDIT_FILE "tmp.toml" +#define EDIT_FILE "edit.toml" +#define FIXED_FILE "fixed.toml" #define MAX_SEARCH_OPTS 5 @@ -464,18 +465,19 @@ void open(char* filepath) { char** search(int argc, char* argv[], char* booki_file) { struct search_opt search_opts = parse_search_options(argc, argv); - bool print = false; // get the books array char* data = load_file(booki_file); if (!data) { - printf("no such array: 'books'"); + printf("couldn't load data from %s\n", booki_file); return NULL; } - FILE *output = NULL; - if (search_opts.edit) - output = fopen(EDIT_FILE, "w"); + FILE *edit_file, *fixed_file = NULL; + if (search_opts.edit) { + edit_file = fopen(EDIT_FILE, "w"); + fixed_file = fopen(FIXED_FILE, "w"); + } // book loop int book_count = 0; @@ -523,19 +525,36 @@ char** search(int argc, char* argv[], char* booki_file) { if (match) { if (search_opts.edit) - write_book(book, output); + write_book(book, edit_file); else print_book(book, search_opts.show); book_count++; + } else if (search_opts.edit) { + write_book(book, fixed_file); } } free(data); - if (output) - fclose(output); - if (search_opts.edit) + // if we're editing, both files are open at this point + if (search_opts.edit) { + fclose(edit_file); open(EDIT_FILE); + data = load_file(EDIT_FILE); + if (!data) { + printf("can't open edit file\n"); + return NULL; + } + cur_data = data; + while ((cur_data = next_book(cur_data)) != NULL) { + init_book(&book); + parse_book(cur_data, &book); + print_book(book, true); + write_book(book, fixed_file); + } + + fclose(fixed_file); + } return argv + optind; } |
