libFAUDES

Sections

Index

cfl_helper.h

Go to the documentation of this file.
00001 /** @file cfl_helper.h   Helper functions */
00002 
00003 /* FAU Discrete Event Systems Library (libfaudes)
00004 
00005    Copyright (C) 2006  Bernd Opitz
00006    Copyright (C) 2008-2010 Thomas Moor
00007    Exclusive copyright is granted to Klaus Schmidt
00008 
00009    This library is free software; you can redistribute it and/or
00010    modify it under the terms of the GNU Lesser General Public
00011    License as published by the Free Software Foundation; either
00012    version 2.1 of the License, or (at your option) any later version.
00013 
00014    This library is distributed in the hope that it will be useful,
00015    but WITHOUT ANY WARRANTY; without even the implied warranty of
00016    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017    Lesser General Public License for more details.
00018 
00019    You should have received a copy of the GNU Lesser General Public
00020    License along with this library; if not, write to the Free Software
00021    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
00022 
00023 
00024 #ifndef FAUDES_HELPER_H
00025 #define FAUDES_HELPER_H
00026 
00027 #include <cstdlib>
00028 #include <cstring>
00029 #include <limits>
00030 #include <string>
00031 #include <iostream>
00032 #include <sstream>
00033 #include <fstream>
00034 #include <iomanip>
00035 #include <map>
00036 
00037 
00038 #include "cfl_definitions.h"
00039 #include "cfl_exception.h"
00040 
00041 namespace faudes {
00042 
00043 // forward
00044 class Type;
00045 
00046 /**
00047  * integer to string
00048  * 
00049  * @param number
00050  *   integer
00051  *
00052  * @return
00053  *   string
00054  */
00055 std::string ToStringInteger(long int number);
00056 
00057 
00058 /**
00059  * integer to string base 16
00060  * 
00061  * @param number
00062  *   integer
00063  *
00064  * @return
00065  *   string
00066  */
00067 std::string ToStringInteger16(long int number);
00068 
00069 /**
00070  * float to string 
00071  * 
00072  * @param number
00073  *   double
00074  *
00075  * @return
00076  *   string
00077  */
00078 std::string ToStringFloat(double number);
00079 
00080 
00081 /**
00082  * Fill string with spaces up to a given length if length of the string
00083  * is smaller than given length parameter.
00084  *
00085  * @param rString
00086  *   string
00087  * @param len
00088  *   Minimum number of charakters in string
00089  *
00090  * @return
00091  *   Expanded string
00092  */
00093 std::string ExpandString(const std::string& rString, unsigned int len);
00094 
00095 /**
00096  * Limit length of string, return head and tail of string
00097  *
00098  * @param rString
00099  *   string
00100  * @param len
00101  *   Maximum number of charakters in string (approx)
00102  *
00103  * @return
00104  *   Collapsed string
00105  */
00106  std::string CollapsString(const std::string& rString, unsigned int len= FD_MAXCONTAINERNAME);
00107 
00108 
00109 /**
00110  * Convert a string to Idx
00111  *
00112  * @param rString
00113  *   string to convert
00114  *
00115  * @return 
00116  *   Idx
00117  *
00118  * @exception
00119  *   Idx overflow (id 600)
00120  */
00121 Idx ToIdx(const std::string& rString);
00122 
00123 /**
00124  * Substitute in string
00125  *
00126  * @param rString
00127  *   Source string to substitute
00128  * @param rFrom
00129  *   String to match
00130  * @param rTo
00131  *   Replacement to fill in 
00132  * @return 
00133  *   Result string
00134  *
00135  */
00136 std::string StringSubstitute(const std::string& rString,const std::string& rFrom,const std::string& rTo);
00137    
00138 
00139 
00140 /**
00141  * Return FAUDES_VERSION as std::string
00142  *
00143  * @return 
00144  *   std::string with FAUDES_VERSION
00145  */
00146 std::string FDVersionString();
00147 
00148 /**
00149  * Return FAUDES_PLUGINS as std::string
00150  *
00151  * @return 
00152  *   std::string with FAUDES_VERSION
00153  */
00154 std::string FDPluginsString();
00155 
00156 /**
00157  * Return contributors as std::string
00158  *
00159  * @return 
00160  *   std::string 
00161  */
00162 std::string FDContributorsString();
00163 
00164 /**
00165  * Convenience function: process dot file
00166  * to graphics output.  If no output format is given,
00167  * try to guess from filename extension. 
00168  *
00169  * @param rDotFile
00170  *   name of dot file
00171  * @param rOutFile
00172  *   name of graphics file 
00173  * @param rOutFormat
00174  *   graphics format eg "png", "jpg"
00175  * @param rDotExec
00176  *   path/name of executable
00177  *
00178  * @exception Exception
00179  *   - error in systemcall (id 3)
00180  *   - unkown format (id 3)
00181  *
00182  */
00183 void ProcessDot(const std::string& rDotFile, const std::string& rOutFile, 
00184   const std::string& rOutFormat = "", const std::string& rDotExec = "dot");
00185 
00186 /**
00187  * Create a temp file, length 0
00188  *
00189  * @return
00190  *   Name of temp file
00191  */
00192 
00193 std::string CreateTempFile(void);
00194 
00195 
00196 /**
00197  * Delete a file
00198  *
00199  * @param rFileName
00200  *   Name of file to delete
00201  */
00202 bool RemoveFile(const std::string& rFileName);
00203 
00204 /**
00205  * Extract directory from full path.
00206  *
00207  * @param rFullPath
00208  *   Full name of file eg "/home/friend/data/generator.gen"
00209  * @return
00210  *   Directory eg "/home/friend/data"
00211  */
00212 std::string ExtractDirectory(const std::string& rFullPath);
00213 
00214 /**
00215  * Extract file name from full path.
00216  *
00217  * @param rFullName
00218  *   Full path of file eg "/home/friend/data/generator.gen"
00219  * @return
00220  *   Filename "generator.gen"
00221  */
00222 std::string ExtractFilename(const std::string& rFullName);
00223 
00224 /**
00225  * Construct full path from directory and filename.
00226  *
00227  * @param rDirectory
00228  *   Directory eg "/home/friend/data"
00229  * @param rFileName
00230  *   File eg "generator.gen"
00231  * @return
00232  *   Path eg "/home/friend/data/generator.gen"
00233  */
00234 std::string PrependDirectory(const std::string& rDirectory, const std::string& rFileName);
00235 
00236 
00237 
00238 /**
00239  * Console Out
00240  *
00241  * All console out messages (errors, progress report etc) are
00242  * meant to use the global ConsoleOut instance gConsoleOut, preumably
00243  * using the convenience macro FAUDES_WRITE_CONSOLE(). The default gConsoleOut
00244  * provides redirection to a named file by gConsoleOut.ToFile("filename"). libFAUDES 
00245  * Applications can use this construct in deriving a specialised class from ConsoleOut to
00246  * grab and inspect all console output.
00247  */
00248 class ConsoleOut {
00249 public:
00250   /** Write a std::string message */
00251   void Write(const std::string& message);
00252   /** Retrieve buffer to write on */
00253   std::ostream& Buffer(void);
00254   /** Flush buffer to output */
00255   void Flush(void);
00256   /** Redirect to file */
00257   void ToFile(const std::string& filename);
00258   /** Query filename */
00259   const std::string& Filename(void) { return mFilename;}; 
00260   /** acess static instance */
00261   static ConsoleOut* G(void);
00262   /** Redirect */
00263   void Redirect(ConsoleOut* out);
00264   /** Mute */
00265   void Mute(bool on) {mMute=on;};
00266 protected:
00267   /** Constructor */
00268   ConsoleOut(void);
00269   /** Destructor */
00270   virtual ~ConsoleOut(void);
00271   /** Writing hook. Re-implement this function in order to grab all output */
00272   virtual void DoWrite(const std::string& message);
00273 private:
00274   /** Private line buffer */
00275   std::ostringstream mLineBuffer;
00276   /** Private output stream */
00277   std::ofstream* pStream;
00278   /** Private record file name */
00279   std::string mFilename;
00280   /** Mute flag */
00281   bool mMute;
00282   /** Private static instance */
00283   static ConsoleOut* spInstance;
00284 };
00285 
00286  
00287 /**
00288  * Globale console out object and reference
00289  */
00290 extern ConsoleOut gConsoleOut;
00291 extern ConsoleOut gpConsoleOut;
00292 
00293 /**
00294  * Debugging counter. Counts items as specified by the type string and reports
00295  * sums on exit. You must define the macro FAUDES_DEBUG_CODE to get a report.
00296  *
00297  * Technical note: we use the somewhat winded static member construct to
00298  * guerantee that our member variables have been constructed befor actual
00299  * counting occurs. This is neccessary since some faudes types might have a
00300  * static instance and, hence, may be consructed rather early. 
00301  *
00302  */
00303 class ObjectCount {
00304  public:
00305   static void Inc(const std::string& rTypeName);
00306   static void Dec(const std::string& rTypeName);
00307   static void Init(void);
00308   static std::map< std::string, long int >* mspMax;
00309   static std::map< std::string, long int >* mspCount;
00310  private:
00311   static bool msDone;
00312   ObjectCount(void);
00313 };
00314 
00315 /**
00316  * Test Protocol. Sets the filename for the test protocol.
00317  *
00318  * @param rSource
00319  *   Source file to protocol
00320  *
00321  */
00322 void TestProtocol(const std::string& rSource);  
00323 void TestProtocol(const std::string& rSource, const std::string& rMessage, const Type& rData, bool core=false); 
00324 void TestProtocol(const std::string& rSource, const std::string& rMessage, bool data); 
00325 void TestProtocol(const std::string& rSource, const std::string& rMessage, long int data); 
00326 void TestProtocol(const std::string& rSource, const std::string& rMessage, const std::string& rData); 
00327 
00328 /** Test protocol macro */
00329 #define FAUDES_TEST_DUMP(mes,dat) { std::stringstream sstr; sstr << \
00330   mes << " [at " << __FILE__ << ":" << __LINE__ << "]" ; \
00331   TestProtocol(__FILE__,sstr.str(),dat); }
00332 
00333 /** Algorithm loop callback 
00334  *
00335  * Set a callback function for libFAUDES algorithms. Applications
00336  * are meant to use this interface to terminate an algorithm on user
00337  * request. libFAUDES algorithms are meant to throw an execption when
00338  * the callback function returns true. See also void LoopCallback(void).
00339  *
00340  * @param pBreakFnct
00341  *
00342  */
00343  void LoopCallback(bool (*pBreakFnct)(void));
00344 
00345  /** Algorithm loop callback
00346   *
00347   * Calls the loop callback function and throws an exception if it
00348   * returns true. 
00349   *
00350   * @exception
00351   *   Break on appliation request (id 110)
00352   *
00353   */
00354  void LoopCallback(void);
00355 
00356 
00357 
00358 } // namespace faudes
00359 
00360 
00361 #endif
00362 

libFAUDES 2.16b --- 2010-9-8 --- c++ source docu by doxygen 1.6.3