From 4b15b9dfe910fd0df63bdc8024a919a566c380be Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Wed, 11 Feb 2015 21:46:02 -0800 Subject: Save and parse dive site structures to XML Read and write divesite sections in the XML file. Read divelogs of version 2 and create dive site structures on the fly. Read version 3 files that have divesiteid instead of location / gps. Saves version 3 files where dives no longer have location and gps but instead refer to a divesiteid The commit contains quite a few fprintf(stderr,...) in order to allow better monitoring of the parsing / transforming of locations and gps to dive sites. This will need to be removed later. Signed-off-by: Dirk Hohndel --- save-xml.c | 54 ++++++++++++++++++++---------------------------------- 1 file changed, 20 insertions(+), 34 deletions(-) (limited to 'save-xml.c') diff --git a/save-xml.c b/save-xml.c index 053b8fc96..dad57dc9b 100644 --- a/save-xml.c +++ b/save-xml.c @@ -110,38 +110,8 @@ static void save_salinity(struct membuffer *b, struct divecomputer *dc) put_string(b, " />\n"); } -static void show_location(struct membuffer *b, struct dive *dive) -{ - degrees_t latitude = dive->latitude; - degrees_t longitude = dive->longitude; - - /* Should we write a location tag at all? */ - if (!(latitude.udeg || longitude.udeg) && !dive->location) - return; - - put_string(b, " location && dive->location[0] != '\0') - show_utf8(b, dive->location, ">", "\n", 0); - else - put_string(b, "/>\n"); -} - static void save_overview(struct membuffer *b, struct dive *dive) { - show_location(b, dive); show_utf8(b, dive->divemaster, " ", "\n", 0); show_utf8(b, dive->buddy, " ", "\n", 0); show_utf8(b, dive->notes, " ", "\n", 0); @@ -424,7 +394,8 @@ void save_one_dive(struct membuffer *b, struct dive *dive) if (dive->visibility) put_format(b, " visibility='%d'", dive->visibility); save_tags(b, dive->tag_list); - + if (dive->dive_site_uuid) + put_format(b, " divesiteid='%8x'", dive->dive_site_uuid); show_date(b, dive->when); put_format(b, " duration='%u:%02u min'>\n", FRACTION(dive->dc.duration.seconds, 60)); @@ -507,7 +478,7 @@ static void save_one_device(void *_f, const char *model, uint32_t deviceid, put_format(b, "/>\n"); } -#define VERSION 2 +#define VERSION 3 int save_dives(const char *filename) { @@ -529,8 +500,23 @@ void save_dives_buffer(struct membuffer *b, const bool select_only) call_for_each_dc(b, save_one_device); if (autogroup) put_format(b, " \n"); - put_format(b, "\n\n"); - + put_format(b, "\n"); + + /* save the dive sites */ + put_format(b, "\n"); + for (i = 0; i < dive_site_table.nr; i++) { + struct dive_site *ds = get_dive_site(i); + put_format(b, "uuid); + show_utf8(b, ds->name, " name='", "'", 1); + if (ds->latitude.udeg || ds->longitude.udeg) { + put_degrees(b, ds->latitude, " gps='", " "); + put_degrees(b, ds->longitude, "", "'"); + } + show_utf8(b, ds->description, " description='", "'", 1); + show_utf8(b, ds->notes, " notes='", "'", 1); + put_format(b, "/>\n"); + } + put_format(b, "\n\n"); for (trip = dive_trip_list; trip != NULL; trip = trip->next) trip->index = 0; -- cgit v1.2.3-70-g09d2 From d4f2b7214863b2a40ebcf69e7add113412f795b8 Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Thu, 12 Feb 2015 23:35:52 -0800 Subject: Make special backup when switching XML versions Since the last few dozen commits change the format in irreversible ways and could therefore be destructive and lose data for testers of the development version, let's try to be extra careful and create "special" backup files that aren't overwritten by subsequent backups. At least this way people can go back to the previous state. Of course people using the git backend don't have to worry about this as they always can go back to any earlier save. Signed-off-by: Dirk Hohndel --- dive.h | 2 ++ parse-xml.c | 4 ++++ save-xml.c | 10 +++++++++- 3 files changed, 15 insertions(+), 1 deletion(-) (limited to 'save-xml.c') diff --git a/dive.h b/dive.h index 09c831e43..9de3423ad 100644 --- a/dive.h +++ b/dive.h @@ -48,6 +48,8 @@ extern "C" { #include #endif +extern int last_xml_version; + enum dive_comp_type {OC, CCR, PSCR, FREEDIVE, NUM_DC_TYPE}; // Flags (Open-circuit and Closed-circuit-rebreather) for setting dive computer type enum cylinderuse {OC_GAS, DILUENT, OXYGEN, NUM_GAS_USE}; // The different uses for cylinders diff --git a/parse-xml.c b/parse-xml.c index c93d25453..6cc2d881a 100644 --- a/parse-xml.c +++ b/parse-xml.c @@ -21,6 +21,7 @@ int verbose, quit; int metric = 1; +int last_xml_version = -1; static xmlDoc *test_xslt_transforms(xmlDoc *doc, const char **params); @@ -1644,6 +1645,9 @@ static void userid_stop(void) static void entry(const char *name, char *buf) { + if (!strncmp(name, "version.program", sizeof("version.program") - 1) || + !strncmp(name, "version.divelog", sizeof("version.divelog") - 1)) + last_xml_version = atoi(buf); if (in_userid) { try_to_fill_userid(name, buf); return; diff --git a/save-xml.c b/save-xml.c index dad57dc9b..5a0c6976c 100644 --- a/save-xml.c +++ b/save-xml.c @@ -591,7 +591,15 @@ static void try_to_backup(const char *filename) while (extension[i][0] != '\0') { int elen = strlen(extension[i]); if (strcasecmp(filename + flen - elen, extension[i]) == 0) { - save_backup(filename, extension[i], "bak"); + if (last_xml_version < VERSION) { + int se_len = strlen(extension[i]) + 5; + char *special_ext = malloc(se_len); + snprintf(special_ext, se_len, "%s.v%d", extension[i], last_xml_version); + save_backup(filename, extension[i], special_ext); + free(special_ext); + } else { + save_backup(filename, extension[i], "bak"); + } break; } i++; -- cgit v1.2.3-70-g09d2 From 6820b13bd58ee382a21215e84427dfd690857dd6 Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Fri, 13 Feb 2015 23:22:40 -0800 Subject: Don't store empty dive sites And remove references to them from the dives. Signed-off-by: Dirk Hohndel --- save-git.c | 11 +++++++++++ save-xml.c | 11 +++++++++++ 2 files changed, 22 insertions(+) (limited to 'save-xml.c') diff --git a/save-git.c b/save-git.c index cf6bd37e9..84eb1a316 100644 --- a/save-git.c +++ b/save-git.c @@ -827,6 +827,17 @@ static void save_divesites(git_repository *repo, struct dir *tree) for (int i = 0; i < dive_site_table.nr; i++) { struct membuffer b = { 0 }; struct dive_site *ds = get_dive_site(i); + if (dive_site_is_empty(ds)) { + int j; + struct dive *d; + for_each_dive(j, d) { + if (d->dive_site_uuid == ds->uuid) + d->dive_site_uuid = 0; + } + delete_dive_site(get_dive_site(i)->uuid); + i--; // since we just deleted that one + continue; + } int size = sizeof("Site-012345678"); char name[size]; snprintf(name, size, "Site-%08x", ds->uuid); diff --git a/save-xml.c b/save-xml.c index 5a0c6976c..c09b90812 100644 --- a/save-xml.c +++ b/save-xml.c @@ -506,6 +506,17 @@ void save_dives_buffer(struct membuffer *b, const bool select_only) put_format(b, "\n"); for (i = 0; i < dive_site_table.nr; i++) { struct dive_site *ds = get_dive_site(i); + if (dive_site_is_empty(ds)) { + int j; + struct dive *d; + for_each_dive(j, d) { + if (d->dive_site_uuid == ds->uuid) + d->dive_site_uuid = 0; + } + delete_dive_site(get_dive_site(i)->uuid); + i--; // since we just deleted that one + continue; + } put_format(b, "uuid); show_utf8(b, ds->name, " name='", "'", 1); if (ds->latitude.udeg || ds->longitude.udeg) { -- cgit v1.2.3-70-g09d2