MCPcopy Create free account
hub / github.com/Selectively11/CloudRedirect / ForEachFieldInSection

Function ForEachFieldInSection

src/common/vdf.cpp:11–70  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

9}
10
11bool ForEachFieldInSection(const std::string& vdfContent,
12 const char* const* sectionPath, int pathLen,
13 FieldCallback cb) {
14 int targetDepth = 0;
15 int depth = 0;
16 bool inTarget = false;
17 int targetBase = 0;
18
19 size_t pos = 0;
20 while (pos < vdfContent.size()) {
21 size_t lineEnd = vdfContent.find('\n', pos);
22 if (lineEnd == std::string::npos) lineEnd = vdfContent.size();
23
24 std::string_view lineView(vdfContent.data() + pos, lineEnd - pos);
25 size_t ls = lineView.find_first_not_of(" \t\r\n");
26 if (ls != std::string_view::npos) {
27 std::string_view trimmed = StripCR(lineView.substr(ls));
28
29 if (trimmed == "{") {
30 depth++;
31 } else if (trimmed == "}") {
32 if (inTarget && depth == targetBase) return true;
33 depth--;
34 if (!inTarget && targetDepth > 0 && depth < targetDepth)
35 targetDepth = depth;
36 } else if (trimmed.size() >= 3 && trimmed[0] == '"') {
37 size_t keyEnd = trimmed.find('"', 1);
38 if (keyEnd != std::string_view::npos) {
39 std::string_view key = trimmed.substr(1, keyEnd - 1);
40
41 if (inTarget) {
42 size_t vq1 = trimmed.find('"', keyEnd + 1);
43 if (vq1 != std::string_view::npos) {
44 size_t vq2 = trimmed.find('"', vq1 + 1);
45 if (vq2 != std::string_view::npos) {
46 std::string_view val = trimmed.substr(vq1 + 1, vq2 - vq1 - 1);
47 size_t trimStart = pos + ls;
48 size_t valAbsStart = trimStart + vq1 + 1;
49 size_t valAbsEnd = trimStart + vq2;
50
51 FieldInfo fi{key, val, valAbsStart, valAbsEnd};
52 if (!cb(fi)) return true;
53 }
54 }
55 } else if (targetDepth < pathLen && depth == targetDepth && key == sectionPath[targetDepth]) {
56 targetDepth++;
57 if (targetDepth == pathLen) {
58 inTarget = true;
59 targetBase = depth + 1;
60 }
61 }
62 }
63 }
64 }
65
66 pos = lineEnd + 1;
67 }
68

Callers 3

ReconcileLocalConfigFunction · 0.85
DetectEffectivePlatformFunction · 0.85

Calls 3

StripCRFunction · 0.85
sizeMethod · 0.80
findMethod · 0.80

Tested by

no test coverage detected