diff options
author | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2020-10-17 20:15:23 +0200 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2020-10-23 18:17:02 -0700 |
commit | d508a16aca91b521719d929cd59db381c44cd7ad (patch) | |
tree | af076eb66fcd4002a01a29a46f8b80d4d4129994 /core | |
parent | b9b51ffd4ebd14e085bef69ee0daf6a1927cc960 (diff) | |
download | subsurface-d508a16aca91b521719d929cd59db381c44cd7ad.tar.gz |
parser: replace params[] code by new xml_params struct
This fixes a load of memory holes, and makes the code
(hopefully) more readable.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Diffstat (limited to 'core')
-rw-r--r-- | core/import-csv.c | 139 | ||||
-rw-r--r-- | core/import-csv.h | 6 | ||||
-rw-r--r-- | core/parse-xml.c | 9 | ||||
-rw-r--r-- | core/parse.h | 4 | ||||
-rw-r--r-- | core/qthelper.cpp | 70 | ||||
-rw-r--r-- | core/qthelper.h | 3 | ||||
-rw-r--r-- | core/save-xml.c | 21 |
7 files changed, 114 insertions, 138 deletions
diff --git a/core/import-csv.c b/core/import-csv.c index 09bcf9e93..9748ce5ef 100644 --- a/core/import-csv.c +++ b/core/import-csv.c @@ -13,6 +13,7 @@ #include "gettext.h" #include "import-csv.h" #include "qthelper.h" +#include "xmlparams.h" #define MATCH(buffer, pattern) \ memcmp(buffer, pattern, strlen(pattern)) @@ -103,7 +104,7 @@ static char *parse_dan_new_line(char *buf, const char *NL) } static int try_to_xslt_open_csv(const char *filename, struct memblock *mem, const char *tag); -static int parse_dan_format(const char *filename, char **params, int pnr, struct dive_table *table, +static int parse_dan_format(const char *filename, struct xml_params *params, struct dive_table *table, struct trip_table *trips, struct dive_site_table *sites, struct filter_preset_table *filter_presets) { @@ -111,9 +112,10 @@ static int parse_dan_format(const char *filename, char **params, int pnr, struct size_t end_ptr = 0; struct memblock mem, mem_csv; char tmpbuf[MAXCOLDIGITS]; + int params_orig_size = xml_params_count(params); char *ptr = NULL; - char *NL = NULL; + const char *NL = NULL; char *iter = NULL; if (readfile(filename, &mem) < 0) @@ -130,8 +132,8 @@ static int parse_dan_format(const char *filename, char **params, int pnr, struct } while ((end_ptr < mem.size) && (ptr = strstr(mem.buffer + end_ptr, "ZDH"))) { + xml_params_resize(params, params_orig_size); // restart with original parameter block char *iter_end = NULL; - unsigned int pnr_local = pnr; mem_csv.buffer = malloc(mem.size + 1); mem_csv.size = mem.size; @@ -141,8 +143,7 @@ static int parse_dan_format(const char *filename, char **params, int pnr, struct if (iter) { memcpy(tmpbuf, ptr + 4, iter - ptr - 4); tmpbuf[iter - ptr - 4] = 0; - params[pnr_local++] = "diveNro"; - params[pnr_local++] = strdup(tmpbuf); + xml_params_add(params, "diveNro", tmpbuf); } //fprintf(stderr, "DEBUG: BEGIN end_ptr %d round %d <%s>\n", end_ptr, j++, ptr); @@ -161,8 +162,7 @@ static int parse_dan_format(const char *filename, char **params, int pnr, struct /* Setting date */ memcpy(tmpbuf, iter, 8); tmpbuf[8] = 0; - params[pnr_local++] = "date"; - params[pnr_local++] = strdup(tmpbuf); + xml_params_add(params, "date", tmpbuf); /* Setting time, gotta prepend it with 1 to * avoid octal parsing (this is stripped out in @@ -170,8 +170,7 @@ static int parse_dan_format(const char *filename, char **params, int pnr, struct tmpbuf[0] = '1'; memcpy(tmpbuf + 1, iter + 8, 6); tmpbuf[7] = 0; - params[pnr_local++] = "time"; - params[pnr_local++] = strdup(tmpbuf); + xml_params_add(params, "time", tmpbuf); /* Air temperature */ memset(tmpbuf, 0, sizeof(tmpbuf)); @@ -183,11 +182,9 @@ static int parse_dan_format(const char *filename, char **params, int pnr, struct if (iter_end) { memcpy(tmpbuf, iter, iter_end - iter); - params[pnr_local++] = "airTemp"; - params[pnr_local++] = strdup(tmpbuf); + xml_params_add(params, "airTemp", tmpbuf); } } - params[pnr_local] = NULL; /* Search for the next line */ if (iter) @@ -210,12 +207,10 @@ static int parse_dan_format(const char *filename, char **params, int pnr, struct if (iter_end) { memcpy(tmpbuf, iter, iter_end - iter); - params[pnr_local++] = "waterTemp"; - params[pnr_local++] = strdup(tmpbuf); + xml_params_add(params, "waterTemp", tmpbuf); } } - params[pnr_local] = NULL; - ret |= parse_xml_buffer(filename, "<csv></csv>", 11, table, trips, sites, filter_presets, (const char **)params); + ret |= parse_xml_buffer(filename, "<csv></csv>", 11, table, trips, sites, filter_presets, params); continue; } @@ -262,29 +257,25 @@ static int parse_dan_format(const char *filename, char **params, int pnr, struct if (iter_end) { memcpy(tmpbuf, iter, iter_end - iter); - params[pnr_local++] = "waterTemp"; - params[pnr_local++] = strdup(tmpbuf); + xml_params_add(params, "waterTemp", tmpbuf); } } - params[pnr_local] = NULL; } if (try_to_xslt_open_csv(filename, &mem_csv, "csv")) return -1; - ret |= parse_xml_buffer(filename, mem_csv.buffer, mem_csv.size, table, trips, sites, filter_presets, (const char **)params); + ret |= parse_xml_buffer(filename, mem_csv.buffer, mem_csv.size, table, trips, sites, filter_presets, params); end_ptr += ptr - (char *)mem_csv.buffer; free(mem_csv.buffer); } free(mem.buffer); - for (i = 0; params[i]; i += 2) - free(params[i + 1]); return ret; } -int parse_csv_file(const char *filename, char **params, int pnr, const char *csvtemplate, +int parse_csv_file(const char *filename, struct xml_params *params, const char *csvtemplate, struct dive_table *table, struct trip_table *trips, struct dive_site_table *sites, struct filter_preset_table *filter_presets) { @@ -306,22 +297,19 @@ int parse_csv_file(const char *filename, char **params, int pnr, const char *csv mem.size = 0; if (!strcmp("DL7", csvtemplate)) { - return parse_dan_format(filename, params, pnr, table, trips, sites, filter_presets); - } else if (strcmp(params[0], "date")) { + return parse_dan_format(filename, params, table, trips, sites, filter_presets); + } else if (strcmp(xml_params_get_key(params, 0), "date")) { time(&now); timep = localtime(&now); strftime(tmpbuf, MAXCOLDIGITS, "%Y%m%d", timep); - params[pnr++] = "date"; - params[pnr++] = strdup(tmpbuf); + xml_params_add(params, "date", tmpbuf); /* As the parameter is numeric, we need to ensure that the leading zero * is not discarded during the transform, thus prepend time with 1 */ strftime(tmpbuf, MAXCOLDIGITS, "1%H%M", timep); - params[pnr++] = "time"; - params[pnr++] = strdup(tmpbuf); - params[pnr++] = NULL; + xml_params_add(params, "time", tmpbuf); } if (try_to_xslt_open_csv(filename, &mem, csvtemplate)) @@ -336,16 +324,14 @@ int parse_csv_file(const char *filename, char **params, int pnr, const char *csv #ifndef SUBSURFACE_MOBILE if (verbose >= 2) { fprintf(stderr, "(echo '<csv>'; cat %s;echo '</csv>') | xsltproc ", filename); - for (i=0; params[i]; i+=2) - fprintf(stderr, "--stringparam %s %s ", params[i], params[i+1]); + for (i = 0; i < xml_params_count(params); i++) + fprintf(stderr, "--stringparam %s %s ", xml_params_get_key(params, i), xml_params_get_value(params, i)); fprintf(stderr, "%s/xslt/%s -\n", SUBSURFACE_SOURCE, csvtemplate); } #endif - ret = parse_xml_buffer(filename, mem.buffer, mem.size, table, trips, sites, filter_presets, (const char **)params); + ret = parse_xml_buffer(filename, mem.buffer, mem.size, table, trips, sites, filter_presets, params); free(mem.buffer); - for (i = 0; params[i]; i += 2) - free(params[i + 1]); return ret; } @@ -807,24 +793,23 @@ int parse_txt_file(const char *filename, const char *csv, struct dive_table *tab #define TIMESTR 6 #define SBPARAMS 40 -static int parse_seabear_csv_file(const char *filename, char **params, int pnr, const char *csvtemplate, +static int parse_seabear_csv_file(const char *filename, struct xml_params *params, const char *csvtemplate, struct dive_table *table, struct trip_table *trips, struct dive_site_table *sites, struct filter_preset_table *filter_presets); int parse_seabear_log(const char *filename, struct dive_table *table, struct trip_table *trips, struct dive_site_table *sites, struct filter_preset_table *filter_presets) { - char *params[SBPARAMS]; - int pnr = 0; + struct xml_params *params = alloc_xml_params(); + int ret; - pnr = parse_seabear_header(filename, params, pnr); + parse_seabear_header(filename, params); + ret = parse_seabear_csv_file(filename, params, "csv", table, trips, sites, filter_presets) < 0 ? -1 : 0; - if (parse_seabear_csv_file(filename, params, pnr, "csv", table, trips, sites, filter_presets) < 0) { - return -1; - } + free_xml_params(params); - return 0; + return ret; } -static int parse_seabear_csv_file(const char *filename, char **params, int pnr, const char *csvtemplate, +static int parse_seabear_csv_file(const char *filename, struct xml_params *params, const char *csvtemplate, struct dive_table *table, struct trip_table *trips, struct dive_site_table *sites, struct filter_preset_table *filter_presets) { @@ -847,15 +832,12 @@ static int parse_seabear_csv_file(const char *filename, char **params, int pnr, timep = localtime(&now); strftime(tmpbuf, MAXCOLDIGITS, "%Y%m%d", timep); - params[pnr++] = "date"; - params[pnr++] = strdup(tmpbuf); + xml_params_add(params, "date", tmpbuf); /* As the parameter is numeric, we need to ensure that the leading zero * is not discarded during the transform, thus prepend time with 1 */ strftime(tmpbuf, MAXCOLDIGITS, "1%H%M", timep); - params[pnr++] = "time"; - params[pnr++] = strdup(tmpbuf); - + xml_params_add(params, "time", tmpbuf); if (filename == NULL) return report_error("No CSV filename"); @@ -879,8 +861,9 @@ static int parse_seabear_csv_file(const char *filename, char **params, int pnr, NL = "\n"; } ptr_old += 2; - } else + } else { ptr_old += 4; + } /* * If file does not contain empty lines, it is not a valid @@ -905,26 +888,27 @@ static int parse_seabear_csv_file(const char *filename, char **params, int pnr, */ if (ptr) { - ptr += strlen(NL) + 2; /* - * pnr is the index of NULL on the params as filled by - * the init function. The two last entries should be - * date and time. Here we overwrite them with the data - * from the CSV header. + * The two last entries should be date and time. + * Here we overwrite them with the data from the + * CSV header. */ + char buf[10]; - memcpy(params[pnr - 3], ptr, 4); - memcpy(params[pnr - 3] + 4, ptr + 5, 2); - memcpy(params[pnr - 3] + 6, ptr + 8, 2); - params[pnr - 3][8] = 0; - - memcpy(params[pnr - 1] + 1, ptr + 11, 2); - memcpy(params[pnr - 1] + 3, ptr + 14, 2); - params[pnr - 1][5] = 0; + ptr += strlen(NL) + 2; + memcpy(buf, ptr, 4); + memcpy(buf + 4, ptr + 5, 2); + memcpy(buf + 6, ptr + 8, 2); + buf[8] = 0; + xml_params_set_value(params, xml_params_count(params) - 2, buf); + + buf[0] = xml_params_get_value(params, xml_params_count(params) - 1)[0]; + memcpy(buf + 1, ptr + 11, 2); + memcpy(buf + 3, ptr + 14, 2); + buf[5] = 0; + xml_params_set_value(params, xml_params_count(params) - 1, buf); } - params[pnr++] = NULL; - /* Move the CSV data to the start of mem buffer */ memmove(mem.buffer, ptr_old, mem.size - (ptr_old - (char*)mem.buffer)); mem.size = (int)mem.size - (ptr_old - (char*)mem.buffer); @@ -940,20 +924,18 @@ static int parse_seabear_csv_file(const char *filename, char **params, int pnr, if (verbose >= 2) { fprintf(stderr, "xsltproc "); - for (i=0; params[i]; i+=2) - fprintf(stderr, "--stringparam %s %s ", params[i], params[i+1]); + for (i = 0; i < xml_params_count(params); i++) + fprintf(stderr, "--stringparam %s %s ", xml_params_get_key(params, i), xml_params_get_value(params, i)); fprintf(stderr, "xslt/csv2xml.xslt\n"); } - ret = parse_xml_buffer(filename, mem.buffer, mem.size, table, trips, sites, filter_presets, (const char **)params); + ret = parse_xml_buffer(filename, mem.buffer, mem.size, table, trips, sites, filter_presets, params); free(mem.buffer); - for (i = 0; params[i]; i += 2) - free(params[i + 1]); return ret; } -int parse_manual_file(const char *filename, char **params, int pnr, struct dive_table *table, struct trip_table *trips, +int parse_manual_file(const char *filename, struct xml_params *params, struct dive_table *table, struct trip_table *trips, struct dive_site_table *sites, struct filter_preset_table *filter_presets) { struct memblock mem; @@ -972,11 +954,8 @@ int parse_manual_file(const char *filename, char **params, int pnr, struct dive_ * is not discarded during the transform, thus prepend time with 1 */ strftime(curtime, TIMESTR, "1%H%M", timep); - params[pnr++] = strdup("date"); - params[pnr++] = strdup(curdate); - params[pnr++] = strdup("time"); - params[pnr++] = strdup(curtime); - params[pnr++] = NULL; + xml_params_add(params, "date", curdate); + xml_params_add(params, "time", curtime); if (filename == NULL) return report_error("No manual CSV filename"); @@ -988,15 +967,13 @@ int parse_manual_file(const char *filename, char **params, int pnr, struct dive_ #ifndef SUBSURFACE_MOBILE if (verbose >= 2) { fprintf(stderr, "(echo '<manualCSV>'; cat %s;echo '</manualCSV>') | xsltproc ", filename); - for (i=0; params[i]; i+=2) - fprintf(stderr, "--stringparam %s %s ", params[i], params[i+1]); + for (i = 0; i < xml_params_count(params); i++) + fprintf(stderr, "--stringparam %s %s ", xml_params_get_key(params, i), xml_params_get_value(params, i)); fprintf(stderr, "%s/xslt/manualcsv2xml.xslt -\n", SUBSURFACE_SOURCE); } #endif - ret = parse_xml_buffer(filename, mem.buffer, mem.size, table, trips, sites, filter_presets, (const char **)params); + ret = parse_xml_buffer(filename, mem.buffer, mem.size, table, trips, sites, filter_presets, params); free(mem.buffer); - for (i = 0; i < pnr - 2; ++i) - free(params[i]); return ret; } diff --git a/core/import-csv.h b/core/import-csv.h index af8836dd7..1ef4ee46b 100644 --- a/core/import-csv.h +++ b/core/import-csv.h @@ -4,6 +4,8 @@ #include "filterpreset.h" +struct xml_params; + enum csv_format { CSV_DEPTH, CSV_TEMP, @@ -23,14 +25,14 @@ enum csv_format { extern "C" { #endif -int parse_csv_file(const char *filename, char **params, int pnr, const char *csvtemplate, struct dive_table *table, +int parse_csv_file(const char *filename, struct xml_params *params, const char *csvtemplate, struct dive_table *table, struct trip_table *trips, struct dive_site_table *sites, struct filter_preset_table *filter_presets); int try_to_open_csv(struct memblock *mem, enum csv_format type, struct dive_table *table, struct trip_table *trips, struct dive_site_table *sites); int parse_txt_file(const char *filename, const char *csv, struct dive_table *table, struct trip_table *trips, struct dive_site_table *sites); int parse_seabear_log(const char *filename, struct dive_table *table, struct trip_table *trips, struct dive_site_table *sites, struct filter_preset_table *filter_presets); -int parse_manual_file(const char *filename, char **params, int pnr, struct dive_table *table, struct trip_table *trips, +int parse_manual_file(const char *filename, struct xml_params *params, struct dive_table *table, struct trip_table *trips, struct dive_site_table *sites, struct filter_preset_table *filter_presets); #ifdef __cplusplus diff --git a/core/parse-xml.c b/core/parse-xml.c index d3dda0c43..d5bbf7ae0 100644 --- a/core/parse-xml.c +++ b/core/parse-xml.c @@ -32,11 +32,12 @@ #include "picture.h" #include "qthelper.h" #include "tag.h" +#include "xmlparams.h" int quit, force_root; int last_xml_version = -1; -static xmlDoc *test_xslt_transforms(xmlDoc *doc, const char **params); +static xmlDoc *test_xslt_transforms(xmlDoc *doc, const struct xml_params *params); const struct units SI_units = SI_UNITS; const struct units IMPERIAL_units = IMPERIAL_UNITS; @@ -1721,7 +1722,7 @@ static const char *preprocess_divelog_de(const char *buffer) int parse_xml_buffer(const char *url, const char *buffer, int size, struct dive_table *table, struct trip_table *trips, struct dive_site_table *sites, - struct filter_preset_table *filter_presets, const char **params) + struct filter_preset_table *filter_presets, const struct xml_params *params) { UNUSED(size); xmlDoc *doc; @@ -2308,7 +2309,7 @@ static struct xslt_files { { NULL, } }; -static xmlDoc *test_xslt_transforms(xmlDoc *doc, const char **params) +static xmlDoc *test_xslt_transforms(xmlDoc *doc, const struct xml_params *params) { struct xslt_files *info = xslt_files; xmlDoc *transformed; @@ -2341,7 +2342,7 @@ static xmlDoc *test_xslt_transforms(xmlDoc *doc, const char **params) report_error(translate("gettextFromC", "Can't open stylesheet %s"), info->file); return doc; } - transformed = xsltApplyStylesheet(xslt, doc, params); + transformed = xsltApplyStylesheet(xslt, doc, xml_params_get(params)); xmlFreeDoc(doc); xsltFreeStylesheet(xslt); diff --git a/core/parse.h b/core/parse.h index f3d4039e3..3b4efe1d8 100644 --- a/core/parse.h +++ b/core/parse.h @@ -9,6 +9,8 @@ #include <sqlite3.h> +struct xml_params; + typedef union { struct event event; char allocation[sizeof(struct event) + MAX_EVENT_NAME]; @@ -140,7 +142,7 @@ int atoi_n(char *ptr, unsigned int len); void parse_xml_init(void); int parse_xml_buffer(const char *url, const char *buf, int size, struct dive_table *table, struct trip_table *trips, struct dive_site_table *sites, - struct filter_preset_table *filter_presets, const char **params); + struct filter_preset_table *filter_presets, const struct xml_params *params); void parse_xml_exit(void); int parse_dm4_buffer(sqlite3 *handle, const char *url, const char *buf, int size, struct dive_table *table, struct trip_table *trips, struct dive_site_table *sites); int parse_dm5_buffer(sqlite3 *handle, const char *url, const char *buf, int size, struct dive_table *table, struct trip_table *trips, struct dive_site_table *sites); diff --git a/core/qthelper.cpp b/core/qthelper.cpp index 7540045b3..a4187747b 100644 --- a/core/qthelper.cpp +++ b/core/qthelper.cpp @@ -23,6 +23,7 @@ #include "tag.h" #include "trip.h" #include "imagedownloader.h" +#include "xmlparams.h" #include <QFile> #include <QRegExp> #include <QDir> @@ -1474,7 +1475,7 @@ QString getUUID() return uuidString; } -int parse_seabear_header(const char *filename, char **params, int pnr) +void parse_seabear_header(const char *filename, struct xml_params *params) { QFile f(filename); @@ -1487,8 +1488,8 @@ int parse_seabear_header(const char *filename, char **params, int pnr) while ((parseLine = f.readLine().trimmed()).length() > 0 && !f.atEnd()) { if (parseLine.contains("//DIVE NR: ")) { - params[pnr++] = strdup("diveNro"); - params[pnr++] = copy_qstring(parseLine.replace(QString::fromLatin1("//DIVE NR: "), QString::fromLatin1(""))); + xml_params_add(params, "diveNro", + qPrintable(parseLine.replace(QString::fromLatin1("//DIVE NR: "), QString::fromLatin1("")))); break; } } @@ -1502,8 +1503,9 @@ int parse_seabear_header(const char *filename, char **params, int pnr) while ((parseLine = f.readLine().trimmed()).length() > 0 && !f.atEnd()) { if (parseLine.contains("//Hardware Version: ")) { - params[pnr++] = strdup("hw"); - params[pnr++] = copy_qstring(parseLine.replace(QString::fromLatin1("//Hardware Version: "), QString::fromLatin1("\"Seabear ")).trimmed().append("\"")); + xml_params_add(params, "hw", + qPrintable(parseLine.replace(QString::fromLatin1("//Hardware Version: "), + QString::fromLatin1("\"Seabear ")).trimmed().append("\""))); break; } } @@ -1515,8 +1517,8 @@ int parse_seabear_header(const char *filename, char **params, int pnr) while ((parseLine = f.readLine().trimmed()).length() > 0 && !f.atEnd()) { if (parseLine.contains("//Log interval: ")) { - params[pnr++] = strdup("delta"); - params[pnr++] = copy_qstring(parseLine.remove(QString::fromLatin1("//Log interval: ")).trimmed().remove(QString::fromLatin1(" s"))); + xml_params_add(params, "delta", + qPrintable(parseLine.remove(QString::fromLatin1("//Log interval: ")).trimmed().remove(QString::fromLatin1(" s")))); break; } } @@ -1530,8 +1532,8 @@ int parse_seabear_header(const char *filename, char **params, int pnr) while ((parseLine = f.readLine().trimmed()).length() > 0 && !f.atEnd()) { QString needle = "//Mode: "; if (parseLine.contains(needle)) { - params[pnr++] = strdup("diveMode"); - params[pnr++] = copy_qstring(parseLine.replace(needle, QString::fromLatin1("")).prepend("\"").append("\"")); + xml_params_add(params, "diveMode", + qPrintable(parseLine.replace(needle, QString::fromLatin1("")).prepend("\"").append("\""))); } } f.seek(0); @@ -1543,8 +1545,8 @@ int parse_seabear_header(const char *filename, char **params, int pnr) while ((parseLine = f.readLine().trimmed()).length() > 0 && !f.atEnd()) { QString needle = "//Firmware Version: "; if (parseLine.contains(needle)) { - params[pnr++] = strdup("Firmware"); - params[pnr++] = copy_qstring(parseLine.replace(needle, QString::fromLatin1("")).prepend("\"").append("\"")); + xml_params_add(params, "Firmware", + qPrintable(parseLine.replace(needle, QString::fromLatin1("")).prepend("\"").append("\""))); } } f.seek(0); @@ -1552,8 +1554,8 @@ int parse_seabear_header(const char *filename, char **params, int pnr) while ((parseLine = f.readLine().trimmed()).length() > 0 && !f.atEnd()) { QString needle = "//Serial number: "; if (parseLine.contains(needle)) { - params[pnr++] = strdup("Serial"); - params[pnr++] = copy_qstring(parseLine.replace(needle, QString::fromLatin1("")).prepend("\"").append("\"")); + xml_params_add(params, "Serial", + qPrintable(parseLine.replace(needle, QString::fromLatin1("")).prepend("\"").append("\""))); } } f.seek(0); @@ -1561,8 +1563,8 @@ int parse_seabear_header(const char *filename, char **params, int pnr) while ((parseLine = f.readLine().trimmed()).length() > 0 && !f.atEnd()) { QString needle = "//GF: "; if (parseLine.contains(needle)) { - params[pnr++] = strdup("GF"); - params[pnr++] = copy_qstring(parseLine.replace(needle, QString::fromLatin1("")).prepend("\"").append("\"")); + xml_params_add(params, "GF", + qPrintable(parseLine.replace(needle, QString::fromLatin1("")).prepend("\"").append("\""))); } } f.seek(0); @@ -1580,36 +1582,26 @@ int parse_seabear_header(const char *filename, char **params, int pnr) unsigned short index = 0; for (const QString &columnText: currColumns) { if (columnText == "Time") { - params[pnr++] = strdup("timeField"); - params[pnr++] = intdup(index++); + xml_params_add_int(params, "timeField", index++); } else if (columnText == "Depth") { - params[pnr++] = strdup("depthField"); - params[pnr++] = intdup(index++); + xml_params_add_int(params, "depthField", index++); } else if (columnText == "Temperature") { - params[pnr++] = strdup("tempField"); - params[pnr++] = intdup(index++); + xml_params_add_int(params, "tempField", index++); } else if (columnText == "NDT") { - params[pnr++] = strdup("ndlField"); - params[pnr++] = intdup(index++); + xml_params_add_int(params, "ndlField", index++); } else if (columnText == "TTS") { - params[pnr++] = strdup("ttsField"); - params[pnr++] = intdup(index++); + xml_params_add_int(params, "ttsField", index++); } else if (columnText == "pO2_1") { - params[pnr++] = strdup("o2sensor1Field"); - params[pnr++] = intdup(index++); + xml_params_add_int(params, "o2sensor1Field", index++); } else if (columnText == "pO2_2") { - params[pnr++] = strdup("o2sensor2Field"); - params[pnr++] = intdup(index++); + xml_params_add_int(params, "o2sensor2Field", index++); } else if (columnText == "pO2_3") { - params[pnr++] = strdup("o2sensor3Field"); - params[pnr++] = intdup(index++); + xml_params_add_int(params, "o2sensor3Field", index++); } else if (columnText == "Ceiling") { /* TODO: Add support for dive computer reported ceiling*/ - params[pnr++] = strdup("ceilingField"); - params[pnr++] = intdup(index++); + xml_params_add_int(params, "ceilingField", index++); } else if (columnText == "Tank pressure") { - params[pnr++] = strdup("pressureField"); - params[pnr++] = intdup(index++); + xml_params_add_int(params, "pressureField", index++); } else { // We do not know about this value qDebug() << "Seabear import found an un-handled field: " << columnText; @@ -1617,16 +1609,12 @@ int parse_seabear_header(const char *filename, char **params, int pnr) } /* Separator is ';' and the index for that in DiveLogImportDialog constructor is 2 */ - params[pnr++] = strdup("separatorIndex"); - params[pnr++] = intdup(2); + xml_params_add_int(params, "separatorIndex", 2); /* And metric units */ - params[pnr++] = strdup("units"); - params[pnr++] = intdup(0); + xml_params_add_int(params, "units", 0); - params[pnr] = NULL; f.close(); - return pnr; } char *intdup(int index) diff --git a/core/qthelper.h b/core/qthelper.h index 9ea51f312..4e4b300a7 100644 --- a/core/qthelper.h +++ b/core/qthelper.h @@ -9,6 +9,7 @@ struct picture; struct dive_trip; +struct xml_params; // 1) Types @@ -148,7 +149,7 @@ char *hashfile_name_string(); char *picturedir_string(); const char *subsurface_user_agent(); enum deco_mode decoMode(); -int parse_seabear_header(const char *filename, char **params, int pnr); +void parse_seabear_header(const char *filename, struct xml_params *params); char *get_current_date(); time_t get_dive_datetime_from_isostring(char *when); void print_qt_versions(); diff --git a/core/save-xml.c b/core/save-xml.c index 6da9a7528..812201e48 100644 --- a/core/save-xml.c +++ b/core/save-xml.c @@ -29,6 +29,7 @@ #include "qthelper.h" #include "gettext.h" #include "tag.h" +#include "xmlparams.h" /* * We're outputting utf8 in xml. @@ -834,17 +835,24 @@ int save_dives_logic(const char *filename, const bool select_only, bool anonymiz return error; } + +static int export_dives_xslt_doit(const char *filename, struct xml_params *params, bool selected, int units, const char *export_xslt, bool anonymize); int export_dives_xslt(const char *filename, const bool selected, const int units, const char *export_xslt, bool anonymize) { + struct xml_params *params = alloc_xml_params(); + int ret = export_dives_xslt_doit(filename, params, selected, units, export_xslt, anonymize); + free_xml_params(params); + return ret; +} + +static int export_dives_xslt_doit(const char *filename, struct xml_params *params, bool selected, int units, const char *export_xslt, bool anonymize) +{ FILE *f; struct membuffer buf = { 0 }; xmlDoc *doc; xsltStylesheetPtr xslt = NULL; xmlDoc *transformed; int res = 0; - char *params[3]; - int pnr = 0; - char unitstr[3]; if (verbose) fprintf(stderr, "export_dives_xslt with stylesheet %s\n", export_xslt); @@ -870,12 +878,9 @@ int export_dives_xslt(const char *filename, const bool selected, const int units if (!xslt) return report_error("Failed to open export conversion stylesheet"); - snprintf(unitstr, 3, "%d", units); - params[pnr++] = "units"; - params[pnr++] = unitstr; - params[pnr++] = NULL; + xml_params_add_int(params, "units", units); - transformed = xsltApplyStylesheet(xslt, doc, (const char **)params); + transformed = xsltApplyStylesheet(xslt, doc, xml_params_get(params)); xmlFreeDoc(doc); /* Write the transformed export to file */ |