From 27927f74a1b11ed76580059443e9d4f963df0513 Mon Sep 17 00:00:00 2001 From: Doug Junkins Date: Sat, 6 Apr 2019 12:05:18 -0700 Subject: Add export option to export dive sites. Selecting "Selected dives" exports the dive sites for the selected dives. Selecting "All dives" exports all dive sites. XML format is the subsection of the divelog XML that describes the sites headed with a section like: Signed-off-by: Doug Junkins --- core/dive.h | 2 ++ core/save-xml.c | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 71 insertions(+), 5 deletions(-) (limited to 'core') diff --git a/core/dive.h b/core/dive.h index 6e4940452..7a6cf4099 100644 --- a/core/dive.h +++ b/core/dive.h @@ -481,6 +481,8 @@ extern int save_dives_logic(const char *filename, bool select_only, bool anonymi extern int save_dive(FILE *f, struct dive *dive, bool anonymize); extern int export_dives_xslt(const char *filename, const bool selected, const int units, const char *export_xslt, bool anonymize); +extern int save_dive_sites_logic(const char *filename, bool select_only, bool anonymize); + struct membuffer; extern void save_one_dive_to_mb(struct membuffer *b, struct dive *dive, bool anonymize); diff --git a/core/save-xml.c b/core/save-xml.c index 50a490c80..02cb2bf87 100644 --- a/core/save-xml.c +++ b/core/save-xml.c @@ -21,6 +21,7 @@ #include "strndup.h" #include "git-access.h" #include "qthelper.h" +#include "gettext.h" /* * We're outputting utf8 in xml. @@ -590,15 +591,16 @@ void save_dives_buffer(struct membuffer *b, const bool select_only, bool anonymi put_format(b, " \n"); put_format(b, "\n"); - /* save the dive sites - to make the output consistent let's sort the table, first */ - sort_dive_site_table(&dive_site_table); - purge_empty_dive_sites(&dive_site_table); + /* 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, &dive_site_table); + /* Don't export empty dive sites */ + if (dive_site_is_empty(ds)) + continue; /* Only write used dive sites when exporting selected dives */ if (select_only && !is_dive_site_used(ds, true)) - continue; + continue; put_format(b, "uuid); show_utf8_blanked(b, ds->name, " name='", "'", 1, anonymize); @@ -734,7 +736,7 @@ int save_dives_logic(const char *filename, const bool select_only, bool anonymiz error = fclose(f); } if (error) - report_error("Save failed (%s)", strerror(errno)); + report_error(translate("gettextFromC", "Failed to save dives to %s (%s)"), filename, strerror(errno)); free_buffer(&buf); return error; @@ -798,3 +800,65 @@ int export_dives_xslt(const char *filename, const bool selected, const int units return res; } + +void save_dive_sites_buffer(struct membuffer *b, const bool select_only, bool anonymize) +{ + int i; + put_format(b, "\n", DATAFORMAT_VERSION); + + /* save the dive sites */ + for (i = 0; i < dive_site_table.nr; i++) { + struct dive_site *ds = get_dive_site(i, &dive_site_table); + /* Don't export empty dive sites */ + if (dive_site_is_empty(ds)) + continue; + /* Only write used dive sites when exporting selected dives */ + if (select_only && !is_dive_site_used(ds, true)) + continue; + + put_format(b, "uuid); + show_utf8_blanked(b, ds->name, " name='", "'", 1, anonymize); + put_location(b, &ds->location, " gps='", "'"); + show_utf8_blanked(b, ds->description, " description='", "'", 1, anonymize); + put_format(b, ">\n"); + show_utf8_blanked(b, ds->notes, " ", " \n", 0, anonymize); + if (ds->taxonomy.nr) { + for (int j = 0; j < ds->taxonomy.nr; j++) { + struct taxonomy *t = &ds->taxonomy.category[j]; + if (t->category != TC_NONE && t->value) { + put_format(b, " category); + put_format(b, " origin='%d'", t->origin); + show_utf8_blanked(b, t->value, " value='", "'/>\n", 1, anonymize); + } + } + } + put_format(b, "\n"); + } + put_format(b, "\n"); +} + +int save_dive_sites_logic(const char *filename, const bool select_only, bool anonymize) +{ + struct membuffer buf = { 0 }; + FILE *f; + int error = 0; + + save_dive_sites_buffer(&buf, select_only, anonymize); + + if (same_string(filename, "-")) { + f = stdout; + } else { + try_to_backup(filename); + error = -1; + f = subsurface_fopen(filename, "w"); + } + if (f) { + flush_buffer(&buf, f); + error = fclose(f); + } + if (error) + report_error(translate("gettextFromC", "Failed to save divesites to %s (%s)"), filename, strerror(errno)); + + free_buffer(&buf); + return error; +} -- cgit v1.2.3-70-g09d2