/
csv.c
183 lines (160 loc) · 5.36 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
/*
* Copyright (c) 2014 Kirk Scheibelhut <kjs@scheibo.com>
*
* This file is free software: you may copy, redistribute and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 2 of the License, or (at your
* option) any later version.
*
* This file is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <string.h>
#include <assert.h>
#include <stdio.h>
#include "csv.h"
#include "util.h"
static const char *DATA_FIELDS[DataFieldCount] = {
"timestamp", "latitude", "longitude", "altitude",
"distance", "speed", "power", "grade",
"heart_rate", "cadence", "lr_balance", "temperature"};
/* test empty option and remove = true... */
static DataField name_to_field(char *name) {
downcase(name);
if (!strcmp(name, "timestamp") || !strcmp(name, "time")) {
return Timestamp;
} else if (!strcmp(name, "latitude") || !strcmp(name, "lat")) {
return Latitude;
} else if (!strcmp(name, "longitude") || !strcmp(name, "lon") ||
!strcmp(name, "long")) {
return Longitude;
} else if (!strcmp(name, "altitude") || !strcmp(name, "elevation") ||
!strcmp(name, "alt") || !strcmp(name, "ele")) {
return Altitude;
} else if (!strcmp(name, "distance") || !strcmp(name, "dist")) {
return Distance;
} else if (!strcmp(name, "speed") || !strcmp(name, "spd")) {
return Speed;
} else if (!strcmp(name, "power") || !strcmp(name, "pow") ||
!strcmp(name, "watts")) {
return Power;
} else if (!strcmp(name, "slope") || !strcmp(name, "grade") ||
!strcmp(name, "gradient")) {
return Grade;
} else if (!strcmp(name, "heart_rate") || !strcmp(name, "hr")) {
return HeartRate;
} else if (!strcmp(name, "cadence") || !strcmp(name, "cad")) {
return Cadence;
} else if (!strcmp(name, "balance") || !strcmp(name, "bal") ||
!strcmp(name, "lr_balance")) {
return LRBalance;
} else if (!strcmp(name, "temperature") || !strcmp(name, "atemp") ||
!strcmp(name, "temp")) {
return Cadence;
} else {/* not found */
return DataFieldCount;
}
}
/* Fields can have several names and be in different orders, but we assume
* all are doubles in base SI unit which we then convert into our format */
Activity *csv_read(char *filename) {
FILE *f = NULL;
char buf[CSV_BUFSIZ], *comma, *last, field_str[CSV_FIELD_SIZE];
DataField data_fields[DataFieldCount], field;
DataPoint point;
unsigned i, count = 0;
/* initialize */
for (i = 0; i < DataFieldCount; i++) {
data_fields[i] = DataFieldCount;
}
unset_data_point(&point);
if (!(f = fopen(filename, "r"))) {
return NULL;
}
/* make sure we can read the header */
if (!fgets(buf, sizeof(buf), f)) {
return NULL;
}
for (last = buf, comma = strchr(buf, ','); count < DataFieldCount && comma;
comma = strchr(last, ',')) {
strncpy(field_str, last, comma - last);
field_str[comma - last] = '\0';
if ((field = name_to_field(field_str))) {
data_fields[count] = field;
count++;
}
last = comma + 1;
}
if (count < DataFieldCount) {
name_to_field(last);
}
/*
// builder header array
for (;;) {
// read in array of doubles - IN ORDER
activity_add_point(a, &point);
unset_data_point(&point);
}
*/
fclose(f);
return NULL;
}
static void write_field(FILE *f, const char *format, size_t i, DataField field,
Activity *a, CSVOptions o, bool *first) {
double d = a->data_points[i].data[field];
if (!o.remove_unset || a->has_data[field]) {
if (!*first) {
fprintf(f, ",");
}
if (d == UNSET_FIELD) {
fprintf(f, "%s", o.unset_value);
} else {
fprintf(f, format, d);
}
*first = false;
}
}
int csv_write_options(char *filename, Activity *a, CSVOptions o) {
FILE *f = NULL;
unsigned i;
bool first = true;
assert(a != NULL);
if (!(f = fopen(filename, "w"))) {
return 1;
}
/* print header */
for (i = 0; i < DataFieldCount; i++) {
if (!o.remove_unset || a->has_data[i]) {
if (first) {
fprintf(f, "%s", DATA_FIELDS[i]);
first = false;
} else {
fprintf(f, ",%s", DATA_FIELDS[i]);
}
}
}
fprintf(f, "\n");
/* print data points - must be at least one non empty */
for (i = 0, first = true; i < a->num_points; i++, first = true) {
write_field(f, "%.0f", i, Timestamp, a, o, &first);
write_field(f, "%.7f", i, Latitude, a, o, &first);
write_field(f, "%.7f", i, Longitude, a, o, &first);
write_field(f, "%.3f", i, Altitude, a, o, &first);
write_field(f, "%.2f", i, Distance, a, o, &first);
write_field(f, "%.2f", i, Speed, a, o, &first);
write_field(f, "%.0f", i, Power, a, o, &first);
write_field(f, "%.2f", i, Grade, a, o, &first);
write_field(f, "%.0f", i, HeartRate, a, o, &first);
write_field(f, "%.0f", i, Cadence, a, o, &first);
write_field(f, "%.0f", i, LRBalance, a, o, &first);
write_field(f, "%.0f", i, Temperature, a, o, &first);
fprintf(f, "\n");
}
fclose(f);
return 0;
}