-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileReader.c
621 lines (533 loc) · 13.2 KB
/
FileReader.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
#include "FileReader.h"
//Return the number of elements in file array
int GetNumberOfElement(FILE* _file)
{
char c = fgetc(_file);
int number = 0;
int isInString = 0;
//Get each char between each array sperator
while (c != ']')
{
if (c == '\"' && !isInString )
{
isInString = 1;
}
else if (c == '\"' && isInString)
{
isInString = 0;
}
//If we found a comma, then add a nuber
if (c == ',' && !isInString )
{
c = fgetc(_file);
number++;
}
c = fgetc(_file);
}
//Return n + 1 values (Or 0 in nothing)
return (number > 0 ? number + 1 : 0);
}
//Check if we are reading an escape sequnce
void CheckForEscapeSequence(FILE* _file, char* _c)
{
//If char is a backslash, define charactere as the good one escape sequence
if (*_c == '\\')
{
//Analysing the next character
*_c = fgetc(_file);
switch (*_c)
{
case 'a':
*_c = '\a';
break;
case 'b':
*_c = '\b';
break;
case 'n':
*_c = '\n';
break;
case 'r':
*_c = '\r';
break;
case 't':
*_c = '\t';
break;
case '\\':
*_c = '\\';
break;
case '\'':
*_c = '\'';
break;
case '"':
*_c = '\"';
break;
case '?':
*_c = '\?';
break;
//If no escape sequence is found, ignore the backslash
default:
break;
}
}
}
//Find token in text file (change cursor position)
int FindCategoryNameInFile(FILE* _file, char* _token)
{
//Set cursor to _file origin
fseek(_file, 0, SEEK_SET);
char c = 0;
char* str[50] = { 0 };
int hasFoundCategory = 0;
//Read each line
while (c != EOF)
{
c = fgetc(_file);
//If character is new line
if (c == '\n')
{
//If scanned line is equals to the given token, break loop
if (strcmp(str, _token) == 0)
{
c = EOF;
hasFoundCategory = 1;
}
//else, reset word and continue
else
{
memset(str, 0, sizeof(str));
}
}
//Concatenate string and readed char
else
{
sprintf(str, "%s%c", str, c);
}
}
if (hasFoundCategory)
{
return 1;
}
else
{
printf("Category not found\n");
return 0;
}
}
//Find attribute in text file (change cursor position)
int FindAttributeNameInFile(FILE* _file, char* _token)
{
char c = 0;
char* str[50] = { 0 };
int hasFoundAttribute = 0;
//Allocate memory for store string we are searching for
char* target = calloc(strlen(_token + 1), sizeof(char));
//Define target string form
sprintf(target, "%s%c", _token, ':');
//Read each line
while (c != EOF)
{
c = fgetc(_file);
//If character is new line or attribute / value separator
if (c == '\n' || c == ':')
{
//If its separator, add char to readed string
if (c == ':')
{
sprintf(str, "%s%c", str, c);
}
//If scanned line is equals to the given token, break loop
if (strstr(str, target))
{
c = EOF;
hasFoundAttribute = 1;
}
//else, reset word and continue
else
{
memset(str, 0, sizeof(str));
}
}
//Concatenate string and readed char
else if (c != EOF)
{
sprintf(str, "%s%c", str, c);
}
//If loop reached another category name flag, nothing was found
if (c == '#')
{
c = EOF;
}
}
if (hasFoundAttribute)
{
return 1;
}
else
{
printf("Attribute not found\n");
return 0;
}
}
//Find categoryname in string
char* GetCategoryName(char* _attribute)
{
char c = 0;
char* str;
int strLen = strlen(_attribute);
//Allocate memory for store string we are searching for
str = (char*)calloc(strLen + 2, sizeof(char));
//Catrgory name always start with its flag
str[0] = '#';
//Read each char
for (int i = 0; i < strLen; i++)
{
c = _attribute[i];
//If point is reached, nreak loop, category name is found
if (c == '.')
{
i = strLen;
}
//Concatenate string and readed char
else
{
sprintf(str, "%s%c", str, c);
}
}
//Allocate memory for category name
char* categoryName = (char*)calloc(strlen(str) + 2, sizeof(char));
//Set category name to readed value
strcpy(categoryName, str);
free(str);
return categoryName;
}
//Get attribute name in string
char* GetAttributeName(char* _attribute)
{
char c = 0;
char* str;
int strLen = strlen(_attribute);
char* categoryName = GetCategoryName(_attribute);
str = (char*)calloc(strLen + 1, sizeof(char));
//Read each char
for (int i = strlen(categoryName); i < strLen; i++)
{
c = _attribute[i];
//if attribute / file separator is reached, attribute name is found
if (c == ':')
{
i = strLen;
}
//Concatenate string and readed char
else
{
sprintf(str, "%s%c", str, c);
}
}
//Allocate just enought spoace for store attribute name
char* attributeName = (char*)calloc(strlen(str) + 1, sizeof(char));
//Set attribute name to readed value
strcpy(attributeName, str);
free(str);
return attributeName;
}
//Return the number of character in a string
int CountCharOfStringInFile(FILE* _file)
{
int len = 0;
char c = fgetc(_file);
//While string end is not reached
while (c != '\"')
{
CheckForEscapeSequence(_file, &c);
len++;
c = fgetc(_file);
}
return len;
}
//Return the string at given attribute
char* GetStringInFile(FILE* _file, char* _attribute)
{
char c = 0;
int strLen = strlen(_attribute);
int curPos = 0;
int valueLen;
char* categoryName = GetCategoryName(_attribute);
char* attributeName = GetAttributeName(_attribute);
char* valueStr = "";
char* str = "";
//Decompose attribute
if (FindCategoryNameInFile(_file, categoryName)
&& FindAttributeNameInFile(_file, attributeName))
{
//Search for the first '"'
c = fgetc(_file);
while (c != '\"')
{
c = fgetc(_file);
}
//Store current cursor position
curPos = ftell(_file);
//Count number of chars of the value to read
valueLen = CountCharOfStringInFile(_file);
//Allocate memory for this savlue (+1 for '\0')
valueStr = (char*)calloc(valueLen + 1, sizeof(char));
str = (char*)calloc(valueLen + 1, sizeof(char));
//Re place cursor in file
fseek(_file, curPos, SEEK_SET);
//Add each char between current pos ans the next '"'
//(except for escape sequence)
c = fgetc(_file);
while (c != '\"')
{
CheckForEscapeSequence(_file, &c);
sprintf(str, "%s%c", str, c);
c = fgetc(_file);
}
free(categoryName);
free(attributeName);
strcpy(valueStr, str);
free(str);
return valueStr;
}
else
{
free(categoryName);
free(attributeName);
return NULL;
}
}
char** GetStringArrayInFile(FILE* _file, char* _attribute, int* _arraySize)
{
char c = 0;
char* categoryName = GetCategoryName(_attribute);
char* attributeName = GetAttributeName(_attribute);
int curPos = 0;
char** stringArray = NULL;
int* value;
//Decompose attribute
if (FindCategoryNameInFile(_file, categoryName)
&& FindAttributeNameInFile(_file, attributeName))
{
//Search for the first array delimiter
c = fgetc(_file);
while (c != '[')
{
c = fgetc(_file);
}
//Store current cursor position
curPos = ftell(_file);
//Count the number of element in the array
*_arraySize = GetNumberOfElement(_file);
//allocate memory for the array
stringArray = (char**)calloc((*_arraySize) + 1, sizeof(char*));
//Re place cursor in file
fseek(_file, curPos, SEEK_SET);
for (int i = 0; i < *_arraySize; i++)
{
char* valueStr = "";
char* str = "";
int valueLen;
//Search for the first '"'
c = fgetc(_file);
while (c != '\"')
{
c = fgetc(_file);
}
//Store current cursor position
curPos = ftell(_file);
//Count number of chars of the value to read
valueLen = CountCharOfStringInFile(_file);
//Allocate memory for this savlue (+1 for '\0')
valueStr = (char*)calloc(valueLen + 1, sizeof(char));
str = (char*)calloc(valueLen + 1, sizeof(char));
//Re place cursor in file
fseek(_file, curPos, SEEK_SET);
//Add each char between current pos ans the next '"'
//(except for escape sequence)
c = fgetc(_file);
while (c != '\"')
{
CheckForEscapeSequence(_file, &c);
sprintf(str, "%s%c", str, c);
c = fgetc(_file);
}
strcpy(valueStr, str);
free(str);
// add string to the array
stringArray[i] = valueStr;
}
}
//Free returned strings after use
free(categoryName);
free(attributeName);
return stringArray;
}
//Return the int at specified attribute
int GetIntInFile(FILE* _file, char* _attribute)
{
char c = 0;
char* categoryName = GetCategoryName(_attribute);
char* attributeName = GetAttributeName(_attribute);
int value = 0;
//Decompose attribute
if (FindCategoryNameInFile(_file, categoryName)
&& FindAttributeNameInFile(_file, attributeName))
{
//Get the integer
(void)fscanf(_file, "%d", &value);
}
//Free returned strings after use
free(categoryName);
free(attributeName);
return value;
}
//Find token in text _file (change cursor position)
int* GetIntsArrayInFile(FILE* _file, char* _attribute, int* _arraySize)
{
char c = 0;
char* categoryName = GetCategoryName(_attribute);
char* attributeName = GetAttributeName(_attribute);
int curPos = 0;
int* value = NULL;
//Decompose attribute
if (FindCategoryNameInFile(_file, categoryName)
&& FindAttributeNameInFile(_file, attributeName))
{
//Seachr for the first array delimiter
c = fgetc(_file);
while (c != '[')
{
c = fgetc(_file);
}
//Store current cursor position
curPos = ftell(_file);
//Count the number of element in the array
*_arraySize = GetNumberOfElement(_file);
//Allocate memory to the array
value = (int*)calloc(*_arraySize, sizeof(int));
//Re place cursor in file
fseek(_file, curPos, SEEK_SET);
//Get each int of the array
for (int i = 0; i < *_arraySize; i++)
{
(void)fscanf(_file, "%d", &value[i]);
fseek(_file, 1, SEEK_CUR);
}
}
//Free returned strings after use
free(categoryName);
free(attributeName);
return value;
}
//Find token in text _file (change cursor position)
float GetFloatInFile(FILE* _file, char* _attribute)
{
char c = 0;
char* categoryName = GetCategoryName(_attribute);
char* attributeName = GetAttributeName(_attribute);
float value = 0.f;
//Decompose attribute
if (FindCategoryNameInFile(_file, categoryName)
&& FindAttributeNameInFile(_file, attributeName))
{
(void)fscanf(_file, "%f", &value);
}
//Free returned strings after use
free(categoryName);
free(attributeName);
return value;
}
//Find token in text _file (change cursor position)
float* GetFloatsArrayInFile(FILE* _file, char* _attribute, int* _arraySize)
{
char c = 0;
char* categoryName = GetCategoryName(_attribute);
char* attributeName = GetAttributeName(_attribute);
int curPos = 0;
float* value = NULL;
//Decompose attribute
if (FindCategoryNameInFile(_file, categoryName)
&& FindAttributeNameInFile(_file, attributeName))
{
//Seachr for the first array delimiter
c = fgetc(_file);
while (c != '[')
{
c = fgetc(_file);
}
//Store current cursor position
curPos = ftell(_file);
//Count the number of element in the array
*_arraySize = GetNumberOfElement(_file);
//Allocate memory to the array
value = (float*)calloc(*_arraySize, sizeof(float));
//Re place cursor in file
fseek(_file, curPos, SEEK_SET);
//Get each float of the array
for (int i = 0; i < *_arraySize; i++)
{
(void)fscanf(_file, "%f", &value[i]);
fseek(_file, 1, SEEK_CUR);
}
}
//Free returned strings after use
free(categoryName);
free(attributeName);
return value;
}
//Find token in text _file (change cursor position)
double GetDoubleInFile(FILE* _file, char* _attribute)
{
char c = 0;
char* categoryName = GetCategoryName(_attribute);
char* attributeName = GetAttributeName(_attribute);
double value = 0.;
//Decompose attribute
if (FindCategoryNameInFile(_file, categoryName)
&& FindAttributeNameInFile(_file, attributeName))
{
(void)fscanf(_file, "%lf", &value);
}
//Free returned strings after use
free(categoryName);
free(attributeName);
return value;
}
//Find token in text _file (change cursor position)
double* GetDoublesArrayInFile(FILE* _file, char* _attribute, int* _arraySize)
{
char c = 0;
char* categoryName = GetCategoryName(_attribute);
char* attributeName = GetAttributeName(_attribute);
int curPos = 0;
double* value = NULL;
//Decompose attribute
if (FindCategoryNameInFile(_file, categoryName)
&& FindAttributeNameInFile(_file, attributeName))
{
//Search for the first array delimiter
c = fgetc(_file);
while (c != '[')
{
c = fgetc(_file);
}
//Store current cursor position
curPos = ftell(_file);
//Count the number of element in the array
*_arraySize = GetNumberOfElement(_file);
//Allocate memory to the array
value = (double*)calloc(*_arraySize, sizeof(double));
//Re place cursor in file
fseek(_file, curPos, SEEK_SET);
//Get each double of the array
for (int i = 0; i < *_arraySize; i++)
{
(void)fscanf(_file, "%lf", &value[i]);
fseek(_file, 1, SEEK_CUR);
}
}
//Free returned strings after use
free(categoryName);
free(attributeName);
return value;
}