-
Notifications
You must be signed in to change notification settings - Fork 0
/
csvReader.c
72 lines (61 loc) · 1.8 KB
/
csvReader.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
#include "csvReader.h"
#include <string.h>
#include <unistd.h>
static double csvGetField(struct CsvFileMems *csvFile, const char *field){
int i = 0;
double val = 0;
for(i = 0; i < csvFile->colSize; i++){
if(strcmp(field, csvFile->headers[i]) == 0){
val = csvFile->dataRow[i];
break;
}
}
return val;
}
static bool scanHeader(CSVFILE *csvFile){
char line[512];
const char *tok;
if(fgets(line, 512, csvFile->file) != NULL){
// split(buf, ",", csvFile->headers, &csvFile->colSize);
csvFile->colSize = 0;
for (tok = strtok(line, ","); tok && *tok; csvFile->colSize++, tok = strtok(NULL, ",\n\r")){
csvFile->headers[csvFile->colSize] = strdup(tok);
}
csvFile->dataRow = (double *)calloc(csvFile->colSize, sizeof(double));
return true;
}
return false;
}
static bool readOneLine(CSVFILE *csvFile){
char line[512];
if(fgets(line, 512, csvFile->file) == NULL) return false;
const char *tok;
int i = 0;
for (tok = strtok(line, ","); tok && *tok; i++, tok = strtok(NULL, ",\n")){
csvFile->dataRow[i] = strtod(tok, NULL);
}
return true;
}
bool csvOpen(CSVFILE *csvFile, const char *filename){
// check exist
if(access(filename, F_OK) != 0) return false;
memset(csvFile, 0x00, sizeof(CSVFILE));
csvFile->GetField = csvGetField;
csvFile->file = fopen(filename, "r");
return scanHeader(csvFile);
}
bool csvReadLine(CSVFILE *csvFile){
if(readOneLine(csvFile)){
return true;
}
fclose(csvFile->file);
return false;
}
void csvReadCallback(const char *filename, CsvRowUpdate callback){
CSVFILE csvFile;
if(csvOpen(&csvFile, filename)){
while(csvReadLine(&csvFile)){
callback(&csvFile);
}
}
}