forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utility.c
363 lines (326 loc) · 7.25 KB
/
utility.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/**
* @file utility.h
*
* @brief Contains functionality for handling Keys used throughout writing and reading.
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*/
#include "utility.h"
#include <kdbassert.h>
#include <kdbease.h>
#include <kdbhelper.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Key * keyAppendIndex (size_t index, const Key * parent)
{
// Key * indexKey = keyDup (parent);
Key * indexKey = keyNew (keyName (parent), KEY_END);
char * indexStr = indexToArrayString (index);
keyAddBaseName (indexKey, indexStr);
elektraFree (indexStr);
return indexKey;
}
void keyUpdateArrayMetakey (Key * key, size_t newIndex)
{
char * indexStr = indexToArrayString (newIndex);
keySetMeta (key, "array", indexStr);
elektraFree (indexStr);
}
char * indexToArrayString (size_t index)
{
size_t digits = 1;
for (size_t value = index; value > 9; digits++)
{
value /= 10;
}
int strLen = 1 + // '#'
(digits - 1) + // underscores
digits + // actual digits
1; // '\0'
char * str = (char *) elektraCalloc (sizeof (char) * strLen);
memset (str, '_', sizeof (char) * strLen);
str[0] = '#';
str[strLen - 1] = 0;
snprintf (str + 1 + (digits - 1), strLen, "%lu", index);
return str;
}
size_t arrayStringToIndex (const char * indexStr)
{
if (*indexStr++ != '#')
{
return 0;
}
while (*indexStr == '_')
{
indexStr++;
}
size_t val = 0;
if (sscanf (indexStr, "%lu", &val) == EOF)
{
ELEKTRA_ASSERT (0, "Could not parse array index");
return 0;
}
return val;
}
bool isArrayIndex (const char * basename)
{
return elektraArrayValidateBaseNameString (basename) == 1;
}
bool isArrayElement (const Key * key)
{
const char * part = (const char *) keyUnescapedName (key);
const char * stop = part + keyGetUnescapedNameSize (key);
while (part < stop)
{
if (isArrayIndex (part))
{
return true;
}
part += elektraStrLen (part);
}
return false;
}
bool isEmptyArray (Key * key)
{
const Key * meta = keyGetMeta (key, "array");
ELEKTRA_ASSERT (meta != NULL, "Supplied key must have array meta key, but hadn't");
const char * sizeStr = keyString (meta);
return elektraStrLen (sizeStr) == 1;
}
size_t getArrayMax (Key * key)
{
const Key * meta = keyGetMeta (key, "array");
ELEKTRA_ASSERT (meta != NULL, "Supplied key must have array meta key, but hadn't");
return arrayStringToIndex (keyString (meta));
}
void setPlainIntMeta (Key * key, const char * metaKeyName, size_t value)
{
char * str = intToStr (value);
keySetMeta (key, metaKeyName, str);
elektraFree (str);
}
char * intToStr (size_t i)
{
char * str = (char *) elektraCalloc (sizeof (char) * 40);
snprintf (str, 40, "%lu", i);
return str;
}
void setOrderForKey (Key * key, size_t order)
{
setPlainIntMeta (key, "order", order);
}
bool isArray (Key * key)
{
return keyGetMeta (key, "array") != NULL;
}
bool isInlineTable (Key * key)
{
return isTomlType (key, "inlinetable");
}
bool isSimpleTable (Key * key)
{
return isTomlType (key, "simpletable");
}
bool isTableArray (Key * key)
{
return isTomlType (key, "tablearray");
}
bool isTomlType (Key * key, const char * type)
{
const Key * meta = keyGetMeta (key, "tomltype");
if (meta == NULL)
{
return false;
}
return elektraStrCmp (keyString (meta), type) == 0;
}
bool isBareString (const char * str)
{
while (*str != 0)
{
if (!((*str >= '0' && *str <= '9') || (*str >= 'a' && *str <= 'z') || (*str >= 'A' && *str <= 'Z') || *str == '-' ||
*str == '_'))
{
return false;
}
str++;
}
return true;
}
char * getRelativeName (Key * parent, Key * key)
{
size_t nameSize = 64;
size_t pos = 0;
char * name = (char *) elektraCalloc (sizeof (char) * nameSize);
bool placeDot = false;
const char * keyPart;
const char * keyStop;
if (keyGetUnescapedNameSize (parent) == 3)
{
// root key -> needs special treatment
keyPart = ((const char *) keyUnescapedName (key)) + 2;
keyStop = ((const char *) keyUnescapedName (key)) + keyGetUnescapedNameSize (key);
}
else
{
keyPart = ((const char *) keyUnescapedName (key)) + keyGetUnescapedNameSize (parent);
keyStop = ((const char *) keyUnescapedName (key)) + keyGetUnescapedNameSize (key);
}
if (isTableArray (parent))
{ // skip array index
keyPart += elektraStrLen (keyPart);
}
while (keyPart < keyStop)
{
if (placeDot)
{
if (pos == nameSize)
{
nameSize *= 2;
if (elektraRealloc ((void **) &name, nameSize) < 0)
{
elektraFree (name);
return NULL;
}
}
name[pos++] = '.';
}
else
{
placeDot = true;
}
// Consider empty strings (only consisting of a zero terminator) non-bare, so that empty quotes get emitted
bool bare = *keyPart != 0 && isBareString (keyPart);
if (!bare)
{
if (pos == nameSize)
{
nameSize *= 2;
if (elektraRealloc ((void **) &name, nameSize) < 0)
{
elektraFree (name);
return NULL;
}
}
name[pos++] = '"';
}
if (pos + elektraStrLen (keyPart) >= nameSize)
{
nameSize *= 2;
if (elektraRealloc ((void **) &name, nameSize) < 0)
{
elektraFree (name);
return NULL;
}
}
strncat (&name[pos], keyPart, nameSize - pos);
pos += elektraStrLen (keyPart) - 1;
if (!bare)
{
if (pos == nameSize)
{
nameSize *= 2;
if (elektraRealloc ((void **) &name, nameSize) < 0)
{
elektraFree (name);
return NULL;
}
}
name[pos++] = '"';
}
keyPart += elektraStrLen (keyPart);
}
name[pos] = 0;
return name;
}
char * getDirectSubKeyName (const Key * parent, const Key * key)
{
if (keyIsBelow (parent, key) <= 0)
{
return NULL;
}
const char * keyPart = ((const char *) keyUnescapedName (key)) + keyGetUnescapedNameSize (parent);
return elektraStrDup (keyPart);
}
void keySetDiff (KeySet * whole, KeySet * part)
{
if (whole == NULL || part == NULL)
{
return;
}
for (elektraCursor it = 0; it < ksGetSize (part); ++it)
{
Key * key = ksAtCursor (part, it);
ksLookup (whole, key, KDB_O_POP);
}
}
KeySet * keysByPredicate (KeySet * ks, bool (*pred) (Key *))
{
KeySet * predicateKeys = ksNew (0, KS_END);
if (predicateKeys == NULL)
{
return NULL;
}
for (elektraCursor it = 0; it < ksGetSize (ks); ++it)
{
Key * key = ksAtCursor (ks, it);
if ((*pred) (key))
{
ksAppendKey (predicateKeys, key);
}
}
return predicateKeys;
}
KeySet * collectSubKeys (KeySet * ks, Key * parent)
{
KeySet * subKeys = ksNew (0, KS_END);
for (elektraCursor it = 0; it < ksGetSize (ks); ++it)
{
Key * key = ksAtCursor (ks, it);
if (keyIsBelow (parent, key) == 1)
{
ksAppendKey (subKeys, key);
}
}
return subKeys;
}
KeySet * extractSubKeys (KeySet * ks, Key * parent)
{
KeySet * sub = collectSubKeys (ks, parent);
keySetDiff (ks, sub);
return sub;
}
bool isLeaf (Key * leafCandidate, KeySet * ks)
{
for (elektraCursor it = 0; it < ksGetSize (ks); ++it)
{
Key * key = ksAtCursor (ks, it);
if (keyIsBelow (leafCandidate, key) == 1)
{
return false;
}
}
return true;
}
bool isBase64String (const char * str)
{
const char * prefix = "@BASE64";
if (elektraStrLen (str) < elektraStrLen (prefix))
{
return false;
}
for (size_t i = 0; i < elektraStrLen (prefix) - 1; i++)
{
if (str[i] != prefix[i])
{
return false;
}
}
return true;
}
bool isNullString (const char * str)
{
const char * nullIndicator = "@NULL";
return elektraStrCmp (str, nullIndicator) == 0;
}