diff --git a/src/csv.c b/src/csv.c index 7d3d649..8b4f52c 100644 --- a/src/csv.c +++ b/src/csv.c @@ -9,7 +9,7 @@ #include "json.h" #include "csv.h" -#define MAX_BUFFER_SIZE 512 +#define MAX_BUFFER_SIZE 1024 int get_next_id() { @@ -31,6 +31,37 @@ int get_next_id() return atoi(id) + 1; } +long find_id(int id_to_find) +{ + FILE* csv; + char* buffer; + size_t line_size = MAX_BUFFER_SIZE - 1; + int i, file_exists; + long file_pos; + + + file_exists = access(FILE_NAME, F_OK); + if (0 != file_exists) { + fprintf(stderr, "%s does not exist!", FILE_NAME); + exit(EXIT_FAILURE); + } + + csv = fopen(FILE_NAME, "r"); + if (NULL == csv) { + fprintf(stderr, "Failed to open %s!\n", FILE_NAME); + exit(EXIT_FAILURE); + } + + for(i = 0; i < id_to_find + 1; i++) + getline(&buffer, &line_size, csv); + + file_pos = ftell(csv); + fclose(csv); + + return file_pos; +} + + void write_to_file(book_t* book) { FILE* file; @@ -52,6 +83,11 @@ void write_to_file(book_t* book) book_id = get_next_id(); } + if (NULL == file) { + fprintf(stderr, "Failed to open %s!\n", FILE_NAME); + exit(EXIT_FAILURE); + } + // now we write the information fprintf(file, "%d,\"%s\",\"%s\",\"%s\",\"%s\",%d,%d\n", book_id, book->isbn, book->title, book->authors, book->image_url, book->year_of_publication, book->page_len); diff --git a/src/csv.h b/src/csv.h index ae57daf..2ae5a43 100644 --- a/src/csv.h +++ b/src/csv.h @@ -7,4 +7,6 @@ int get_next_id(); void write_to_file(book_t* book); +long find_id(int id_to_find); + #endif diff --git a/src/main.c b/src/main.c index cbd6715..3c3f4a0 100644 --- a/src/main.c +++ b/src/main.c @@ -80,6 +80,8 @@ 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;