11// concore.hpp -- this C++ include file will be the equivalent of concore.py
2+ #ifndef CONCORE_HPP
3+ #define CONCORE_HPP
4+
25#include < iostream>
36#include < vector>
47#include < iomanip> // for setprecision
1922#endif
2023#include < cstring>
2124#include < cctype>
25+ #include < regex>
2226
2327using namespace std ;
2428
@@ -47,8 +51,10 @@ class Concore{
4751 double delay = 1 ;
4852 int retrycount = 0 ;
4953 double simtime;
54+ int maxtime = 100 ;
5055 map <string, int > iport;
5156 map <string, int > oport;
57+ map <string, string> params;
5258
5359 /* *
5460 * @brief Constructor for Concore class.
@@ -57,7 +63,9 @@ class Concore{
5763 */
5864 Concore (){
5965 iport = mapParser (" concore.iport" );
60- oport = mapParser (" concore.oport" );
66+ oport = mapParser (" concore.oport" );
67+ default_maxtime (100 );
68+ load_params ();
6169
6270 int iport_number = -1 ;
6371 int oport_number = -1 ;
@@ -592,6 +600,96 @@ class Concore{
592600 }
593601 }
594602
603+ /* *
604+ * @brief Strips leading and trailing whitespace from a string.
605+ * @param str The input string.
606+ * @return The stripped string.
607+ */
608+ string stripstr (string str){
609+ size_t start = str.find_first_not_of (" \t\n\r " );
610+ if (start == string::npos) return " " ;
611+ size_t end = str.find_last_not_of (" \t\n\r " );
612+ return str.substr (start, end - start + 1 );
613+ }
614+
615+ /* *
616+ * @brief Strips surrounding single or double quotes from a string.
617+ * @param str The input string.
618+ * @return The unquoted string.
619+ */
620+ string stripquotes (string str){
621+ if (str.size () >= 2 && ((str.front () == ' \' ' && str.back () == ' \' ' ) || (str.front () == ' "' && str.back () == ' "' )))
622+ return str.substr (1 , str.size () - 2 );
623+ return str;
624+ }
625+
626+ /* *
627+ * @brief Parses a dict-formatted string into a string-to-string map.
628+ * @param str The input string in {key: val, ...} format.
629+ * @return A map of key-value string pairs.
630+ */
631+ map<string, string> parsedict (string str){
632+ map<string, string> result;
633+ string trimmed = stripstr (str);
634+ if (trimmed.size () < 2 || trimmed.front () != ' {' || trimmed.back () != ' }' )
635+ return result;
636+ string inner = trimmed.substr (1 , trimmed.size () - 2 );
637+ stringstream ss (inner);
638+ string token;
639+ while (getline (ss, token, ' ,' )) {
640+ size_t colon = token.find (' :' );
641+ if (colon == string::npos) continue ;
642+ string key = stripquotes (stripstr (token.substr (0 , colon)));
643+ string val = stripquotes (stripstr (token.substr (colon + 1 )));
644+ if (!key.empty ()) result[key] = val;
645+ }
646+ return result;
647+ }
648+
649+ /* *
650+ * @brief Sets maxtime from the concore.maxtime file, falling back to defaultValue.
651+ * @param defaultValue The fallback value if the file is missing.
652+ */
653+ void default_maxtime (int defaultValue){
654+ maxtime = defaultValue;
655+ ifstream file (inpath + " /1/concore.maxtime" );
656+ if (file) {
657+ file >> maxtime;
658+ }
659+ }
660+
661+ /* *
662+ * @brief Loads simulation parameters from concore.params into the params map.
663+ */
664+ void load_params (){
665+ ifstream file (inpath + " /1/concore.params" );
666+ if (!file) return ;
667+ stringstream buffer;
668+ buffer << file.rdbuf ();
669+ string sparams = buffer.str ();
670+
671+ if (!sparams.empty () && sparams[0 ] == ' "' ) {
672+ sparams = sparams.substr (1 , sparams.find (' "' , 1 ) - 1 );
673+ }
674+
675+ if (!sparams.empty () && sparams[0 ] != ' {' ) {
676+ sparams = " {\" " + regex_replace (regex_replace (regex_replace (sparams, regex (" ," ), " ,\" " ), regex (" =" ), " \" :" ), regex (" " ), " " ) + " }" ;
677+ }
678+ try {
679+ params = parsedict (sparams);
680+ } catch (...) {}
681+ }
682+
683+ /* *
684+ * @brief Returns the value of a param by name, or a default if not found.
685+ * @param n The parameter name.
686+ * @param i The default value.
687+ * @return The parameter value or the default.
688+ */
689+ string tryparam (string n, string i){
690+ return params.count (n) ? params[n] : i;
691+ }
692+
595693 /* *
596694 * @brief Initializes the system with the given input values.
597695 * @param f The input string containing the values.
@@ -601,6 +699,8 @@ class Concore{
601699 // parsing
602700 vector<double > val = parser (f);
603701
702+ if (val.empty ()) return val;
703+
604704 // determining simtime
605705 simtime = val[0 ];
606706
@@ -609,3 +709,5 @@ class Concore{
609709 return val;
610710 }
611711};
712+
713+ #endif // CONCORE_HPP
0 commit comments