Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix mxml bug - need to retain a node

  • Loading branch information...
commit 15e462754faaef1abdec752de6319732c2a38e36 1 parent 4ea1e65
@scheibo authored
View
3  TODO
@@ -1,5 +1,6 @@
+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
-mxml gpx bug
gpx write
activity clean/verify points
activity laps
View
8 csv.c
@@ -45,7 +45,7 @@ static DataField name_to_field(char *name) {
/* 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 */
-int csv_read(char *filename, Activity *a) {
+Activity *csv_read(char *filename) {
FILE *f = NULL;
char buf[CSV_BUFSIZ], *comma, *last, field_str[CSV_FIELD_SIZE];
DataField data_fields[DataFieldCount], field;
@@ -59,12 +59,12 @@ int csv_read(char *filename, Activity *a) {
unset_data_point(&point);
if (!(f = fopen(filename, "r"))) {
- return 1;
+ return NULL;
}
/* make sure we can read the header */
if (!fgets(buf, sizeof(buf), f)) {
- return 1;
+ return NULL;
}
for (last = buf, comma = strchr(buf, ','); count < DataFieldCount && comma; comma = strchr(last, ',')) {
@@ -94,7 +94,7 @@ int csv_read(char *filename, Activity *a) {
*/
fclose(f);
- return 1;
+ return NULL;
}
static void write_field(FILE *f, const char *format, size_t i, DataField field,
View
2  csv.h
@@ -14,7 +14,7 @@ typedef struct {
/* Something Lap related? */
} CSVOptions;
-int csv_read(char *filename, Activity *activity);
+Activity *csv_read(char *filename);
int csv_write_options(char *filename, Activity *activity, CSVOptions options);
static inline int csv_write(char *filename, Activity *activity) {
View
2  fit.c
@@ -1,5 +1,5 @@
#include "fit.h"
-int fit_read(char *filename, Activity *activity) { return 1; }
+Activity *fit_read(char *filename) { return NULL; }
int fit_write(char *filename, Activity *activity) { return 1; }
View
2  fit.h
@@ -3,7 +3,7 @@
#include "activity.h"
-int fit_read(char *filename, Activity *activity);
+Activity *fit_read(char *filename);
int fit_write(char *filename, Activity *activity);
#endif /* _FIT_H_ */
View
20 fitparse.c
@@ -45,21 +45,22 @@ static FileFormat file_format_from_name(char *filename) {
return UnknownFileFormat;
}
-int fitparse_read(char *filename, Activity *activity) {
+Activity *fitparse_read(char *filename) {
+ Activity *a;
size_t i;
FileFormat format = file_format_from_name(filename);
- if (format != UnknownFileFormat &&
- !fitparse_read_format(filename, format, activity)) {
- return 0;
+
+ if (format != UnknownFileFormat) {
+ return (a = fitparse_read_format(filename, format)) ? a : NULL;
}
for (i = 0; i < ARRAY_SIZE(readers); i++) {
- if (!readers[i](filename, activity)) {
- return 0;
+ if ((a = readers[i](filename))) {
+ return a;
}
}
- return 1;
+ return NULL;
}
int fitparse_write(char *filename, Activity *activity) {
@@ -70,9 +71,8 @@ int fitparse_write(char *filename, Activity *activity) {
return fitparse_write_format(filename, format, activity);
}
-
-int fitparse_read_format(char *filename, FileFormat format, Activity *activity) {
- return readers[format](filename, activity);
+Activity *fitparse_read_format(char *filename, FileFormat format) {
+ return readers[format](filename);
}
int fitparse_write_format(char *filename, FileFormat format, Activity *activity) {
View
6 fitparse.h
@@ -3,13 +3,13 @@
#include "activity.h"
-typedef int (*ReadFn)(char *, Activity *);
+typedef Activity *(*ReadFn)(char *);
typedef int (*WriteFn)(char *, Activity *);
-int fitparse_read(char *filename, Activity *activity);
+Activity *fitparse_read(char *filename);
int fitparse_write(char *filename, Activity *activity);
/* helper functions - could just call the *_read or *_write function directly */
-int fitparse_read_format(char *filename, FileFormat format, Activity *activity);
+Activity *fitparse_read_format(char *filename, FileFormat format);
int fitparse_write_format(char *filename, FileFormat format, Activity *activity);
#endif /* _FITPARSE_H_ */
View
30 gpx.c
@@ -30,9 +30,14 @@ static int sax_cb(mxml_node_t *node, mxml_sax_event_t event, void *sax_data) {
name = mxmlGetElement(node);
- if (state->first_element && strcmp(name, "gpx")) {
- fprintf(stderr, "error\n"); /* TODO */
- return 1; /* stop reading the file */
+ if (state->first_element) {
+ if (strcmp(name, "gpx")) {
+ return 1; /* stop reading the file */
+ }
+
+ /* TODO confirm no memory leak */
+ /* We must retain an element so that mxmlSAXLoadFile can return it */
+ mxmlRetain(node);
}
if (!strcmp(name, "metadata")) {
@@ -84,34 +89,31 @@ static int sax_cb(mxml_node_t *node, mxml_sax_event_t event, void *sax_data) {
return 0;
}
-int gpx_read(char *filename, Activity *activity) {
+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}}};
unset_data_point(&(state.dp));
if (!(f = fopen(filename, "r"))) {
- return 1;
+ return NULL;
}
if (!(state.activity = activity_new())) {
- return 1;
+ return NULL;
}
- if (!mxmlSAXLoadFile(NULL, f, MXML_OPAQUE_CALLBACK, sax_cb, (void *)&state)) {
-
- fprintf(stderr, "failed\n"); /* TODO */
-
+ if (!(tree = mxmlSAXLoadFile(NULL, f, MXML_OPAQUE_CALLBACK, sax_cb, (void *)&state))) {
activity_destroy(state.activity);
fclose(f);
- return 1;
+ return NULL;
}
- fprintf(stderr, "Made it\n"); /* TODO */
state.activity->format = GPX;
- activity = state.activity;
+ mxmlDelete(tree);
fclose(f);
- return 0;
+ return state.activity;
}
static mxml_node_t *to_gpx_xml(Activity *a) {
View
2  gpx.h
@@ -3,7 +3,7 @@
#include "activity.h"
-int gpx_read(char *filename, Activity *activity);
+Activity *gpx_read(char *filename);
int gpx_write(char *filename, Activity *activity);
#endif /* _GPX_H_ */
View
2  tcx.c
@@ -1,5 +1,5 @@
#include "tcx.h"
-int tcx_read(char *filename, Activity *activity) { return 1; }
+Activity *tcx_read(char *filename) { return NULL; }
int tcx_write(char *filename, Activity *activity) { return 1; }
View
2  tcx.h
@@ -3,7 +3,7 @@
#include "activity.h"
-int tcx_read(char *filename, Activity *activity);
+Activity *tcx_read(char *filename);
int tcx_write(char *filename, Activity *activity);
#endif /* _TCX_H_ */
View
5 test.c
@@ -24,7 +24,7 @@ bool test(const char *filename, const char *dir) {
size_t len = strlen(dir);
/* read in the file in its original format */
- if (fitparse_read((char *)filename, a)) {
+ if (!(a = fitparse_read((char *)filename))) {
print("failed to read in '%s'\n", filename);
return false;
}
@@ -42,12 +42,13 @@ bool test(const char *filename, const char *dir) {
print("writing to file %s\n", namebuf);
fitparse_write_format(namebuf, CSV, a);
+ activity_destroy(a);
return 0;
#if 0
fitparse_write(namebuf, a);
/* read in the file in its original format */
- if (fitparse_read(namebuf, b)) {
+ if (!(b = fitparse_read(namebuf))) {
print("failed to read in '%s'\n", namebuf);
activity_destroy(a);
return false;
Please sign in to comment.
Something went wrong with that request. Please try again.