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
/
xmltool.c
84 lines (70 loc) · 2.13 KB
/
xmltool.c
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
/**
* @file
*
* @brief
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*/
#include "xmltool.h"
#include "kdbtools.h"
#include <errno.h>
#include "kdbconfig.h"
#include "kdberrors.h"
#include <stdio.h>
#include <string.h>
int elektraXmltoolGet (Plugin * handle ELEKTRA_UNUSED, KeySet * returned, Key * parentKey)
{
if (!strcmp (keyName (parentKey), "system:/elektra/modules/xmltool"))
{
KeySet * moduleConfig =
ksNew (30, keyNew ("system:/elektra/modules/xmltool", KEY_VALUE, "xmltool plugin waits for your orders", KEY_END),
keyNew ("system:/elektra/modules/xmltool/exports", KEY_END),
keyNew ("system:/elektra/modules/xmltool/exports/get", KEY_FUNC, elektraXmltoolGet, KEY_END),
keyNew ("system:/elektra/modules/xmltool/exports/set", KEY_FUNC, elektraXmltoolSet, KEY_END),
keyNew ("system:/elektra/modules/xmltool/exports/ksFromXML", KEY_FUNC, ksFromXMLfile, KEY_END),
keyNew ("system:/elektra/modules/xmltool/exports/ksToStream", KEY_FUNC, ksToStream, KEY_END),
#include "readme_xmltool.c"
keyNew ("system:/elektra/modules/xmltool/infos/version", KEY_VALUE, PLUGINVERSION, KEY_END), KS_END);
ksAppend (returned, moduleConfig);
ksDel (moduleConfig);
return 1;
}
/* get all keys */
int errnosave = errno;
if (ksFromXMLfile (returned, keyString (parentKey)) == -1)
{
ELEKTRA_SET_ERROR_GET (parentKey);
errno = errnosave;
return -1;
}
return 1;
}
int elektraXmltoolSet (Plugin * handle ELEKTRA_UNUSED, KeySet * returned, Key * parentKey)
{
/* set all keys */
int errnosave = errno;
FILE * fout = fopen (keyString (parentKey), "w");
if (fout == 0)
{
ELEKTRA_SET_ERROR_SET (parentKey);
errno = errnosave;
return -1;
}
// TODO: proper error handling + use correct errors
ksToStream (returned, fout, KDB_O_HEADER);
if (fclose (fout))
{
ELEKTRA_SET_ERROR_SET (parentKey);
errno = errnosave;
return -1;
}
return 1; /* success */
}
Plugin * ELEKTRA_PLUGIN_EXPORT
{
// clang-format off
return elektraPluginExport("xmltool",
ELEKTRA_PLUGIN_GET, &elektraXmltoolGet,
ELEKTRA_PLUGIN_SET, &elektraXmltoolSet,
ELEKTRA_PLUGIN_END);
}