forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkconfig_serializer.cpp
267 lines (233 loc) · 5.26 KB
/
kconfig_serializer.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
#include "kconfig_serializer.hpp"
#include "base.hpp"
#include <algorithm>
#include <vector>
KConfigSerializer::KConfigSerializer (CppKeySet & keySetParam, CppKey & parentParam, std::unique_ptr<std::ostream> oParam)
: o{ std::move (oParam) }, keySet{ keySetParam }, parentKeyNameSize{ parentParam.getName ().size () + 1 }, lastPrintedGroup{ "" },
isFirstKey{ true }
{
std::string parentKeyName = parentParam.getName ();
if (parentKeyName == "/")
{
parentKeyNameSize = 1;
}
else
{
parentKeyNameSize = parentKeyName.size () + 1;
}
}
void KConfigSerializer::save ()
{
std::vector<CppKey> keys{ keySet.begin (), keySet.end () };
// std::sort (keys.begin (), keys.end (), KConfigSerializer::KeyNameComparator{ parent });
const CppKey * groupCandidate{ nullptr };
for (const auto & k : keys)
{
if (groupCandidate != nullptr)
{
if (k.getName ().rfind (groupCandidate->getName (), 0) == 0)
{
saveGroupKey (*groupCandidate);
lastPrintedGroup = groupCandidate->getName ();
}
else
{
saveLeafKeyWithGroupCandidate (*groupCandidate);
}
groupCandidate = nullptr;
}
if (!k.getString ().empty ())
{
saveLeafKeyWithGroupCandidate (k);
}
else
{
groupCandidate = &k;
}
}
}
void KConfigSerializer::saveAndEscapeString (const std::string & val, bool isGroupKey)
{
std::ostream & out = *o;
char currentChar;
for (std::size_t i{ 0 }; i < val.size (); ++i)
{
currentChar = val[i];
switch (currentChar)
{
case '\\':
if (isGroupKey && i < val.size () && val[i + 1] == '/')
{
// Forward slash is parsed as "\/"
out << '/';
++i;
}
else
{
out << "\\\\";
}
break;
case '/':
if (isGroupKey)
{
out << "][";
}
else
{
out << '/';
}
break;
case '\n':
out << "\\n";
break;
case '\r':
out << "\\r";
break;
case '\t':
out << "\\t";
break;
default:
out << currentChar;
}
}
}
void KConfigSerializer::saveGroupKey (CppKey const & k)
{
saveGroupKeyWithoutMeta (k.getName (), false);
std::string metadata{ k.getMeta<std::string> (KCONFIG_METADATA_KEY) };
std::ostream & out = *o;
if (!metadata.empty ())
{
out << character_open_bracket;
out << character_dollar_sign;
out << metadata;
out << character_close_bracket;
}
out << character_newline;
}
void KConfigSerializer::saveGroupKeyWithoutMeta (std::string const & group, bool newline)
{
std::ostream & out = *o;
std::size_t skipChars = parentKeyNameSize;
if (group.size () > skipChars)
{
std::string outstr{ group.substr (skipChars) };
if (!isFirstKey)
{
out << character_newline;
}
else
{
isFirstKey = false;
}
out << character_open_bracket;
saveAndEscapeString (outstr, true);
out << character_close_bracket;
if (newline)
{
out << character_newline;
}
}
}
void KConfigSerializer::saveLeafKeyWithGroupCandidate (CppKey const & k)
{
std::string currentGroupName{ groupNameFromLeaf (k.getName ()) };
if (lastPrintedGroup != currentGroupName)
{
saveGroupKeyWithoutMeta (currentGroupName);
lastPrintedGroup = currentGroupName;
}
saveLeafKey (k);
}
void KConfigSerializer::saveLeafKey (CppKey const & key)
{
std::ostream & out = *o;
isFirstKey = false;
saveAndEscapeString (key.getBaseName (), false);
std::string meta = key.getMeta<std::string> (KCONFIG_METADATA_KEY);
for (char c : meta)
{
out << character_open_bracket;
out << character_dollar_sign;
out << c;
out << character_close_bracket;
}
out << character_equals_sign;
saveAndEscapeString (key.getString (), false);
out << character_newline;
}
std::size_t KConfigSerializer::findLastSlash (std::string const & s)
{
std::size_t count{ 0 };
for (std::size_t i{ 0 }; i < s.size (); i++)
{
switch (s[i])
{
case '/':
count = i;
break;
case '\\':
++i;
}
}
return count;
}
std::string KConfigSerializer::groupNameFromLeaf (std::string const & leafKeyName)
{
return leafKeyName.substr (0, findLastSlash (leafKeyName));
}
KConfigSerializer::KeyNameComparator::KeyNameComparator (CppKey const & parent)
{
auto iter = parent.begin ();
parentKeyCount = 0;
while (iter != parent.end ())
{
iter++;
parentKeyCount++;
}
}
bool KConfigSerializer::KeyNameComparator::operator() (CppKey const & keyA, CppKey const & keyB)
{
auto itA = keyA.begin ();
auto itB = keyB.begin ();
// Don't compare the parent
this->skipParent (itA);
this->skipParent (itB);
auto endA = keyA.end ();
auto endB = keyB.end ();
while (true)
{
// pointer to where the current keys are stored
const char * strPosA = itA.pos ();
const char * strPosB = itB.pos ();
++itA;
++itB;
// Check if they're group keys, or leaf keys
bool endsA{ itA == endA };
bool endsB{ itB == endB };
if (endsA || endsB)
{
// If one of them is a leaf key, we don't need to iterate further
if (endsA && endsB)
{
// If both are leaf keys, return the compared value
return strcmp (strPosA, strPosB) < 0;
}
return endsA;
}
// Compare the current branch key of a to that of b
int compareAtoB{ strcmp (strPosA, strPosB) };
if (compareAtoB != 0)
{
// If the names don't match, don't continue iterating and return
return compareAtoB < 0;
}
}
}
void KConfigSerializer::KeyNameComparator::skipParent (CppKey::iterator & it)
{
for (std::size_t i{ 0 }; i < parentKeyCount; ++i)
{
it++;
}
}