-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCSE232TermProject.c
537 lines (499 loc) · 12.3 KB
/
CSE232TermProject.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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#define MAXCHAR 40
#define MAXNODE 30
bool edit (char *,int);
bool insert(int, char *);
bool delete(int);
bool print();
bool save();
bool menu();
bool commit();
char toUpperChar(char);
bool deleteEmpty();
void doMethod(int index);
bool insertDiffArrayDelete(int , int );
bool insertDiffArrayInsert(int , int , char* );
void doMethod(int );
struct dfs
{
int code; // 1-insertion 2-deletion 0-otherwise
int statno;
char statement[40]; // only for insertion
};
struct node
{
int statno;
char statement[MAXCHAR];
struct node* next;
};
typedef struct node node;
typedef struct dfs dfs;
struct dfs diffs[20]; // max. 20 changes
int version=0;
node textbuffer[MAXNODE];
node *head = &textbuffer[0];
char txt[50];
char dif[50];
int main()
{
int i = 1;
for (i = 1; i <= MAXNODE; i++) // assigns the appropriate numbers at startup to statno and next variables in the textbuffer array
{
textbuffer[i - 1].statno = 50;
textbuffer[i - 1].next = &textbuffer[i];
textbuffer[i -1].statement[0] = '*';
textbuffer[i -1].statement[1] = '\n';
}
textbuffer[MAXNODE-1].next = NULL; //sets the last element's .next to null
i = 0;
for(i ; i < 20 ; i++)
{
diffs[i].code = 0;
}
menu(); // menu is a seperate function for extended flexibilty
return 0;
}
bool menu()
{
int stat;
char input , c;
char chPtr[50];
do
{
printf("Enter;\nE for Edit\nI for Insert\nD for Delete\nP for Print\nS for Save\nX to Quit\nC To commit>> ");
scanf(" %c", &input);
while((c = getchar()) != '\n');
input = toUpperChar(input);
if(input == 'E') // at edit we get the statement no and statemen. we also empty buffer each time to avoid unneccessary bugs
{
int versionCheck = 0;
printf("Enter file name:\n");
scanf("%s", chPtr);
while((c = getchar()) != '\n');
printf("Enter version check (0 for latest)(1 for startup) :\n");
scanf("%d",&versionCheck);
while((c = getchar()) != '\n');
edit(chPtr,versionCheck);
}
else if(input == 'I')
{
printf("Enter Statement Number\n");
scanf(" %d", &stat);
while((c = getchar()) != '\n');
printf("Enter Statement\n");
gets(chPtr);
strcat(chPtr,"\n");
insertDiffArrayInsert(1,stat,chPtr); // add to the diffs buffer
insert(stat,chPtr); // and now insert the line to the textbuffer
}
else if(input == 'D')
{
printf("Enter Statement Number\n");
scanf(" %d", &stat);
while((c = getchar()) != '\n');
insertDiffArrayDelete(2,stat); // same as insert, add the modification to the diffs buffer as well
delete(stat);
}
else if(input == 'P')
{
print();
}
else if(input == 'S')
{
save();
}
else if(input == 'C')
{
commit();
}
else
{
printf("invalid input try again\n");
}
} while (input != 'X');
printf("Quitting\n");
}
bool edit(char *filename, int versionCheck)
{
strcpy(txt, filename); // gets the file name and saves it to global variables with ".txt" and ".dif" extensions for future use
strcat(txt, ".txt");
strcpy(dif,filename);
strcat(dif, ".dif");
FILE *file, *fileCom;
int count = 0;
int nodeCount = 0;
char str[MAXCHAR];
struct node* temp = head;
struct dfs* tempdfs = &diffs[0];
int incnt = 0;
file = fopen(txt, "r");
if (!file) // see if we could open the file properly
{
printf("Can't open file\nCreating %s instead\n", filename);
file = fopen((txt), "w+"); // else create it
//exit( EXIT_FAILURE );
}
fileCom = fopen(dif, "r"); // create a pointer to the dif file
if (!fileCom)
{
fileCom = fopen(dif, "w+");
}
while (fgets(str, MAXCHAR, file) != NULL){ // read the whole .txt file line by line
sscanf(str,"%d",&temp->statno);
if(temp->statno < 10)
{
for(count = 2;count < MAXCHAR;count++)
temp->statement[count-2] = str[count];
}
else
{
for(count = 3;count < MAXCHAR;count++)
temp->statement[count-3] = str[count];
}
temp = temp->next;
}
while (fgets(str, 20, fileCom) != NULL) // get variables from .dif file in a formatted fashion with sscanf
{
sscanf(str, "%d %d", &diffs[incnt].code, &diffs[incnt].statno);
for(count = 4; count < 43 ; count++)
{
diffs[incnt].statement[count-4] = str[count];
}
incnt++;
}
if (versionCheck == 0) // if user wants the latest version
{
int i = 1;
int counter = 1;
while(diffs[i].code != 0)
{
if(diffs[i].code == -1 && diffs[i+1].code == 0 )
{
counter++;
i++;
break;
}
else if(diffs[i].code == -1)
{
counter++;
i = i+2;
}
else
{
doMethod(i);
i++;
}
}
diffs[i].code = counter;
version = counter;
return true;
}
else if(versionCheck != 1) // if user wants a specific version
{
int i = 1;
int counter = 1;
while(diffs[i].code != 0)
{
if(diffs[i].code == -1)
{
counter++;
if(counter == versionCheck)
{
printf("done");
i = i+1;
diffs[i].code = counter;
i++;
for(i ; i < 20 ; i++)
{
diffs[i].code = 0;
}
version = counter;
return true;
}
i = i+2;
}
else
{
doMethod(i);
i++;
}
}
}
else if(versionCheck == 1) // if user wants the startup version
{
diffs[0].code = 1;
}
version++;
fclose(file); // close the file when we are done with it
return true;
}
bool insert(int statno, char *stat)
{
struct node* prev = head;
struct node* temp = head;
struct node* tmpPtrPrev = head;
int i = 0;
int j = 0;
int counter = 1;
if (statno > 30) // if the line to be inserted is above 30 break since limit is 30
{
print("error");
return false;
}
if (head->statement[0] == '*') // for the case of first insertion to the list
{
for (j = 0 ; j < 40 ; j++)
{
head->statement[j] = stat[j]; // set the heads statement to the input from function
}
head->statno = statno;
return true;
}
else if (prev->statno >= statno) // when the insertion is added at the start of the list
{
if (prev->statno == statno)
{
for(j = 0 ; j < 40 ; j++)
{
prev->statement[j] = stat[j];
}
prev->statno = statno;
return true;
}
else
{
while (temp->next->statement[0] != '*')
{
temp = temp->next;
}
tmpPtrPrev = temp->next;
temp->next = temp->next->next;
tmpPtrPrev->next = prev;
head = tmpPtrPrev;
for (j = 0 ; j < 40 ; j++)
{
tmpPtrPrev->statement[j] = stat[j];
}
tmpPtrPrev->statno = statno;
return true;
}
}
else // for the case of inserting to the middle and to the end of the list
{
while (prev->next->statno <= statno)
{
if(prev->next->statno == statno)
{
for(j = 0 ; j < 40 ; j++)
{
prev->next->statement[j] = stat[j];
}
prev->next->statno = statno;
return true;
}
else
{
prev = prev->next;
}
}
while(temp->next->statement[0] != '*')
{
temp = temp->next;
}
for(j = 0 ; j < 40 ; j++)
{
temp->next->statement[j] = stat[j];
}
temp->next->statno = statno;
tmpPtrPrev = temp->next;
temp->next = temp->next->next;
tmpPtrPrev->next = prev->next;
prev->next = tmpPtrPrev;
return true;
}
}
bool delete(int statno)
{
struct node *prev = head;
struct node *tmp;
struct node *tmp2 = head;
int counter = 0;
if(head->statno == 50) // 50 is our signature to see if the list is empty
{
printf("list is empty\n");
return false;
}
else if(head->statno == statno) // if we delete from head
{
head = head->next;
for(int i = 0; i< 29 ; i++)
{
tmp2 = tmp2->next;
}
tmp2 ->next = prev->next;
prev ->next = NULL;
prev->statno == 50;
prev->statement[0] = '*';
return true;
}
else // normally delete
{
while(prev->next->statno != statno)
{
prev = prev->next;
counter++;
if(counter == 29) //null pointer ex
{
printf("not found\n");
return false;
}
}
}
while(prev->next->statno != statno)
{
prev = prev->next;
counter++;
if(counter == 29) //null pointer ex
{
printf("not found");
return false;
}
}
tmp = prev->next;
for(int i = 0; i< 29 ; i++) // we find the last element
{
tmp2 = tmp2->next;
}
tmp2 ->next = prev->next;
prev->next = prev->next->next;
tmp ->next = NULL;
tmp->statno == 50;
tmp->statement[0] = '*';
}
bool print()
{
struct node* temp = head;
int charCount=0;
printf("printing\n");
int i=0;
while(temp->statement[0] != '*') // print the while textbuffer to the console in a linked list fashion
{
printf("%d ", temp->statno);
printf("%s", temp->statement);
temp = temp->next;
}
return true;
}
bool save()
{
FILE* filePtr = fopen(dif,"w"); // open the file from global variable we saved before
fprintf(filePtr, "%d\n", diffs[0].code);
int i = 1;
for(i = 0;i<20;i++) // find the first 0 in the diffs buffer and set it to -1 for better formmatting and to avoid bugs
{
if(diffs[i].code == 0)
{
diffs[i].code = -1;
break;
}
}
for (i=0; diffs[i].code != 0; i++) // while saving we have 3 cases
{
if (diffs[i].code == 1 && i!=0) // format the line if the code is for insert
{
fprintf(filePtr,"%d %d %s",diffs[i].code, diffs[i].statno, diffs[i].statement);
}
else if (diffs[i].code == 2) // for the delete
{
fprintf(filePtr,"%d %d\n",diffs[i].code, diffs[i].statno);
}
else if (diffs[i].code == -1 && diffs[i+1].code == 0) // the case for control
{
fprintf(filePtr, "%d\n",diffs[i].code); //
version++;
diffs[i+1].code = version;
break;
}
else if(diffs[i].code == -1) // and if the line is -1 which means end of version print -1 and the version
{
fprintf(filePtr, "%d\n",diffs[i].code);
i=i+1;
fprintf(filePtr,"%d\n",diffs[i].code);
}
}
fclose(filePtr);
printf("Saved\n");
return true;
}
bool commit()
{
FILE *filePtr = fopen(txt, "w"); // at commit we print the contents of buffertext to the .txt file
node* iter = head;
int counter = 0;
for (counter ; counter < 29 ; counter++) // gradually continue whole buffer
{
if (iter->statement[0] == '*') // skip if the line is a *
continue;
fprintf(filePtr, "%d %s", iter->statno, iter->statement); // then using fprintf print it
iter = iter->next;
}
fclose(filePtr);
version = 0;
filePtr = fopen(dif, "w"); // also open .dif file to empty it and write 0 in it as demonstrated in the pdf file
fprintf(filePtr, "%d", version);
fclose(filePtr);
printf("Commit Done\n");
for(counter = 0; counter < 20 ; counter++)
{
diffs[counter].code = 0;
}
return true;
}
char toUpperChar(char ch) // for the input in menu to avoid any mistakes
{
if (ch >= 'A' && ch <= 'Z')
return ch;
else
return ch - 32;
}
void doMethod(int index) // for making the same changes in diffs buffer
{
if(diffs[index].code == 1) // if the line is insert
{
insert(diffs[index].statno,diffs[index].statement);
}
else if(diffs[index].code == 2) // if the line is delete
{
delete(diffs[index].statno);
}
}
bool insertDiffArrayInsert(int code , int statno , char* ptr) // inserting to the diffs buffer
{
int i=0;
for(i ; i<20 ; i++ )
{
if(diffs[i].code == 0)
{
diffs[i].code = code;
diffs[i].statno = statno;
strcpy(diffs[i].statement,ptr);
return 1;
}
}
}
bool insertDiffArrayDelete(int code , int statno) // and deleting from the diffs buffers
{
int i=0;
for(i ; i<20 ; i++ )
{
if(diffs[i].code == 0)
{
diffs[i].code = code;
diffs[i].statno = statno;
return 1;
}
}
}