-
Notifications
You must be signed in to change notification settings - Fork 0
/
asst2.c
1239 lines (1109 loc) · 29.1 KB
/
asst2.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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "asst2.h"
static masterFileList *paths = NULL;
static treeNode **mhArray = NULL;
static unsigned iCounter = 0;
int main(int argc, char * argv[]){
if(argc < 3){
fprintf(stderr, "Error. Not enough arguments.\n");
return -1;
}
if(argc > 5){
fprintf(stderr, "Error. Too many arguments.\n");
return -1;
}
if((argv[1][0] == '-' && argv[1][1] == 'b' && argv[2][0] == '-' && argv[2][1] == 'R') || (argv[1][0] == '-' && argv[1][1] == 'R' && argv[2][0] == '-' && argv[2][1] == 'b')){
treeNode * head = NULL;
head = fileIterator(argv[3], head);
char *outputf = concat(argv[3], "/codebook");
finalOutput(head, outputf);
tDestroy(head);
}
if(argv[1][0] == '-' && argv[1][1] == 'd'){
//decompress stuff goes here
}
if(argv[1][0] == '-' && argv[1][1] == 'c'){
char* dir = argv[2];
compressFiles(dir);
}
return 0;
}
fileList * createLinkNode(char * fileName){
fileList * temp = (fileList*)malloc(sizeof(fileList));
temp->counter = 1;
temp->fileName = strdup(fileName);
temp->next = NULL;
return temp;
}
fileList * addToFileList(fileList * fl, fileList * newLink){
if(fl == NULL){
fl = newLink;
return fl;
}
else if(strcmp(fl->fileName, newLink->fileName) == 0){
fl->counter++;
free(newLink->fileName);
free(newLink);
return fl;
}
else{
fl->next = addToFileList(fl->next, newLink);
return fl;
}
}
treeNode * createNode(char * newStr){
treeNode * temp = (treeNode*)malloc(sizeof(treeNode));
temp->left = NULL;
temp->right = NULL;
temp->files = NULL;
temp->str = strdup(newStr);
return temp;
}
treeNode * addToTree(treeNode * head, treeNode *newNode, fileList * newLink){
if(head == NULL){
head = newNode;
head->files = addToFileList(head->files, newLink);
return head;
}
else if(strcasecmp(head->str, newNode->str) == 0){
head->files = addToFileList(head->files, newLink);
free(newNode->str);
free(newNode);
return head;
}
else if(strcasecmp(head->str, newNode->str) > 0){
if(head->left == NULL){
head->left = newNode;
head->left->files = addToFileList(head->left->files, newLink);
}
else{
head->left = addToTree(head->left, newNode, newLink);
}
return head;
}
else if(strcasecmp(head->str, newNode->str) < 0){
if(head->right == NULL){
head->right = newNode;
head->right->files = addToFileList(head->right->files, newLink);
}
else{
head->right = addToTree(head->right, newNode, newLink);
}
return head;
}
return head;
}
//Frees the Tree since its all dynamic
void tDestroy(treeNode * head){
if(head == NULL){
return;
}
if(head->left != NULL){
tDestroy(head->left);
}
if(head->right != NULL){
tDestroy(head->right);
}
lDestroy(head->files);
free(head->str);
free(head);
return;
}
//Frees the List since its all dynamic
void lDestroy(fileList * fl){
if(fl->next != NULL){
lDestroy(fl->next);
}
free(fl->fileName);
free(fl);
}
//Takes in a string and pulls out a smaller string. Turns a setence into its words basically
char * pullString(int start, int end, int size, char * originalString){
int x, y;
char * toReturn = (char*)calloc(size + 1, sizeof(char));
for(x = 0, y = start; y < end; x++, y++){
toReturn[x] = originalString[y];
}
return toReturn;
}
/**
* Tokenizes the words that are passed to it.
*/
treeNode * tokenize(char * fileContents, treeNode * head, char * currentFile){
if(fileContents == NULL){
return head;
}
char * inputString = fileContents;
char * tempString;
int startingPos = -1;
int endingPos = 0;
int sizeOfString = 0;
int len = 0;
int i = 0;
len = strlen(inputString);
treeNode * tempNode;
fileList * tempLink;
for(i = 0; i <= len; i++){
//if(isalpha(inputString[i]) == 0 && isdigit(inputString[i]) == 0){
if(isspace(inputString[i]) != 0){
if(sizeOfString == 0){
continue;
}
else{
endingPos = i;
tempString = pullString(startingPos, endingPos, sizeOfString, inputString);
tempNode = createNode(tempString);
tempLink = createLinkNode(currentFile);
head = addToTree(head, tempNode, tempLink);
free(tempString);
startingPos = -1;
sizeOfString = 0;
}
}
else{
if(startingPos == -1){
startingPos = i;
sizeOfString++;
}
else{
sizeOfString++;
}
}
}
return head;
}
//Gets ready to output the tree/book
void finalOutput(treeNode * head, char * outputFileName){
errno = 0;
int errsv;
int status = 0;
int amtToWrite;
char line[256];
int fd = open(outputFileName, O_RDONLY);
close(fd);
fd = open(outputFileName, O_WRONLY | O_APPEND | O_CREAT, 00700);
errsv = errno;
if(errsv == 13){
fprintf(stderr, "\nYou don't have access to file \"%s\"\n", outputFileName);
fprintf(stderr, "No output file can be written.\n");
return;
}
if(fd == -1){
fprintf(stderr, "\nError opening file \"%s\" to write our output.\n", outputFileName);
return;
}
if(head == NULL){
fprintf(stderr, "\nNo output, empty tree.\n");
fprintf(stderr, "There were no files in the directory, the files were ");
fprintf(stderr, "empty or access to the files wasn't grated.\n");
return;
}
// printTree(head, fd); //Change this line to writeBook one you are ready to test the programs ability to make codebooks
writeBook(head, fd);
close(fd);
}
/**
* This is where we write the codebook.
* @param head The head of the tree created
* @param fd stands for file descriptor. This is what is used to write the codebook file
* @return NULL
*/
void writeBook(treeNode * head, int fd){
fileList * ptr = head->files;
if(head == NULL){
fprintf(stderr, "Empty Tree\n");
fprintf(stderr, "There were no files in the directory, the files were ");
fprintf(stderr, "empty or access to the files wasn't grated.\n");
return;
}
unsigned numWords = getLeafCount(head);
toArray(head, numWords);
HuffmanCodes(numWords, fd);
}
/**
* Pulls all the data out of a given file designated by path
*/
char * extract(char * path){
errno = 0;
int fd = open(path, O_RDONLY);
int errsv;
if(errsv == 13){
fprintf(stderr, "\nNot allowed to open file \"%s\".\n", path);
return NULL;
}
int fileLength = lseek(fd, 0, SEEK_END);
int status = 0;
int amtToRead = fileLength;
lseek(fd, 0, SEEK_SET);
if(fd == -1){
fprintf(stderr, "\nError opening file \"%s\".\n", path);
return NULL;
}
if(fileLength == 0){
fprintf(stderr, "Error, file \"%s\" is empty.\nMoving to next file in directory.\n", path);
return NULL;
}
char * fileContents = (char*)malloc((sizeof(char) * fileLength) + 1);
while(amtToRead > 0){
status = read(fd, fileContents, amtToRead);
amtToRead -= status;
}
fileContents[fileLength] = '\0';
close(fd);
return fileContents;
}
/**
* Prints out the unsorted tree
*/
void printTree(treeNode * head, int fd){
fileList * ptr = head->files;
if(head == NULL){
fprintf(stderr, "Empty Tree\n");
fprintf(stderr, "There were no files in the directory, the files were ");
fprintf(stderr, "empty or access to the files wasn't grated.\n");
return;
}
sorter(ptr);
if(head->left != NULL){
printTree(head->left, fd);
}
printf("%s\n",head->str);
ptr = head->files;
while(ptr != NULL){
printf("%s %d\n", ptr->fileName, ptr->counter);
ptr = ptr->next;
}
if(head->right != NULL)
{
printTree(head->right, fd);
}
}
/**
* This function will eventually sort the tree in descending frequency.
*/
void sorter(fileList * fl){
fileList * currentLink = fl;
fileList * iterPtr;
fileList * max;
while(currentLink != NULL){
max = currentLink;
iterPtr = currentLink->next;
while(iterPtr != NULL){
if(max->counter < iterPtr->counter){
max = iterPtr;
}
else if(max->counter == iterPtr->counter){
if(max->counter < iterPtr->counter){
max = iterPtr;
}
}
iterPtr = iterPtr->next;
}
swap(currentLink, max);
currentLink = currentLink->next;
}
}
/**
* Iterates through a directory opening up the files
*/
treeNode * fileIterator(char * dirName, treeNode * head){
DIR * dir;
struct dirent * entry;
char * fileContents;
errno = 0;
int errsv;
if((dir = opendir(dirName)) == NULL){
errsv = errno;
if(errsv == 2){
fprintf(stderr, "\nNo such file, directory, or improperly formed path: \"%s\".\n", dirName);
return head;
}
if(errsv == 20){
fileContents = extract(dirName);
if(fileContents == NULL){
return head;
}
head = tokenize(fileContents, head, dirName);
free(fileContents);
return head;
}
return NULL;
}
while((entry = readdir(dir))){
// if it is a directory
if(entry->d_type == DT_DIR){
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0){
continue;
}
else{
char *temp = concat(entry->d_name, "/");
char * path = concat(dirName, temp);
free(temp);
head = fileIterator(path, head);
free(path);
}
}
// if it is a normal file
else if(entry->d_type == DT_REG){
paths = addPath(dirName, entry->d_name, paths);
printPaths();
char * path = mkPath(dirName, entry->d_name);
fileContents = extract(path);
head = tokenize(fileContents, head, entry->d_name);
free(fileContents);
free(path);
}
// if it is anything else
else{
char * path = mkPath(dirName, entry->d_name);
fprintf(stderr, "I don't know how to handle this entry. %s\n", path);
free(path);
}
}
closedir(dir);
return head;
}
/**
* makes a pointer to the path that you passed to it
*/
char * mkPath(char * currentPath, char * nextDir){
int len = strlen(currentPath)+strlen(nextDir)+2;
char * path = malloc(len);
if(strcmp(currentPath, "./") == 0){
snprintf(path, (len), "%s%s", currentPath, nextDir);
}
else{
snprintf(path, (len), "%s/%s", currentPath, nextDir);
}
return path;
}
//Returns the number of digits in an int. Was needed at one point but not anymore. Havent deleted yet incase it is needed again
int getLen(int x){
int toReturn = 0;
while(x > 0){
toReturn++;
x /= 10;
}
return toReturn;
}
/**
* Swaps the contents of two nodes except there leaves.
*/
void swap(fileList * link1, fileList * link2){
char * tempFileName = link1->fileName;
int tempCounter = link1->counter;
link1->fileName = link2->fileName;
link1->counter = link2->counter;
link2->fileName = tempFileName;
link2->counter = tempCounter;
}
/**
* Find all of the files in the directory, pair up their paths, then put that in a chain. An easy way to keep track of the files found.
*/
masterFileList *addPath(const char *path, const char *name, masterFileList *pathsHere){
// the initial path add
if(pathsHere == NULL){
pathsHere = malloc(sizeof(masterFileList));
pathsHere->fileName = (char*)malloc((strlen(name)+1) * sizeof(char));
pathsHere->path = (char*)malloc((strlen(path)+1) * sizeof(char));
pathsHere->next = NULL;
strcpy(pathsHere->path, path);
strcpy(pathsHere->fileName, name);
printf("created paths and added %s\n", pathsHere->fileName);
return pathsHere;
}
else{
if(pathsHere->next != NULL){
addPath(path, name, pathsHere->next);
return pathsHere;
}
else if(pathsHere->next == NULL){
masterFileList *tempPath = malloc(sizeof(masterFileList));
tempPath->fileName = (char*)malloc((strlen(name)+1) * sizeof(char));
tempPath->path = (char*)malloc((strlen(path)+1) * sizeof(char));
tempPath->next = NULL;
strcpy(tempPath->path, path);
strcpy(tempPath->fileName, name);
pathsHere->next = tempPath;
printf("%s added to paths\n", pathsHere->next->fileName);
return pathsHere;
}
}
}
void printPaths(){
masterFileList *temp = paths;
int go = 1;
printf("paths:");
if(paths != NULL){
while(go){
printf(" %s", temp->fileName);
if(temp->next != NULL){
temp = temp->next;
printf(",");
}
else{
printf("\n");
go = 0;
}
}
}
}
unsigned countPaths(){
masterFileList *temp = paths;
int go = 1;
unsigned count = 0;
while(go){
++count;
if(temp->next != NULL){
temp = temp->next;
}
else{
go = 0;
}
}
return count;
}
treeNode **toArray(treeNode *head, unsigned size){
mhArray = (treeNode**)malloc(size * sizeof(treeNode*));
//free(head);
getLeaf(head);
}
treeNode *getLeaf(treeNode *head){
treeNode *temp;
if(head != NULL && head->left != NULL){
temp = getLeaf(head->left);
head->left = NULL;
getLeaf(head);
// ++iCounter;
return temp;
}
else if(head != NULL && head->left == NULL){
}
if(head != NULL && head->right != NULL){
temp = getLeaf(head->right);
head->right = NULL;
getLeaf(head);
// ++iCounter;
return temp;
}
else if(head != NULL && head->right == NULL){
}
if(head != NULL && head->left == NULL && head->right == NULL){
mhArray[iCounter] = head;
++iCounter;
return head;
}
}
/************************************************/
// HUFFMAN CODE
/************************************************/
#define MAX_TREE_HT 1000
/**
* A utility function allocate a new min heap node with given string and frequency of the string
*/
treeNode* newNode(char *data, unsigned freq){
treeNode * temp = (treeNode*)malloc(sizeof(treeNode));
temp->left = NULL;
temp->right = NULL;
temp->files = NULL;
temp->str = strdup(data);
fileList *fls = (fileList*)malloc(sizeof(fileList));
fls->fileName = NULL;
fls->counter = freq;
fls->next = NULL;
temp->files = fls;
return temp;
}
/**
* A utility function to create a min heap of given capacity
*/
MinHeap* createMinHeap(unsigned capacity){
MinHeap* minHeap = (MinHeap*)malloc(sizeof(MinHeap));
// current size is 0
minHeap->size = 0;
minHeap->capacity = iCounter;
// minHeap->array = (treeNode**)malloc(3 * sizeof(treeNode*));
minHeap->array = NULL;
return minHeap;
}
/**
* A utility function to swap two min heap nodes
*/
void swapMinHeapNode(treeNode** a, treeNode** b){
treeNode* t = *a;
*a = *b;
*b = t;
}
/**
* The standard minHeapify function.
*/
void minHeapify(MinHeap* minHeap,unsigned size, int idx){
int smallest = idx;
int left = 2 * idx + 1;
int right = 2 * idx + 2;
if (left < size && minHeap->array[left]->files->counter < minHeap->array[smallest]->files->counter)
smallest = left;
if (right < size && minHeap->array[right]->files->counter < minHeap->array[smallest]->files->counter)
smallest = right;
if (smallest != idx) {
swapMinHeapNode(&minHeap->array[smallest], &minHeap->array[idx]);
minHeapify(minHeap, size, smallest);
}
}
/**
* A utility function to check if size of heap is 1 or not
*/
int isSizeOne(MinHeap* minHeap){
return (minHeap->size == 1);
}
/**
* A standard function to extract minimum value node from heap
*/
treeNode* extractMin(MinHeap* minHeap){
treeNode* temp = minHeap->array[0];
minHeap->array[0] = minHeap->array[minHeap->size - 1];
--minHeap->size;
minHeapify(minHeap, minHeap->size, 0);
return temp;
}
/**
* A utility function to insert a new node to Min Heap
*/
void insertMinHeap(MinHeap* minHeap, treeNode* minHeapNode){
++minHeap->size;
int i = minHeap->size - 1;
while (i && minHeapNode->files->counter < minHeap->array[(i - 1) / 2]->files->counter) {
minHeap->array[i] = minHeap->array[(i - 1) / 2];
i = (i - 1) / 2;
}
minHeap->array[i] = minHeapNode;
}
/**
* build min heap
*/
void buildMinHeap(MinHeap* minHeap){
int n = minHeap->size - 1;
int i;
for (i = (n - 1) / 2; i >= 0; --i){
minHeapify(minHeap, minHeap->size, i);
}
}
/**
* Utility function to check if this node is leaf
*/
int isLeaf(treeNode* root){
return !(root->left) && !(root->right);
}
/**
* Creates a min heap of capacity equal to size and inserts all character of data[] in min heap. Initially size of min heap is equal to capacity
*/
MinHeap* createAndBuildMinHeap(treeNode **arr, int size){
MinHeap* minHeap = createMinHeap(size);
int i;
minHeap->array = mhArray;
minHeap->size = size;
printf("building minheap...\n");
buildMinHeap(minHeap);
return minHeap;
}
// The main function that builds Huffman tree
treeNode* buildHuffmanTree(treeNode **arr, int size){
treeNode *left, *right, *top;
// Make the minheap (array)
MinHeap* minHeap = createAndBuildMinHeap(mhArray, size);
// Iterate while size of heap doesn't become 1
while (!isSizeOne(minHeap)) {
// Find the two minimum freq items from min heap
left = extractMin(minHeap);
right = extractMin(minHeap);
// '$' is a special value for internal nodes, not used
top = newNode("$", left->files->counter + right->files->counter);
top->left = left;
top->right = right;
insertMinHeap(minHeap, top);
}
// tree is complete.
return extractMin(minHeap);
}
/**
* A utility function to print an array of size n
*/
char *printArr(char arr[], int n, int fd){
char *send = "";
int i;
for (i = 0; i < n; ++i){
send = charAppend(send, arr[i]);
}
return send;
}
/**
* Prints huffman codes from the root of Huffman Tree. It uses arr[] to store codes
*/
int printCodes(treeNode* root, char arr[], int top, char *rslt, int fd){
// Assign 0 to left edge and recur
if (root->left) {
arr[top] = '0';
printCodes(root->left, arr, top + 1, rslt, fd);
}
// Assign 1 to right edge and recur
if (root->right) {
arr[top] = '1';
printCodes(root->right, arr, top + 1, rslt, fd);
}
/**
* If this is a leaf node, then it contains one of the
* input characters, print the character and its code from arr[]
*/
if (isLeaf(root)) {
//printf("%s\n", printArr(arr, top));
char *code = printArr(arr, top, fd);
rslt = concat(rslt, tabConcat(code, "\t") );
rslt = concat(rslt, root->str);
rslt = concat(rslt, "\n\0");
// printf("%s\n", rslt);
if(write(fd, rslt, strlen(rslt) ) != strlen(rslt)){
char * err = "There was an error writing to";
printf("%s\n", concat(err, root->str));
return 1;
}
free(rslt);
return 0;
}
}
/**
* The main function that builds a Huffman Tree and print codes by traversing the built Huffman Tree
* @param head the head of the tree
* @param fileNames an array of all of the file names in the dir
* @return NULL
*/
void HuffmanCodes(unsigned size, int fd){
// Construct Huffman Tree
treeNode* root = buildHuffmanTree(mhArray, size);
// Print Huffman codes using the Huffman tree built above
char arr[MAX_TREE_HT];
int top = 0;
char *rslt = "";
if(printCodes(root, arr, top, rslt, fd) == 1){
};
}
/************************************************/
// Compress functions
/************************************************/
void compressFiles(char *dirName){
wordsList *words = NULL;
bitDict *dict = findCodebook(words, dirName);
scrubFiles(dirName, words, dict);
}
wordsList *scrubFiles(char *dirName, wordsList *words, bitDict *dict){
DIR * dir;
struct dirent * entry;
char * fileContents;
errno = 0;
int errsv;
if((dir = opendir(dirName)) == NULL){
errsv = errno;
if(errsv == 2){
fprintf(stderr, "\nNo such file, directory, or improperly formed path: \"%s\".\n", dirName);
return words;
}
if(errsv == 20){
fileContents = extract(dirName);
if(fileContents == NULL){
return words;
}
words = tokenize2(fileContents, words, dirName);
free(fileContents);
return words;
}
return NULL;
}
while((entry = readdir(dir))){
// if it is a directory
if(entry->d_type == DT_DIR){
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0){
continue;
}
else{
char *temp = concat(entry->d_name, "/");
char * path = concat(dirName, temp);
free(temp);
words = scrubFiles(path, words, dict);
free(path);
}
}
// if it is a normal file
else if(entry->d_type == DT_REG){
paths = addPath(dirName, entry->d_name, paths);
printPaths();
char * path = mkPath(dirName, entry->d_name);
fileContents = extract(path);
words = tokenize2(fileContents, words, entry->d_name);
// Compress the file found:
compressFile(dirName, entry->d_name, words, dict);
free(fileContents);
free(path);
}
// if it is anything else
else{
char * path = mkPath(dirName, entry->d_name);
fprintf(stderr, "I don't know how to handle this entry. %s\n", path);
free(path);
}
}
closedir(dir);
return words;
// if(files->next != NULL){
// compressFiles(files->next);
// }
}
/**
* Tokenizes the words that are passed to it.
*/
wordsList * tokenize2(char * fileContents, wordsList *words, char * currentFile){
if(fileContents == NULL){
return words;
}
char * inputString = fileContents;
char * tempString;
int startingPos = -1;
int endingPos = 0;
int sizeOfString = 0;
int len = 0;
int i = 0;
len = strlen(inputString);
wordsList * tempLink;
for(i = 0; i <= len; i++){
// if it is not a letter or a digit
//if(isalpha(inputString[i]) == 0 && isdigit(inputString[i]) == 0){
if(isspace(inputString[i]) != 0){
if(sizeOfString == 0){
continue;
}
else{
endingPos = i;
tempString = pullString(startingPos, endingPos, sizeOfString, inputString);
tempLink = createWordLink(tempString);
words = addToChain(words, tempLink);
free(tempString);
startingPos = -1;
sizeOfString = 0;
}
}
else{
if(startingPos == -1){
startingPos = i;
sizeOfString++;
}
else{
sizeOfString++;
}
}
}
// printChain(words);
return words;
}
void compressFile(char *dirName, char *fileName, wordsList *words, bitDict *dict){
char *comFile = concat(fileName, COMP_EXT);
printf("New File Name: %s\n", comFile);
char *temp = malloc(2*sizeof(char));
strcpy(temp, "");
// printChain(words);
char *latestOutput = getCompressed(words, dict, dict, temp);
printf("DATA: %s\n", latestOutput);
errno = 0;
int errsv;
int status = 0;
char line[256];
int fd = open(comFile, O_RDONLY);
close(fd);
fd = open(comFile, O_WRONLY | O_APPEND | O_CREAT, 00700);
errsv = errno;
if(errsv == 13){
fprintf(stderr, "\nYou don't have access to file \"%s\"\n", comFile);
fprintf(stderr, "No output file can be written.\n");
return;
}
if(fd == -1){
fprintf(stderr, "\nError opening file \"%s\" to write our output.\n", comFile);
return;
}
if(write(fd, latestOutput, strlen(latestOutput) ) != strlen(latestOutput)){
printf("There was an error writing to\n");
}
}
/**
* A recursive function to get the huffman compressed bits of the data given
* @param words the linked list of words/data
* @param dict the linked list of tokens and bit representations generated earlier.
* @param head same as dict but not meant to be changed though the recusion
* @param str a starting string to work off of.
* @return a string representing the bits of the given data.
*/
char *getCompressed(wordsList *words, bitDict *dict, bitDict *head, char *str){
printf("?\n");
// if the current position in dict and the word we are looking at are the same
if(words != NULL && dict != NULL && strcmp(words->word, dict->token) == 0){
// add the bit representation from the dict into the string
printf("????????????????\n");
printf("Found a match for : %s\n", words->word);
char *temp = concat2(str, dict->bits);
// free(str);
printf("Data Currently: %s\n", temp);
// if there is another word
if(words->next != NULL){
return getCompressed(words->next, head, head, temp);
}
// if there are no more words left return the string
else{
return temp;
}
}
// if they aren't
else if (words != NULL && dict != NULL && strcmp(words->word, dict->token) != 0){
printf("??\n");
// if we still have more tokens in the dict
if(dict->next != NULL){
printf("???\n");
return getCompressed(words, dict->next, head, str);
}
// if we've reached the end of the dict
else{
printf("\"%s\" does not exist in the codebook...\n", words->word);
if(words->next != NULL)
return getCompressed(words->next, head, head, str);
// if there are no more words left return the string
else{
return str;
}
}
}
else{
printf("I'm here for some reason...\n" );
}
}
/**
* This will take in the codebook and tokenize it into a bit dictionary, a structure that will allow easy compressions and decompression.
* @param fileContents a string of the contents of a file
* @param dict a bitDict to insert the data into.
*/
bitDict * tokenizeCodebook(char * fileContents, bitDict *dict){
if(fileContents == NULL){
return dict;
}