helper.cpp

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

Generated on Fri May 9 11:26:47 2008 for libFAUDES 2.09b by  doxygen 1.4.4