-
Notifications
You must be signed in to change notification settings - Fork 0
/
inputOutput.c
371 lines (301 loc) · 11.8 KB
/
inputOutput.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
/* ____________________________________________________________________________
Module inputOutput.c
This module contains functions which deal with input and output. Also it
contains functions to create edge, node, and functions to deallocate edge,
node, and arraylists containing them.
____________________________________________________________________________
*/
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include "arrayList.h"
#include "structs.h"
#include "inputOutput.h"
/* ____________________________________________________________________________
graphNode *createNode(char *input)
Tries to create node from input string. If it is successful it returns
pointer on new node, else NULL.
____________________________________________________________________________
*/
graphNode *createNode(char *input) {
graphNode *newNode;
char *inputPart;
char *invalidPart;
if (!input || strlen(input) < 5) return NULL;
newNode = malloc(sizeof(graphNode));
if (!newNode) return NULL;
inputPart = strtok(input, ",");
newNode->id = strtol(inputPart, &invalidPart, 10);
inputPart = strtok(NULL, ",");
newNode->wkt = malloc(strlen(inputPart));
if (!newNode->wkt) {
free(newNode);
return NULL;
}
strncpy(newNode->wkt, inputPart, strlen(inputPart));
return newNode;
}
/* ____________________________________________________________________________
graphEdge *createEdge(char *input, int workWithInvalid)
Tries to create edge from input string. Checks if edge is valid, if it is
returns pointer to new edge, else return NULL.
____________________________________________________________________________
*/
graphEdge *createEdge(char *input, int workWithInvalid) {
graphEdge *newEdge;
int i;
uint wktLength;
uint inputLength;
uint parsedLength;
char *partString;
char *part2;
if (!input || strlen(input) < 14) return NULL;
inputLength = strlen(input);
parsedLength = 5;
newEdge = calloc(1, sizeof(graphEdge));
if (!newEdge) return NULL;
partString = strtok(input, ",");
for (i = 0; i < 4; i++) {
switch (i) {
case 0:
newEdge->id = strtol(partString, &part2, 10);
break;
case 1:
newEdge->source = strtol(partString, &part2, 10);
break;
case 2:
newEdge->target = strtol(partString, &part2, 10);
break;
case 3:
if (strchr(partString, '"')) {
newEdge->capacity = strtol(&partString[1], &part2, 10);
} else {
newEdge->capacity = strtol(partString, &part2, 10);
}
break;
default:
perror("Unexpected error has occurred\n");
}
parsedLength += strlen(partString);
partString = strtok(NULL, ",");
}
if (!strncmp(partString, "True", 4)) {
newEdge->isValid = 'Y';
} else {
if (!workWithInvalid) {
freeGraphEdge(&newEdge);
return NULL;
}
newEdge->isValid = 'N';
}
parsedLength += strlen(partString);
partString = strtok(NULL, "&");
wktLength = inputLength - parsedLength + 1;
newEdge->wkt = malloc(wktLength);
strncpy(newEdge->wkt, partString, wktLength);
newEdge->flow = 0;
return newEdge;
}
/* ____________________________________________________________________________
hashTable *loadNodes(char *inputName)
Tries to load data from file described by inputName parameter and create
hashTable containing newly created nodes, it filters duplicate nodes.
Returns pointer to hashTable or NULL if some problem occurred.
____________________________________________________________________________
*/
hashTable *loadNodes(char *inputName) {
int counter = 0;
hashTable *table;
char *string;
graphNode *newNode;
FILE *inputFile;
if (!inputName) return NULL;
inputFile = fopen(inputName, "r");
if (!inputFile) return NULL;
string = malloc(MAXLENGTH);
if (!string) {
freeHashTable(&table);
fclose(inputFile);
return NULL;
}
fgets(string, MAXLENGTH, inputFile);
if (strncmp(NODES_ID, string, strlen(NODES_ID)) != 0) {
free(string);
freeHashTable(&table);
fclose(inputFile);
return NULL;
}
do {
fgets(string, MAXLENGTH, inputFile);
counter++;
} while (!feof(inputFile));
rewind(inputFile);
fgets(string, MAXLENGTH, inputFile);
table = createHashTable(counter + 2, sizeof(graphNode));
if (!table) {
fclose(inputFile);
return NULL;
}
do {
fgets(string, MAXLENGTH, inputFile);
newNode = createNode(string);
if (newNode) {
/* check if node with this id was already loaded */
if (hashTableContains(newNode->id, table)) {
freeGraphNode(&newNode);
} else {
hashTableAddElement(newNode, newNode->id, table);
}
}
} while (!feof(inputFile));
fclose(inputFile);
free(string);
return table;
}
/* ____________________________________________________________________________
hashTable *loadEdges(char *inputName, int workWithInvalid, int tableSize)
Tries to load data from file described by inputName parameter and create
hashTable containing newly created edges, it filters duplicates.
WorkWithInvalid says if invalid edges should be used, tableSize determines
size of the hashTable. Returns pointer to hashTable or NULL if some problem
occurred.
____________________________________________________________________________
*/
hashTable *loadEdges(char *inputName, int workWithInvalid, int tableSize) {
hashTable *table;
char *string;
graphEdge *edgePointer;
FILE *inputFile;
if (!inputName) return NULL;
inputFile = fopen(inputName, "r");
if (!inputFile) return NULL;
table = createHashTable(tableSize, sizeof(graphEdge));
if (!table) {
fclose(inputFile);
return NULL;
}
string = malloc(MAXLENGTH);
if (!string) {
freeHashTable(&table);
fclose(inputFile);
return NULL;
}
fgets(string, MAXLENGTH, inputFile);
if (strncmp(EDGES_ID, string, strlen(EDGES_ID)) != 0) {
free(string);
freeHashTable(&table);
fclose(inputFile);
return NULL;
}
do {
fgets(string, MAXLENGTH, inputFile);
edgePointer = createEdge(string, workWithInvalid);
if (edgePointer) {
/* check if edge with this id was already loaded */
if (hashTableContains(edgePointer->id, table)) {
freeGraphEdge(&edgePointer);
} else {
hashTableAddElement(edgePointer, edgePointer->id, table);
}
}
} while (!feof(inputFile));
fclose(inputFile);
free(string);
return table;
}
/* ____________________________________________________________________________
void freeGraphNode(graphNode **nodePointer)
Deallocates memory used by node.
____________________________________________________________________________
*/
void freeGraphNode(graphNode **nodePointer) {
if (!nodePointer || !*nodePointer) return;
if ((*nodePointer)->wkt) free((*nodePointer)->wkt);
free(*nodePointer);
*nodePointer = NULL;
}
/* ____________________________________________________________________________
void freeGraphEdge(graphEdge **edgePointer)
Deallocates memory used by edge.
____________________________________________________________________________
*/
void freeGraphEdge(graphEdge **edgePointer) {
if (!edgePointer || !*edgePointer) return;
if ((*edgePointer)->wkt) free((*edgePointer)->wkt);
free(*edgePointer);
*edgePointer = NULL;
}
/* ____________________________________________________________________________
int writeToOutputFile(char *fileName, graph *graphPointer)
Tries to write output to file described by fileName, writes all edges in
the min-cut. Then returns if it was successful as boolean value.
____________________________________________________________________________
*/
int writeToOutputFile(char *fileName, graph *graphPointer) {
int i;
int j;
graphEdge *edgePointer;
graphNode *sourceNode;
graphNode *targetNode;
char *validity;
arrayList *list;
FILE *output;
if (!fileName || !graphPointer) return FAILURE;
output = fopen(fileName, "w");
if (!output) return FAILURE;
list = createArrayList(INITIAL_SIZE, sizeof(graphEdge));
if (!list) {
fclose(output);
return FAILURE;
}
fprintf(output,"%s", EDGES_ID);
for (i = 0; i < graphPointer->edges->size; i++) {
for (j = 0; j < graphPointer->edges->array[i]->filledItems; j++) {
edgePointer = arrayListGetPointer(graphPointer->edges->array[i], j);
if (edgePointer) {
/* get rid of reverse edges, and edges which don't have fulfilled capacity */
if (edgePointer->capacity == 0 || edgePointer->capacity != edgePointer->flow) continue;
sourceNode = hashTableGetElement(edgePointer->source, graphPointer->nodes);
targetNode = hashTableGetElement(edgePointer->target, graphPointer->nodes);
/* nodes which are "to the left" from the min-cut have level != -1,
nodes which are "to the right" from the min-cut have level == -1,
so we want to write just the edges, which have source in the
left side, and target in the right side or edges which have source in the right side and
target in the left side*/
if ((sourceNode->level != -1 && targetNode->level == -1) || (sourceNode->level == -1
&& targetNode->level != -1)) {
arrayListAdd(list, edgePointer);
}
}
}
}
qsort(list->data, list->filledItems, sizeof(graphEdge *), compareEdgeById);
for (i = 0; i < list->filledItems; i++) {
edgePointer = arrayListGetPointer(list, i);
validity = edgePointer->isValid == 'Y' ? VALID : INVALID;
fprintf(output, "%d,%d,%d,%ld,%s,%s", edgePointer->id, edgePointer->source, edgePointer->target,
edgePointer->capacity, validity, edgePointer->wkt);
}
freeArrayList(&list);
fclose(output);
return SUCCESS;
}
/* ____________________________________________________________________________
int compareEdgeById(const void *a, const void *b)
Auxiliary function which defines how two edges should be compared by id.
Necessary for using qsort.
____________________________________________________________________________
*/
int compareEdgeById(const void *a, const void *b) {
return (*(graphEdge **) a)->id - (*(graphEdge **) b)->id;
}
/* ____________________________________________________________________________
int compareEdgeBySource(const void *a, const void *b)
Auxiliary function which defines how two edges should be compared by source.
Necessary for using qsort.
____________________________________________________________________________
*/
int compareEdgeBySource(const void *a, const void *b) {
return (*(graphEdge **) a)->source - (*(graphEdge **) b)->source;
}