-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbookOrders.c
370 lines (319 loc) · 9.34 KB
/
bookOrders.c
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
362
363
364
365
366
367
368
369
#define _GNU_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "bookOrders.h"
#include <pthread.h>
#include <unistd.h>
#include "datstr.h"
#include <semaphore.h>
int main(int argc, char **argv)
{
struct User** users=NULL;/*the user database*/
int size=0; /*number of users in database*/
FILE * dbFile=NULL;
FILE * orderFile=NULL;
if (argc > 1)
{
if (strcmp(argv[1],"-h") == 0)
{
printf("To use this program please input the following\nArg 1: the name of the database input file\nArg 2: the name of the book order input file\nArg 3: the list of catgory names (alphanumeric strings separated by blanks in\na single- or double-quoted string)\n\n");
return 0;
}
}
if (argc != 4)
{
printf("User entered in incorrect number of arguments\n");
return 0;
}
dbFile = fopen(argv[1],"r");
if(dbFile==NULL){
fprintf(stderr,"ERROR: failed to open user database file\n");
return 0;
}
orderFile = fopen(argv[2],"r");
if(orderFile==NULL){
fprintf(stderr,"ERROR: failed to open orders file\n");
fclose(dbFile);
return 0;
}
users = openDatabase(dbFile, &size);
if (users == NULL)
{
printf("ERROR: database creation failed\n");
fclose(dbFile);
fclose(orderFile);
return 0;
}
/*This should populate the orders within the users*/
openOrders(orderFile,users,size);
fclose(dbFile);
fclose(orderFile);
printToFile(users,size);
freeUsers(users,size);
return 0;
}
/*create the user database*/
struct User ** openDatabase (FILE * dbFile, int* size)
{
size_t length = 0;
int largest = 0 ;
int existUsersFlag=0;
struct User *newUser =NULL;
struct Node *tail =NULL;
struct Node *temp;
struct Node *it;
struct User **userList;
char * username =NULL;
char * uid = NULL;
char * buffer=NULL;
char * input = NULL;
if (dbFile == NULL)
{
fprintf(stderr, "ERROR: invalid database file\n");/*shouldn't happen, this was checked in main*/
return NULL;
}
/*username | userID | credits | address | state | zip*/
while(getline(&input,&length,dbFile) != -1)
{
existUsersFlag=1;
buffer = strtok(input,"|");
username = (char*)calloc(strlen(buffer)+1,sizeof(char));
strcpy(username,buffer);
buffer= strtok(NULL,"|");
uid = (char*)calloc(strlen(buffer)+1,sizeof(char));
strcpy(uid,buffer);
buffer= strtok(NULL,"|"); /*buffer == credits*/
newUser = createUser(username,atoi(uid),atof(buffer));
free(uid);
if (newUser->uid > largest)
{
largest = newUser->uid;
}
if (tail != NULL)
{
temp = calloc(1,sizeof(struct Node));
temp->data = newUser;
temp->next = tail->next;
tail->next = temp;
tail = temp;
}
else
{
/*Create a new node and add to the head */
tail = calloc(1,sizeof(struct Node));
tail->data = newUser;
tail->next = tail;
}
}
if (existUsersFlag==0){
fprintf(stderr, "ERROR: no users in database\n");
return NULL;
}
userList = calloc(largest+1,sizeof(struct User*));
it = tail->next;
*size=largest+1;
while(it != tail)
{
userList[((struct User*)it->data)->uid] = ((struct User*)it->data);
temp = it;
it = it->next;
free(temp);
}
userList[((struct User*)it->data)->uid] = ((struct User*)it->data);
free(it);
free(input);
return userList;
}
/*producer thread creates the consumer threads here*/
void openOrders (FILE * bookOrderFile,struct User **users, int size)
{
int counter;
size_t length = 0;
char * input = NULL;
struct ConsumerThreadData* data;
pthread_t id;
sem_t * semaphore = (sem_t*)malloc(sizeof(sem_t));
pthread_cond_t *condition = (pthread_cond_t*)malloc(sizeof(pthread_cond_t));
pthread_mutex_t *condLock = (pthread_mutex_t*)malloc(sizeof(pthread_mutex_t));
sem_init(semaphore,0,0);
pthread_cond_init(condition,NULL);
pthread_mutex_init(condLock,NULL);
if(bookOrderFile == NULL || users == NULL)
{
return;
}
while(getline(&input,&length,bookOrderFile) != -1)
{
/*start consumer thread, feed it a struct containing the input line which contains a single order and the pointer to the user database*/
data = (struct ConsumerThreadData*) calloc (1, sizeof(struct ConsumerThreadData));
data->users = users;
data->input = input;
data->usersSize = size;
data->condition = condition;
data->condLock = condLock;
sem_post(semaphore);
data->semaphore = semaphore;
pthread_create(&id, NULL, consumer, data);
/*set input to NULL so getline will allocate new memory for every order, the consumer threads are responsible for freeing their input strings*/
input=NULL;
}
/*wait for all the consumer threads to finish*/
sem_getvalue(semaphore,&counter);
pthread_mutex_lock(condLock);
while(counter!=0){
pthread_cond_wait(condition,condLock);
sem_getvalue(semaphore,&counter);
}
free(input);/*if we're short one book, remove this line. May cause leaks though.*/
free(condition);
pthread_mutex_destroy(condLock);
free(condLock);
free(semaphore);
}
void *consumer(void* dat){
char* endptr,*resptr;
char *bookTitle, *cost, *uid;
int id;
struct ConsumerThreadData* data = (struct ConsumerThreadData*)dat;
sem_t* semaphore = data->semaphore;
pthread_mutex_t * condLock = data->condLock;
pthread_cond_t * condition = data->condition;
pthread_detach(pthread_self());
if(data->input ==NULL){
fprintf(stderr, "\n\n\ninput error in consumer\n\n\n\n");
free(data);
sem_wait(semaphore);
return NULL;
}
bookTitle=strtok_r(data->input,"|",&resptr);
cost=strtok_r(NULL,"|",&resptr);
uid=strtok_r(NULL,"|",&resptr);
id = (int)strtol(uid,&endptr,10);
if(id+1 > data->usersSize){
fprintf(stderr,"\n\n\nuser id error(1) in consumer: id = %d\n%s\n\n\n\n",id,data->input);
free(data->input);
free(data);
pthread_mutex_lock(condLock);
sem_wait(semaphore);
pthread_cond_signal(condition);
pthread_mutex_unlock(condLock);
return NULL;
}
if(id<1){
fprintf(stderr,"\n\n\nuser id error(2) in consumer: id = %d\n%s\n\n\n\n",id,data->input);
free(data->input);
free(data);
pthread_mutex_lock(condLock);
sem_wait(semaphore);
pthread_cond_signal(condition);
pthread_mutex_unlock(condLock);
return NULL;
}
if(data->users[id]==NULL){
fprintf(stderr,"\n\n\nuser id error(3) in consumer: id = %d\n%s\n\n\n\n",id,data->input);
free(data->input);
free(data);
pthread_mutex_lock(condLock);
sem_wait(semaphore);
pthread_cond_signal(condition);
pthread_mutex_unlock(condLock);
return NULL;
}
pthread_mutex_lock(data->users[id]->userMutex);
purchase(data->users[id]->success, data->users[id]->fail, data->users[id], strtod(cost,&endptr), bookTitle);
pthread_mutex_unlock(data->users[id]->userMutex);
free(data);
pthread_mutex_lock(condLock);
sem_wait(semaphore);
pthread_cond_signal(condition);
pthread_mutex_unlock(condLock);
return NULL;
}
/*prints the output file*/
void printToFile (struct User ** userArray,int length)
{
int i = 0;
struct Node *ptr;
FILE* file = fopen ("report.txt","w");
if (userArray == NULL)
{
return;
}
else
{
while(i<length)
{
if(userArray[i]!=NULL){
fprintf(file,"=== BEGIN CUSTOMER INFO ===\n");
fprintf(file,"### BALANCE ###\n");
fprintf(file,"Customer name: %s\n",userArray[i]->username);
fprintf(file,"Customer ID number: %d\n",userArray[i]->uid);
fprintf(file,"Remaining credit balance after all purchases (a dollar amount): %f\n",userArray[i]->remainingCredits);
fprintf(file,"### SUCCESSFUL ORDERS ###\n");
if(userArray[i]->success!=NULL){
ptr=userArray[i]->success->next;
while(ptr!=userArray[i]->success){
fprintf(file,"%s| %f| %f\n", ((struct Order*)ptr->data)->bookTitle, ((struct Order*)ptr->data)->cost, ((struct Order*)ptr->data)->currentCredits);
ptr=ptr->next;
}
fprintf(file,"%s| %f| %f\n", ((struct Order*)userArray[i]->success->data)->bookTitle, ((struct Order *)userArray[i]->success->data)->cost, ((struct Order*)userArray[i]->success->data)->currentCredits);
}
fprintf(file,"### REJECTED ORDERS ###\n");
if(userArray[i]->fail!=NULL){
ptr=userArray[i]->fail->next;
while(ptr!=userArray[i]->fail){
fprintf(file,"%s| %f\n", ((struct Order*)ptr->data)->bookTitle, ((struct Order*)ptr->data)->cost);
ptr=ptr->next;
}
fprintf(file,"%s| %f\n", ((struct Order*)userArray[i]->fail->data)->bookTitle, ((struct Order*)userArray[i]->fail->data)->cost);
}
fprintf(file,"=== END CUSTOMER INFO ===\n\n");
}
i++;
}
}
fclose(file);
}
/*helper function to open orders*/
void purchase(struct Node * success, struct Node * fail,struct User * currentUser,double cost,char *bookTitle)
{
struct Node *temp;
struct Order *order;
if (currentUser->remainingCredits - cost < 0)
{
/*Add to the fail node list */
temp = (struct Node*)calloc(1,sizeof(struct Node));
order = (struct Order*)calloc(1,sizeof(struct Order));
order->bookTitle = bookTitle;
order->cost = cost;
order->currentCredits = currentUser->remainingCredits;
temp->data = order;
if(fail==NULL){
currentUser->fail = temp;
temp->next=temp;
}else{
temp->next = fail->next;
fail->next = temp;
fail = temp;
}
}
else
{
/* Add to the success node list */
temp = (struct Node *)calloc(1,sizeof(struct Node));
order = (struct Order *)calloc(1,sizeof(struct Order));
order->bookTitle = bookTitle;
order->cost = cost;
currentUser->remainingCredits = order->currentCredits = currentUser->remainingCredits-cost;
temp->data = order;
if(success==NULL){
currentUser->success=temp;
temp->next=temp;
}else{
temp->next = success->next;
success->next = temp;
success = temp;
}
}
}