libFAUDES

Sections

Index

helper.h

Go to the documentation of this file.
00001 /** @file helper.h   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 #ifndef FAUDES_HELPER_H
00024 #define FAUDES_HELPER_H
00025 
00026 #include <cstdlib>
00027 #include <cstring>
00028 #include <limits>
00029 #include <string>
00030 #include <iostream>
00031 #include <sstream>
00032 #include <fstream>
00033 #include <iomanip>
00034 #include <map>
00035 
00036 
00037 #include "definitions.h"
00038 #include "exception.h"
00039 
00040 namespace faudes {
00041 
00042 // forward
00043 class Type;
00044 
00045 /**
00046  * integer to string
00047  * 
00048  * @param number
00049  *   integer
00050  *
00051  * @return
00052  *   string
00053  */
00054 std::string ToStringInteger(long int number);
00055 
00056 
00057 /**
00058  * integer to string base 16
00059  * 
00060  * @param number
00061  *   integer
00062  *
00063  * @return
00064  *   string
00065  */
00066 std::string ToStringInteger16(long int number);
00067 
00068 /**
00069  * float to string 
00070  * 
00071  * @param number
00072  *   double
00073  *
00074  * @return
00075  *   string
00076  */
00077 std::string ToStringFloat(double number);
00078 
00079 
00080 /**
00081  * Fill string with spaces up to a given length if length of the string
00082  * is smaller than given length parameter.
00083  *
00084  * @param rString
00085  *   string
00086  * @param len
00087  *   Minimum number of charakters in string
00088  *
00089  * @return
00090  *   Expanded string
00091  */
00092 std::string ExpandString(const std::string& rString, unsigned int len);
00093 
00094 /**
00095  * Limit length of string, return head and tail of string
00096  *
00097  * @param rString
00098  *   string
00099  * @param len
00100  *   Maximum number of charakters in string (approx)
00101  *
00102  * @return
00103  *   Collapsed string
00104  */
00105  std::string CollapsString(const std::string& rString, unsigned int len= FD_MAXCONTAINERNAME);
00106 
00107 
00108 /**
00109  * Convert a string to Idx
00110  *
00111  * @param rString
00112  *   string to convert
00113  *
00114  * @return 
00115  *   Idx
00116  *
00117  * @exception
00118  *   Idx overflow (id 600)
00119  */
00120 Idx ToIdx(const std::string& rString);
00121 
00122 /**
00123  * Substitute in string
00124  *
00125  * @param rString
00126  *   Source string to substitute
00127  * @param rFrom
00128  *   String to match
00129  * @param rTo
00130  *   Replacement to fill in 
00131  * @return 
00132  *   Result string
00133  *
00134  */
00135 std::string StringSubstitute(const std::string& rString,const std::string& rFrom,const std::string& rTo);
00136    
00137 
00138 
00139 /**
00140  * Return FAUDES_VERSION as std::string
00141  *
00142  * @return 
00143  *   std::string with FAUDES_VERSION
00144  */
00145 std::string FDVersionString();
00146 
00147 /**
00148  * Return FAUDES_PLUGINS as std::string
00149  *
00150  * @return 
00151  *   std::string with FAUDES_VERSION
00152  */
00153 std::string FDPluginsString();
00154 
00155 /**
00156  * Return contributors as std::string
00157  *
00158  * @return 
00159  *   std::string 
00160  */
00161 std::string FDContributorsString();
00162 
00163 /**
00164  * Convenience function: process dot file
00165  * to graphics output.  If no output format is given,
00166  * try to guess from filename extension. 
00167  *
00168  * @param rDotFile
00169  *   name of dot file
00170  * @param rOutFile
00171  *   name of graphics file 
00172  * @param rOutFormat
00173  *   graphics format eg "png", "jpg"
00174  * @param rDotExec
00175  *   path/name of executable
00176  *
00177  * @exception Exception
00178  *   - error in systemcall (id 3)
00179  *   - unkown format (id 3)
00180  *
00181  */
00182 void ProcessDot(const std::string& rDotFile, const std::string& rOutFile, 
00183   const std::string& rOutFormat = "", const std::string& rDotExec = "dot");
00184 
00185 /**
00186  * Create a temp file, length 0
00187  *
00188  * @return
00189  *   Name of temp file
00190  */
00191 
00192 std::string CreateTempFile(void);
00193 
00194 
00195 /**
00196  * Delete a file
00197  *
00198  * @param rFileName
00199  *   Name of file to delete
00200  */
00201 bool RemoveFile(const std::string& rFileName);
00202 
00203 /**
00204  * Extract directory from full path.
00205  *
00206  * @param rFullPath
00207  *   Full name of file eg "/home/friend/data/generator.gen"
00208  * @return
00209  *   Directory eg "/home/friend/data"
00210  */
00211 std::string ExtractDirectory(const std::string& rFullPath);
00212 
00213 /**
00214  * Extract file name from full path.
00215  *
00216  * @param rFullName
00217  *   Full path of file eg "/home/friend/data/generator.gen"
00218  * @return
00219  *   Filename "generator.gen"
00220  */
00221 std::string ExtractFilename(const std::string& rFullName);
00222 
00223 /**
00224  * Construct full path from directory and filename.
00225  *
00226  * @param rDirectory
00227  *   Directory eg "/home/friend/data"
00228  * @param rFileName
00229  *   File eg "generator.gen"
00230  * @return
00231  *   Path eg "/home/friend/data/generator.gen"
00232  */
00233 std::string PrependDirectory(const std::string& rDirectory, const std::string& rFileName);
00234 
00235 
00236 /**
00237  * Set filename for debug log.
00238  * Debugging to file is meant for platforms that dont provide
00239  * a console for std::err (eg embedded systems); see also
00240  * FAUDES_WRITE_STDERR makro in definitions.
00241  *
00242  * @param rFileName
00243  *   File to log to 
00244  */
00245 void DebugToFile(const std::string& rFileName="tmp_faudes_debug.log");
00246 
00247 /**
00248  * Set stream for debug log.
00249  *
00250  * @param pStream
00251  *   Stream to log to 
00252  */
00253 void DebugToStream(std::ostream* pStream);
00254 
00255 /**
00256  * Stream used for debugging
00257  */
00258 extern std::ostream* pDebugStream;
00259 
00260 /**
00261  * Get debugging stream.
00262  *
00263  * @return
00264  *   Debugging Output Stream 
00265  */
00266 std::ostream* DebugStream(void);
00267 
00268 /**
00269  * Debugging counter. Counts items as specified by the type string and reports
00270  * sums on exit. You must define the macro FAUDES_DEBUG_CODE to get a report.
00271  *
00272  * Technical note: we use the somewhat winded static member construct to
00273  * guerantee that our member variables have been constructed befor actual
00274  * counting occurs. This is neccessary since some faudes types might have a
00275  * static instance and, hence, may be consructed rather early. 
00276  *
00277  */
00278 class ObjectCount {
00279  public:
00280   static void Inc(const std::string& rTypeName);
00281   static void Dec(const std::string& rTypeName);
00282   static void Init(void);
00283   static std::map< std::string, long int >* mspMax;
00284   static std::map< std::string, long int >* mspCount;
00285  private:
00286   static bool msDone;
00287   ObjectCount(void);
00288 };
00289 
00290 /**
00291  * Test Protocol. Sets the filename for the test protocol.
00292  *
00293  * @param rSource
00294  *   Source file to protocol
00295  *
00296  */
00297 void TestProtocol(const std::string& rSource);  
00298 void TestProtocol(const std::string& rSource, const std::string& rMessage, const Type& rData, bool core=false); 
00299 void TestProtocol(const std::string& rSource, const std::string& rMessage, bool data); 
00300 void TestProtocol(const std::string& rSource, const std::string& rMessage, long int data); 
00301 void TestProtocol(const std::string& rSource, const std::string& rMessage, const std::string& rData); 
00302 
00303 /** Test protocol macro */
00304 #define FAUDES_TEST_DUMP(mes,dat) { std::stringstream sstr; sstr << \
00305   mes << " [at " << __FILE__ << ":" << __LINE__ << "]" ; \
00306   TestProtocol(__FILE__,sstr.str(),dat); }
00307 
00308 
00309 } // namespace faudes
00310 
00311 #endif
00312 

libFAUDES 2.14g --- 2009-12-3 --- c++ source docu by doxygen 1.5.6