| |
libFAUDES
Sections
Index
|
helper.cppGo to the documentation of this file.00001 /** @file helper.cpp Helper functions */ 00002 00003 /* FAU Discrete Event Systems Library (libfaudes) 00004 00005 Copyright (C) 2006 Bernd Opitz 00006 Exclusive copyright is granted to Klaus Schmidt 00007 00008 This library is free software; you can redistribute it and/or 00009 modify it under the terms of the GNU Lesser General Public 00010 License as published by the Free Software Foundation; either 00011 version 2.1 of the License, or (at your option) any later version. 00012 00013 This library is distributed in the hope that it will be useful, 00014 but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 Lesser General Public License for more details. 00017 00018 You should have received a copy of the GNU Lesser General Public 00019 License along with this library; if not, write to the Free Software 00020 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ 00021 00022 00023 #include "helper.h" 00024 00025 // debug includes 00026 #include "attributes.h" 00027 #include "rtiregistry.h" 00028 #include "rtitypes.h" 00029 00030 /* Include extra defs for mswindows */ 00031 #ifdef FAUDES_WINEXTRA 00032 #include "winextra.h" 00033 #endif 00034 00035 00036 namespace faudes { 00037 00038 // ToStringInteger(number) 00039 std::string ToStringInteger(long int number) { 00040 if(number>= std::numeric_limits<long int>::max()) return "inf"; 00041 if(number<= std::numeric_limits<long int>::min()+1) return "-inf"; 00042 std::string res; 00043 std::stringstream sstr; 00044 sstr << number; 00045 sstr >> res; 00046 return res; 00047 } 00048 00049 // ToStringInteger16(number) 00050 std::string ToStringInteger16(long int number) { 00051 std::string res; 00052 std::stringstream sstr; 00053 sstr << "0x" << std::setbase(16) << number; 00054 sstr >> res; 00055 return res; 00056 } 00057 00058 // ToStringFloat(number) 00059 // todo: check range, prevent sci notation 00060 std::string ToStringFloat(double number) { 00061 if(number == (long int) number) 00062 return(ToStringInteger((long int) number)); 00063 std::string res; 00064 std::stringstream sstr; 00065 sstr << std::fixed; 00066 sstr << number; 00067 sstr >> res; 00068 return res; 00069 } 00070 00071 // ExpandString(rString, len) 00072 std::string ExpandString(const std::string& rString, unsigned int len) { 00073 std::string res; 00074 res = rString; 00075 std::string::size_type xtra = (std::string::size_type) len - rString.length(); 00076 if ((xtra > 0) && (xtra < 10000)) { 00077 res.append(xtra, ' '); 00078 } 00079 return res; 00080 } 00081 00082 // CollapseString(rString, len) 00083 std::string CollapsString(const std::string& rString, unsigned int len) { 00084 if(len <=1) return rString; 00085 if(rString.length() <= len) return rString; 00086 int chead = len/2; 00087 int ctail = len-chead; 00088 return rString.substr(0,chead) + "..." + rString.substr(rString.length()-ctail,ctail); 00089 } 00090 00091 // ToIdx(rString) 00092 Idx ToIdx(const std::string& rString) { 00093 char * end; 00094 unsigned long ul = strtoul (rString.c_str(), &end, 0); 00095 unsigned long idxmax = std::numeric_limits<Idx>::max(); 00096 if (ul > idxmax) { 00097 throw Exception("atoidx", "Idx overflow", 600); 00098 } 00099 else { 00100 return (Idx) ul; 00101 } 00102 } 00103 00104 // String Substitution 00105 std::string StringSubstitute(const std::string& rString,const std::string& rFrom,const std::string& rTo) { 00106 // prep result 00107 std::string res; 00108 std::size_t pos=0; 00109 // loop over occurences of "from" 00110 while(pos<rString.length()) { 00111 std::size_t next=rString.find(rFrom,pos); 00112 if(next==std::string::npos) break; 00113 res.append(rString.substr(pos, next)); 00114 res.append(rTo); 00115 pos=next+rFrom.length(); 00116 } 00117 // get end 00118 if(pos<rString.length()) 00119 res.append(rString.substr(pos)); 00120 // done 00121 return res; 00122 } 00123 00124 // FDVersionString() 00125 std::string FDVersionString() { 00126 return std::string(FAUDES_VERSION); 00127 } 00128 00129 // FDPluginsString() 00130 std::string FDPluginsString() { 00131 return std::string(FAUDES_PLUGINS); 00132 } 00133 00134 // FDContributorsString() 00135 std::string FDContributorsString() { 00136 return 00137 "Berndt, Breindl, Barthel, Doerr, Duevel, Franchi, Hellenschmidt, Mohr, Moor, Musunoi, " 00138 "Opitz, Petzoldt, Perk, Rempel, Ritter, Schlein, Schmidt, Singer, Wittmann, Zaddach, et al"; 00139 } 00140 00141 00142 // ProcessDot(infile,outfile,format) 00143 void ProcessDot(const std::string& rDotFile, 00144 const std::string& rOutFile, const std::string& rOutFormat, const std::string& rDotExec) 00145 { 00146 std::string format=rOutFormat; 00147 // guess format from filename 00148 if(format=="") { 00149 if(rOutFile.rfind('.')+1 < rOutFile.size()) { 00150 format=rOutFile.substr(rOutFile.rfind('.')+1); 00151 } 00152 } 00153 // test format 00154 if (format == "canon"); 00155 else if (format == "dot"); 00156 else if (format == "xdot"); 00157 else if (format == "cmap"); 00158 else if (format == "dia"); 00159 else if (format == "fig"); 00160 else if (format == "gd"); 00161 else if (format == "gd2"); 00162 else if (format == "gif"); 00163 else if (format == "hpgl"); 00164 else if (format == "imap"); 00165 else if (format == "cmapx"); 00166 else if (format == "ismap"); 00167 else if (format == "jpg"); 00168 else if (format == "jpeg"); 00169 else if (format == "mif"); 00170 else if (format == "mp"); 00171 else if (format == "pcl"); 00172 else if (format == "pic"); 00173 else if (format == "plain"); 00174 else if (format == "plain-ext"); 00175 else if (format == "png"); 00176 else if (format == "ps"); 00177 else if (format == "ps2"); 00178 else if (format == "svg"); 00179 else if (format == "svgz"); 00180 else if (format == "vrml"); 00181 else if (format == "vtx"); 00182 else if (format == "wbmp"); 00183 else { 00184 std::stringstream errstr; 00185 errstr << "Dot output format \"" << format << "\" unknown"; 00186 throw Exception("faudes::ProcessDot", errstr.str(), 3); 00187 } 00188 std::string dotcommand = rDotExec + " -T"+format+" \""+rDotFile+"\" -o \""+rOutFile+"\""; 00189 if(system(dotcommand.c_str()) != 0) { 00190 throw Exception("faudes::ProcessDot", 00191 "Error in running " + dotcommand, 3); 00192 } 00193 } 00194 00195 00196 // CreateTempFile(void) 00197 // todo: sys dependant, report, investigate threads 00198 std::string CreateTempFile(void) { 00199 char filename[]= "faudes_temp_XXXXXX"; 00200 int filedes = mkstemp(filename); 00201 if(filedes==-1) { 00202 FD_DF("faudes::CreateTempFile(): error"); 00203 return ""; 00204 } 00205 close(filedes); 00206 std::string res(filename); 00207 FD_DF("faudes::CreateTempFile(): " << res); 00208 return(res); 00209 } 00210 00211 00212 // RemoveFile(void) 00213 // todo: sys dependant * 00214 bool RemoveFile(const std::string& rFileName) { 00215 return std::remove(rFileName.c_str()) == 0; 00216 } 00217 00218 00219 #ifndef FAUDES_DIR_SEPARATORS 00220 #define FAUDES_DIR_SEPARATORS "/" 00221 #endif 00222 00223 // ExtractPath 00224 std::string ExtractDirectory(const std::string& rFullPath) { 00225 std::string res=""; 00226 unsigned int seppos = rFullPath.find_last_of(FAUDES_DIR_SEPARATORS); 00227 if(seppos==std::string::npos) return res; 00228 res=rFullPath.substr(0,seppos+1); 00229 return res; 00230 } 00231 00232 // ExtractFilename 00233 std::string ExtractFilename(const std::string& rFullPath) { 00234 std::string res=rFullPath; 00235 unsigned int seppos = rFullPath.find_last_of(FAUDES_DIR_SEPARATORS); 00236 if(seppos==std::string::npos) return res; 00237 res=rFullPath.substr(seppos+1); 00238 return res; 00239 } 00240 00241 // PrependPath 00242 std::string PrependDirectory(const std::string& rDirectory, const std::string& rFileName) { 00243 std::string res=rDirectory; 00244 char sepchar=std::string(FAUDES_DIR_SEPARATORS).at(0); 00245 if(res!="") 00246 if(res.at(res.length()-1)!=sepchar) 00247 res.append(1,sepchar); 00248 res.append(rFileName); 00249 return res; 00250 } 00251 00252 // Debug/Error Logging 00253 std::ostream* pDebugStream=NULL; 00254 std::ostream* DebugStream(void) { return pDebugStream ? pDebugStream : &std::cerr;} 00255 void DebugToStream(std::ostream* pStream) { 00256 pDebugStream = pStream; 00257 } 00258 void DebugToFile(const std::string& rFilename) { 00259 if(pDebugStream) delete pDebugStream; 00260 std::ofstream* fdebug = new std::ofstream(); 00261 fdebug->open(rFilename.c_str()); 00262 DebugToStream(fdebug); 00263 } 00264 00265 00266 00267 // debugging: objectcount 00268 std::map<std::string,long int>* ObjectCount::mspMax=NULL; 00269 std::map<std::string,long int>* ObjectCount::mspCount=NULL; 00270 bool ObjectCount::msDone=false; 00271 ObjectCount::ObjectCount(void) { 00272 mspCount= new std::map<std::string,long int>(); 00273 mspMax= new std::map<std::string,long int>(); 00274 msDone=true; 00275 } 00276 void ObjectCount::Init(void) { 00277 if(!msDone) ObjectCount(); 00278 } 00279 void ObjectCount::Inc(const std::string& rTypeName) { 00280 if(!msDone) ObjectCount(); 00281 long int cnt = ((*mspCount)[rTypeName]+=1); 00282 if((*mspMax)[rTypeName]<cnt) (*mspMax)[rTypeName]=cnt; 00283 } 00284 void ObjectCount::Dec(const std::string& rTypeName) { 00285 if(!msDone) ObjectCount(); 00286 (*mspCount)[rTypeName]-=1; 00287 } 00288 00289 00290 // debugging: report on exit function 00291 void ExitFunction(void){ 00292 #ifdef FAUDES_DEBUG_CODE 00293 FAUDES_WRITE_STDERR("faudes::ExitFunction:"); 00294 // be sure its up and running 00295 ObjectCount::Init(); 00296 // get rid of all registry prototypes 00297 TypeRegistry::G()->Clear(); 00298 // prepare report 00299 std::map<std::string,long int>::iterator cit; 00300 cit=ObjectCount::mspCount->begin(); 00301 for(;cit!=ObjectCount::mspCount->end();cit++) { 00302 FAUDES_WRITE_STDERR( cit->first << ": #" << ToStringInteger(cit->second) << 00303 " (max #" << (*ObjectCount::mspMax)[cit->first] << ")"); 00304 } 00305 #endif 00306 } 00307 00308 00309 #ifdef FAUDES_DEBUG_CODE 00310 // report on exit install 00311 class ExitFunctionInstall { 00312 private: 00313 static bool mDone; 00314 static ExitFunctionInstall mInstance; 00315 ExitFunctionInstall(void) { 00316 if(mDone) return; 00317 FAUDES_WRITE_STDERR("ExitFunctionInstall()"); 00318 std::atexit(ExitFunction); 00319 mDone=true; 00320 } 00321 }; 00322 // exit function: global data 00323 bool ExitFunctionInstall::mDone=false; 00324 ExitFunctionInstall ExitFunctionInstall::mInstance; 00325 #endif 00326 00327 // test protocol: token writer 00328 TokenWriter* gTestProtocolTw=NULL; 00329 00330 // test protocol: setup 00331 void TestProtocol(const std::string& rSource) { 00332 if(gTestProtocolTw) delete gTestProtocolTw; 00333 std::string filename=rSource; 00334 // remove directory 00335 filename=ExtractFilename(filename); 00336 // remove extension 00337 std::string::size_type pos=0; 00338 for(;pos<filename.length();pos++) 00339 if(filename.at(pos)=='.') filename.at(pos)='_'; 00340 // append extansion 00341 filename.append(".prot"); 00342 // prepend prefix 00343 filename.insert(0,"tmp_"); 00344 // initialise token writer 00345 gTestProtocolTw= new TokenWriter(filename); 00346 } 00347 00348 // test protocol: dump 00349 void TestProtocol(const std::string& rSource, const std::string& rMessage, const Type& rData, bool core) { 00350 if(!gTestProtocolTw) TestProtocol(rSource); 00351 gTestProtocolTw->WriteComment("%%% test mark: " + rMessage); 00352 if(core) rData.Write(*gTestProtocolTw); 00353 else rData.SWrite(*gTestProtocolTw); 00354 gTestProtocolTw->WriteComment(""); 00355 gTestProtocolTw->WriteComment(""); 00356 gTestProtocolTw->WriteComment(""); 00357 *gTestProtocolTw << "\n"; 00358 } 00359 void TestProtocol(const std::string& rSource, const std::string& rMessage, bool data) { 00360 Boolean fbool(data); 00361 TestProtocol(rSource,rMessage,fbool,true); 00362 } 00363 void TestProtocol(const std::string& rSource, const std::string& rMessage, long int data) { 00364 Integer fint(data); 00365 TestProtocol(rSource,rMessage,fint,true); 00366 } 00367 void TestProtocol(const std::string& rSource, const std::string& rMessage, const std::string& rData) { 00368 String fstr(rData); 00369 TestProtocol(rSource,rMessage,fstr,true); 00370 } 00371 00372 00373 } // namespace faudes |
libFAUDES 2.14g --- 2009-12-3 --- c++ source docu by doxygen 1.5.6