diff options
author | Linus Torvalds <torvalds@linux-foundation.org> | 2012-09-30 12:36:18 -0700 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2012-09-30 12:36:18 -0700 |
commit | 3244a828f3df9b0403db7de742de399edcb395bb (patch) | |
tree | 2d87c17db616796fc030b4dac14c10922ea0a638 | |
parent | 946d9c7166f0690689f7a1078952a1ca0844fefb (diff) | |
download | subsurface-3244a828f3df9b0403db7de742de399edcb395bb.tar.gz |
New XML format for saving dives
This patch makes the trips nest, and it also fixes the fact that you never
saved the trip notes (you could edit it, but saving would throw it away).
I did *not* change the indentation of the dives, so the trip stuff shows
up the the beginning of the line, at the same level as the <dive> and
<dives> thing. I think it's fairly readable xml, though, and we haven't
really had proper "indentation shows nesting" anyway, since the top-level
"<dives>" thing also didn't indent stuff inside of it.
Anyway, the way I wrote it, it still parses your old "INTRIP" stuff etc,
so as far as I know, it should happily read the old-style XML too. At
least it seemed to work with your xml file that already had the old-style
one (I haven't committed my divetrips, exactly because I didn't like the
new format).
It always saves in the new style, though.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
-rw-r--r-- | parse-xml.c | 20 | ||||
-rw-r--r-- | save-xml.c | 34 |
2 files changed, 40 insertions, 14 deletions
diff --git a/parse-xml.c b/parse-xml.c index 90ad91c79..44ef45746 100644 --- a/parse-xml.c +++ b/parse-xml.c @@ -1171,13 +1171,20 @@ static void dive_start(void) return; cur_dive = alloc_dive(); memset(&cur_tm, 0, sizeof(cur_tm)); + if (cur_trip) { + cur_dive->divetrip = cur_trip; + cur_dive->tripflag = IN_TRIP; + } } static void dive_end(void) { - if (!is_dive()) + if (!cur_dive) return; - record_dive(cur_dive); + if (!is_dive()) + free(cur_dive); + else + record_dive(cur_dive); cur_dive = NULL; cur_cylinder_index = 0; cur_ws_index = 0; @@ -1187,6 +1194,7 @@ static void trip_start(void) { if (cur_trip) return; + dive_end(); cur_trip = calloc(sizeof(dive_trip_t),1); memset(&cur_tm, 0, sizeof(cur_tm)); } @@ -1262,14 +1270,14 @@ static void entry(const char *name, int size, const char *raw) try_to_fill_sample(cur_sample, name, buf); return; } - if (cur_trip) { - try_to_fill_trip(&cur_trip, name, buf); - return; - } if (cur_dive) { try_to_fill_dive(&cur_dive, name, buf); return; } + if (cur_trip) { + try_to_fill_trip(&cur_trip, name, buf); + return; + } free(buf); } diff --git a/save-xml.c b/save-xml.c index 63e5a9ae3..8fa723f61 100644 --- a/save-xml.c +++ b/save-xml.c @@ -304,7 +304,9 @@ static void save_trip(FILE *f, dive_trip_t *trip) tm.tm_hour, tm.tm_min, tm.tm_sec); if (trip->location) show_utf8(f, trip->location, " location=\'","\'", 1); - fprintf(f, " />\n"); + fprintf(f, ">\n"); + if (trip->notes) + show_utf8(f, trip->notes, "<notes>","</notes>\n", 0); } static void save_dive(FILE *f, struct dive *dive) @@ -317,7 +319,13 @@ static void save_dive(FILE *f, struct dive *dive) fputs("<dive", f); if (dive->number) fprintf(f, " number='%d'", dive->number); - if (dive->tripflag != TF_NONE) + /* + * TF_NONE is the default for dives with no trips + * IN_TRIP is the default for dives with trips + * ASSIGNED_TRIP is an in-memory thing and gets converted + * to IN_TRIP by the save code. + */ + if (dive->tripflag != TF_NONE && dive->tripflag != IN_TRIP && dive->tripflag != ASSIGNED_TRIP) fprintf(f, " tripflag='%s'", tripflag_names[dive->tripflag]); if (dive->rating) fprintf(f, " rating='%d'", dive->rating); @@ -341,7 +349,8 @@ static void save_dive(FILE *f, struct dive *dive) void save_dives(const char *filename) { int i; - GList *trip = NULL; + struct dive *dive; + dive_trip_t *trip = NULL; FILE *f = fopen(filename, "w"); @@ -353,13 +362,22 @@ void save_dives(const char *filename) fprintf(f, "<dives>\n<program name='subsurface' version='%d'></program>\n", VERSION); - /* save the trips */ - while ((trip = NEXT_TRIP(trip)) != NULL) - save_trip(f, trip->data); - /* save the dives */ - for (i = 0; i < dive_table.nr; i++) + for_each_dive(i, dive) { + dive_trip_t *thistrip = dive->divetrip; + if (trip != thistrip) { + /* Close the old trip? */ + if (trip) + fprintf(f, "</trip>\n"); + /* Open the new one */ + if (thistrip) + save_trip(f, thistrip); + trip = thistrip; + } save_dive(f, get_dive(i)); + } + if (trip) + fprintf(f, "</trip>\n"); fprintf(f, "</dives>\n"); fclose(f); } |