cfl_helper.h
Go to the documentation of this file.
1 /** @file cfl_helper.h Helper functions */
2 
3 /* FAU Discrete Event Systems Library (libfaudes)
4 
5  Copyright (C) 2006 Bernd Opitz
6  Copyright (C) 2008-2010 Thomas Moor
7  Exclusive copyright is granted to Klaus Schmidt
8 
9  This library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU Lesser General Public
11  License as published by the Free Software Foundation; either
12  version 2.1 of the License, or (at your option) any later version.
13 
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public
20  License along with this library; if not, write to the Free Software
21  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22 
23 
24 #ifndef FAUDES_HELPER_H
25 #define FAUDES_HELPER_H
26 
27 #include "cfl_definitions.h"
28 #include "cfl_platform.h"
29 #include "cfl_exception.h"
30 
31 namespace faudes {
32 
33 // forward
34 class Type;
35 
36 /**
37  * integer to string
38  *
39  * @param number
40  * integer
41  *
42  * @return
43  * string
44  */
45 extern FAUDES_API std::string ToStringInteger(Int number);
46 
47 
48 /**
49  * integer to string base 16
50  *
51  * @param number
52  * integer
53  *
54  * @return
55  * string
56  */
57 extern FAUDES_API std::string ToStringInteger16(Int number);
58 
59 /**
60  * float to string
61  *
62  * @param number
63  * double
64  *
65  * @return
66  * string
67  */
68 extern FAUDES_API std::string ToStringFloat(Float number);
69 
70 
71 /**
72  * Fill string with spaces up to a given length if length of the string
73  * is smaller than given length parameter.
74  *
75  * @param rString
76  * string
77  * @param len
78  * Minimum number of charakters in string
79  *
80  * @return
81  * Expanded string
82  */
83 extern FAUDES_API std::string ExpandString(const std::string& rString, unsigned int len);
84 
85 /**
86  * Limit length of string, return head and tail of string
87  *
88  * @param rString
89  * string
90  * @param len
91  * Maximum number of charakters in string (approx)
92  *
93  * @return
94  * Collapsed string
95  */
96 extern FAUDES_API std::string CollapsString(const std::string& rString, unsigned int len= FD_MAXCONTAINERNAME);
97 
98 
99 /**
100  * Convert a string to Idx
101  *
102  * @param rString
103  * string to convert
104  *
105  * @return
106  * Idx
107  *
108  * @exception
109  * Idx overflow (id 600)
110  */
111 extern FAUDES_API Idx ToIdx(const std::string& rString);
112 
113 /**
114  * Substitute in string
115  *
116  * @param rString
117  * Source string to substitute
118  * @param rFrom
119  * String to match
120  * @param rTo
121  * Replacement to fill in
122  * @return
123  * Result string
124  *
125  */
126 extern FAUDES_API std::string StringSubstitute(const std::string& rString,const std::string& rFrom,const std::string& rTo);
127 
128 
129 
130 /**
131  * Return FAUDES_VERSION as std::string
132  *
133  * @return
134  * std::string with FAUDES_VERSION
135  */
136 extern FAUDES_API std::string VersionString();
137 
138 /**
139  * Return FAUDES_PLUGINS as std::string
140  *
141  * @return
142  * std::string with FAUDES_VERSION
143  */
144 extern FAUDES_API std::string PluginsString();
145 
146 /**
147  * Return contributors as std::string
148  *
149  * @return
150  * std::string
151  */
152 extern FAUDES_API std::string ContributorsString();
153 
154 
155 /**
156  * Convenience function: process dot file
157  * to graphics output. If no output format is given,
158  * try to guess from filename extension.
159  *
160  * @param rDotFile
161  * name of dot file
162  * @param rOutFile
163  * name of graphics file
164  * @param rOutFormat
165  * graphics format eg "png", "jpg"
166  * @param rDotExec
167  * path/name of executable
168  *
169  * @exception Exception
170  * - error in systemcall (id 3)
171  * - unkown format (id 3)
172  *
173  */
174 extern FAUDES_API void ProcessDot(const std::string& rDotFile, const std::string& rOutFile,
175  const std::string& rOutFormat = "", const std::string& rDotExec = "dot");
176 
177 
178 /**
179  * Create a temp file, length 0
180  *
181  * @return
182  * Name of temp file
183  */
184 extern FAUDES_API std::string CreateTempFile(void);
185 
186 
187 /**
188  * Delete a file
189  *
190  * @param rFileName
191  * Name of file to delete
192  */
193 extern FAUDES_API bool RemoveFile(const std::string& rFileName);
194 
195 /**
196  * Std dir-separator.
197  * @return
198  * Separator as one-char string
199  */
200 extern FAUDES_API const std::string& PathSeparator(void);
201 
202 /**
203  * Extract directory from full path.
204  *
205  * @param rFullPath
206  * Full name of file eg "/home/friend/data/generator.gen"
207  * @return
208  * Directory eg "/home/friend/data"
209  */
210 extern FAUDES_API std::string ExtractDirectory(const std::string& rFullPath);
211 
212 /**
213  * Extract file name from full path.
214  *
215  * @param rFullName
216  * Full path of file eg "/home/friend/data/generator.gen"
217  * @return
218  * Filename "generator.gen"
219  */
220 extern FAUDES_API std::string ExtractFilename(const std::string& rFullName);
221 
222 /**
223  * Extract file name from full path. This version also
224  * removes the last suffix.
225  *
226  * @param rFullName
227  * Full path of file eg "/home/friend/data/generator.gen"
228  * @return
229  * Filename "generator"
230  */
231 extern FAUDES_API std::string ExtractBasename(const std::string& rFullName);
232 
233 /**
234  * Extract file name from full path. This version also
235  * remove the last suffix.
236  *
237  * @param rFullName
238  * Full path of file eg "/home/friend/data/generator.gen"
239  * @return
240  * Extension "gen"
241  */
242 extern FAUDES_API std::string ExtractExtension(const std::string& rFullName);
243 
244 /**
245  * Construct full path from directory and filename.
246  *
247  * @param rDirectory
248  * Directory eg "/home/friend/data"
249  * @param rFileName
250  * File eg "generator.gen"
251  * @return
252  * Path eg "/home/friend/data/generator.gen"
253  */
254 extern FAUDES_API std::string PrependDirectory(const std::string& rDirectory, const std::string& rFileName);
255 
256 /**
257  * Test existence of file
258  *
259  * @param rFilename
260  * Name of file to test
261  * @return
262  * True <> can open file for reading
263  */
264 extern FAUDES_API bool FileExists(const std::string& rFilename);
265 
266 /**
267  * Delete file
268  *
269  * @param rFilename
270  * Name of file to delete
271  * @return
272  * True <> could delete the file
273  */
274 extern FAUDES_API bool FileDelete(const std::string& rFilename);
275 
276 /**
277  * Copy file
278  *
279  * @param rFromFile
280  * Name of source file
281  * @param rToFile
282  * Name of dest file
283  * @return
284  * True <> operation ok
285  */
286 extern FAUDES_API bool FileCopy(const std::string& rFromFile, const std::string& rToFile);
287 
288 /**
289  * Test existence of directory
290  *
291  * @param rDirectory
292  * Name of file to test
293  * @return
294  * True <> can open directory for reading
295  */
296 extern FAUDES_API bool DirectoryExists(const std::string& rDirectory);
297 
298 
299 /**
300  * Read the contents of the specified directors.
301  *
302  * @param rDirectory
303  * Directory eg "/home/friend/data"
304  * @return
305  * List of files, e.g. "gen1.gen gen2.gen data subdir"
306  */
307 extern FAUDES_API std::set< std::string > ReadDirectory(const std::string& rDirectory);
308 
309 
310 /**
311  * Console Out
312  *
313  * All console out messages (errors, progress report etc) are
314  * meant to use the global ConsoleOut instance gConsoleOut, presumably
315  * using the convenience macro FAUDES_WRITE_CONSOLE(). The default ConsoleOut::G()
316  * provides optional redirection to a named file by G()->ConsoleOut.ToFile("filename").
317  * libFAUDES itself does not set/respect verbosity levels for its diagnostic
318  * output, this feature is implemented to support console applications.
319  *
320  * The main motivation of the entire construct is to support gui applications that may
321  * grab all console output by 1) deriving a specialised class from ConsoleOut and 2)
322  * redirection by ConsoleOut::G()->Redirect(derived_class_instance).
323  */
325 public:
326  /** Acess static instance */
327  static ConsoleOut* G(void);
328  /** Write a std::string message (optional progress report and verbosity) */
329  virtual void Write(const std::string& message,long int cntnow=0, long int cntdone=0, int verb=0);
330  /** Redirect to file */
331  void ToFile(const std::string& filename);
332  /** Query filename */
333  const std::string& Filename(void) { return mFilename;};
334  /** Redirect */
335  void Redirect(ConsoleOut* out);
336  /** Mute */
337  void Mute(bool on) {mMute=on;};
338  bool Mute() { return mMute;};
339  /** Verbosity */
340  void Verb(int verb) {mVerb=verb;};
341  int Verb() { return mVerb;};
342 protected:
343  /** Constructor */
344  ConsoleOut(void);
345  /** Destructor */
346  virtual ~ConsoleOut(void);
347  /** Writing hook. Re-implement this function in order to grab all output */
348  virtual void DoWrite(const std::string& message,long int cntnow=0, long int cntdone=0, int verb=0);
349 private:
350  /** Private output stream */
351  std::ofstream* pStream;
352  /** Private record file name */
353  std::string mFilename;
354  /** Mute flag */
355  bool mMute;
356  int mVerb;
357  /** Redirect */
359  /** Private static instance */
361 };
362 
363 
364 /**
365  * Debugging counter. Counts items as specified by the type string and reports
366  * sums on exit. You must define the macro FAUDES_DEBUG_CODE to get a report.
367  *
368  * Technical note: we use the somewhat winded static member construct to
369  * guarantee that our member variables have been constructed befor actual
370  * counting occurs. This is neccessary since some faudes types might have a
371  * static instance and, hence, may be consructed rather early.
372  *
373  */
375  public:
376  static void Inc(const std::string& rTypeName);
377  static void Dec(const std::string& rTypeName);
378  static void Init(void);
379  static std::map< std::string, long int >* mspMax;
380  static std::map< std::string, long int >* mspCount;
381  private:
382  static bool msDone;
383  ObjectCount(void);
384 };
385 
386 
387 /**
388  * Test Protocol.
389  * Sets the filename for the test protocol by
390  * - removing any path specififucation,
391  * - replacing "." by "_",
392  * - appending ".prot", and finaly
393  * - prepending "tmp_".
394  * The function returns the filename except for the
395  * "tmp_" prefix. The latter is considered the nominal
396  * protocol output (aka reference protocol).
397  *
398  * Note: only the first invocation of this functions actually sets
399  * the protocol file. Further invocations are ignored, but can be
400  * used to query the reference protocol.
401  *
402  * @param rSource
403  * Source file to protocol
404  * @return
405  * Filename with nominal protocol.
406  *
407  */
408 extern FAUDES_API std::string TestProtocol(const std::string& rSource);
409 
410 
411 /**
412  * Test Protocol.
413  * This function dumps the specified data to the protocol file for
414  * the purpose of later comparison with a refernce value.
415  * If the protocol file has not been set up, this
416  * function does nothing; see also TestProtocol(const std::string&.
417  *
418  * @param rMessage
419  * Informal identifyer of the test
420  * @param rData
421  * Formal result of the test case
422  * @param core
423  * Whether to record full token io or statistics only.
424  *
425  */
426 extern FAUDES_API void TestProtocol(const std::string& rMessage, const Type& rData, bool core=false);
427 
428 /**
429  * Test Protocol.
430  * Specialized version for boolean test data.
431  * See also TestProtocol(const std::string&, const Type&, bool);
432  *
433  * @param rMessage
434  * Informal identifyer of the test
435  * @param data
436  * Test data
437  *
438  */
439 extern FAUDES_API void TestProtocol(const std::string& rMessage, bool data);
440 
441 /**
442  * Test Protocol.
443  * Specialized version for integer test data.
444  * See also TestProtocol(const std::string&, const Type&, bool);
445  *
446  * @param rMessage
447  * Informal identifyer of the test
448  * @param data
449  * Test data
450  *
451  */
452 extern FAUDES_API void TestProtocol(const std::string& rMessage, long int data);
453 
454 
455 /**
456  * Test Protocol.
457  * Specialized version for string data.
458  * See also TestProtocol(const std::string&, const Type&, bool);
459  *
460  * @param rMessage
461  * Informal identifyer of the test
462  * @param data
463  * Test data
464  *
465  */
466 extern FAUDES_API void TestProtocol(const std::string& rMessage, const std::string& data);
467 
468 
469 /**
470  * Test Protocol.
471  * Perform a comparison of the recent protocol file and the
472  * corresponding reference. Returns true if the test passes.
473  *
474  * Note: this function closes the current protocol.
475  *
476  * @return
477  * True <=> test past
478  */
479 extern FAUDES_API bool TestProtocol(void);
480 
481 
482 /** Test protocol record macro ("mangle" filename for platform independance)*/
483 #define FAUDES_TEST_DUMP(mes,dat) { \
484  TestProtocol(__FILE__); \
485  std::string fname= __FILE__; \
486  std::replace(fname.begin(),fname.end(),'\\','/'); \
487  fname=ExtractFilename(fname); \
488  std::stringstream sstr; \
489  sstr << mes << " [at " << fname << ":" << __LINE__ << "]" ; \
490  TestProtocol(sstr.str(),dat); }
491 
492 /** Test protocol diff macro */
493 #define FAUDES_TEST_DIFF() { if(!TestProtocol()) { \
494  FAUDES_WRITE_CONSOLE("FAUDES_TEST_DIFF: sensed test case error in " << __FILE__); exit(1);} }
495 
496 
497 /** Algorithm loop callback
498  *
499  * Set a callback function for libFAUDES algorithms. Applications
500  * are meant to use this interface to terminate an algorithm on user
501  * request. libFAUDES algorithms are meant to throw an execption when
502  * the callback function returns true. See also void LoopCallback(void).
503  *
504  * @param pBreakFnct
505  *
506  */
507 extern FAUDES_API void LoopCallback(bool (*pBreakFnct)(void));
508 
509  /** Algorithm loop callback
510  *
511  * Calls the loop callback function and throws an exception if it
512  * returns true.
513  *
514  * @exception
515  * Break on appliation request (id 110)
516  *
517  */
518 extern FAUDES_API void LoopCallback(void);
519 
520 
521 
522 } // namespace faudes
523 
524 
525 #endif
526 
Compiletime options.
#define FD_MAXCONTAINERNAME
Max length of automatic container names (set to -1 for unlimited)
Class Exception.
Platform dependant wrappers.
#define FAUDES_API
Interface export/import symbols: windows.
Definition: cfl_platform.h:81
Console Out.
Definition: cfl_helper.h:324
ConsoleOut * pInstance
Redirect.
Definition: cfl_helper.h:358
std::ofstream * pStream
Private output stream.
Definition: cfl_helper.h:351
static ConsoleOut * smpInstance
Private static instance.
Definition: cfl_helper.h:360
const std::string & Filename(void)
Query filename.
Definition: cfl_helper.h:333
bool mMute
Mute flag.
Definition: cfl_helper.h:355
void Verb(int verb)
Verbosity.
Definition: cfl_helper.h:340
void Mute(bool on)
Mute.
Definition: cfl_helper.h:337
std::string mFilename
Private record file name.
Definition: cfl_helper.h:353
Debugging counter.
Definition: cfl_helper.h:374
static bool msDone
Definition: cfl_helper.h:382
static std::map< std::string, long int > * mspCount
Definition: cfl_helper.h:380
static std::map< std::string, long int > * mspMax
Definition: cfl_helper.h:379
Base class of all libFAUDES objects that participate in the run-time interface.
Definition: cfl_types.h:239
libFAUDES resides within the namespace faudes.
uint32_t Idx
Type definition for index type (allways 32bit)
void ProcessDot(const std::string &rDotFile, const std::string &rOutFile, const std::string &rOutFormat, const std::string &rDotExec)
Convenience function: process dot file to graphics output.
Definition: cfl_helper.cpp:148
std::string VersionString()
Return FAUDES_VERSION as std::string.
Definition: cfl_helper.cpp:131
std::string ExtractDirectory(const std::string &rFullPath)
Extract directory from full path.
Definition: cfl_helper.cpp:262
bool RemoveFile(const std::string &rFileName)
Delete a file.
Definition: cfl_helper.cpp:256
void LoopCallback(bool pBreak(void))
Definition: cfl_helper.cpp:621
std::string ExtractExtension(const std::string &rFullPath)
Extract file name from full path.
Definition: cfl_helper.cpp:294
std::string ExtractBasename(const std::string &rFullPath)
Extract file name from full path.
Definition: cfl_helper.cpp:280
Idx ToIdx(const std::string &rString)
Convert a string to Idx.
Definition: cfl_helper.cpp:100
std::string CreateTempFile(void)
Create a temp file, length 0.
Definition: cfl_helper.cpp:205
bool FileCopy(const std::string &rFromFile, const std::string &rToFile)
Copy file.
Definition: cfl_helper.cpp:385
std::string PluginsString()
Return FAUDES_PLUGINS as std::string.
Definition: cfl_helper.cpp:136
std::string ExpandString(const std::string &rString, unsigned int len)
Fill string with spaces up to a given length if length of the string is smaller than given length par...
Definition: cfl_helper.cpp:80
std::string PrependDirectory(const std::string &rDirectory, const std::string &rFileName)
Construct full path from directory and filename.
Definition: cfl_helper.cpp:309
std::string ExtractFilename(const std::string &rFullPath)
Extract file name from full path.
Definition: cfl_helper.cpp:271
bool FileDelete(const std::string &rFilename)
Delete file.
Definition: cfl_helper.cpp:380
std::string ToStringFloat(Float number)
float to string
Definition: cfl_helper.cpp:64
std::string CollapsString(const std::string &rString, unsigned int len)
Limit length of string, return head and tail of string.
Definition: cfl_helper.cpp:91
std::string ToStringInteger16(Int number)
integer to string base 16
Definition: cfl_helper.cpp:54
double Float
Type definition for real type.
std::string ContributorsString()
Return contributors as std::string.
Definition: cfl_helper.cpp:141
std::string ToStringInteger(Int number)
integer to string
Definition: cfl_helper.cpp:43
std::set< std::string > ReadDirectory(const std::string &rDirectory)
Read the contents of the specified directors.
Definition: cfl_helper.cpp:336
std::string StringSubstitute(const std::string &rString, const std::string &rFrom, const std::string &rTo)
Substitute in string.
Definition: cfl_helper.cpp:111
bool DirectoryExists(const std::string &rDirectory)
Test existence of directory.
Definition: cfl_helper.cpp:320
std::string TestProtocol(const std::string &rSource)
Test Protocol.
Definition: cfl_helper.cpp:506
FAUDES_API const std::string & PathSeparator(void)
Std dir-separator.
bool FileExists(const std::string &rFilename)
Test existence of file.
Definition: cfl_helper.cpp:373
long int Int
Type definition for integer type (let target system decide, minimum 32bit)

libFAUDES 2.32b --- 2024.03.01 --- c++ api documentaion by doxygen