From bef87a5a7ea923aee8d1d83ce9ba4add3dca4176 Mon Sep 17 00:00:00 2001 From: Avinash Kumar Deepak Date: Mon, 9 Feb 2026 21:32:32 +0530 Subject: [PATCH] Fix broken functions in concoredocker.hpp to match Python behavior - safe_literal_eval now reads and parses file content instead of always returning default - load_params populates the params map from concore.params file - read() parses list data, extracts simtime from first element, returns rest - Added initval() to parse simtime string and extract data portion - Fixed naming to match codebase style (no underscores in function names) Fixes #224 --- concoredocker.hpp | 80 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 78 insertions(+), 2 deletions(-) diff --git a/concoredocker.hpp b/concoredocker.hpp index cbce718..2471e3e 100644 --- a/concoredocker.hpp +++ b/concoredocker.hpp @@ -12,6 +12,7 @@ #include #include #include +#include class Concore { public: @@ -26,6 +27,52 @@ class Concore { int maxtime = 100; std::unordered_map params; + std::string stripstr(const std::string& str) { + size_t start = str.find_first_not_of(" \t\n\r"); + if (start == std::string::npos) return ""; + size_t end = str.find_last_not_of(" \t\n\r"); + return str.substr(start, end - start + 1); + } + + std::string stripquotes(const std::string& str) { + if (str.size() >= 2 && ((str.front() == '\'' && str.back() == '\'') || (str.front() == '"' && str.back() == '"'))) + return str.substr(1, str.size() - 2); + return str; + } + + std::unordered_map parsedict(const std::string& str) { + std::unordered_map result; + std::string trimmed = stripstr(str); + if (trimmed.size() < 2 || trimmed.front() != '{' || trimmed.back() != '}') + return result; + std::string inner = trimmed.substr(1, trimmed.size() - 2); + std::stringstream ss(inner); + std::string token; + while (std::getline(ss, token, ',')) { + size_t colon = token.find(':'); + if (colon == std::string::npos) continue; + std::string key = stripquotes(stripstr(token.substr(0, colon))); + std::string val = stripquotes(stripstr(token.substr(colon + 1))); + if (!key.empty()) result[key] = val; + } + return result; + } + + std::vector parselist(const std::string& str) { + std::vector result; + std::string trimmed = stripstr(str); + if (trimmed.size() < 2 || trimmed.front() != '[' || trimmed.back() != ']') + return result; + std::string inner = trimmed.substr(1, trimmed.size() - 2); + std::stringstream ss(inner); + std::string token; + while (std::getline(ss, token, ',')) { + std::string val = stripstr(token); + if (!val.empty()) result.push_back(val); + } + return result; + } + Concore() { iport = safe_literal_eval("concore.iport", {}); oport = safe_literal_eval("concore.oport", {}); @@ -39,7 +86,14 @@ class Concore { std::cerr << "Error reading " << filename << "\n"; return defaultValue; } - return defaultValue; + std::stringstream buf; + buf << file.rdbuf(); + std::string content = buf.str(); + try { + return parsedict(content); + } catch (...) { + return defaultValue; + } } void load_params() { @@ -54,8 +108,11 @@ class Concore { } if (!sparams.empty() && sparams[0] != '{') { - sparams = "{'" + std::regex_replace(std::regex_replace(std::regex_replace(sparams, std::regex(","), ", '"), std::regex("="), "':"), std::regex(" "), "") + "}"; + sparams = "{\"" + std::regex_replace(std::regex_replace(std::regex_replace(sparams, std::regex(","), ",\""), std::regex("="), "\":"), std::regex(" "), "") + "}"; } + try { + params = parsedict(sparams); + } catch (...) {} } std::string tryparam(const std::string& n, const std::string& i) { @@ -106,6 +163,14 @@ class Concore { } s += ins; + try { + std::vector inval = parselist(ins); + if (!inval.empty()) { + int file_simtime = (int)std::stod(inval[0]); + simtime = std::max(simtime, file_simtime); + return std::vector(inval.begin() + 1, inval.end()); + } + } catch (...) {} return {ins}; } @@ -125,6 +190,17 @@ class Concore { simtime += delta; } } + + std::vector initval(const std::string& simtime_val) { + try { + std::vector val = parselist(simtime_val); + if (!val.empty()) { + simtime = (int)std::stod(val[0]); + return std::vector(val.begin() + 1, val.end()); + } + } catch (...) {} + return {}; + } }; #endif