forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdirectoryvalue.cpp
More file actions
141 lines (119 loc) · 3.64 KB
/
Copy pathdirectoryvalue.cpp
File metadata and controls
141 lines (119 loc) · 3.64 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/**
* @file
*
* @brief Source for directoryvalue plugin
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*
*/
#include "directoryvalue.hpp"
#include <kdbhelper.h>
using ckdb::keyNew;
using std::exception;
using std::range_error;
using elektra::DirectoryValueDelegate;
using CppKey = kdb::Key;
using CppKeySet = kdb::KeySet;
namespace
{
/**
* @brief This function returns a key set containing the plugin contract.
*
* @return A key set specifying the capabilities of the plugin
*/
CppKeySet getContract ()
{
return CppKeySet{ 30,
keyNew ("system/elektra/modules/directoryvalue", KEY_VALUE, "directoryvalue plugin waits for your orders",
KEY_END),
keyNew ("system/elektra/modules/directoryvalue/exports", KEY_END),
keyNew ("system/elektra/modules/directoryvalue/exports/open", KEY_FUNC, elektraDirectoryValueOpen, KEY_END),
keyNew ("system/elektra/modules/directoryvalue/exports/close", KEY_FUNC, elektraDirectoryValueClose, KEY_END),
keyNew ("system/elektra/modules/directoryvalue/exports/get", KEY_FUNC, elektraDirectoryValueGet, KEY_END),
keyNew ("system/elektra/modules/directoryvalue/exports/set", KEY_FUNC, elektraDirectoryValueSet, KEY_END),
#include ELEKTRA_README
keyNew ("system/elektra/modules/directoryvalue/infos/version", KEY_VALUE, PLUGINVERSION, KEY_END),
KS_END };
}
} // end namespace
extern "C" {
typedef Delegator<DirectoryValueDelegate> delegator;
/** @see elektraDocOpen */
int elektraDirectoryValueOpen (Plugin * handle, Key * key)
{
return delegator::open (handle, key);
}
/** @see elektraDocClose */
int elektraDirectoryValueClose (Plugin * handle, Key * key)
{
return delegator::close (handle, key);
}
/** @see elektraDocGet */
int elektraDirectoryValueGet (Plugin * handle, KeySet * returned, Key * parentKey)
{
CppKeySet keys{ returned };
CppKey parent{ parentKey };
if (parent.getName () == "system/elektra/modules/directoryvalue")
{
keys.append (getContract ());
parent.release ();
keys.release ();
return ELEKTRA_PLUGIN_STATUS_SUCCESS;
}
int status = ELEKTRA_PLUGIN_STATUS_ERROR;
try
{
status = delegator::get (handle)->convertToDirectories (keys);
}
catch (range_error const & error)
{
ELEKTRA_SET_VALIDATION_SYNTACTIC_ERRORF (*parent, "Unable to insert array value %s", error.what ());
}
catch (exception const & error)
{
ELEKTRA_SET_PLUGIN_MISBEHAVIOR_ERRORF (*parent, "Uncaught Exception: %s", error.what ());
}
#ifdef HAVE_LOGGER
for (auto key : keys)
{
ELEKTRA_LOG_DEBUG ("\t“%s”: “%s”", key.getName ().c_str (),
key.getBinarySize () == 0 ? "NULL" : key.isBinary () ? "binary value!" : key.getString ().c_str ());
}
#endif
parent.release ();
keys.release ();
return status;
}
/** @see elektraDocSet */
int elektraDirectoryValueSet (Plugin * handle, KeySet * returned, Key * parentKey)
{
CppKeySet keys{ returned };
CppKey parent{ parentKey };
int status = ELEKTRA_PLUGIN_STATUS_ERROR;
try
{
status = delegator::get (handle)->convertToLeaves (keys);
}
catch (range_error const & error)
{
ELEKTRA_SET_VALIDATION_SYNTACTIC_ERRORF (*parent, "Unable to insert array value %s", error.what ());
}
catch (exception const & error)
{
ELEKTRA_SET_PLUGIN_MISBEHAVIOR_ERRORF (*parent, "Uncaught exception: %s", error.what ());
}
parent.release ();
keys.release ();
return status;
}
Plugin * ELEKTRA_PLUGIN_EXPORT
{
// clang-format off
return elektraPluginExport ("directoryvalue",
ELEKTRA_PLUGIN_OPEN, &elektraDirectoryValueOpen,
ELEKTRA_PLUGIN_CLOSE, &elektraDirectoryValueClose,
ELEKTRA_PLUGIN_GET, &elektraDirectoryValueGet,
ELEKTRA_PLUGIN_SET, &elektraDirectoryValueSet,
ELEKTRA_PLUGIN_END);
}
} // end extern "C"