diff --git a/src/csv.c b/src/csv.c index 9733fc7..9aed956 100644 --- a/src/csv.c +++ b/src/csv.c @@ -31,14 +31,34 @@ int get_next_id() return atoi(id) + 1; } -long find_id(int id_to_find) +void update_line(char** line, int new_id) { - FILE* csv; + int i; char* buffer; - size_t line_size = MAX_BUFFER_SIZE - 1; - int i, file_exists; - long file_pos; + for (i = 0; i < MAX_BUFFER_SIZE; i++) { + if ((*line)[i] == ',') + break; + } + + if (i == MAX_BUFFER_SIZE - 1) { + fprintf(stderr, "There was an error in %s file!\n", FILE_NAME); + exit(EXIT_FAILURE); + } + buffer = malloc(sizeof(char) * MAX_BUFFER_SIZE); + strncpy(buffer, (*line) + i, MAX_BUFFER_SIZE); + + snprintf(*line, MAX_BUFFER_SIZE, "%d%s", new_id, buffer); + free(buffer); +} + +void remove_line_from_file(int id_to_remove) +{ + int file_exists, line_count; + FILE* csv; + FILE* new_csv; + char* line; + size_t line_size = MAX_BUFFER_SIZE; file_exists = access(FILE_NAME, F_OK); if (0 != file_exists) { @@ -52,16 +72,25 @@ long find_id(int id_to_find) exit(EXIT_FAILURE); } - for(i = 0; i < id_to_find + 1; i++) - getline(&buffer, &line_size, csv); + new_csv = fopen("temp.csv", "w"); - file_pos = ftell(csv); + line_count = 0; + while(getline(&line, &line_size, csv) != -1) { + if (id_to_remove > line_count) { + fprintf(new_csv, "%s", line); + } else if (id_to_remove < line_count) { + update_line(&line, line_count - 1); + fprintf(new_csv, "%s", line); + } + line_count++; + } + fclose(new_csv); fclose(csv); - return file_pos; + remove(FILE_NAME); + rename("temp.csv", FILE_NAME); // new csv is now the original file, without that line } - void write_to_file(book_t* book) { FILE* file; diff --git a/src/csv.h b/src/csv.h index 4bbf1d3..4982952 100644 --- a/src/csv.h +++ b/src/csv.h @@ -19,11 +19,20 @@ int get_next_id(); void write_to_file(book_t* book); /** - * long find_id - * int id_to_find - The ID we're looking for + * void remove_line_from_file + * int id_to_remove - The book ID that we don't want anymore * - * Returns the position of a FILE to the line with the ID we're looking for + * Removes a book from the CSV file */ -long find_id(int id_to_find); +void remove_line_from_file(int id_to_remove); + +/** + * void update_line + * char** line - Pointer to the book entry string + * int new_id - The new ID that should be placed into this book entry + * + * Takes a book entry and changes the ID to the given one + */ +void update_line(char** line, int new_id); #endif diff --git a/src/main.c b/src/main.c index 3c3f4a0..147ffdf 100644 --- a/src/main.c +++ b/src/main.c @@ -58,6 +58,7 @@ void do_ISBN_get(char* argv[]) void process_args(char* argv[]) { + int id; if (0 == strcmp(argv[1], "--help")) { printf("%s - An ISBN lookup tool.\n", argv[0]); printf("Author: Nathan Singer\n"); @@ -68,8 +69,19 @@ void process_args(char* argv[]) puts("[isbn] -- Attempts to download a book from the given ISBN-10 or ISBN-13 input."); puts("remove [id] -- Removes a book with the given ID from the book database."); } else if (0 == strcmp(argv[1], "remove")) { - // remove a book here - return; + if (NULL == argv[2]) { + printf("Not enough arguments! Try typing %s --help\n", argv[0]); + exit(EXIT_FAILURE); + } + + id = atoi(argv[2]); + + if (0 == id) { + printf("Invalid book ID given!\n"); + exit(EXIT_FAILURE); + } + + remove_line_from_file(id); } else { // lets assume its an ISBN and let the other functions fail if its not do_ISBN_get(argv); @@ -80,8 +92,6 @@ void process_args(char* argv[]) int main(int argc, char* argv[]) { - return 0; - if (1 == argc) { printf("Not enough arguments! Try typing %s --help\n", argv[0]); return EXIT_FAILURE;