Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "BOOK.h"
- #include <stdio.h>
- #include <string.h>
- #include "../cJSON.h"
- #include <dirent.h>
- void loadAllBooks()
- {
- totalBooks = totalBooksDeleted= 0;
- DIR *dir;
- struct dirent *ent;
- if ((dir = opendir ("database\\books\\")) != NULL)
- {
- /* print all the files and directories within directory */
- while ((ent = readdir (dir)) != NULL)
- {
- if(strstr(ent->d_name,".json"))
- loadBook(ent->d_name);
- }
- closedir (dir);
- }
- else
- {
- /* could not open directory */
- printf("Can`t find directory");
- return 0;
- }
- }
- void deleteBook_()
- {
- printf("Enter your book ISBN:");
- char ISBN[14];
- getchar();
- gets(ISBN);
- int i,bookpos = -1;
- for(i = 0; i<totalBooks; i++)
- if(!strcmp(myBooks[i]->ISBN,ISBN))
- {
- bookpos = i;
- break;
- }
- if(bookpos == -1)
- {
- printf("No such ISBN for a book.\n");
- return;
- }
- printf("Are you sure you want to delete the book \"%s\" (y/n)?\n",myBooks[i]->title);
- char in;
- scanf("%c",&in);
- switch(in)
- {
- case 'y':
- case 'Y':
- break;
- default:
- return;
- }
- deletedBooks[totalBooksDeleted++] = myBooks[bookpos];
- myBooks[bookpos] = myBooks[totalBooks];
- totalBooks--;
- printf("Deleted the book successfully..\n");
- }
- void saveAndDeleteBooks()
- {
- printf("\n-------------\n");
- int i;
- for(i=0; i<totalBooks; i++)
- if(myBooks[i]->needSave)
- saveBook(myBooks[i]);
- printf("Save of books done..\n-------------\n");
- for(i=0; i<totalBooksDeleted; i++)
- deleteBook(deletedBooks[i]);
- printf("Delete of books done..\n-------------\n");
- }
- void deleteBook(Book* myBook)
- {
- if(!remove(myBook->PATH_ON_DISK))
- printf("Deleted book of ISBN -->%s\n",myBook->ISBN);
- }
- int find_Isbn(char* isbn)
- {
- int i,found = 0;
- for(i=0; i<totalBooks; i++)
- if(!strcmp(myBooks[i]->ISBN,isbn))
- {
- found = 1;
- break;
- }
- return found;
- }
- int isBook_Available(char* isbn)
- {
- int i,book_pos = -1;
- for(i=0; i<totalBooks; i++)
- if(!strcmp(myBooks[i]->ISBN,isbn))
- {
- printf("Copies : %d\n",myBooks[i]->currentAvailable);
- if(myBooks[i]->currentAvailable >0)
- book_pos = i;
- break;
- }
- return book_pos;
- }
- void bookTask(int type)
- {
- switch(type)
- {
- case 1:// Insert a new book..
- {
- addANewBook();
- break;
- }
- case 2:// search for a book
- {
- searchForABook();
- break;
- }
- case 3:
- {
- addNewCopies();
- break;
- }
- case 4:// delete books
- {
- deleteBook_();
- break;
- }
- case 5:
- {
- int i;
- for(i=0; i<totalBooks; i++)
- printf("NoOfCopies:%d , Name:%s\n",myBooks[i]->noOfCopies,myBooks[i]->title);
- break;
- }
- }
- }
- void addNewCopies()
- {
- char ISBN[14];
- printf("Enter the ISBN:");
- getchar();
- gets(ISBN);
- int i;
- for(i = 0; i<totalBooks; i++)
- if(!strcmp(ISBN,myBooks[i]->ISBN))
- {
- printf("Current no. of copies for the book \"%s\" is : %d\n",myBooks[i]->title,myBooks[i]->noOfCopies);
- printf("Enter the new no. of copies:");
- scanf("%d",&myBooks[i]->noOfCopies);
- myBooks[i]->needSave = 1;
- printf("Updated the book..\n");
- break;
- }
- }
- void searchForABook()
- {
- system("@cls");
- char c;
- printf("Search by ISBN, Title, Category\nEnter the first character:");
- getchar();
- scanf("%c",&c);
- switch(c)
- {
- case 'c':
- case 'C':
- {
- int cat;
- getchar();
- printf("Categories 1.Programming - 2.Comic - 3.Action\n");
- printf("Enter you Category:");
- scanf("%d",&cat);
- int i,found = 0;
- printf("Books of category : %s\n",getCat(cat));
- for(i = 0; i< totalBooks; i++)
- if(myBooks[i]->bookCategory == cat)
- {
- printBook(myBooks[i]);
- found++;
- }
- printf("Total search results : %d\n-------------\n",found);
- break;
- }
- case 't':
- case 'T':
- {
- getchar();
- printf("Enter the book title or a part of it:");
- char tit[100];
- gets(tit);
- int i,found = 0;
- for(i = 0; i< totalBooks; i++)
- if(strstr(myBooks[i]->title,tit))
- {
- printBook(myBooks[i]);
- found++;
- }
- printf("Total search results : %d\n-------------\n",found);
- break;
- }
- case 'i':
- case 'I':
- {
- char ISBN[14];
- getchar();
- printf("Enter you ISBN:");
- gets(ISBN);
- int i,found = 0;
- for(i = 0; i< totalBooks; i++)
- if(!strcmp(myBooks[i]->ISBN,ISBN))
- {
- printBook(myBooks[i]);
- found++;
- }
- printf("Total search results : %d\n",found);
- break;
- }
- default:
- printf("Invalid search character..\n");
- }
- }
- void addANewBook()
- {
- system("@cls");
- Book* newBook = (Book*)malloc(sizeof(Book));
- gets(newBook->author);
- printf("Author:");
- gets(newBook->author);
- printf("Publisher:");
- gets(newBook->publisher);
- printf("Title:");
- gets(newBook->title);
- printf("ISBN:");
- gets(newBook->ISBN);
- printf("NoOfCopies:");
- scanf("%d",&newBook->noOfCopies);
- printf("Current Available:");
- scanf("%d",&newBook->currentAvailable);
- printf("Categories 1.Programming - 2.Comic - 3.Action\n");
- printf("Category:");
- scanf("%d",&newBook->bookCategory);
- char fileName[30] ="database\\books\\";
- strcat(fileName,newBook->ISBN);
- strcat(fileName,".json");
- if(fileExists(fileName))
- {
- printf("This book already exists, Try using another ISBN.\n--------\n");
- return;
- }
- printBook(newBook);
- newBook->needSave = 1;
- int pos = totalBooks++;
- myBooks[pos] = newBook;
- }
- void saveBook(Book* newBook)
- {
- cJSON *root;
- root = cJSON_CreateObject();
- cJSON_AddItemToObject(root, "Title", cJSON_CreateString(newBook->title));
- cJSON_AddItemToObject(root, "ISBN", cJSON_CreateString(newBook->ISBN));
- cJSON_AddItemToObject(root, "Author", cJSON_CreateString(newBook->author));
- cJSON_AddItemToObject(root, "Publisher", cJSON_CreateString(newBook->publisher));
- cJSON_AddItemToObject(root,"NoOfCopies",cJSON_CreateNumber(newBook->noOfCopies));
- cJSON_AddItemToObject(root,"CurrentAvailable",cJSON_CreateNumber(newBook->currentAvailable));
- cJSON_AddItemToObject(root,"Category",cJSON_CreateNumber(newBook->bookCategory));
- FILE* fptr;
- char fileName[30] ="database\\books\\";
- char *jsonFile = cJSON_Print(root);
- strcat(fileName,newBook->ISBN);
- strcat(fileName,".json");
- fptr = fopen(fileName,"w");
- fprintf(fptr,jsonFile);
- fclose(fptr);
- newBook->needSave = 0;
- printf("Book %s updated successfully.\n",newBook->ISBN);
- }
- void printBook(Book* myBook)
- {
- printf("\n---------------\n");
- printf("Author: %s \nPublisher: %s \nTitle: %s \nISBN: %s \nNo Of Copies: %d \nCurrent Available: %d\n",myBook-> author,myBook-> publisher,myBook-> title,myBook-> ISBN,myBook -> noOfCopies,myBook->currentAvailable);
- printf("Category : %s\n----------------\n",getCat(myBook->bookCategory));
- }
- char* getCat(int cat)
- {
- char* myStr = (char*)malloc(20);
- switch(cat)
- {
- case 1:
- strcpy(myStr,"Programming");
- break;
- case 2:
- strcpy(myStr,"Comic");
- break;
- case 3:
- strcpy(myStr,"Action");
- break;
- }
- return myStr;
- }
- Book* createNewBook(int pos)
- {
- Book* ptr = (Book*)malloc(sizeof(Book));
- myBooks[pos] = ptr;
- return ptr;
- }
- void loadBook(char* bookName)
- {
- int current = totalBooks++;
- // realloc(myBooks,sizeof(Book)*totalBooks);
- Book* newBook= createNewBook(current);
- FILE *fileptr;
- char *buffer;
- long filelen;
- char bookPath[50] = "database\\books\\";
- strcat(bookPath,bookName);
- fileptr = fopen(bookPath, "rb"); // Open the file in binary mode
- if(!fileExists(bookPath)) return;
- fseek(fileptr, 0, SEEK_END); // Jump to the end of the file
- filelen = ftell(fileptr); // Get the current byte offset in the file
- rewind(fileptr); // Jump back to the beginning of the file
- buffer = (char *)malloc((filelen+1)*sizeof(char)); // Enough memory for file + \0
- fread(buffer, filelen, 1, fileptr); // Read in the entire file
- fclose(fileptr); // Close the file
- cJSON *root = cJSON_Parse(buffer);
- cJSON *title = cJSON_GetObjectItemCaseSensitive(root, "Title");
- cJSON *isbn = cJSON_GetObjectItemCaseSensitive(root, "ISBN");
- cJSON *author = cJSON_GetObjectItemCaseSensitive(root, "Author");
- cJSON *publisher = cJSON_GetObjectItemCaseSensitive(root, "Publisher");
- cJSON *noOfCopies = cJSON_GetObjectItemCaseSensitive(root, "NoOfCopies");
- cJSON *CurrentAvailable = cJSON_GetObjectItemCaseSensitive(root, "CurrentAvailable");
- cJSON *bookCategory = cJSON_GetObjectItemCaseSensitive(root, "Category");
- strcpy(newBook->title,title->valuestring);
- strcpy(newBook->ISBN,isbn->valuestring);
- strcpy(newBook->author,author->valuestring);
- strcpy(newBook->publisher,publisher->valuestring);
- newBook->noOfCopies=noOfCopies->valueint;
- newBook->currentAvailable=CurrentAvailable->valueint;
- newBook->bookCategory=bookCategory->valueint;
- newBook->needSave = 0;
- strcpy(newBook->PATH_ON_DISK,bookPath);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement