-
Notifications
You must be signed in to change notification settings - Fork 1
/
wbImport.cpp
612 lines (496 loc) · 13.8 KB
/
wbImport.cpp
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
#include <wb.h>
static inline void wbImportCSV_setFile(wbImportCSV_t csv, const char *path) {
if (csv != NULL) {
if (wbImportCSV_getFile(csv) != NULL) {
wbFile_delete(wbImportCSV_getFile(csv));
}
if (path != NULL) {
wbImportCSV_getFile(csv) = wbFile_open(path, "r");
} else {
wbImportCSV_getFile(csv) = NULL;
}
}
return;
}
static inline wbImportCSV_t wbImportCSV_new(void) {
wbImportCSV_t csv;
csv = wbNew(struct st_wbImportCSV_t);
wbImportCSV_setRowCount(csv, -1);
wbImportCSV_setColumnCount(csv, -1);
wbImportCSV_setData(csv, NULL);
wbImportCSV_getFile(csv) = NULL;
wbImportCSV_setSeperator(csv, '\0');
return csv;
}
static inline void wbImportCSV_delete(wbImportCSV_t csv) {
if (csv != NULL) {
wbImportCSV_setFile(csv, NULL);
if (wbImportCSV_getData(csv)) {
wbDelete(wbImportCSV_getData(csv));
}
wbDelete(csv);
}
}
static inline wbImportCSV_t
wbImportCSV_findDimensions(wbImportCSV_t csv, int *resRows, int *resColumns) {
int rows = 0, columns = -1;
char *line;
wbFile_t file;
char seperator[2];
if (csv == NULL) {
return NULL;
}
if (wbImportCSV_getSeperator(csv) == '\0') {
seperator[0] = ',';
} else {
seperator[0] = wbImportCSV_getSeperator(csv);
}
seperator[1] = '\0';
file = wbImportCSV_getFile(csv);
while ((line = wbFile_readLine(file)) != NULL) {
int currColumn = 0;
char *token = strtok(line, seperator);
while (token != NULL) {
token = strtok(NULL, seperator);
currColumn++;
}
rows++;
if (columns == -1) {
columns = currColumn;
}
if (columns != currColumn) {
wbLog(ERROR, "The csv file is not rectangular.");
}
wbAssert(columns == currColumn);
}
wbFile_rewind(file);
*resRows = rows;
*resColumns = columns;
return csv;
}
static inline int *csv_readAsInteger(wbFile_t file, char sep, int rows,
int columns) {
int ii = 0;
int *data;
char *line;
int var;
char seperator[2];
if (file == NULL) {
return NULL;
}
data = wbNewArray(int, rows *columns);
if (sep == '\0') {
seperator[0] = ',';
} else {
seperator[0] = sep;
}
seperator[1] = '\0';
//printf("cols = %d rows = %d\n", columns, rows);
if (columns == 1) {
while ((line = wbFile_readLine(file)) != NULL) {
sscanf(line, "%d", &var);
//printf("reading %d\n", var);
data[ii++] = var;
}
} else {
while ((line = wbFile_readLine(file)) != NULL) {
char *token = strtok(line, seperator);
while (token != NULL) {
sscanf(token, "%d", &var);
token = strtok(NULL, seperator);
data[ii++] = var;
}
}
}
return data;
}
static inline wbReal_t *csv_readAsReal(wbFile_t file, char sep, int rows,
int columns) {
int ii = 0;
wbReal_t *data;
char *line;
wbReal_t var;
char seperator[2];
if (file == NULL) {
return NULL;
}
data = wbNewArray(wbReal_t, rows * columns);
if (sep == '\0') {
seperator[0] = ',';
} else {
seperator[0] = sep;
}
seperator[1] = '\0';
if (columns == 1) {
while ((line = wbFile_readLine(file)) != NULL) {
sscanf(line, "%f", &var);
data[ii++] = var;
}
} else {
while ((line = wbFile_readLine(file)) != NULL) {
char *token = strtok(line, seperator);
while (token != NULL) {
sscanf(token, "%f", &var);
token = strtok(NULL, seperator);
data[ii++] = var;
}
}
}
return data;
}
static inline wbImportCSV_t wbImportCSV_read(wbImportCSV_t csv, wbType_t type) {
void *data;
wbFile_t file;
char seperator;
int rows, columns;
if (csv == NULL) {
return NULL;
}
if (wbImportCSV_getRowCount(csv) == -1 ||
wbImportCSV_getColumnCount(csv) == -1) {
if (wbImportCSV_findDimensions(csv, &rows, &columns) == NULL) {
wbLog(ERROR, "Failed to figure out csv dimensions.");
return NULL;
}
wbImportCSV_setRowCount(csv, rows);
wbImportCSV_setColumnCount(csv, columns);
}
file = wbImportCSV_getFile(csv);
seperator = wbImportCSV_getSeperator(csv);
rows = wbImportCSV_getRowCount(csv);
columns = wbImportCSV_getColumnCount(csv);
if (wbImportCSV_getData(csv) != NULL) {
wbDelete(wbImportCSV_getData(csv));
wbImportCSV_setData(csv, NULL);
}
if (type == wbType_integer) {
//printf("ReadXXXing as integer...\n");
data = csv_readAsInteger(file, seperator, rows, columns);
} else {
data = csv_readAsReal(file, seperator, rows, columns);
}
wbImportCSV_setData(csv, data);
return csv;
}
static inline wbImportCSV_t wbImportCSV_readAsInteger(wbImportCSV_t csv) {
return wbImportCSV_read(csv, wbType_integer);
}
static inline wbImportCSV_t wbImportCSV_readAsReal(wbImportCSV_t csv) {
return wbImportCSV_read(csv, wbType_real);
}
static inline void wbImportRaw_setFile(wbImportRaw_t raw, const char *path) {
if (raw != NULL) {
if (wbImportRaw_getFile(raw) != NULL) {
wbFile_delete(wbImportRaw_getFile(raw));
}
if (path != NULL) {
wbImportRaw_getFile(raw) = wbFile_open(path, "r");
} else {
wbImportRaw_getFile(raw) = NULL;
}
}
return;
}
static inline wbImportRaw_t wbImportRaw_new(void) {
wbImportRaw_t raw;
raw = wbNew(struct st_wbImportRaw_t);
wbImportRaw_setRowCount(raw, -1);
wbImportRaw_setColumnCount(raw, -1);
wbImportRaw_setData(raw, NULL);
wbImportRaw_getFile(raw) = NULL;
return raw;
}
static inline void wbImportRaw_delete(wbImportRaw_t raw) {
if (raw != NULL) {
wbImportRaw_setFile(raw, NULL);
if (wbImportRaw_getData(raw)) {
wbDelete(wbImportRaw_getData(raw));
}
wbDelete(raw);
}
}
static inline wbBool lineHasSpace(const char *line) {
while (*line != '\0') {
if (*line == ' ') {
return wbTrue;
}
line++;
}
return wbFalse;
}
static inline char *lineStrip(const char *line) {
char *sl = wbString_duplicate(line);
char *iter = sl;
size_t slen = strlen(line);
iter += slen - 1;
while (*iter == '\0' || *iter == '\r' || *iter == '\t' || *iter == '\n' ||
*iter == ' ') {
*iter-- = '\0';
}
return sl;
}
static inline wbBool wbImportRaw_findDimensions(wbImportRaw_t raw) {
if (raw != NULL) {
int rows;
int columns;
char *line;
wbFile_t file;
char *strippedLine;
file = wbImportRaw_getFile(raw);
wbFile_rewind(file);
line = wbFile_readLine(file);
if (line == NULL) {
return wbTrue;
}
strippedLine = lineStrip(line);
if (lineHasSpace(strippedLine)) {
sscanf(strippedLine, "%d %d", &rows, &columns);
} else {
columns = 1;
sscanf(strippedLine, "%d", &rows);
}
wbImportRaw_setRowCount(raw, rows);
wbImportRaw_setColumnCount(raw, columns);
wbDelete(strippedLine);
return wbFalse;
}
return wbTrue;
}
static inline wbImportRaw_t wbImportRaw_read(wbImportRaw_t raw, wbType_t type) {
void *data;
wbFile_t file;
char seperator;
int rows, columns;
if (raw == NULL) {
return NULL;
}
if (wbImportRaw_getRowCount(raw) == -1 ||
wbImportRaw_getColumnCount(raw) == -1) {
if (wbImportRaw_findDimensions(raw)) {
wbLog(ERROR, "Failed to figure out raw dimensions.");
return NULL;
}
}
file = wbImportRaw_getFile(raw);
seperator = ' ';
rows = wbImportRaw_getRowCount(raw);
columns = wbImportRaw_getColumnCount(raw);
if (wbImportRaw_getData(raw) != NULL) {
wbDelete(wbImportRaw_getData(raw));
wbImportRaw_setData(raw, NULL);
}
if (type == wbType_integer) {
//printf("Rdin gas integer...\n");
data = csv_readAsInteger(file, seperator, rows, columns);
} else {
data = csv_readAsReal(file, seperator, rows, columns);
}
wbImportRaw_setData(raw, data);
return raw;
}
static inline wbImportRaw_t wbImportRaw_readAsInteger(wbImportRaw_t raw) {
return wbImportRaw_read(raw, wbType_integer);
}
static inline wbImportRaw_t wbImportRaw_readAsReal(wbImportRaw_t raw) {
return wbImportRaw_read(raw, wbType_real);
}
static inline wbImport_t wbImport_open(const char *file, wbImportKind_t kind) {
wbImport_t imp;
if (file == NULL) {
wbLog(ERROR, "Go NULL for file value.");
wbExit();
}
if (!wbFile_existsQ(file)) {
wbLog(ERROR, "File ", file, " does not exist.");
wbExit();
}
wbImport_setKind(imp, kind);
if (kind == wbImportKind_raw) {
wbImportRaw_t raw = wbImportRaw_new();
wbImportRaw_setFile(raw, file);
wbImport_setRaw(imp, raw);
} else if (kind == wbImportKind_tsv || kind == wbImportKind_csv) {
wbImportCSV_t csv = wbImportCSV_new();
if (kind == wbImportKind_csv) {
wbImportCSV_setSeperator(csv, ',');
} else {
wbImportCSV_setSeperator(csv, '\t');
}
wbImportCSV_setFile(csv, file);
wbImport_setCSV(imp, csv);
} else if (kind == wbImportKind_ppm) {
wbImage_t img = wbPPM_import(file);
wbImport_setImage(imp, img);
} else {
wbLog(ERROR, "Invalid import type.");
wbExit();
}
return imp;
}
static inline wbImport_t wbImport_open(const char *file, const char *type0) {
wbImport_t imp;
wbImportKind_t kind;
char *type;
type = wbString_toLower(type0);
if (wbString_sameQ(type, "csv")) {
kind = wbImportKind_csv;
} else if (wbString_sameQ(type, "tsv")) {
kind = wbImportKind_tsv;
} else if (wbString_sameQ(type, "raw") || wbString_sameQ(type, "dat")) {
kind = wbImportKind_raw;
} else if (wbString_sameQ(type, "ppm")) {
kind = wbImportKind_ppm;
} else {
wbLog(ERROR, "Invalid import type ", type0);
wbExit();
}
imp = wbImport_open(file, kind);
wbDelete(type);
return imp;
}
static inline void wbImport_close(wbImport_t imp) {
wbImportKind_t kind;
kind = wbImport_getKind(imp);
if (kind == wbImportKind_tsv || kind == wbImportKind_csv) {
wbImportCSV_t csv = wbImport_getCSV(imp);
wbImportCSV_delete(csv);
wbImport_setCSV(imp, NULL);
} else if (kind == wbImportKind_raw) {
wbImportRaw_t raw = wbImport_getRaw(imp);
wbImportRaw_delete(raw);
wbImport_setRaw(imp, NULL);
} else if (kind == wbImportKind_ppm) {
} else {
wbLog(ERROR, "Invalid import type.");
wbExit();
}
return;
}
static inline void *wbImport_read(wbImport_t imp, wbType_t type) {
void *data = NULL;
wbImportKind_t kind;
kind = wbImport_getKind(imp);
if (kind == wbImportKind_tsv || kind == wbImportKind_csv) {
wbImportCSV_t csv = wbImport_getCSV(imp);
wbImportCSV_read(csv, type);
data = wbImportCSV_getData(csv);
} else if (kind == wbImportKind_raw) {
wbImportRaw_t raw = wbImport_getRaw(imp);
wbImportRaw_read(raw, type);
data = wbImportRaw_getData(raw);
} else {
wbLog(ERROR, "Invalid import type.");
wbExit();
}
return data;
}
static inline int *wbImport_readAsInteger(wbImport_t imp) {
void *data = wbImport_read(imp, wbType_integer);
return (int *)data;
}
static inline wbReal_t *wbImport_readAsReal(wbImport_t imp) {
void *data = wbImport_read(imp, wbType_real);
return (wbReal_t *)data;
}
static wbImportKind_t _parseImportExtension(const char *file) {
char *extension;
wbImportKind_t kind;
extension = wbFile_extension(file);
if (wbString_sameQ(extension, "csv")) {
kind = wbImportKind_csv;
} else if (wbString_sameQ(extension, "tsv")) {
kind = wbImportKind_tsv;
} else if (wbString_sameQ(extension, "raw") ||
wbString_sameQ(extension, "dat")) {
kind = wbImportKind_raw;
} else if (wbString_sameQ(extension, "ppm")) {
kind = wbImportKind_ppm;
} else {
kind = wbImportKind_unknown;
wbLog(ERROR, "File ", file, " does not have a compatible extension.");
}
wbDelete(extension);
return kind;
}
void *wbImport(const char *file, int *resRows, int *resColumns,
const char *type) {
void *data, *res;
wbImport_t imp;
size_t sz;
int columns = 0, rows = 0;
wbImportKind_t kind;
if (file == NULL) {
fprintf(stderr, "Failed to import file.\n");
wbExit();
}
kind = _parseImportExtension(file);
wbAssert(kind != wbImportKind_unknown);
imp = wbImport_open(file, kind);
if (wbString_sameQ(type, "Real")) {
data = wbImport_readAsReal(imp);
sz = sizeof(wbReal_t);
} else {
//printf("Reading as integer..d\n");
data = wbImport_readAsInteger(imp);
sz = sizeof(int);
}
if (kind == wbImportKind_csv || kind == wbImportKind_tsv) {
rows = wbImportCSV_getRowCount(wbImport_getCSV(imp));
columns = wbImportCSV_getColumnCount(wbImport_getCSV(imp));
} else if (kind == wbImportKind_raw) {
rows = wbImportRaw_getRowCount(wbImport_getRaw(imp));
columns = wbImportRaw_getColumnCount(wbImport_getRaw(imp));
}
if (rows == 1 && columns > 0) {
rows = columns;
columns = 1;
}
if (resRows != NULL) {
*resRows = rows;
}
if (resColumns != NULL) {
*resColumns = columns;
}
res = wbMalloc(sz * rows * columns);
memcpy(res, data, sz * rows * columns);
wbImport_close(imp);
return res;
}
void *wbImport(const char *file, int *rows, int *columns) {
return wbImport(file, rows, columns, "Real");
}
EXTERN_C void *wbImport(const char *file, int *rows) {
return wbImport(file, rows, NULL, "Real");
}
void *wbImport(const char *file, int *res_rows, const char *type) {
int cols, rows;
void *res = wbImport(file, &rows, &cols, type);
if (rows == 1 && cols > 1) {
rows = cols;
}
*res_rows = rows;
return res;
}
wbImage_t wbImport(const char *file) {
wbImage_t img;
wbImport_t imp;
wbImportKind_t kind;
if (file == NULL) {
fprintf(stderr, "Failed to import file.\n");
wbExit();
}
kind = _parseImportExtension(file);
wbAssert(kind == wbImportKind_ppm);
imp = wbImport_open(file, kind);
img = wbImport_getImage(imp);
wbImport_close(imp);
return img;
}
int wbImport_flag(const char *file) {
int res;
wbFile_t fh = wbFile_open(file, "r");
const char * line = wbFile_readLine(fh);
sscanf(line, "%d", &res);
wbFile_close(fh);
return res;
}