Advertisement
Guest User

Untitled

a guest
Dec 14th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.83 KB | None | 0 0
  1. #include "BOOK.h"
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include "../cJSON.h"
  5. #include <dirent.h>
  6.  
  7. void loadAllBooks()
  8. {
  9. totalBooks = totalBooksDeleted= 0;
  10. DIR *dir;
  11. struct dirent *ent;
  12. if ((dir = opendir ("database\\books\\")) != NULL)
  13. {
  14. /* print all the files and directories within directory */
  15. while ((ent = readdir (dir)) != NULL)
  16. {
  17. if(strstr(ent->d_name,".json"))
  18. loadBook(ent->d_name);
  19. }
  20. closedir (dir);
  21. }
  22. else
  23. {
  24. /* could not open directory */
  25. printf("Can`t find directory");
  26. return 0;
  27. }
  28. }
  29. void deleteBook_()
  30. {
  31. printf("Enter your book ISBN:");
  32. char ISBN[14];
  33. getchar();
  34. gets(ISBN);
  35. int i,bookpos = -1;
  36. for(i = 0; i<totalBooks; i++)
  37. if(!strcmp(myBooks[i]->ISBN,ISBN))
  38. {
  39. bookpos = i;
  40. break;
  41. }
  42.  
  43. if(bookpos == -1)
  44. {
  45. printf("No such ISBN for a book.\n");
  46. return;
  47. }
  48. printf("Are you sure you want to delete the book \"%s\" (y/n)?\n",myBooks[i]->title);
  49. char in;
  50. scanf("%c",&in);
  51. switch(in)
  52. {
  53. case 'y':
  54. case 'Y':
  55. break;
  56. default:
  57. return;
  58. }
  59. deletedBooks[totalBooksDeleted++] = myBooks[bookpos];
  60. myBooks[bookpos] = myBooks[totalBooks];
  61. totalBooks--;
  62. printf("Deleted the book successfully..\n");
  63. }
  64. void saveAndDeleteBooks()
  65. {
  66. printf("\n-------------\n");
  67. int i;
  68.  
  69. for(i=0; i<totalBooks; i++)
  70. if(myBooks[i]->needSave)
  71. saveBook(myBooks[i]);
  72. printf("Save of books done..\n-------------\n");
  73.  
  74. for(i=0; i<totalBooksDeleted; i++)
  75. deleteBook(deletedBooks[i]);
  76. printf("Delete of books done..\n-------------\n");
  77.  
  78. }
  79. void deleteBook(Book* myBook)
  80. {
  81. if(!remove(myBook->PATH_ON_DISK))
  82. printf("Deleted book of ISBN -->%s\n",myBook->ISBN);
  83. }
  84. int find_Isbn(char* isbn)
  85. {
  86. int i,found = 0;
  87. for(i=0; i<totalBooks; i++)
  88. if(!strcmp(myBooks[i]->ISBN,isbn))
  89. {
  90. found = 1;
  91. break;
  92. }
  93. return found;
  94. }
  95. int isBook_Available(char* isbn)
  96. {
  97. int i,book_pos = -1;
  98. for(i=0; i<totalBooks; i++)
  99. if(!strcmp(myBooks[i]->ISBN,isbn))
  100. {
  101. printf("Copies : %d\n",myBooks[i]->currentAvailable);
  102. if(myBooks[i]->currentAvailable >0)
  103. book_pos = i;
  104. break;
  105. }
  106. return book_pos;
  107. }
  108. void bookTask(int type)
  109. {
  110. switch(type)
  111. {
  112. case 1:// Insert a new book..
  113. {
  114. addANewBook();
  115. break;
  116. }
  117. case 2:// search for a book
  118. {
  119. searchForABook();
  120. break;
  121. }
  122. case 3:
  123. {
  124. addNewCopies();
  125. break;
  126. }
  127. case 4:// delete books
  128. {
  129. deleteBook_();
  130. break;
  131. }
  132. case 5:
  133. {
  134. int i;
  135. for(i=0; i<totalBooks; i++)
  136. printf("NoOfCopies:%d , Name:%s\n",myBooks[i]->noOfCopies,myBooks[i]->title);
  137. break;
  138. }
  139.  
  140. }
  141. }
  142. void addNewCopies()
  143. {
  144. char ISBN[14];
  145. printf("Enter the ISBN:");
  146. getchar();
  147. gets(ISBN);
  148. int i;
  149. for(i = 0; i<totalBooks; i++)
  150. if(!strcmp(ISBN,myBooks[i]->ISBN))
  151. {
  152. printf("Current no. of copies for the book \"%s\" is : %d\n",myBooks[i]->title,myBooks[i]->noOfCopies);
  153. printf("Enter the new no. of copies:");
  154. scanf("%d",&myBooks[i]->noOfCopies);
  155. myBooks[i]->needSave = 1;
  156. printf("Updated the book..\n");
  157. break;
  158. }
  159. }
  160. void searchForABook()
  161. {
  162. system("@cls");
  163. char c;
  164. printf("Search by ISBN, Title, Category\nEnter the first character:");
  165. getchar();
  166. scanf("%c",&c);
  167. switch(c)
  168. {
  169. case 'c':
  170. case 'C':
  171. {
  172. int cat;
  173. getchar();
  174. printf("Categories 1.Programming - 2.Comic - 3.Action\n");
  175. printf("Enter you Category:");
  176. scanf("%d",&cat);
  177. int i,found = 0;
  178. printf("Books of category : %s\n",getCat(cat));
  179. for(i = 0; i< totalBooks; i++)
  180. if(myBooks[i]->bookCategory == cat)
  181. {
  182. printBook(myBooks[i]);
  183. found++;
  184. }
  185. printf("Total search results : %d\n-------------\n",found);
  186. break;
  187. }
  188. case 't':
  189. case 'T':
  190. {
  191. getchar();
  192. printf("Enter the book title or a part of it:");
  193. char tit[100];
  194. gets(tit);
  195. int i,found = 0;
  196. for(i = 0; i< totalBooks; i++)
  197. if(strstr(myBooks[i]->title,tit))
  198. {
  199. printBook(myBooks[i]);
  200. found++;
  201. }
  202. printf("Total search results : %d\n-------------\n",found);
  203. break;
  204. }
  205. case 'i':
  206. case 'I':
  207. {
  208. char ISBN[14];
  209. getchar();
  210. printf("Enter you ISBN:");
  211. gets(ISBN);
  212. int i,found = 0;
  213. for(i = 0; i< totalBooks; i++)
  214. if(!strcmp(myBooks[i]->ISBN,ISBN))
  215. {
  216. printBook(myBooks[i]);
  217. found++;
  218. }
  219. printf("Total search results : %d\n",found);
  220. break;
  221. }
  222. default:
  223. printf("Invalid search character..\n");
  224. }
  225.  
  226.  
  227. }
  228. void addANewBook()
  229. {
  230. system("@cls");
  231. Book* newBook = (Book*)malloc(sizeof(Book));
  232. gets(newBook->author);
  233. printf("Author:");
  234. gets(newBook->author);
  235. printf("Publisher:");
  236. gets(newBook->publisher);
  237. printf("Title:");
  238. gets(newBook->title);
  239. printf("ISBN:");
  240. gets(newBook->ISBN);
  241. printf("NoOfCopies:");
  242. scanf("%d",&newBook->noOfCopies);
  243. printf("Current Available:");
  244. scanf("%d",&newBook->currentAvailable);
  245. printf("Categories 1.Programming - 2.Comic - 3.Action\n");
  246. printf("Category:");
  247. scanf("%d",&newBook->bookCategory);
  248.  
  249. char fileName[30] ="database\\books\\";
  250. strcat(fileName,newBook->ISBN);
  251. strcat(fileName,".json");
  252. if(fileExists(fileName))
  253. {
  254. printf("This book already exists, Try using another ISBN.\n--------\n");
  255. return;
  256. }
  257.  
  258. printBook(newBook);
  259. newBook->needSave = 1;
  260. int pos = totalBooks++;
  261. myBooks[pos] = newBook;
  262. }
  263. void saveBook(Book* newBook)
  264. {
  265. cJSON *root;
  266. root = cJSON_CreateObject();
  267. cJSON_AddItemToObject(root, "Title", cJSON_CreateString(newBook->title));
  268. cJSON_AddItemToObject(root, "ISBN", cJSON_CreateString(newBook->ISBN));
  269. cJSON_AddItemToObject(root, "Author", cJSON_CreateString(newBook->author));
  270. cJSON_AddItemToObject(root, "Publisher", cJSON_CreateString(newBook->publisher));
  271. cJSON_AddItemToObject(root,"NoOfCopies",cJSON_CreateNumber(newBook->noOfCopies));
  272. cJSON_AddItemToObject(root,"CurrentAvailable",cJSON_CreateNumber(newBook->currentAvailable));
  273. cJSON_AddItemToObject(root,"Category",cJSON_CreateNumber(newBook->bookCategory));
  274. FILE* fptr;
  275. char fileName[30] ="database\\books\\";
  276. char *jsonFile = cJSON_Print(root);
  277. strcat(fileName,newBook->ISBN);
  278. strcat(fileName,".json");
  279. fptr = fopen(fileName,"w");
  280. fprintf(fptr,jsonFile);
  281. fclose(fptr);
  282. newBook->needSave = 0;
  283. printf("Book %s updated successfully.\n",newBook->ISBN);
  284. }
  285. void printBook(Book* myBook)
  286. {
  287. printf("\n---------------\n");
  288. 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);
  289. printf("Category : %s\n----------------\n",getCat(myBook->bookCategory));
  290. }
  291. char* getCat(int cat)
  292. {
  293. char* myStr = (char*)malloc(20);
  294. switch(cat)
  295. {
  296. case 1:
  297. strcpy(myStr,"Programming");
  298. break;
  299. case 2:
  300. strcpy(myStr,"Comic");
  301. break;
  302. case 3:
  303. strcpy(myStr,"Action");
  304. break;
  305. }
  306. return myStr;
  307. }
  308. Book* createNewBook(int pos)
  309. {
  310. Book* ptr = (Book*)malloc(sizeof(Book));
  311. myBooks[pos] = ptr;
  312. return ptr;
  313. }
  314. void loadBook(char* bookName)
  315. {
  316. int current = totalBooks++;
  317. // realloc(myBooks,sizeof(Book)*totalBooks);
  318. Book* newBook= createNewBook(current);
  319. FILE *fileptr;
  320. char *buffer;
  321. long filelen;
  322. char bookPath[50] = "database\\books\\";
  323. strcat(bookPath,bookName);
  324. fileptr = fopen(bookPath, "rb"); // Open the file in binary mode
  325. if(!fileExists(bookPath)) return;
  326. fseek(fileptr, 0, SEEK_END); // Jump to the end of the file
  327. filelen = ftell(fileptr); // Get the current byte offset in the file
  328. rewind(fileptr); // Jump back to the beginning of the file
  329.  
  330. buffer = (char *)malloc((filelen+1)*sizeof(char)); // Enough memory for file + \0
  331. fread(buffer, filelen, 1, fileptr); // Read in the entire file
  332. fclose(fileptr); // Close the file
  333. cJSON *root = cJSON_Parse(buffer);
  334. cJSON *title = cJSON_GetObjectItemCaseSensitive(root, "Title");
  335. cJSON *isbn = cJSON_GetObjectItemCaseSensitive(root, "ISBN");
  336. cJSON *author = cJSON_GetObjectItemCaseSensitive(root, "Author");
  337. cJSON *publisher = cJSON_GetObjectItemCaseSensitive(root, "Publisher");
  338. cJSON *noOfCopies = cJSON_GetObjectItemCaseSensitive(root, "NoOfCopies");
  339. cJSON *CurrentAvailable = cJSON_GetObjectItemCaseSensitive(root, "CurrentAvailable");
  340. cJSON *bookCategory = cJSON_GetObjectItemCaseSensitive(root, "Category");
  341. strcpy(newBook->title,title->valuestring);
  342. strcpy(newBook->ISBN,isbn->valuestring);
  343. strcpy(newBook->author,author->valuestring);
  344. strcpy(newBook->publisher,publisher->valuestring);
  345. newBook->noOfCopies=noOfCopies->valueint;
  346. newBook->currentAvailable=CurrentAvailable->valueint;
  347. newBook->bookCategory=bookCategory->valueint;
  348. newBook->needSave = 0;
  349. strcpy(newBook->PATH_ON_DISK,bookPath);
  350. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement