Skip to content

Commit 9a1eb84

Browse files
authored
Merge pull request #234 from avinxshKD/fix/concoredocker-hpp-broken-functions
Fix broken functions in concoredocker.hpp to match Python behavior
2 parents 3e06d5b + bef87a5 commit 9a1eb84

1 file changed

Lines changed: 78 additions & 2 deletions

File tree

concoredocker.hpp

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <filesystem>
1313
#include <stdexcept>
1414
#include <regex>
15+
#include <algorithm>
1516

1617
class Concore {
1718
public:
@@ -26,6 +27,52 @@ class Concore {
2627
int maxtime = 100;
2728
std::unordered_map<std::string, std::string> params;
2829

30+
std::string stripstr(const std::string& str) {
31+
size_t start = str.find_first_not_of(" \t\n\r");
32+
if (start == std::string::npos) return "";
33+
size_t end = str.find_last_not_of(" \t\n\r");
34+
return str.substr(start, end - start + 1);
35+
}
36+
37+
std::string stripquotes(const std::string& str) {
38+
if (str.size() >= 2 && ((str.front() == '\'' && str.back() == '\'') || (str.front() == '"' && str.back() == '"')))
39+
return str.substr(1, str.size() - 2);
40+
return str;
41+
}
42+
43+
std::unordered_map<std::string, std::string> parsedict(const std::string& str) {
44+
std::unordered_map<std::string, std::string> result;
45+
std::string trimmed = stripstr(str);
46+
if (trimmed.size() < 2 || trimmed.front() != '{' || trimmed.back() != '}')
47+
return result;
48+
std::string inner = trimmed.substr(1, trimmed.size() - 2);
49+
std::stringstream ss(inner);
50+
std::string token;
51+
while (std::getline(ss, token, ',')) {
52+
size_t colon = token.find(':');
53+
if (colon == std::string::npos) continue;
54+
std::string key = stripquotes(stripstr(token.substr(0, colon)));
55+
std::string val = stripquotes(stripstr(token.substr(colon + 1)));
56+
if (!key.empty()) result[key] = val;
57+
}
58+
return result;
59+
}
60+
61+
std::vector<std::string> parselist(const std::string& str) {
62+
std::vector<std::string> result;
63+
std::string trimmed = stripstr(str);
64+
if (trimmed.size() < 2 || trimmed.front() != '[' || trimmed.back() != ']')
65+
return result;
66+
std::string inner = trimmed.substr(1, trimmed.size() - 2);
67+
std::stringstream ss(inner);
68+
std::string token;
69+
while (std::getline(ss, token, ',')) {
70+
std::string val = stripstr(token);
71+
if (!val.empty()) result.push_back(val);
72+
}
73+
return result;
74+
}
75+
2976
Concore() {
3077
iport = safe_literal_eval("concore.iport", {});
3178
oport = safe_literal_eval("concore.oport", {});
@@ -39,7 +86,14 @@ class Concore {
3986
std::cerr << "Error reading " << filename << "\n";
4087
return defaultValue;
4188
}
42-
return defaultValue;
89+
std::stringstream buf;
90+
buf << file.rdbuf();
91+
std::string content = buf.str();
92+
try {
93+
return parsedict(content);
94+
} catch (...) {
95+
return defaultValue;
96+
}
4397
}
4498

4599
void load_params() {
@@ -54,8 +108,11 @@ class Concore {
54108
}
55109

56110
if (!sparams.empty() && sparams[0] != '{') {
57-
sparams = "{'" + std::regex_replace(std::regex_replace(std::regex_replace(sparams, std::regex(","), ", '"), std::regex("="), "':"), std::regex(" "), "") + "}";
111+
sparams = "{\"" + std::regex_replace(std::regex_replace(std::regex_replace(sparams, std::regex(","), ",\""), std::regex("="), "\":"), std::regex(" "), "") + "}";
58112
}
113+
try {
114+
params = parsedict(sparams);
115+
} catch (...) {}
59116
}
60117

61118
std::string tryparam(const std::string& n, const std::string& i) {
@@ -106,6 +163,14 @@ class Concore {
106163
}
107164

108165
s += ins;
166+
try {
167+
std::vector<std::string> inval = parselist(ins);
168+
if (!inval.empty()) {
169+
int file_simtime = (int)std::stod(inval[0]);
170+
simtime = std::max(simtime, file_simtime);
171+
return std::vector<std::string>(inval.begin() + 1, inval.end());
172+
}
173+
} catch (...) {}
109174
return {ins};
110175
}
111176

@@ -125,6 +190,17 @@ class Concore {
125190
simtime += delta;
126191
}
127192
}
193+
194+
std::vector<std::string> initval(const std::string& simtime_val) {
195+
try {
196+
std::vector<std::string> val = parselist(simtime_val);
197+
if (!val.empty()) {
198+
simtime = (int)std::stod(val[0]);
199+
return std::vector<std::string>(val.begin() + 1, val.end());
200+
}
201+
} catch (...) {}
202+
return {};
203+
}
128204
};
129205

130206
#endif

0 commit comments

Comments
 (0)