forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathccode.cpp
105 lines (86 loc) · 2.76 KB
/
ccode.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
/**
* @file
*
* @brief
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*/
#include "ccode.hpp"
#include "coder.hpp"
#include <kdberrors.h>
#include <kdbplugin.hpp>
using ckdb::keyNew;
using elektra::Coder;
using CppKeySet = kdb::KeySet;
namespace
{
typedef Delegator<Coder> coderDelegator;
/**
* @brief This function returns a key set containing the contract of this plugin.
*
* @return A contract describing the functionality of this plugin.
*/
inline KeySet * contract (void)
{
return ksNew (30, keyNew ("system:/elektra/modules/ccode", KEY_VALUE, "ccode plugin waits for your orders", KEY_END),
keyNew ("system:/elektra/modules/ccode/exports", KEY_END),
keyNew ("system:/elektra/modules/ccode/exports/open", KEY_FUNC, elektraCcodeOpen, KEY_END),
keyNew ("system:/elektra/modules/ccode/exports/close", KEY_FUNC, elektraCcodeClose, KEY_END),
keyNew ("system:/elektra/modules/ccode/exports/get", KEY_FUNC, elektraCcodeGet, KEY_END),
keyNew ("system:/elektra/modules/ccode/exports/set", KEY_FUNC, elektraCcodeSet, KEY_END),
#include "readme_ccode.c"
keyNew ("system:/elektra/modules/ccode/infos/version", KEY_VALUE, PLUGINVERSION, KEY_END), KS_END);
}
} // end namespace
extern "C" {
// ====================
// = Plugin Interface =
// ====================
/** @see elektraDocOpen */
int elektraCcodeOpen (Plugin * handle, Key * key)
{
return coderDelegator::open (handle, key);
}
/** @see elektraDocClose */
int elektraCcodeClose (Plugin * handle ELEKTRA_UNUSED, Key * key)
{
return coderDelegator::close (handle, key);
}
/** @see elektraDocGet */
int elektraCcodeGet (Plugin * handle, KeySet * returned, Key * parentKey)
{
if (!strcmp (keyName (parentKey), "system:/elektra/modules/ccode"))
{
KeySet * const pluginConfig = contract ();
ksAppend (returned, pluginConfig);
ksDel (pluginConfig);
return ELEKTRA_PLUGIN_STATUS_SUCCESS;
}
CppKeySet keys{ returned };
CppKeySet decoded = coderDelegator::get (handle)->decodeKeySet (keys);
keys.release ();
ksCopy (returned, decoded.getKeySet ());
ksDel (decoded.release ());
return ELEKTRA_PLUGIN_STATUS_SUCCESS;
}
/** @see elektraDocSet */
int elektraCcodeSet (Plugin * handle, KeySet * returned, Key * parentKey ELEKTRA_UNUSED)
{
CppKeySet keys{ returned };
CppKeySet encoded = coderDelegator::get (handle)->encodeKeySet (keys);
keys.release ();
ksCopy (returned, encoded.getKeySet ());
ksDel (encoded.release ());
return ELEKTRA_PLUGIN_STATUS_SUCCESS;
}
Plugin * ELEKTRA_PLUGIN_EXPORT
{
// clang-format off
return elektraPluginExport("ccode",
ELEKTRA_PLUGIN_OPEN, &elektraCcodeOpen,
ELEKTRA_PLUGIN_CLOSE, &elektraCcodeClose,
ELEKTRA_PLUGIN_GET, &elektraCcodeGet,
ELEKTRA_PLUGIN_SET, &elektraCcodeSet,
ELEKTRA_PLUGIN_END);
}
} // end extern "C"