00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "BaseRegmas.h"
00021 #include "ThreadManager.h"
00022 #include <algorithm>
00023
00024
00025 using namespace std;
00026
00027 BaseRegmas::BaseRegmas()
00028 {
00029 MTHREAD=NULL;
00030 }
00031
00032 BaseRegmas::~BaseRegmas()
00033 {
00034 }
00035
00044 void
00045 BaseRegmas::msgOut(int msgCode_h, string msg_h, bool refreshGUI_h) const {
00046
00047 msgOut2(msgCode_h, msg_h, refreshGUI_h);
00048
00049 }
00050
00059 void
00060 BaseRegmas::msgOut(int msgCode_h, int msg_h, bool refreshGUI_h) const {
00061 ostringstream out;
00062 out<<msg_h;
00063 msgOut2(msgCode_h, out.str(), refreshGUI_h);
00064 }
00065
00074 void
00075 BaseRegmas::msgOut(int msgCode_h, double msg_h, bool refreshGUI_h) const {
00076
00077 ostringstream out;
00078 out<<msg_h;
00079 msgOut2(msgCode_h, out.str(), refreshGUI_h);
00080
00081 }
00082
00087 void
00088 BaseRegmas::msgOut2(int msgCode_h, string msg_h, bool refreshGUI_h) const {
00089
00090 string prefix;
00091 switch (msgCode_h){
00092 case MSG_NO_MSG:
00093 return;
00094 case MSG_DEBUG:
00095 prefix="*DEBUG: ";
00096 break;
00097 case MSG_INFO:
00098 prefix="**INFO: ";
00099 break;
00100 case MSG_WARNING:
00101 prefix="**WARNING: ";
00102 break;
00103 case MSG_ERROR:
00104 prefix="***ERROR: ";
00105 break;
00106 case MSG_CRITICAL_ERROR:
00107 prefix="****CRITICAL ERROR: ";
00108 break;
00109 default:
00110 cerr<<"I got an unknow error code: "<<msgCode_h<<" ("<<msg_h<<")"<<endl;
00111 exit(EXIT_FAILURE);
00112 }
00113
00114 string message = prefix+msg_h;
00115 if (MTHREAD && MTHREAD->usingGUI()){
00116 MTHREAD->msgOut(msgCode_h, message);
00117 }
00118 else {
00119 string totalMsg = prefix+msg_h;
00120 cout<< totalMsg <<endl;
00121 }
00122
00123 if(refreshGUI_h) {refreshGUI();}
00124
00125 if(msgCode_h==MSG_CRITICAL_ERROR){
00126 if (MTHREAD && MTHREAD->usingGUI()){
00127 throw(2);
00128 }
00129 else {
00130
00131 exit(EXIT_FAILURE);
00132 }
00133 }
00134 }
00135
00136 void
00137 BaseRegmas::refreshGUI()const{
00138 if (MTHREAD && MTHREAD->usingGUI()){
00139 MTHREAD->refreshGUI();
00140 }
00141 };
00142
00143 int
00144 BaseRegmas::s2i ( string string_h) const {
00145 if (string_h == "") return 0;
00146 int valueAsInteger;
00147 string valueAsString = string_h;
00148 istringstream totalSString( valueAsString );
00149 totalSString >> valueAsInteger;
00150 return valueAsInteger;
00151 }
00152
00154 double
00155 BaseRegmas::s2d ( string string_h) const {
00156 if (string_h == "") return 0;
00157 double valueAsDouble;
00158 string valueAsString = string_h;
00159
00160 replace(valueAsString.begin(), valueAsString.end(), ',', '.');
00161 istringstream totalSString( valueAsString );
00162 totalSString >> valueAsDouble;
00163 return valueAsDouble;
00164 }
00165
00167 bool
00168 BaseRegmas::s2b ( string string_h) const {
00169 if (string_h == "true" || string_h == "vero" || string_h == "TRUE" || string_h == "1")
00170 return true;
00171 else if (string_h == "false" || string_h == "falso" || string_h == "FALSE" || string_h == "0")
00172 return false;
00173
00174 msgOut(MSG_ERROR, "Sorry, I don't know how to convert "+string_h+" to a bool value. I return true... hope for the best");
00175 return true;
00176 }
00177
00178 string
00179 BaseRegmas::i2s (int int_h) const{
00180 ostringstream out;
00181 out<<int_h;
00182 return out.str();
00183 }
00184
00185 string
00186 BaseRegmas::d2s (double double_h) const{
00187 ostringstream out;
00188 out<<double_h;
00189 return out.str();
00190 }
00191
00192 string
00193 BaseRegmas::b2s (bool bool_h) const{
00194 string out;
00195 if(bool_h)
00196 out = "true";
00197 else
00198 out = "false";
00199 return out;
00200 }
00201
00202 vector <int>
00203 BaseRegmas::s2i( vector <string> string_h) const{
00204 vector <int> valuesAsInteger;
00205 for (uint i=0;i<string_h.size();i++){
00206 if (string_h.at(i) == "") {
00207 valuesAsInteger.push_back(0);
00208 continue;
00209 }
00210 int valueAsInteger;
00211 istringstream totalSString( string_h.at(i));
00212 totalSString >> valueAsInteger;
00213 valuesAsInteger.push_back(valueAsInteger);
00214 }
00215 return valuesAsInteger;
00216 };
00217
00219 vector <double>
00220 BaseRegmas::s2d (vector <string> string_h) const{
00221 vector <double> valuesAsDouble;
00222 for (uint i=0;i<string_h.size();i++){
00223 if (string_h.at(i) == "") {
00224 valuesAsDouble.push_back(0);
00225 continue;
00226 }
00227 double valueAsDouble;
00228
00229 replace(string_h.at(i).begin(), string_h.at(i).end(), ',', '.');
00230 istringstream totalSString( string_h.at(i));
00231 totalSString >> valueAsDouble;
00232 valuesAsDouble.push_back(valueAsDouble);
00233 }
00234 return valuesAsDouble;
00235 }
00236
00238 vector <bool>
00239 BaseRegmas::s2b( vector <string> string_h) const{
00240 vector <bool> valuesAsBool;
00241 for (uint i=0;i<string_h.size();i++){
00242 valuesAsBool.push_back(s2b(string_h.at(i)));
00243 }
00244 return valuesAsBool;
00245 };
00246
00247 vector <string>
00248 BaseRegmas::i2s (vector <int> int_h) const{
00249 vector <string> valuesAsString;
00250 for (uint i=0;i<int_h.size();i++){
00251 ostringstream valueAsString;
00252 valueAsString<<int_h.at(i);
00253 valuesAsString.push_back(valueAsString.str());
00254 }
00255 return valuesAsString;
00256 }
00257
00258 vector <string>
00259 BaseRegmas::d2s (vector <double> double_h) const{
00260 vector <string> valuesAsString;
00261 for (uint i=0;i<double_h.size();i++){
00262 ostringstream valueAsString;
00263 valueAsString<<double_h.at(i);
00264 valuesAsString.push_back(valueAsString.str());
00265 }
00266 return valuesAsString;
00267 }
00268
00269 vector <string>
00270 BaseRegmas::b2s (vector <bool> bool_h) const{
00271 vector <string> valuesAsString;
00272 for (uint i=0;i<bool_h.size();i++){
00273 string temp;
00274 if(bool_h.at(i)) temp = "true";
00275 else temp = "false";
00276 valuesAsString.push_back(temp);
00277 }
00278 return valuesAsString;
00279 }
00280
00281
00282 int
00283 BaseRegmas::getType(string &type_h){
00284 int toReturn=0;
00285 if (type_h == "int") toReturn = TYPE_INT;
00286 else if (type_h == "double") toReturn = TYPE_DOUBLE;
00287 else if (type_h == "string") toReturn = TYPE_STRING;
00288 else if (type_h == "bool") toReturn = TYPE_BOOL;
00289 else msgOut(MSG_CRITICAL_ERROR, "Unknow type "+type_h+".");
00290 return toReturn;
00291 }
00292
00293
00294 template<typename T> std::string
00295 BaseRegmas::toString(const T& x) {
00296 std::ostringstream oss;
00297 oss << x;
00298 return oss.str();
00299 }
00300
00301 template<typename T> T
00302 BaseRegmas::stringTo(const std::string& s) {
00303 std::istringstream iss(s);
00304 T x;
00305 iss >> x;
00306 return x;
00307 }
00308
00309
00310 int
00311 BaseRegmas::iVSum (const vector<int> &intVector_h){
00312 int toReturn=0;
00313 for(uint i=0;i<intVector_h.size();i++){
00314 toReturn += intVector_h[i];
00315 }
00316 return toReturn;
00317 }
00318
00319 double
00320 BaseRegmas::dVSum (const vector<double> &doubleVector_h){
00321 double toReturn=0;
00322 for(uint i=0;i<doubleVector_h.size();i++){
00323 toReturn += doubleVector_h[i];
00324 }
00325 return toReturn;
00326 }
00327
00328
00329 void
00330 BaseRegmas::tokenize(const string& str, vector<string>& tokens, const string& delimiter){
00331
00332 string::size_type lastPos = str.find_first_not_of(delimiter, 0);
00333
00334 string::size_type pos = str.find_first_of(delimiter, lastPos);
00335
00336 while (string::npos != pos || string::npos != lastPos)
00337 {
00338
00339 tokens.push_back(str.substr(lastPos, pos - lastPos));
00340
00341 lastPos = str.find_first_not_of(delimiter, pos);
00342
00343 pos = str.find_first_of(delimiter, lastPos);
00344 }
00345 }