Skip to content
Browse files

Extension utilities and formating

  • Loading branch information...
1 parent abec407 commit c7a1e3eed30b8cd4ec52a8136f5f7c09cbb59e6b @scheibo committed Mar 1, 2014
Showing with 60 additions and 33 deletions.
  1. +0 −2 TODO
  2. +1 −3 activity.c
  3. +1 −5 activity.h
  4. +12 −5 csv.c
  5. +7 −9 fitparse.c
  6. +2 −1 fitparse.h
  7. +9 −3 gpx.c
  8. +7 −4 test.c
  9. +8 −1 util.c
  10. +13 −0 util.h
View
2 TODO
@@ -1,5 +1,3 @@
-extension util = rename to csv when printing from gpx etc.
- - store space for filename in activity = need to be malloced...
csv read - make sure to exit if invalid
gpx write
activity clean/verify points
View
4 activity.c
@@ -128,6 +128,4 @@ int activity_add_lap(Activity *a, uint32_t lap) {
return 0;
}
-bool activity_equal(Activity *a, Activity *b) {
- return false; /* TODO */
-}
+bool activity_equal(Activity *a, Activity *b) { return false; /* TODO */ }
View
6 activity.h
@@ -77,10 +77,6 @@ typedef enum {
typedef struct {
double data[DataFieldCount];
} DataPoint;
-static const char *DATA_FIELDS[DataFieldCount] = {
- "timestamp", "latitude", "longitude", "altitude",
- "distance", "speed", "power", "grade",
- "heart_rate", "cadence", "lr_balance", "temperature"};
/* TODO convert to array...
//typedef struct Summary {
@@ -151,7 +147,7 @@ typedef enum {
typedef struct {
Sport sport;
FileFormat format; /* the original format it was read in from */
- uint32_t *laps; /* TODO array of timestamps, always at least one */
+ uint32_t *laps; /* TODO array of timestamps, always at least one */
DataPoint *data_points;
size_t num_points;
size_t points_alloc;
View
17 csv.c
@@ -20,11 +20,17 @@
#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) {
- /* TODO convert name to lower case.... */
+ downcase(name);
if (!strcmp(name, "timestamp") || !strcmp(name, "time")) {
return Timestamp;
} else if (!strcmp(name, "latitude") || !strcmp(name, "lat")) {
@@ -84,7 +90,8 @@ Activity *csv_read(char *filename) {
return NULL;
}
- for (last = buf, comma = strchr(buf, ','); count < DataFieldCount && comma; comma = strchr(last, ',')) {
+ for (last = buf, comma = strchr(buf, ','); count < DataFieldCount && comma;
+ comma = strchr(last, ',')) {
strncpy(field_str, last, comma - last);
field_str[comma - last] = '\0';
@@ -115,10 +122,10 @@ Activity *csv_read(char *filename) {
}
static void write_field(FILE *f, const char *format, size_t i, DataField field,
- Activity *a, CSVOptions o, bool *first) {
+ Activity *a, CSVOptions o, bool *first) {
double d = a->data_points[i].data[field];
if (!o.remove_unset || a->has_data[field]) {
- if (!*first) {
+ if (!*first) {
fprintf(f, ",");
}
if (d == UNSET_FIELD) {
@@ -154,7 +161,7 @@ int csv_write_options(char *filename, Activity *a, CSVOptions o) {
}
fprintf(f, "\n");
- /* print data points - must be at least one non empty */
+ /* 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, "%.15f", i, Latitude, a, o, &first);
View
16 fitparse.c
@@ -20,6 +20,7 @@
#include "fitparse.h"
#include "activity.h"
+#include "util.h"
#include "csv.h"
#include "fit.h"
#include "gpx.h"
@@ -40,19 +41,15 @@
*/
/* indexed by FileFormat */
-static const ReadFn readers[] = {csv_read, gpx_read, tcx_read, fit_read };
+static const ReadFn readers[] = {csv_read, gpx_read, tcx_read, fit_read};
/* indexed by FileFormat */
-static const WriteFn writers[] = {csv_write, gpx_write, tcx_write, fit_write };
+static const WriteFn writers[] = {csv_write, gpx_write, tcx_write, fit_write};
static FileFormat file_format_from_name(char *filename) {
char ext[4];
- int i;
- size_t len = strlen(filename);
- for (i = 2; i >= 0; i--) {
- ext[i] = tolower(*(filename + len - 3 + i));
- }
- ext[3] = '\0';
+ strcpy(ext, extension(filename));
+ downcase(ext);
if (!strcmp("csv", ext)) return CSV;
if (!strcmp("gpx", ext)) return GPX;
@@ -92,6 +89,7 @@ Activity *fitparse_read_format(char *filename, FileFormat format) {
return readers[format](filename);
}
-int fitparse_write_format(char *filename, FileFormat format, Activity *activity) {
+int fitparse_write_format(char *filename, FileFormat format,
+ Activity *activity) {
return writers[format](filename, activity);
}
View
3 fitparse.h
@@ -27,6 +27,7 @@ Activity *fitparse_read(char *filename);
int fitparse_write(char *filename, Activity *activity);
/* helper functions - could just call the *_read or *_write function directly */
Activity *fitparse_read_format(char *filename, FileFormat format);
-int fitparse_write_format(char *filename, FileFormat format, Activity *activity);
+int fitparse_write_format(char *filename, FileFormat format,
+ Activity *activity);
#endif /* _FITPARSE_H_ */
View
12 gpx.c
@@ -101,7 +101,8 @@ static int sax_cb(mxml_node_t *node, mxml_sax_event_t event, void *sax_data) {
return 0;
} else if (!strcmp(name, "time")) {
state->dp.data[Timestamp] = parse_timestamp(data);
- if (state->first_time == UNSET_FIELD && state->dp.data[Timestamp] != UNSET_FIELD) {
+ if (state->first_time == UNSET_FIELD &&
+ state->dp.data[Timestamp] != UNSET_FIELD) {
state->first_time = state->dp.data[Timestamp];
}
} else if (!strcmp(name, "ele")) {
@@ -130,7 +131,11 @@ static int sax_cb(mxml_node_t *node, mxml_sax_event_t event, void *sax_data) {
Activity *gpx_read(char *filename) {
FILE *f = NULL;
mxml_node_t *tree;
- State state = { NULL, false /* metadata */, true /* first_element */, UNSET_FIELD /* first_time */, {{0}}};
+ State state = {NULL,
+ false /* metadata */,
+ true /* first_element */,
+ UNSET_FIELD /* first_time */,
+ {{0}}};
unset_data_point(&(state.dp));
if (!(f = fopen(filename, "r"))) {
@@ -141,7 +146,8 @@ Activity *gpx_read(char *filename) {
return NULL;
}
- if (!(tree = mxmlSAXLoadFile(NULL, f, MXML_OPAQUE_CALLBACK, sax_cb, (void *)&state))) {
+ if (!(tree = mxmlSAXLoadFile(NULL, f, MXML_OPAQUE_CALLBACK, sax_cb,
+ (void *)&state))) {
activity_destroy(state.activity);
fclose(f);
return NULL;
View
11 test.c
@@ -20,9 +20,12 @@
#include <string.h>
#include "fitparse.h"
+#include "util.h"
#define PREFIX "out."
+/*#ifndef DEFAULT_DIR*/
#define DEFAULT_DIR "tests/out"
+/*#endif*/
static void print(const char *format, ...) {
#ifdef DEBUG
@@ -49,15 +52,15 @@ bool test(const char *filename, const char *dir) {
/* create a new filename by prefixing the old filename and the test dir */
strcpy(namebuf, dir);
- if (dir[len-1] != '/') {
+ if (dir[len - 1] != '/') {
namebuf[len] = '/';
len++;
}
- strncpy(namebuf+len, PREFIX, sizeof(PREFIX)-1);
- strcpy(namebuf+len+(sizeof(PREFIX)-1), strrchr(filename, '/')+1);
+ strncpy(namebuf + len, PREFIX, sizeof(PREFIX) - 1);
+ strcpy(namebuf + len + (sizeof(PREFIX) - 1), strrchr(filename, '/') + 1);
- print("writing to file %s\n", namebuf);
+ change_extension(namebuf, "csv"), print("writing to file %s\n", namebuf);
fitparse_write_format(namebuf, CSV, a);
activity_destroy(a);
return 0;
View
9 util.c
@@ -15,9 +15,9 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
+#include <string.h>
#include <stdint.h>
#include <time.h>
-#include <ctype.h>
#include "activity.h"
#include "util.h"
@@ -37,3 +37,10 @@ int format_timestamp(char *buf, uint32_t timestamp) {
struct tm *tm = gmtime(&time);
return !strftime(buf, 21, "%Y-%m-%dT%H:%M:%SZ", tm) ? -1 : 0;
}
+
+char *change_extension(char *filename, char *ext) {
+ char *cur = extension(filename);
+ if (strlen(cur) != strlen(ext)) return NULL;
+ strcpy(cur, ext);
+ return filename;
+}
View
13 util.h
@@ -19,8 +19,21 @@
#define _UTIL_H_
#include <stdint.h>
+#include <ctype.h>
+#include <string.h>
+
+static inline char *extension(char *filename) {
+ char *s = strrchr(filename, '.');
+ return s ? s + 1 : s;
+}
+
+static inline char *downcase(char *str) {
+ for (; *str; ++str) *str = tolower(*str);
+ return str;
+}
uint32_t parse_timestamp(const char *date);
int format_timestamp(char *buf, uint32_t timestamp);
+char *change_extension(char *filename, char *ext);
#endif /* _UTIL_H_ */

0 comments on commit c7a1e3e

Please sign in to comment.
Something went wrong with that request. Please try again.