Line data Source code
1 : /**
2 : * @file
3 : *
4 : * @brief This file contains a function to convert a YAML file to a key set.
5 : *
6 : * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
7 : */
8 :
9 : // -- Imports ------------------------------------------------------------------
10 :
11 : #include "kdberrors.h"
12 :
13 : #include "convert.hpp"
14 : #include "driver.hpp"
15 :
16 : using std::string;
17 :
18 : using CppKey = kdb::Key;
19 : using CppKeySet = kdb::KeySet;
20 :
21 : // -- Function -----------------------------------------------------------------
22 :
23 : /**
24 : * @brief This function converts the given YAML file to keys and adds the
25 : * result to `keySet`.
26 : *
27 : * @param keySet The function adds the converted keys to this variable.
28 : * @param parent The function uses this parent key of `keySet` to emit error
29 : * information.
30 : * @param filename This parameter stores the path of the YAML file this
31 : * function converts.
32 : *
33 : * @retval -3 if the file could not be opened for reading
34 : * @retval -2 if parsing was unsuccessful due to memory exhaustion
35 : * @retval -1 if there was an syntax error converting the YAML file
36 : * @retval 0 if parsing was successful and the function did not change the given keyset
37 : * @retval 1 if parsing was successful and the function did change `keySet`
38 : */
39 63 : int addToKeySet (CppKeySet & keySet, CppKey & parent, string const & filename)
40 : {
41 126 : Driver driver{ parent };
42 :
43 63 : int status = driver.parse (filename);
44 :
45 63 : if (status < 0)
46 : {
47 8 : if (status == -3)
48 : {
49 0 : ELEKTRA_SET_RESOURCE_ERRORF (parent.getKey (), "Unable to open file '%s'", filename.c_str ());
50 : }
51 8 : else if (status == -2)
52 : {
53 0 : ELEKTRA_SET_OUT_OF_MEMORY_ERROR (parent.getKey (), "Parsing failed due to memory exhaustion");
54 : }
55 8 : else if (status == -1)
56 : {
57 32 : ELEKTRA_SET_VALIDATION_SYNTACTIC_ERROR (parent.getKey (), driver.getErrorMessage ().c_str ());
58 : }
59 :
60 : return status;
61 : }
62 :
63 55 : CppKeySet keys = driver.getKeySet ();
64 55 : status = (keys.size () <= 0) ? 0 : 1;
65 :
66 55 : keySet.append (keys);
67 :
68 55 : return status;
69 156 : }
|