This repository has been archived by the owner on Oct 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 123
/
directoryvalue.cpp
156 lines (130 loc) · 3.83 KB
/
directoryvalue.cpp
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/**
* @file
*
* @brief Source for directoryvalue plugin
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*
*/
#include <kdberrors.h>
#include <kdbplugin.hpp>
#include "directoryvalue.hpp"
#include "directoryvalue_delegate.hpp"
#include "log.hpp"
using std::exception;
using std::range_error;
using ckdb::keyNew;
using ckdb::Plugin;
using elektra::DirectoryValueDelegate;
namespace
{
/**
* @brief This function returns a key set containing the plugin contract.
*
* @return A key set specifying the capabilities of the plugin
*/
kdb::KeySet getContract ()
{
return kdb::KeySet{ 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" {
using delegator = Delegator<DirectoryValueDelegate>;
/** @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)
{
kdb::KeySet keys{ returned };
kdb::Key parent{ parentKey };
if (parent.getName () == "system:/elektra/modules/directoryvalue")
{
keys.append (getContract ());
parent.release ();
keys.release ();
return ELEKTRA_PLUGIN_STATUS_SUCCESS;
}
#ifdef HAVE_LOGGER
ELEKTRA_LOG_DEBUG ("Convert keys:");
logKeySet (keys);
#endif
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
ELEKTRA_LOG_DEBUG ("Converted keys:");
logKeySet (keys);
#endif
parent.release ();
keys.release ();
return status;
}
/** @see elektraDocSet */
int elektraDirectoryValueSet (Plugin * handle, KeySet * returned, Key * parentKey)
{
kdb::KeySet keys{ returned };
kdb::Key parent{ parentKey };
int status = ELEKTRA_PLUGIN_STATUS_ERROR;
#ifdef HAVE_LOGGER
ELEKTRA_LOG_DEBUG ("Convert keys:");
logKeySet (keys);
#endif
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 ());
}
#ifdef HAVE_LOGGER
ELEKTRA_LOG_DEBUG ("Converted keys:");
logKeySet (keys);
#endif
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"