forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.hpp
94 lines (79 loc) · 2.03 KB
/
util.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/**
* @file
*
* @brief Utility functions used in the xerces plugin
*
* Provides unique pointers that act as a bridge between XMLCh and std::string
* Provides a general XercesPluginException
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*/
#ifndef ELEKTRA_PLUGIN_XERCES_UTIL_H
#define ELEKTRA_PLUGIN_XERCES_UTIL_H
#include <memory>
#include <xercesc/util/TransService.hpp>
#include <xercesc/util/XMLString.hpp>
namespace xerces
{
template <typename T>
struct XercesDeleter
{
void operator() (T * ptr) const
{
ptr->release ();
}
};
template <>
struct XercesDeleter<XMLCh>
{
void operator() (XMLCh * ptr) const
{
XERCES_CPP_NAMESPACE::XMLString::release (&ptr);
}
};
template <>
struct XercesDeleter<char>
{
void operator() (char * ptr) const
{
XERCES_CPP_NAMESPACE::XMLString::release (&ptr);
}
};
template <typename T>
using XercesPtr = std::unique_ptr<T, XercesDeleter<T>>;
inline XercesPtr<XMLCh> toXMLCh (std::string const & str)
{
// XMLByte required by TranscodeFromStr is an unsigned char * but basically they can be used equivalently
return XercesPtr<XMLCh> (
XERCES_CPP_NAMESPACE::TranscodeFromStr (reinterpret_cast<const XMLByte *> (str.c_str ()), str.size (), "UTF-8").adopt ());
}
inline XercesPtr<char> toCStr (XMLCh const * xmlCh)
{
// XMLByte returned by TranscodeToStr is an unsigned char * but basically they can be used equivalently
return XercesPtr<char> (reinterpret_cast<char *> (XERCES_CPP_NAMESPACE::TranscodeToStr (xmlCh, "UTF-8").adopt ()));
}
inline std::string toStr (XMLCh const * xmlCh)
{
return std::string (toCStr (xmlCh).get ());
}
#define asXMLCh(str) toXMLCh (str).get ()
#define asCStr(str) toCStr (str).get ()
class XercesPluginException : public std::exception
{
public:
explicit XercesPluginException (std::string const & m) : msg (m)
{
}
~XercesPluginException () throw ()
{
}
const char * what () const throw ()
{
return msg.c_str ();
}
private:
std::string msg;
};
} // namespace xerces
#define ELEKTRA_XERCES_ORIGINAL_ROOT_NAME "xerces/rootname"
#endif