-
Notifications
You must be signed in to change notification settings - Fork 0
/
csv.c
189 lines (155 loc) · 4.42 KB
/
csv.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
#include "csv.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void split_line(char *line, float *floatbuf, int buflen, int *ncols);
int count_columns(FILE *csvfile);
csv_file* csv_new(char *filename) {
int len = strlen(filename);
csv_file *csv = malloc(sizeof(csv_file));
csv->filename = malloc(len+1);
memcpy(csv->filename, filename, len+1);
FILE *f = fopen(csv->filename, "r");
if(!f) {
fprintf(stderr, "Unable to open file '%s'\n", csv->filename);
return NULL;
}
csv->colcount = count_columns(f);
csv->rowcount = 0;
char buf[4096];
int ncols;
int curlines = 1;
float **data = malloc(curlines * sizeof(float*));
data[0] = malloc(csv->colcount * sizeof(float));
while(!feof(f)) {
if(fscanf(f, "%4096s", buf) != 1) {
break;
}
if(csv->rowcount >= curlines) {
data = realloc(data, curlines*2*sizeof(float*));
for(int i = curlines; i < curlines*2; i++) {
data[i] = malloc(csv->colcount * sizeof(float));
}
curlines *= 2;
}
split_line(buf, data[csv->rowcount], csv->colcount, &ncols);
csv->rowcount += 1;
if(ncols != csv->colcount) {
fprintf(stderr, "Warning! Expected %d columns, got %d on line %d\n",
csv->colcount, ncols, csv->rowcount);
}
}
csv->data = malloc(csv->rowcount * sizeof(float*));
for(int i = 0; i < csv->rowcount; i++) {
csv->data[i] = data[i];
}
for(int i = csv->rowcount; i < curlines; i++) {
free(data[i]);
}
free(data);
fclose(f);
return csv;
}
void csv_free(csv_file *csv) {
if(csv == NULL ) {
return;
}
for(int i = 0; i < csv->rowcount; i++) {
free(csv->data[i]);
}
free(csv->data);
free(csv->filename);
free(csv);
}
float csv_col_min(csv_file *csv, int col) {
if(col >= csv->colcount) {
fprintf(stderr, "csv_col_min got an invalid column: %d (range is 0-%d)\n", col, csv->colcount-1);
return 0;
}
float min = csv->data[0][col];
for(int i = 0; i < csv->rowcount; i++) {
if(csv->data[i][col] < min) {
min = csv->data[i][col];
}
}
return min;
}
float csv_col_max(csv_file *csv, int col) {
if(col >= csv->colcount || col < 0) {
fprintf(stderr, "csv_col_max got an invalid column: %d (range is 0-%d)\n", col, csv->colcount-1);
return 0;
}
float max = csv->data[0][col];
for(int i = 0; i < csv->rowcount; i++) {
if(csv->data[i][col] > max) {
max = csv->data[i][col];
}
}
return max;
}
float csv_col_mean(csv_file *csv, int col) {
if(col >= csv->colcount || col < 0) {
fprintf(stderr, "csv_col_mean got an invalid column: %d (range is 0-%d)\n", col, csv->colcount-1);
return 0;
}
float mean = 0;
for(int i = 0; i < csv->rowcount; i++) {
mean += csv->data[i][col];
}
return mean / csv->rowcount;
}
float csv_col_variance(csv_file *csv, int col) {
if(col >= csv->colcount || col < 0) {
fprintf(stderr, "csv_col_mean got an invalid column: %d (range is 0-%d)\n", col, csv->colcount-1);
return 0;
}
float mean = csv_col_mean(csv, col);
float variance = 0;
for(int i = 0; i < csv->rowcount; i++) {
float diff = mean - csv->data[i][col];
variance += diff * diff;
}
return variance / csv->rowcount;
}
void split_line(char *line, float *buf, int buflen, int *ncols) {
float val;
char *pos = line;
char *tmppos;
*ncols = 0;
while(sscanf(pos, "%f,", &val) == 1) {
if(*ncols == buflen) {
return;
}
buf[*ncols] = val;
tmppos = strchr(pos, ',');
if(tmppos == NULL) {
break;
}
else {
pos = tmppos + 1;
}
*ncols += 1;
}
if(*ncols <= buflen) {
if(sscanf(pos, "%f", &val) == 1) {
buf[*ncols] = val;
*ncols += 1;
}
}
}
int count_columns(FILE *csvfile) {
char line[4096];
if(fscanf(csvfile, "%4096s", line) != 1) {
fseek(csvfile, 0, SEEK_SET);
return 0;
}
int c = 0;
for(int i = 0; i < strlen(line); i++) {
if(line[i] == ',') {
c += 1;
}
}
fseek(csvfile, 0, SEEK_SET);
// turns out that little +1 is really important...
return c+1;
}