aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--booki.c39
1 files changed, 29 insertions, 10 deletions
diff --git a/booki.c b/booki.c
index 1cf9522..1cc0aad 100644
--- a/booki.c
+++ b/booki.c
@@ -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;
}