cfl_utils.h
Go to the documentation of this file.
1 /** @file cfl_utils.h C-level utilities functions */
2 
3 /* FAU Discrete Event Systems Library (libfaudes)
4 
5  Copyright (C) 2006 Bernd Opitz
6  Copyright (C) 2008-2024 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_UTILS_H
25 #define FAUDES_UTILS_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 globally 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  * Std dir-separator.
189  * @return
190  * Separator as one-char string
191  */
192 extern FAUDES_API const std::string& PathSeparator(void);
193 
194 /**
195  * Extract directory from (full) path; i.e., remove the last
196  * separator and anything thereafer.
197  *
198  * @param rFullPath
199  * Full name of file eg (1) "/home/friend/data/generator.gen"
200  * Full name of file eg (2) "C:\data\project\generator.gen"
201  * @return
202  * Directory eg (1) "/home/friend/data"
203  * Directory eg (2) "C:\data\project"
204  */
205 extern FAUDES_API std::string ExtractDirectory(const std::string& rFullPath);
206 
207 /**
208  * Extract file name from full path.
209  *
210  * @param rFullName
211  * Full path of file eg "/home/friend/data/generator.gen"
212  * @return
213  * Filename "generator.gen"
214  */
215 extern FAUDES_API std::string ExtractFilename(const std::string& rFullName);
216 
217 /**
218  * Extract file basename from full path. This version also
219  * removes the last suffix.
220  *
221  * @param rFullName
222  * Full path of file eg "/home/friend/data/generator.gen"
223  * @return
224  * Filename "generator"
225  */
226 extern FAUDES_API std::string ExtractBasename(const std::string& rFullName);
227 
228 /**
229  * Extract extension from full path, i.e. ontain the last suffix.
230  *
231  * @param rFullName
232  * Full path of file eg "/home/friend/data/generator.gen"
233  * @return
234  * Extension "gen"
235  */
236 extern FAUDES_API std::string ExtractSuffix(const std::string& rFullName);
237 
238 /**
239  * Prepend one path before another. Specifically, insert a
240  * path seperator if necessary.
241  *
242  * @param rLeft
243  * Directory eg "/home/friend/data"
244  * @param rRight
245  * File eg "generator.gen"
246  * @return
247  * Path eg "/home/friend/data/generator.gen"
248  */
249 extern FAUDES_API std::string PrependPath(const std::string& rLeft, const std::string& rRight);
250 
251 /**
252  * Test existence of file
253  *
254  * @param rFilename
255  * Name of file to test
256  * @return
257  * True <> can open file for reading
258  */
259 extern FAUDES_API bool FileExists(const std::string& rFilename);
260 
261 /**
262  * Delete file
263  *
264  * @param rFilename
265  * Name of file to delete
266  * @return
267  * True <> could delete the file
268  */
269 extern FAUDES_API bool FileDelete(const std::string& rFilename);
270 
271 /**
272  * Copy file
273  *
274  * @param rFromFile
275  * Name of source file
276  * @param rToFile
277  * Name of dest file
278  * @return
279  * True <> operation ok
280  */
281 extern FAUDES_API bool FileCopy(const std::string& rFromFile, const std::string& rToFile);
282 
283 /**
284  * Test existence of directory
285  *
286  * @param rDirectory
287  * Name of file to test
288  * @return
289  * True <> can open directory for reading
290  */
291 extern FAUDES_API bool DirectoryExists(const std::string& rDirectory);
292 
293 
294 /**
295  * Read the contents of the specified directors.
296  *
297  * @param rDirectory
298  * Directory eg "/home/friend/data"
299  * @return
300  * List of files, e.g. "gen1.gen gen2.gen data subdir"
301  */
302 extern FAUDES_API std::set< std::string > ReadDirectory(const std::string& rDirectory);
303 
304 
305 /**
306  * Console Out
307  *
308  * All console out messages (errors, progress report etc) are
309  * meant to use the global ConsoleOut instance gConsoleOut, presumably
310  * using the convenience macro FAUDES_WRITE_CONSOLE(). The default ConsoleOut::G()
311  * provides optional redirection to a named file by G()->ConsoleOut.ToFile("filename").
312  * libFAUDES itself does not set/respect verbosity levels for its diagnostic
313  * output, this feature is implemented to support console applications.
314  *
315  * The main motivation of the entire construct is to support gui applications that may
316  * grab all console output by 1) deriving a specialised class from ConsoleOut and 2)
317  * redirection by ConsoleOut::G()->Redirect(derived_class_instance).
318  */
320 public:
321  /** Acess static instance */
322  static ConsoleOut* G(void);
323  /** Write a std::string message (optional progress report and verbosity) */
324  virtual void Write(const std::string& message,long int cntnow=0, long int cntdone=0, int verb=0);
325  /** Redirect to file */
326  void ToFile(const std::string& filename);
327  /** Query filename */
328  const std::string& Filename(void) { return mFilename;};
329  /** Redirect */
330  void Redirect(ConsoleOut* out);
331  /** Mute */
332  void Mute(bool on) {mMute=on;};
333  bool Mute() { return mMute;};
334  /** Verbosity */
335  void Verb(int verb) {mVerb=verb;};
336  int Verb() { return mVerb;};
337 protected:
338  /** Constructor */
339  ConsoleOut(void);
340  /** Destructor */
341  virtual ~ConsoleOut(void);
342  /** Writing hook. Re-implement this function in order to grab all output */
343  virtual void DoWrite(const std::string& message,long int cntnow=0, long int cntdone=0, int verb=0);
344 private:
345  /** Private output stream */
346  std::ofstream* pStream;
347  /** Private record file name */
348  std::string mFilename;
349  /** Mute flag */
350  bool mMute;
351  int mVerb;
352  /** Redirect */
354  /** Private static instance */
356 };
357 
358 
359 /**
360  * Debugging counter. Counts items as specified by the type string and reports
361  * sums on exit. You must define the macro FAUDES_DEBUG_CODE to get a report.
362  *
363  * Technical note: we use the somewhat winded static member construct to
364  * guarantee that our member variables have been constructed befor actual
365  * counting occurs. This is neccessary since some faudes types might have a
366  * static instance and, hence, may be consructed rather early.
367  *
368  */
370  public:
371  static void Inc(const std::string& rTypeName);
372  static void Dec(const std::string& rTypeName);
373  static void Init(void);
374  static std::map< std::string, long int >* mspMax;
375  static std::map< std::string, long int >* mspCount;
376  private:
377  static bool msDone;
378  ObjectCount(void);
379 };
380 
381 
382 /**
383  * Test Protocol.
384  * Sets the filename for the test protocol by
385  * - removing any path specififucation,
386  * - replacing "." by "_",
387  * - appending ".prot", and finaly
388  * - prepending "tmp_".
389  * The function returns the filename except for the
390  * "tmp_" prefix. The latter is considered the nominal
391  * protocol output (aka reference protocol).
392  *
393  * Note: only the first invocation of this functions actually sets
394  * the protocol file. Further invocations are ignored, but can be
395  * used to query the reference protocol.
396  *
397  * @param rSource
398  * Source file to protocol
399  * @return
400  * Filename with nominal protocol.
401  *
402  */
403 extern FAUDES_API std::string TestProtocol(const std::string& rSource);
404 
405 
406 /**
407  * Test Protocol.
408  * This function dumps the specified data to the protocol file for
409  * the purpose of later comparison with a refernce value.
410  * If the protocol file has not been set up, this
411  * function does nothing; see also TestProtocol(const std::string&.
412  *
413  * @param rMessage
414  * Informal identifyer of the test
415  * @param rData
416  * Formal result of the test case
417  * @param core
418  * Whether to record full token io or statistics only.
419  *
420  */
421 extern FAUDES_API void TestProtocol(const std::string& rMessage, const Type& rData, bool core=false);
422 
423 /**
424  * Test Protocol.
425  * Specialized version for boolean test data.
426  * See also TestProtocol(const std::string&, const Type&, bool);
427  *
428  * @param rMessage
429  * Informal identifyer of the test
430  * @param data
431  * Test data
432  *
433  */
434 extern FAUDES_API void TestProtocol(const std::string& rMessage, bool data);
435 
436 /**
437  * Test Protocol.
438  * Specialized version for integer test data.
439  * See also TestProtocol(const std::string&, const Type&, bool);
440  *
441  * @param rMessage
442  * Informal identifyer of the test
443  * @param data
444  * Test data
445  *
446  */
447 extern FAUDES_API void TestProtocol(const std::string& rMessage, long int data);
448 
449 
450 /**
451  * Test Protocol.
452  * Specialized version for string data.
453  * See also TestProtocol(const std::string&, const Type&, bool);
454  *
455  * @param rMessage
456  * Informal identifyer of the test
457  * @param data
458  * Test data
459  *
460  */
461 extern FAUDES_API void TestProtocol(const std::string& rMessage, const std::string& data);
462 
463 
464 /**
465  * Test Protocol.
466  * Perform a comparison of the recent protocol file and the
467  * corresponding reference. Returns true if the test passes.
468  *
469  * Note: this function closes the current protocol.
470  *
471  * @return
472  * True <=> test past
473  */
474 extern FAUDES_API bool TestProtocol(void);
475 
476 
477 /** Test protocol record macro ("mangle" filename for platform independance)*/
478 #define FAUDES_TEST_DUMP(mes,dat) { \
479  TestProtocol(__FILE__); \
480  std::string fname= __FILE__; \
481  std::replace(fname.begin(),fname.end(),'\\','/'); \
482  fname=ExtractFilename(fname); \
483  std::stringstream sstr; \
484  sstr << mes << " [at " << fname << ":" << __LINE__ << "]" ; \
485  TestProtocol(sstr.str(),dat); }
486 
487 /** Test protocol diff macro */
488 #define FAUDES_TEST_DIFF() { if(!TestProtocol()) { \
489  FAUDES_WRITE_CONSOLE("FAUDES_TEST_DIFF: sensed test case error in " << __FILE__); exit(1);} }
490 
491 
492 /** Algorithm loop callback
493  *
494  * Set a callback function for libFAUDES algorithms. Applications
495  * are meant to use this interface to terminate an algorithm on user
496  * request. libFAUDES algorithms are meant to throw an execption when
497  * the callback function returns true. See also void LoopCallback(void).
498  *
499  * @param pBreakFnct
500  *
501  */
502 extern FAUDES_API void LoopCallback(bool (*pBreakFnct)(void));
503 
504  /** Algorithm loop callback
505  *
506  * Calls the loop callback function and throws an exception if it
507  * returns true.
508  *
509  * @exception
510  * Break on appliation request (id 110)
511  *
512  */
513 extern FAUDES_API void LoopCallback(void);
514 
515 
516 
517 } // namespace faudes
518 
519 
520 #endif
521 
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:80
Console Out.
Definition: cfl_utils.h:319
ConsoleOut * pInstance
Redirect.
Definition: cfl_utils.h:353
std::ofstream * pStream
Private output stream.
Definition: cfl_utils.h:346
static ConsoleOut * smpInstance
Private static instance.
Definition: cfl_utils.h:355
const std::string & Filename(void)
Query filename.
Definition: cfl_utils.h:328
bool mMute
Mute flag.
Definition: cfl_utils.h:350
void Verb(int verb)
Verbosity.
Definition: cfl_utils.h:335
void Mute(bool on)
Mute.
Definition: cfl_utils.h:332
std::string mFilename
Private record file name.
Definition: cfl_utils.h:348
Debugging counter.
Definition: cfl_utils.h:369
static bool msDone
Definition: cfl_utils.h:377
static std::map< std::string, long int > * mspCount
Definition: cfl_utils.h:375
static std::map< std::string, long int > * mspMax
Definition: cfl_utils.h:374
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_utils.cpp:148
std::string VersionString()
Return FAUDES_VERSION as std::string.
Definition: cfl_utils.cpp:131
std::string ExtractDirectory(const std::string &rFullPath)
Extract directory from (full) path; i.e., remove the last separator and anything thereafer.
Definition: cfl_utils.cpp:256
void LoopCallback(bool pBreak(void))
Definition: cfl_utils.cpp:633
std::string PrependPath(const std::string &rLeft, const std::string &rRight)
Prepend one path before another.
Definition: cfl_utils.cpp:303
std::string ExtractBasename(const std::string &rFullPath)
Extract file basename from full path.
Definition: cfl_utils.cpp:274
Idx ToIdx(const std::string &rString)
Convert a string to Idx.
Definition: cfl_utils.cpp:100
std::string CreateTempFile(void)
Create a temp file, length 0.
Definition: cfl_utils.cpp:205
bool FileCopy(const std::string &rFromFile, const std::string &rToFile)
Copy file.
Definition: cfl_utils.cpp:397
std::string PluginsString()
Return FAUDES_PLUGINS as std::string.
Definition: cfl_utils.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_utils.cpp:80
std::string ExtractFilename(const std::string &rFullPath)
Extract file name from full path.
Definition: cfl_utils.cpp:265
bool FileDelete(const std::string &rFilename)
Delete file.
Definition: cfl_utils.cpp:392
std::string ToStringFloat(Float number)
float to string
Definition: cfl_utils.cpp:64
std::string CollapsString(const std::string &rString, unsigned int len)
Limit length of string, return head and tail of string.
Definition: cfl_utils.cpp:91
std::string ToStringInteger16(Int number)
integer to string base 16
Definition: cfl_utils.cpp:54
double Float
Type definition for real type.
std::string ExtractSuffix(const std::string &rFullPath)
Extract extension from full path, i.e.
Definition: cfl_utils.cpp:288
std::string ContributorsString()
Return contributors as std::string.
Definition: cfl_utils.cpp:141
std::string ToStringInteger(Int number)
integer to string
Definition: cfl_utils.cpp:43
std::set< std::string > ReadDirectory(const std::string &rDirectory)
Read the contents of the specified directors.
Definition: cfl_utils.cpp:348
std::string StringSubstitute(const std::string &rString, const std::string &rFrom, const std::string &rTo)
Substitute globally in string.
Definition: cfl_utils.cpp:111
bool DirectoryExists(const std::string &rDirectory)
Test existence of directory.
Definition: cfl_utils.cpp:332
std::string TestProtocol(const std::string &rSource)
Test Protocol.
Definition: cfl_utils.cpp:518
FAUDES_API const std::string & PathSeparator(void)
Std dir-separator.
bool FileExists(const std::string &rFilename)
Test existence of file.
Definition: cfl_utils.cpp:385
long int Int
Type definition for integer type (let target system decide, minimum 32bit)

libFAUDES 2.32f --- 2024.12.22 --- c++ api documentaion by doxygen