From ba9bf1894e32a41f9ad70b2b7dd355becf54971a Mon Sep 17 00:00:00 2001 From: Johanness Date: Tue, 19 May 2026 02:23:13 +0300 Subject: [PATCH] Read MXL flow labels from top-level metadata --- MxlSdkFeed.cpp | 118 +++++++++++++++++++++++++++++++++++-------------- 1 file changed, 86 insertions(+), 32 deletions(-) diff --git a/MxlSdkFeed.cpp b/MxlSdkFeed.cpp index a369db7..aa7aa9b 100644 --- a/MxlSdkFeed.cpp +++ b/MxlSdkFeed.cpp @@ -59,44 +59,98 @@ namespace const char* json, const char* key) { - std::string searchKey = std::string("\"") + key + "\""; - const char* pos = std::strstr(json, searchKey.c_str()); - if (!pos) + int depth = 0; + + for (const char* p = json; *p != '\0'; ++p) { - return ""; - } - - pos += searchKey.size(); - - while (*pos == ' ' || *pos == '\t' || *pos == '\n' || - *pos == '\r' || *pos == ':') - { - pos++; - } - - if (*pos != '"') - { - return ""; - } - - ++pos; - const char* valueBegin = pos; - bool escaped = false; - - while (*pos != '\0') - { - if (!escaped && *pos == '"') + if (*p == '"') { - return unescapeJsonString(valueBegin, pos); + const char* keyBegin = p + 1; + const char* keyEnd = keyBegin; + bool keyEscaped = false; + + while (*keyEnd != '\0') + { + if (!keyEscaped && *keyEnd == '"') + { + break; + } + + keyEscaped = !keyEscaped && *keyEnd == '\\'; + if (*keyEnd != '\\') + { + keyEscaped = false; + } + + ++keyEnd; + } + + if (*keyEnd == '\0') + { + return ""; + } + + const char* value = keyEnd + 1; + while (*value == ' ' || *value == '\t' || + *value == '\n' || *value == '\r') + { + ++value; + } + + if (depth == 1 && + *value == ':' && + unescapeJsonString(keyBegin, keyEnd) == key) + { + ++value; + while (*value == ' ' || *value == '\t' || + *value == '\n' || *value == '\r') + { + ++value; + } + + if (*value != '"') + { + return ""; + } + + ++value; + const char* valueBegin = value; + bool valueEscaped = false; + + while (*value != '\0') + { + if (!valueEscaped && *value == '"') + { + return unescapeJsonString( + valueBegin, + value); + } + + valueEscaped = + !valueEscaped && *value == '\\'; + if (*value != '\\') + { + valueEscaped = false; + } + + ++value; + } + + return ""; + } + + p = keyEnd; + continue; } - escaped = !escaped && *pos == '\\'; - if (*pos != '\\') + if (*p == '{' || *p == '[') { - escaped = false; + ++depth; + } + else if ((*p == '}' || *p == ']') && depth > 0) + { + --depth; } - - ++pos; } return "";