helper.cpp

Go to the documentation of this file.
00001 
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 /* Include extra defs for mswindows */
00026 #ifdef FAUDES_WINEXTRA
00027 #include "winextra.h"
00028 #endif
00029 
00030 
00031 namespace faudes {
00032 
00033 // ToStringInteger(number)
00034 std::string ToStringInteger(long int number) { 
00035    std::string res;
00036    std::stringstream sstr;
00037    sstr << number;
00038    sstr >> res;
00039    return res;
00040 }
00041 
00042 // ToStringInteger16(number)
00043 std::string ToStringInteger16(long int number) { 
00044    std::string res;
00045    std::stringstream sstr;
00046    sstr << "0x" << std::setbase(16) << number;
00047    sstr >> res;
00048    return res;
00049 }
00050 
00051 // ToStringFloat(number)
00052 // todo: check range, prevent sci notation
00053 std::string ToStringFloat(double number) { 
00054    if(number == (long int) number) 
00055      return(ToStringInteger((long int) number));
00056    std::string res;
00057    std::stringstream sstr;
00058    sstr << std::fixed;
00059    sstr << number;
00060    sstr >> res;
00061    return res;
00062 }
00063 
00064 // ExpandString(rString, len) 
00065 std::string ExpandString(const std::string& rString, unsigned int len) {
00066   std::string res;
00067   res = rString;
00068   std::string::size_type xtra = (std::string::size_type) len - rString.length();
00069   if ((xtra > 0) && (xtra < 10000)) {
00070     res.append(xtra, ' ');
00071   }
00072   return res;
00073 } 
00074 
00075 // CollapseString(rString, len) 
00076 std::string  CollapsString(const std::string& rString, unsigned int len) {
00077   if(len <0) return rString;
00078   if(rString.length() < len) return rString;
00079   int head = len/2;
00080   int tail = len/2;
00081   return rString.substr(0,head) + "..." + rString.substr(len-tail,tail);
00082 } 
00083 
00084 // ToIdx(rString)
00085 Idx ToIdx(const std::string& rString) {
00086   char * end;
00087   unsigned long ul = strtoul (rString.c_str(), &end, 0);
00088   unsigned long idxmax = std::numeric_limits<Idx>::max();
00089   if (ul > idxmax) {
00090     throw Exception("atoidx", "Idx overflow", 600);
00091   }
00092   else {
00093     return (Idx) ul;
00094   }
00095 }
00096 
00097 // FDVersionString()
00098 std::string FDVersionString() {
00099   return std::string(FAUDES_VERSION);
00100 }
00101 
00102 // FDPluginsString()
00103 std::string FDPluginsString() {
00104   return std::string(FAUDES_PLUGINS);
00105 }
00106 
00107 // FDContributorsString()
00108 std::string FDContributorsString() {
00109   return 
00110     "Berndt, Breindel, Doerr, Duevel, Franchi, Hellenschmidt, Moor, Musunoi, "
00111     "Opitz, Perk, Rempel, Ritter, Schlein, Schmidt, Singer, Wittmann, Zaddach, et al";
00112 }
00113 
00114 
00115 // ProcessDot(infile,outfile,format)
00116 void ProcessDot(const std::string& rDotFile, 
00117   const std::string& rOutFile, const std::string& rOutFormat, const std::string& rDotExec)
00118 {
00119   std::string format=rOutFormat;
00120   // guess format from filename
00121   if(format=="") {
00122     if(rOutFile.rfind('.')+1 < rOutFile.size()) {
00123        format=rOutFile.substr(rOutFile.rfind('.')+1);
00124     }
00125   }  
00126   // test format
00127   if (format == "canon"); 
00128   else if (format == "dot"); 
00129   else if (format == "xdot"); 
00130   else if (format == "cmap");
00131   else if (format == "dia");
00132   else if (format == "fig"); 
00133   else if (format == "gd"); 
00134   else if (format == "gd2"); 
00135   else if (format == "gif"); 
00136   else if (format == "hpgl"); 
00137   else if (format == "imap"); 
00138   else if (format == "cmapx");
00139   else if (format == "ismap"); 
00140   else if (format == "jpg"); 
00141   else if (format == "jpeg"); 
00142   else if (format == "mif"); 
00143   else if (format == "mp"); 
00144   else if (format == "pcl"); 
00145   else if (format == "pic"); 
00146   else if (format == "plain"); 
00147   else if (format == "plain-ext"); 
00148   else if (format == "png"); 
00149   else if (format == "ps"); 
00150   else if (format == "ps2"); 
00151   else if (format == "svg"); 
00152   else if (format == "svgz"); 
00153   else if (format == "vrml"); 
00154   else if (format == "vtx"); 
00155   else if (format == "wbmp"); 
00156   else {
00157     std::stringstream errstr;
00158     errstr << "Dot output format \"" << format << "\" unknown";
00159     throw Exception("faudes::ProcessDot", errstr.str(), 3);
00160   }
00161   std::string dotcommand = rDotExec + " -T"+format+" \""+rDotFile+"\" -o \""+rOutFile+"\"";
00162   if (system(dotcommand.c_str()) != 0) {
00163     throw Exception("faudes::ProcessDot", 
00164         "Error in running dot", 3);
00165   }
00166 }
00167 
00168 
00169 // CreateTempFile(void)
00170 // todo: sys dependant, report, investigate threads
00171 std::string CreateTempFile(void) {
00172   char filename[]= "faudes_temp_XXXXXX";
00173   int filedes = mkstemp(filename);
00174   if(filedes==-1) {
00175     FD_DF("faudes::CreateTempFile(): error");
00176     return "";
00177   }
00178   close(filedes);
00179   std::string res(filename);
00180   FD_DF("faudes::CreateTempFile(): " << res);
00181   return(res);
00182 }
00183 
00184 
00185 // RemoveFile(void)
00186 // todo: sys dependant *
00187 bool RemoveFile(const std::string& rFileName) {
00188   return std::remove(rFileName.c_str()) == 0;
00189 }
00190 
00191 
00192 #ifndef FAUDES_DIR_SEPARATORS 
00193 #define FAUDES_DIR_SEPARATORS "/"
00194 #endif
00195 
00196 // ExtractPath
00197 std::string ExtractPath(const std::string& rFullName) {
00198   std::string res="";
00199   unsigned int seppos = rFullName.find_last_of(FAUDES_DIR_SEPARATORS);
00200   if(seppos==std::string::npos) return res;
00201   res=rFullName.substr(0,seppos+1);
00202   return res;
00203 }
00204 
00205 // PrependPath
00206 std::string PrependPath(const std::string& rPathName, const std::string& rFileName) {
00207   std::string res=rPathName;
00208   char sepchar=std::string(FAUDES_DIR_SEPARATORS).at(0);
00209   if(res!="")
00210   if(res.at(res.length()-1)!=sepchar)
00211     res.append(1,sepchar);
00212   res.append(rFileName);
00213   return res;
00214 }
00215 
00216 // Debug/Error Logging
00217 std::ofstream* pDebugFileStream=NULL; 
00218 std::ostream* DebugStream(void) { return pDebugFileStream ? pDebugFileStream : &std::cerr;}
00219 void DebugToFile(const std::string& rFilename) {
00220   if(pDebugFileStream) delete pDebugFileStream;
00221   pDebugFileStream = new std::ofstream();
00222   pDebugFileStream->open(rFilename.c_str());
00223 }
00224 
00225 } // namespace faudes

Generated on Mon Nov 10 08:13:14 2008 for libFAUDES 2.11v by  doxygen 1.4.4