diff options
Diffstat (limited to 'booki.c')
| -rw-r--r-- | booki.c | 38 | 
1 files changed, 38 insertions, 0 deletions
| @@ -622,6 +622,42 @@ void search(int argc, char* argv[], char* booki_file) {          }      }  } +void add(char *booki_file) { +    FILE* edit_file = fopen(EDIT_FILE, "w"); + +    // load an empty book to the edit file +    fputs("[[books]]\n", edit_file); +    fputs("#isbn = \"\"\n", edit_file); +    fputs("title = \"\"\n", edit_file); +    fputs("author = \"\"\n", edit_file); +    fputs("#pages = 0\n", edit_file); +    fputs("#published = 0\n", edit_file); +    fputs("language = \"English\"\n", edit_file); +    fputs("#translator = \"\"\n", edit_file); +    fputs("#on = \"\"\n", edit_file); +    fputs("\n", edit_file); + +    // open the file for editing +    fclose(edit_file); +    open(EDIT_FILE); + +    // read in file, add to booki file +    FILE* output = fopen(booki_file, "a"); +    char* data = load_file(EDIT_FILE); +    char* cur_data = data; +    struct Book book; +    while ((cur_data = next_book(cur_data)) != NULL) { +        init_book(&book); +        parse_book(cur_data, &book); +        print_book(book, true); +        write_book(book, output); +        free_book(book); +    } +    free(data); +    fclose(output); +    unlink(EDIT_FILE); +} +  void help(bool err) {      printf("booki! it's a thing\n"); @@ -645,6 +681,8 @@ int main(int argc, char* argv[]) {          open(booki_file);      } else if (strcmp(argv[1], "search") == 0) {          search(argc - 1, argv + 1, booki_file); +    } else if (strcmp(argv[1], "add") == 0) { +        add(booki_file);      } else {          printf("unknown subcommand: '%s'\n", argv[1]);          return 1; | 
