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
/
unit.c
205 lines (172 loc) · 4.89 KB
/
unit.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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/**
* @file
*
* @brief unit plugin
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*
*/
#include "unit.h"
#include <kdberrors.h>
#include <kdbhelper.h>
#include <kdbtypes.h>
#include <stdio.h>
#include <stdlib.h>
// @param input string, of which possibly occurring spaces are removed
static void deblank (char * input)
{
int count = 0;
for (int i = 0; input[i]; i++)
{
if (input[i] != ' ')
{
input[count++] = input[i];
}
}
input[count] = '\0';
}
static kdb_unsigned_long_long_t isValidKey (Key * key)
{
const char * value = keyString (key);
// else we manipulate the original
char * tempval = elektraStrDup (value);
char * endPtr;
// convert to long, if valid key, pointer should point to spaces or unit suffix like MB, GB
ELEKTRA_UNSIGNED_LONG_LONG_S (tempval, &endPtr, 10);
// before deblanking check if pointer is invalid
if (endPtr == tempval)
{
elektraFree (tempval);
return 0;
}
// remove possibly occurring blanks
deblank (endPtr);
kdb_unsigned_long_long_t factor = 0;
// calculate the factor based on the suffix of the mameory value, return 0 if there is no matching to indicate an error of the
// function
if (strcmp (endPtr, "KB") == 0)
{
factor = 1000;
}
else if (strcmp (endPtr, "MB") == 0)
{
factor = 1000000;
}
else if (strcmp (endPtr, "GB") == 0)
{
factor = 1000000000;
}
else if (strcmp (endPtr, "TB") == 0)
{
factor = 1000000000000;
}
else if (strcmp (endPtr, "PB") == 0)
{
factor = 1000000000000000;
}
else if (strcmp (endPtr, "B") == 0)
{
factor = 1;
}
elektraFree (tempval);
return factor;
}
// @param formatFactor unsigned long, used to determine the factor for normalizing to bytes
static int elektraUnitConvertToByteString (Key * key, kdb_unsigned_long_long_t formatFactor)
{
const char * str = keyString (key);
char * origvalue = elektraStrDup (str);
char * ptr;
kdb_unsigned_long_long_t ret;
kdb_unsigned_long_long_t normalizedMemVal;
ret = ELEKTRA_UNSIGNED_LONG_LONG_S (str, &ptr, 10);
// check if return value within bounds
if (ret > UINT64_MAX / formatFactor)
{
elektraFree (origvalue);
return 1;
}
normalizedMemVal = ret * formatFactor;
// convert back to string
const int n = snprintf (NULL, 0, ELEKTRA_UNSIGNED_LONG_LONG_F, normalizedMemVal);
char buf[n + 1];
snprintf (buf, n + 1, ELEKTRA_UNSIGNED_LONG_LONG_F, normalizedMemVal);
keySetString (key, buf);
keySetMeta (key, "origvalue", origvalue);
elektraFree (origvalue);
return 0;
}
static void elektraUnitRestore (Key * key)
{
const Key * oldval = keyGetMeta (key, "origvalue");
if (oldval != NULL)
{
keySetString (key, keyString (oldval));
}
}
int elektraUnitGet (Plugin * handle ELEKTRA_UNUSED, KeySet * returned, Key * parentKey)
{
if (!elektraStrCmp (keyName (parentKey), "system:/elektra/modules/unit"))
{
KeySet * contract =
ksNew (30, keyNew ("system:/elektra/modules/unit", KEY_VALUE, "unit plugin waits for your orders", KEY_END),
keyNew ("system:/elektra/modules/unit/exports", KEY_END),
keyNew ("system:/elektra/modules/unit/exports/get", KEY_FUNC, elektraUnitGet, KEY_END),
keyNew ("system:/elektra/modules/unit/exports/set", KEY_FUNC, elektraUnitSet, KEY_END),
#include ELEKTRA_README
keyNew ("system:/elektra/modules/unit/infos/version", KEY_VALUE, PLUGINVERSION, KEY_END), KS_END);
ksAppend (returned, contract);
ksDel (contract);
return ELEKTRA_PLUGIN_STATUS_SUCCESS;
}
Key * cur;
int rc = 1;
for (elektraCursor it = 0; it < ksGetSize (returned); ++it)
{
cur = ksAtCursor (returned, it);
const Key * meta = keyGetMeta (cur, "check/unit");
if (meta)
{
kdb_unsigned_long_long_t format = isValidKey (cur);
if (format == 0)
{
ELEKTRA_SET_VALIDATION_SYNTACTIC_ERRORF (
parentKey,
"The string '%s' is not following the format guidelines of (<numerical value><optional "
"space><memory unit>, e.g. 128 MB) !",
keyString (cur));
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
elektraUnitConvertToByteString (cur, format);
}
}
return rc;
}
int elektraUnitSet (Plugin * handle ELEKTRA_UNUSED, KeySet * returned ELEKTRA_UNUSED, Key * parentKey ELEKTRA_UNUSED)
{
for (elektraCursor it = 0; it < ksGetSize (returned); ++it)
{
Key * cur = ksAtCursor (returned, it);
const Key * meta = keyGetMeta (cur, "check/unit");
if (!meta)
{
continue;
}
elektraUnitRestore (cur);
kdb_unsigned_long_long_t format = isValidKey (cur);
if (format == 0)
{
ELEKTRA_SET_VALIDATION_SYNTACTIC_ERRORF (parentKey,
"The string '%s' is not following the format guidelines of (<numerical "
"value><optional space><memory unit>, "
"e.g. 128 MB) !",
keyString (cur));
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
}
return ELEKTRA_PLUGIN_STATUS_SUCCESS;
}
Plugin * ELEKTRA_PLUGIN_EXPORT
{
return elektraPluginExport ("unit", ELEKTRA_PLUGIN_GET, &elektraUnitGet, ELEKTRA_PLUGIN_SET, &elektraUnitSet, ELEKTRA_PLUGIN_END);
}