diff options
author | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2020-10-17 16:29:32 +0200 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2020-10-23 18:17:02 -0700 |
commit | b9b51ffd4ebd14e085bef69ee0daf6a1927cc960 (patch) | |
tree | 78549b41ebf8dd7cfc707d220a10d578c5a7873c /core/xmlparams.cpp | |
parent | ac3042b48bf8ee3a8f69a0384c5124c512108b11 (diff) | |
download | subsurface-b9b51ffd4ebd14e085bef69ee0daf6a1927cc960.tar.gz |
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 <bstoeger@mail.tuwien.ac.at>
Diffstat (limited to 'core/xmlparams.cpp')
-rw-r--r-- | core/xmlparams.cpp | 62 |
1 files changed, 62 insertions, 0 deletions
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(); +} |