-
Notifications
You must be signed in to change notification settings - Fork 0
/
books.h
363 lines (312 loc) · 8.91 KB
/
books.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "functions.h"
// global identifier variable used for auto increment book id
long int identifier =1l;
struct Book{
int id;
char name[40];
char author[30];
int price ;
} ;
struct node {
struct Book book;
struct node* next;
struct node* prev;
} ;
// Book details functions //
struct Book fillBookData();
void printBook(struct Book book);
// list functions //
struct node * createnode(struct Book book);
int addnode(struct node** phead,struct node** ptail, struct Book book);
int readFileIntoList(struct node* * phead , struct node** ptail,char path[]);
int writeIntoFile(struct Book* book ,char path[] );
void printList(struct node* phead);
int getSize(struct node* phead);
struct Book * search(struct node* phead,char name[]);
// sorting
void swap(struct node* a , struct node* b);
void bubbleSort(struct node* head);
int removeBook(struct node ** phead,struct node** ptail,int id,char path[]);
/**
* @Fill book details from user input
* @param void
* @return book : struct Book
*/
struct Book fillBookData(){
char temp;
struct Book book;
book.id = identifier;
printf(KGRN);
printf("Enter the book name : ");
scanf("%c",&temp);
fgets(book.name,40,stdin);
//scanf("%s",book.name);
printf("Enter book Author : ");
scanf("%s",book.author);
printf("Enter the book price : ");
scanf("%d",&book.price);
printf(KNRM);
return book;
}
/**
* @print the details of a single book
* @param book : struct Book
* @return void
*/
void printBook(struct Book book){
printf("\n");
printf("-------------------------\n");
printf(KYEL "book's id : ");
printf(KWHT"%d \n",book.id);
printf(KYEL "book's name : ");
printf(KWHT"%s \n",book.name);
printf(KYEL "book's author: ");
printf(KWHT"%s \n",book.author);
printf(KYEL "book's price : ");
printf(KWHT"%d \n",book.price);
printf("-------------------------\n");
printf("\n");
}
// list functions
/**
* @brief createnode function return a pointer to node
* @param book : struct Book
* @return ptr : struct node*
*/
struct node * createnode( struct Book book){
struct node * ptr = (struct node *) malloc(sizeof(struct node));
if(ptr){
ptr->book = book;
ptr->next = NULL;
ptr->prev = NULL;
}
return ptr;
}
/**
* @brief append a node to the list
* @param phead : struct node** ( pointer to the head pointer of the list )
* @param ptail : struct node** ( pointer to the tail pointer of the list)
* @param book : struct Book
* @return retval : int (check if addition was successful)
*/
int addnode(struct node** phead,struct node** ptail, struct Book book){
int retval =0;
struct node* ptr = createnode(book);
if(ptr){
if(!*phead){
*phead =*ptail = ptr;
}
else{
ptr->prev = *ptail;
(*ptail)->next = ptr;
*ptail = ptr;
}
identifier++;
retval =1;
}
return retval;
}
/**
* @brief write data into the .dat file
* @param book : struct Book*
* @param path : char [] ( the file path to write into)
* @return retval : int (check if writing was successful)
*/
int writeIntoFile(struct Book* book ,char path[] ){
int retval =0;
FILE * outfile ;
outfile = fopen (path, "a");
// check if the file is opened
if (outfile == NULL)
{
fprintf(stderr, "\nError opening the file .. data may not be saved \n");
}
else{
// writing the content of book struct into the file
fwrite (book,sizeof(struct Book),1,outfile);
fclose (outfile);
retval =1;
}
return retval;
}
/**
* @brief load the data from file into list
* @param phead : struct node**
* @param ptail : struct node**
* @param path : char [] ( the file path to read from)
* @return retval : int (check if reading was successful)
*/
int readFileIntoList(struct node** phead , struct node** ptail, char path[] ){
int retval =0;
struct Book input;
FILE * infile ;
infile = fopen(path, "r");
if(infile != NULL){
while(fread(&input, sizeof(struct Book), 1, infile)) {
// add each struct into the list
addnode(phead,ptail,input);
identifier=input.id;
}
// close file
fclose (infile);
retval =1;
identifier++;
}
return retval;
}
// print the whole list of books
void printList(struct node* phead){
struct node * temp = phead;
if(!temp){
printf(KRED"\nLibrary is empty.\n\n");
printf(KWHT);
}
else{
printf("\n\n [[\n\n");
while(temp){
printf(KRED " -------------------\n");
printf(KRED " -------------------\n");
printf(KYEL " Id ") ;
printf(KWHT ": %d\n",temp->book.id);
printf(KYEL " Name ");
printf(KWHT ": %s\n",temp->book.name);
printf(KRED " -------------------\n");
printf(KRED " -------------------\n");
temp= temp->next;
printf( KWHT " ,\n");
}
printf("\n ]]\n\n");
}
}
// get list size
int getSize(struct node* phead){
int size =0;
struct node* temp = phead;
while(temp){
size ++;
temp = temp->next;
}
return size;
}
/**
* @brief search for a book using its name
* @param phead : struct node*
* @param name : char [] ( the book name )
* @return ptr : struct Book * ( pointer to the found book " NULL if not found")
*/
struct Book * search(struct node* phead,char name[]){
struct node* temp = phead;
struct Book* ptr = NULL;
char tempName[40];
// convert to lower case for ignore case comparison
tolowercase(name);
while (temp){
strcpy(tempName,temp->book.name);
tolowercase(tempName);
if(strcmp(tempName,name) == 0){
ptr =& (temp->book);
temp = NULL;
}
else{
temp = temp->next;
}
}
return ptr;
}
// sort according to book names
void bubbleSort(struct node *head)
{
int swapped, i;
struct node *ptr1;
struct node *lptr = NULL;
char tmp1 [40] ;
char tmp2[40];
/* Checking for empty list */
if (head == NULL)
return;
do
{
swapped = 0;
ptr1 = head;
while (ptr1->next != lptr)
{
strcpy(tmp1 , ptr1->book.name);
strcpy(tmp2,ptr1->next->book.name);
// for non case sensitive comparison
tolowercase(tmp1);
tolowercase(tmp2);
if (strcmp(tmp1, tmp2 ) >0)
{
swap(ptr1, ptr1->next);
swapped = 1;
}
ptr1 = ptr1->next;
}
lptr = ptr1;
}
while (swapped);
}
/* function to swap data of two nodes a and b*/
void swap(struct node *a, struct node *b)
{
struct Book temp = a->book;
a->book = b->book;
b->book= temp;
}
/**
* @brief Delete a book
* @param &phead
* @param &ptail
* @param id
*/
int removeBook(struct node** phead ,struct node** ptail, int id,char path[]){
int found =0 , deleted =0;
struct node * temp = *phead;
// if the list isn't empty
if(temp){
// still in the list boundary and didn't find the record yet
while(temp && !found){
if(temp->book.id == id){
// check if the found record is the only record
if(temp == *phead && temp == *ptail){
*phead = *ptail = NULL;
identifier = 1;
}
// if not the only record but the first record
else if(temp == *phead){
( *phead)->next->prev = NULL;
(*phead) = (*phead)->next;
free(temp);
}
// if not the only record but the last record
else if(temp == *ptail){
(*ptail)->prev->next = NULL;
*ptail = (*ptail)->prev;
free(temp);
identifier = id;
}
// a record in between any other 2 records
else{
temp->next->prev = temp->prev;
temp->prev->next = temp->next;
free(temp);
}
found =1;
deleted =1;
}
temp = temp->next;
}
}
if(deleted){
remove(path);
temp = *phead;
while(temp){
writeIntoFile(&(temp->book) , path);
temp = temp->next;
}
}
return deleted ;
}