From b9b51ffd4ebd14e085bef69ee0daf6a1927cc960 Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Sat, 17 Oct 2020 16:29:32 +0200 Subject: core: add a small helper-struct that keeps track of xml-parameters The XML-parameter code is a mess. Ownership is unclear. Allocation and freeing of strings is in different functions. Sometimes only every second string is free()d, because keys are not copied. But this is done inconsistently. The caller has to know how many parameters the callee may add. Instead, let's add a small helper-struct that uses C++ memory management, but exports a C-API. The array for the XML-library is generated on the fly. This is only the implementation, the old code is not yet replaced. Signed-off-by: Berthold Stoeger --- core/CMakeLists.txt | 2 ++ core/xmlparams.cpp | 62 +++++++++++++++++++++++++++++++++++++ core/xmlparams.h | 40 ++++++++++++++++++++++++ packaging/ios/Subsurface-mobile.pro | 2 ++ 4 files changed, 106 insertions(+) create mode 100644 core/xmlparams.cpp create mode 100644 core/xmlparams.h diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt index caaa2b74c..079d1a1fe 100644 --- a/core/CMakeLists.txt +++ b/core/CMakeLists.txt @@ -189,6 +189,8 @@ set(SUBSURFACE_CORE_LIB_SRCS worldmap-options.h worldmap-save.c worldmap-save.h + xmlparams.cpp + xmlparams.h xmp_parser.cpp xmp_parser.h diff --git a/core/xmlparams.cpp b/core/xmlparams.cpp new file mode 100644 index 000000000..a12739a4d --- /dev/null +++ b/core/xmlparams.cpp @@ -0,0 +1,62 @@ +// SPDX-License-Identifier: GPL-2.0 +#include "xmlparams.h" + +extern "C" struct xml_params *alloc_xml_params() +{ + return new xml_params; +} + +extern "C" void free_xml_params(struct xml_params *params) +{ + delete params; +} + +extern "C" void xml_params_resize(struct xml_params *params, int count) +{ + params->items.resize(count); +} + +extern "C" void xml_params_add(struct xml_params *params, const char *key, const char *value) +{ + params->items.push_back({ std::string(key), std::string(value) }); +} + +extern "C" void xml_params_add_int(struct xml_params *params, const char *key, int value) +{ + params->items.push_back({ std::string(key), std::to_string(value) }); +} + +extern "C" int xml_params_count(const struct xml_params *params) +{ + return (int)params->items.size(); +} + +extern "C" const char *xml_params_get_key(const struct xml_params *params, int idx) +{ + return params->items[idx].first.c_str(); +} + +extern "C" const char *xml_params_get_value(const struct xml_params *params, int idx) +{ + return params->items[idx].second.c_str(); +} + +extern void xml_params_set_value(struct xml_params *params, int idx, const char *value) +{ + if (idx < 0 || idx >= (int)params->items.size()) + return; + params->items[idx].second = value; +} + +extern "C" const char **xml_params_get(const struct xml_params *params) +{ + if (!params) + return nullptr; + params->data.resize(params->items.size() * 2 + 1); + for (size_t i = 0; i < params->items.size(); ++i) { + params->data[i * 2] = params->items[i].first.c_str(); + params->data[i * 2 + 1] = params->items[i].second.c_str(); + } + params->data[params->items.size() * 2] = nullptr; + return params->data.data(); +} diff --git a/core/xmlparams.h b/core/xmlparams.h new file mode 100644 index 000000000..38e175935 --- /dev/null +++ b/core/xmlparams.h @@ -0,0 +1,40 @@ +// SPDX-License-Identifier: GPL-2.0 +// Small helper class that keeps track of key/value pairs to +// pass to the XML-routines as parameters. Uses C++ for memory +// management, but provides a C interface via anonymous struct. + +#ifdef __cplusplus +#include +#include + +struct xml_params { + std::vector> items; + mutable std::vector data; +}; + +#else + +struct xml_params; + +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +// Return values marked as "not stable" may be invalidated when calling +// an xml_params_*() function that takes a non-const xml_params parameter. +extern struct xml_params *alloc_xml_params(); +extern void free_xml_params(struct xml_params *params); +extern void xml_params_resize(struct xml_params *params, int count); +extern void xml_params_add(struct xml_params *params, const char *key, const char *value); +extern void xml_params_add_int(struct xml_params *params, const char *key, int value); +extern int xml_params_count(const struct xml_params *params); +extern const char *xml_params_get_key(const struct xml_params *params, int idx); // not stable +extern const char *xml_params_get_value(const struct xml_params *params, int idx); // not stable +extern void xml_params_set_value(struct xml_params *params, int idx, const char *value); +extern const char **xml_params_get(const struct xml_params *params); // not stable + +#ifdef __cplusplus +} +#endif diff --git a/packaging/ios/Subsurface-mobile.pro b/packaging/ios/Subsurface-mobile.pro index c83ebb57e..7a6498ebd 100644 --- a/packaging/ios/Subsurface-mobile.pro +++ b/packaging/ios/Subsurface-mobile.pro @@ -104,6 +104,7 @@ SOURCES += ../../subsurface-mobile-main.cpp \ ../../core/uploadDiveShare.cpp \ ../../core/uploadDiveLogsDE.cpp \ ../../core/save-profiledata.c \ + ../../core/xmlparams.cpp \ ../../core/settings/qPref.cpp \ ../../core/settings/qPrefCloudStorage.cpp \ ../../core/settings/qPrefDisplay.cpp \ @@ -255,6 +256,7 @@ HEADERS += \ ../../core/save-profiledata.h \ ../../core/uploadDiveShare.h \ ../../core/uploadDiveLogsDE.h \ + ../../core/xmlparams.h \ ../../core/settings/qPref.h \ ../../core/settings/qPrefCloudStorage.h \ ../../core/settings/qPrefDisplay.h \ -- cgit v1.2.3-70-g09d2