subsurface/core/xmlparams.h
Berthold Stoeger b9b51ffd4e 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>
2020-10-23 18:17:02 -07:00

40 lines
1.4 KiB
C++

// 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 <string>
#include <vector>
struct xml_params {
std::vector<std::pair<std::string, std::string>> items;
mutable std::vector<const char *> 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