forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcl.cpp
119 lines (100 loc) · 3 KB
/
tcl.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
/**
* @file
*
* @brief
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*/
#include "tcl.hpp"
#include <key.hpp>
#include <keyset.hpp>
#include <fstream>
#include "errno.h"
#include <boost/spirit/include/qi_expect.hpp>
#include <boost/spirit/include/support_istream_iterator.hpp>
using namespace ckdb;
#include <kdberrors.h>
extern "C" {
int elektraTclGet (Plugin *, KeySet * returned, Key * parentKey)
{
kdb::Key parent (parentKey);
if (parent.getName () == "system/elektra/modules/tcl")
{
/* get config */
KeySet * n;
ksAppend (returned,
n = ksNew (30, keyNew ("system/elektra/modules/tcl", KEY_VALUE, "tcl plugin waits for your orders", KEY_END),
keyNew ("system/elektra/modules/tcl/exports", KEY_END),
keyNew ("system/elektra/modules/tcl/exports/get", KEY_FUNC, elektraTclGet, KEY_END),
keyNew ("system/elektra/modules/tcl/exports/set", KEY_FUNC, elektraTclSet, KEY_END),
keyNew ("system/elektra/modules/tcl/exports/cpp_serialise", KEY_SIZE, sizeof (&elektra::serialise),
KEY_BINARY, KEY_VALUE, &elektra::serialise, KEY_END),
keyNew ("system/elektra/modules/tcl/exports/cpp_unserialise", KEY_SIZE, sizeof (&elektra::unserialise),
KEY_BINARY, KEY_VALUE, &elektra::unserialise, KEY_END),
#include "readme_tcl.c"
keyNew ("system/elektra/modules/tcl/infos/version", KEY_VALUE, PLUGINVERSION, KEY_END), KS_END));
ksDel (n);
return ELEKTRA_PLUGIN_STATUS_SUCCESS;
}
/* get all keys */
int errnosave = errno;
std::ifstream in (parent.getString (), std::ios::binary); // we get our input from this file
if (!in.is_open ())
{
ELEKTRA_SET_ERROR_GET (*parent);
errno = errnosave;
parent.release ();
return -1;
}
kdb::KeySet input (returned);
int ret = 0;
try
{
elektra::unserialise (in, input, parent);
}
catch (boost::spirit::qi::expectation_failure<boost::spirit::istream_iterator> const & e)
{
ELEKTRA_SET_ERROR (61, *parent,
std::string (std::string ("file: ") + parent.getString () +
" could not be parsed because: " + std::string (e.first, e.last))
.c_str ());
ret = -1;
}
catch (std::exception const & e)
{
ELEKTRA_SET_ERROR (
61, *parent,
std::string (std::string ("file: ") + parent.getString () + " could not be parsed because: " + e.what ()).c_str ());
ret = -1;
}
input.release ();
parent.release ();
return ret;
}
int elektraTclSet (Plugin *, KeySet * returned, Key * parentKey)
{
/* set all keys */
int errnosave = errno;
kdb::Key parent (parentKey);
std::ofstream ofs (parent.getString (), std::ios::binary);
if (!ofs.is_open ())
{
ELEKTRA_SET_ERROR_SET (*parent);
errno = errnosave;
return -1;
}
kdb::KeySet output (returned);
elektra::serialise (ofs, output, parent);
parent.release ();
output.release ();
return 1; /* success */
}
Plugin * ELEKTRA_PLUGIN_EXPORT
{
// clang-format off
return elektraPluginExport("tcl",
ELEKTRA_PLUGIN_GET, &elektraTclGet,
ELEKTRA_PLUGIN_SET, &elektraTclSet,
ELEKTRA_PLUGIN_END);
}
} // end extern "C"