forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
write.cpp
537 lines (473 loc) · 15.5 KB
/
write.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
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
/**
* @file
*
* @brief Write key sets using yaml-cpp
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*/
#include "write.hpp"
#include "log.hpp"
#include "yaml-cpp/yaml.h"
#include <kdbassert.h>
#include <kdbease.h>
#include <kdblogger.h>
#include <kdbplugin.h>
#include <fstream>
using namespace std;
using namespace kdb;
namespace
{
using KeySetPair = pair<KeySet, KeySet>;
/**
* @brief This function checks if `element` is an array element of `parent`.
*
* @pre The key `child` must be below `parent`.
*
* @param parent This parameter specifies a parent key.
* @param keys This variable stores a direct or indirect child of `parent`.
*
* @retval true If `element` is an array element
* @retval false Otherwise
*/
bool isArrayElementOf (Key const & parent, Key const & child)
{
char const * relative = elektraKeyGetRelativeName (*child, *parent);
auto offsetIndex = ckdb::elektraArrayValidateBaseNameString (relative);
if (offsetIndex <= 0) return false;
// Skip `#`, underscores and digits
relative += 2 * offsetIndex;
// The next character has to be the separation char (`/`) or end of string
if (relative[0] != '\0' && relative[0] != '/') return false;
return true;
}
/**
* @brief This function determines if the given key is an array parent.
*
* @param parent This parameter specifies a possible array parent.
* @param keys This variable stores the key set of `parent`.
*
* @retval true If `parent` is the parent key of an array
* @retval false Otherwise
*/
bool isArrayParent (Key const & parent, KeySet const & keys)
{
for (auto const & key : keys)
{
if (!key.isBelow (parent)) continue;
if (!isArrayElementOf (parent, key)) return false;
}
return true;
}
/**
* @brief This function returns all array parents for a given key set.
*
* @note This function also adds empty parent keys for arrays, if they did not exist beforehand. For example for the key set that **only**
* contains the keys:
*
* - `user/array/#0`, and
* - `user/array/#1`
*
* the function will add the array parent `user/array` to the returned key set.
*
* @param keys This parameter contains the key set this function searches for array parents.
*
* @return A key sets that contains all array parents stored in `keys`
*/
KeySet splitArrayParents (KeySet const & keys)
{
KeySet arrayParents;
keys.rewind ();
Key previous;
for (; keys.next (); previous = keys.current ())
{
if (keys.current ().hasMeta ("array"))
{
arrayParents.append (keys.current ());
continue;
}
if (keys.current ().getBaseName ()[0] == '#')
{
if (!keys.current ().isDirectBelow (previous))
{
Key directParent{ keys.current ().getName (), KEY_END };
ckdb::keySetBaseName (*directParent, NULL);
if (isArrayParent (*directParent, keys)) arrayParents.append (directParent);
}
else if (isArrayParent (*previous, keys))
{
arrayParents.append (previous);
}
}
}
#ifdef HAVE_LOGGER
ELEKTRA_LOG_DEBUG ("Array parents:");
logKeySet (arrayParents);
#endif
return arrayParents;
}
/**
* @brief This function splits `keys` into two key sets, one for array parents and elements, and the other one for all other keys.
*
* @param arrayParents This key set contains a (copy) of all array parents of `keys`.
* @param keys This parameter contains the key set this function splits.
*
* @return A pair of key sets, where the first key set contains all array parents and elements,
* and the second key set contains all other keys
*/
KeySetPair splitArrayOther (KeySet const & arrayParents, KeySet const & keys)
{
KeySet others = keys.dup ();
KeySet arrays;
for (auto const & parent : arrayParents)
{
arrays.append (others.cut (parent));
}
return make_pair (arrays, others);
}
/**
* @brief This function removes all **non-essential** array metadata from a given key set.
*
* @param keys This parameter contains the key set this function modifies.
*
* @return A copy of `keys` that only contains array metadata for empty arrays
*/
KeySet removeArrayMetaData (KeySet const & keys)
{
KeySet result;
for (auto const & key : keys)
{
result.append (key.dup ());
}
Key previous;
result.rewind ();
while (result.next ())
{
if (result.current ().isBelow (previous)) previous.delMeta ("array");
previous = result.current ();
}
ELEKTRA_ASSERT (keys.size () == result.size (), "Size of input and output keys set is different (%zu ≠ %zu)", keys.size (),
result.size ());
return result;
}
/**
* @brief This function determines all keys “missing” from the given keyset.
*
* The term “missing” refers to keys that are not part of the hierarchy. For example in a key set with the parent key
*
* - `user/parent`
*
* that contains the keys
*
* - `user/parent/level1/level2`, and
* - `user/parent/level1/level2/level3/level4`
*
* , the keys
*
* - `user/parent/level1`, and
* - user/parent/level1/level2/level3
*
* are missing.
*
* @param keys This parameter contains the key set for which this function determines missing keys.
* @param parent This value stores the parent key of `keys`.
*
* @return A key set that contains all keys missing from `keys`
*/
KeySet missingKeys (KeySet const & keys, Key const & parent)
{
KeySet missing;
keys.rewind ();
Key previous{ parent.getName (), KEY_BINARY, KEY_END };
for (; keys.next (); previous = keys.current ())
{
if (keys.current ().isDirectBelow (previous) || !keys.current ().isBelow (previous)) continue;
Key current{ keys.current ().getName (), KEY_BINARY, KEY_END };
while (!current.isDirectBelow (previous))
{
ckdb::keySetBaseName (*current, NULL);
missing.append (current);
current = current.dup ();
}
}
return missing;
}
/**
* @brief This function returns a `NameIterator` starting at the first level that is not part of `parent`.
*
* @pre The parameter `key` must be a child of `parent`.
*
* @param key This is the key for which this function returns a relative iterator.
* @param parent This key specifies the part of the name iterator that will not be part of the return value of this function.
*
* @returns A relative iterator that starts with the first part of the name of `key` not contained in `parent`.
*/
NameIterator relativeKeyIterator (Key const & key, Key const & parent)
{
auto parentIterator = parent.begin ();
auto keyIterator = key.begin ();
while (parentIterator != parent.end () && keyIterator != key.end ())
{
parentIterator++;
keyIterator++;
}
return keyIterator;
}
/**
* @brief This function checks if a key name specifies an array key.
*
* If the key name contains a valid array index that is smaller than `unsigned long long`, then the function will also return this index.
*
* @param nameIterator This iterator specifies the name of the key.
*
* @retval (true, arrayIndex) if `name` specifies an array key, where `arrayIndex` specifies the index stored in the array key.
* @retval (false, 0) otherwise
*/
std::pair<bool, unsigned long long> isArrayIndex (NameIterator const & nameIterator)
{
string const name = *nameIterator;
auto const offsetIndex = ckdb::elektraArrayValidateBaseNameString (name.c_str ());
auto const isArrayElement = offsetIndex >= 1;
return { isArrayElement, isArrayElement ? stoull (name.substr (offsetIndex)) : 0 };
}
/**
* @brief This function creates a YAML node representing a key value.
*
* @param key This key specifies the data that should be saved in the YAML node returned by this function.
*
* @note Since YAML does not support non-empty binary data directly this function replaces data stored in binary keys with the string
* `Unsupported binary value!`. If you need support for binary data, please load the Base64 plugin before you use YAML CPP.
*
* @returns A new YAML node containing the data specified in `key`
*/
YAML::Node createMetaDataNode (Key const & key)
{
if (key.hasMeta ("array"))
{
return YAML::Node (YAML::NodeType::Sequence);
}
if (key.getBinarySize () == 0)
{
return YAML::Node (YAML::NodeType::Null);
}
if (key.isBinary ())
{
return YAML::Node ("Unsupported binary value!");
}
auto value = key.get<string> ();
if (value == "0" || value == "1")
{
return YAML::Node (key.get<bool> ());
}
return YAML::Node (value);
}
/**
* @brief This function creates a YAML Node containing a key value and optionally metadata.
*
* @param key This key specifies the data that should be saved in the YAML node returned by this function.
*
* @note Since YAML does not support non-empty binary data directly this function replaces data stored in binary keys with the string
* `Unsupported binary value!`. If you need support for binary data, please load the Base64 before you use YAML CPP.
*
* @returns A new YAML node containing the data and metadata specified in `key`
*/
YAML::Node createLeafNode (Key & key)
{
YAML::Node metaNode{ YAML::Node (YAML::NodeType::Map) };
YAML::Node dataNode = createMetaDataNode (key);
key.rewindMeta ();
while (Key meta = key.nextMeta ())
{
if (meta.getName () == "array" || meta.getName () == "binary") continue;
if (meta.getName () == "type" && meta.getString () == "binary")
{
dataNode.SetTag ("tag:yaml.org,2002:binary");
continue;
}
metaNode[meta.getName ()] = meta.getString ();
ELEKTRA_LOG_DEBUG ("Add metakey “%s: %s”", meta.getName ().c_str (), meta.getString ().c_str ());
}
if (metaNode.size () <= 0)
{
ELEKTRA_LOG_DEBUG ("Return leaf node with value “%s”",
dataNode.IsNull () ? "~" : dataNode.IsSequence () ? "[]" : dataNode.as<string> ().c_str ());
return dataNode;
}
YAML::Node node{ YAML::Node (YAML::NodeType::Sequence) };
node.SetTag ("!elektra/meta");
node.push_back (dataNode);
node.push_back (metaNode);
#ifdef HAVE_LOGGER
ostringstream data;
data << node;
ELEKTRA_LOG_DEBUG ("Return meta leaf node with value “%s”", data.str ().c_str ());
#endif
return node;
}
/**
* @brief This function adds `null` elements to the given YAML collection.
*
* @param sequence This node stores the collection to which this function adds `numberOfElements` empty elements.
* @param numberOfElements This parameter specifies the number of empty element this function adds to `sequence`.
*/
void addEmptyArrayElements (YAML::Node & sequence, unsigned long long const numberOfElements)
{
ELEKTRA_LOG_DEBUG ("Add %lld empty array elements", numberOfElements);
for (auto missingFields = numberOfElements; missingFields > 0; missingFields--)
{
sequence.push_back ({});
}
}
/**
* @brief This function adds a key that is not part of any array to a YAML node.
*
* @param data This node stores the data specified via `keyIterator`.
* @param keyIterator This iterator specifies the current part of the key name this function adds to `data`.
* @param key This parameter specifies the key that should be added to `data`.
*/
void addKeyNoArray (YAML::Node & data, NameIterator & keyIterator, Key & key)
{
if (data.IsScalar ()) data = YAML::Node (YAML::NodeType::Undefined);
#ifdef HAVE_LOGGER
ostringstream output;
output << data;
ELEKTRA_LOG_DEBUG ("Add key part “%s”", (*keyIterator).c_str ());
#endif
if (keyIterator == key.end ())
{
ELEKTRA_LOG_DEBUG ("Create leaf node for key “%s”", key.getName ().c_str ());
data = createLeafNode (key);
return;
}
if (keyIterator == --key.end ())
{
data[*keyIterator] = createLeafNode (key);
return;
}
YAML::Node node;
node = (data[*keyIterator] && !data[*keyIterator].IsScalar ()) ? data[*keyIterator] : YAML::Node ();
data[*keyIterator] = node;
addKeyNoArray (node, ++keyIterator, key);
}
/**
* @brief This function adds a key that is either, element of an array, or an array parent to a YAML node.
*
* @param data This node stores the data specified via `keyIterator`.
* @param keyIterator This iterator specifies the current part of the key name this function adds to `data`.
* @param key This parameter specifies the key that should be added to `data`.
*/
void addKeyArray (YAML::Node & data, NameIterator & keyIterator, Key & key)
{
auto const isArrayAndIndex = isArrayIndex (keyIterator);
auto const isArrayElement = isArrayAndIndex.first;
auto const arrayIndex = isArrayAndIndex.second;
if (data.IsScalar ()) data = YAML::Node (YAML::NodeType::Undefined);
#ifdef HAVE_LOGGER
ostringstream output;
output << data;
ELEKTRA_LOG_DEBUG ("Add key part “%s”", (*keyIterator).c_str ());
#endif
if (keyIterator == key.end ())
{
ELEKTRA_LOG_DEBUG ("Create leaf node for key “%s”", key.getName ().c_str ());
data = createLeafNode (key);
return;
}
if (keyIterator == --key.end ())
{
if (isArrayElement)
{
addEmptyArrayElements (data, arrayIndex - data.size ());
data.push_back (createLeafNode (key));
}
else
{
data[*keyIterator] = createLeafNode (key);
}
return;
}
YAML::Node node;
if (isArrayElement)
{
node = (data[arrayIndex] && !data[arrayIndex].IsScalar ()) ? data[arrayIndex] : YAML::Node ();
data[arrayIndex] = node;
}
else
{
node = (data[*keyIterator] && !data[*keyIterator].IsScalar ()) ? data[*keyIterator] : YAML::Node ();
data[*keyIterator] = node;
}
addKeyArray (node, ++keyIterator, key);
}
/**
* @brief This function adds a key set to a YAML node.
*
* @param data This node stores the data specified via `mappings`.
* @param mappings This keyset specifies all keys and values this function adds to `data`.
* @param parent This key is the root of all keys stored in `mappings`.
* @param isArray This value specifies if the keys inside `keys` are all part of an array (either element or parent), or if none of them is
* part of an array.
*/
void addKeys (YAML::Node & data, KeySet const & mappings, Key const & parent, bool const isArray = false)
{
for (auto key : mappings)
{
ELEKTRA_LOG_DEBUG ("Convert key “%s”: “%s”", key.getName ().c_str (),
key.getBinarySize () == 0 ? "NULL" : key.isString () ? key.getString ().c_str () : "binary value!");
NameIterator keyIterator = relativeKeyIterator (key, parent);
if (isArray)
{
addKeyArray (data, keyIterator, key);
}
else
{
addKeyNoArray (data, keyIterator, key);
}
#ifdef HAVE_LOGGER
ostringstream output;
output << data;
ELEKTRA_LOG_DEBUG ("Converted Data:");
ELEKTRA_LOG_DEBUG ("——————————");
istringstream stream (output.str ());
for (string line; std::getline (stream, line);)
{
ELEKTRA_LOG_DEBUG ("%s", line.c_str ());
}
ELEKTRA_LOG_DEBUG ("——————————");
#endif
}
}
} // end namespace
/**
* @brief This function saves the key-value pairs stored in `mappings` as YAML data in the location specified via `parent`.
*
* @param mappings This key set stores the mappings that should be saved as YAML data.
* @param parent This key specifies the path to the YAML data file that should be written.
*/
void yamlcpp::yamlWrite (KeySet const & mappings, Key const & parent)
{
auto keys = removeArrayMetaData (mappings);
auto missing = missingKeys (keys, parent);
keys.append (missing);
KeySet arrayParents;
KeySet arrays;
KeySet nonArrays;
arrayParents = splitArrayParents (keys);
tie (arrays, nonArrays) = splitArrayOther (arrayParents, keys);
auto data = YAML::Node ();
addKeys (data, nonArrays, parent);
addKeys (data, arrays, parent, true);
#ifdef HAVE_LOGGER
ELEKTRA_LOG_DEBUG ("Write Data:");
ELEKTRA_LOG_DEBUG ("——————————");
ostringstream outputString;
outputString << data;
istringstream stream (outputString.str ());
for (string line; std::getline (stream, line);)
{
ELEKTRA_LOG_DEBUG ("%s", line.c_str ());
}
ELEKTRA_LOG_DEBUG ("——————————");
#endif
ofstream output (parent.getString ());
output << data;
}