-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLLSort.c
135 lines (127 loc) · 4.38 KB
/
LLSort.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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <pthread.h>
#include <dirent.h>
#include <errno.h>
#include <limits.h>
#include "fileIO.h"
#include "requestUtils.h"
#include "exitLeaks.h"
#include "manifestControl.h"
#include "md5.h"
#include "LLSort.h"
void freeMList(struct manifestNode* head){ //frees linked list
struct manifestNode* current = head;
struct manifestNode* current2;
while(current != NULL){
current2 = current;
current = current->next;
free(current2->data);
free(current2);
}
}
int stringCmp(void* first, void* second){ //comparator for 2 strings
char* arg1 = (char*) first;
char* arg2 = (char*) second;
int length1 = strlen(first);
int length2 = strlen(second);
int lowLength, i;
if(length1 == length2) lowLength = length1;
else if(length1 > length2) lowLength = length2;
else lowLength = length1; //determining the size of the shorter string
for(i = 0; i < lowLength; i++){
if(arg1[i] < arg2[i]) return -1;
else if(arg1[i] > arg2[i]) return 1;
}
if(length1 < length2) return -1;
else if(length1 > length2) return 1;
return 0;
}
struct manifestNode* insertManifest(struct manifestNode* head, struct manifestNode* toInsert){ //simple insert at the end of a LL
if(head == NULL) {
head = toInsert; //if empty return manifestNode
toInsert->index = 0;
globalIndex++;
}
else{
struct manifestNode* current = head;
while(current->next != NULL) {
current = current->next;
}
current->next = toInsert;
toInsert->prev = current;
toInsert->index = globalIndex;
globalIndex++;
}
return head;
}
void printMList(struct manifestNode* head){
struct manifestNode* current = head;
while(current != NULL){
printf("Node %d: %s\n", current->index, current->data);
current = current->next;
}
}
struct manifestNode* createMNode(char* str){ //createMNodes new manifestNode with argument as data
struct manifestNode* toInsert = malloc(sizeof(struct manifestNode));
if( toInsert == NULL )
printf( "Warning: malloc returned null! Continuing...\n");
toInsert->next = NULL;
toInsert->prev = NULL;
toInsert->data = malloc(sizeof(char) * strlen(str)+1);
strcpy(toInsert->data, str);
toInsert->data[strlen(toInsert->data)] = '\0';
toInsert->index = -1;
return toInsert;
}
int insertionSort(void* toSort, int (*comparator)(void*, void*)){
struct manifestNode* current = toSort;
struct manifestNode* backTrav = NULL;
if(current->next == NULL || current == NULL) return 1; //if empty linked list or linked list only contains 1 node return
current = current->next;
while(current != NULL){
backTrav = current;
while(backTrav != NULL){
if(backTrav->prev != NULL){
//get the filepaths to compare instead of comparing the whole ass thing
char filePath1[256] = {0};
char filePath2[256] = {0};
int i, j;
for(i = 0; i < strlen(backTrav->data); i++){
if(backTrav->data[i] == ' ') break;
}
i++;
for(j = i; j < strlen(backTrav->data); j++){
if(backTrav->data[j] == ' ') break;
}
strncpy(filePath1, backTrav->data+i, j - i);
filePath1[strlen(filePath1)] = '\0';
for(i = 0; i < strlen(backTrav->prev->data); i++){
if(backTrav->prev->data[i] == ' ') break;
}
i++;
for(j = i; j < strlen(backTrav->prev->data); j++){
if(backTrav->prev->data[j] == ' ') break;
}
strncpy(filePath2, backTrav->prev->data+i, j-i);
filePath2[strlen(filePath2)] = '\0';
if( comparator(filePath1, filePath2) < 0 ) {
char* temp = backTrav->data;
backTrav->data = backTrav->prev->data;
backTrav->prev->data = temp;
}
else break;
}
backTrav = backTrav->prev;
}
current = current->next;
}
//printf("tried to sort\n");
return 1;
}