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  * Convert to lower case
115  *
116  * @param rString
117  * Source string to convert
118  * @return
119  * Lower case string
120  *
121  */
122 extern FAUDES_API std::string ToLowerCase(const std::string& rString);
123 
124 /**
125  * Substitute globally in string
126  *
127  * @param rString
128  * Source string to substitute
129  * @param rFrom
130  * String to match
131  * @param rTo
132  * Replacement to fill in
133  * @return
134  * Result string
135  *
136  */
137 extern FAUDES_API std::string StringSubstitute(const std::string& rString,const std::string& rFrom,const std::string& rTo);
138 
139 
140 /**
141  * Return FAUDES_VERSION as std::string
142  *
143  * @return
144  * std::string with FAUDES_VERSION
145  */
146 extern FAUDES_API std::string VersionString();
147 
148 /**
149  * Return FAUDES_PLUGINS as std::string
150  *
151  * @return
152  * std::string with FAUDES_VERSION
153  */
154 extern FAUDES_API std::string PluginsString();
155 
156 /**
157  * Return contributors as std::string
158  *
159  * @return
160  * std::string
161  */
162 extern FAUDES_API std::string ContributorsString();
163 
164 /**
165  * Return contributors as std::string
166  *
167  * @return
168  * std::string
169  */
170 extern FAUDES_API std::string BuildString();
171 
172 /**
173  * Convenience function: process dot file
174  * to graphics output. If no output format is given,
175  * try to guess from filename extension.
176  *
177  * @param rDotFile
178  * name of dot file
179  * @param rOutFile
180  * name of graphics file
181  * @param rOutFormat
182  * graphics format eg "png", "jpg"
183  * @param rDotExec
184  * path/name of executable
185  *
186  * @exception Exception
187  * - error in systemcall (id 3)
188  * - unkown format (id 3)
189  *
190  */
191 extern FAUDES_API void ProcessDot(const std::string& rDotFile, const std::string& rOutFile,
192  const std::string& rOutFormat = "", const std::string& rDotExec = "dot");
193 
194 
195 /**
196  * Convenience function: test for dot exec to exits
197  *
198  * @param rDotExec
199  * path/name of executable
200  *
201  */
202 extern FAUDES_API bool DotReady(const std::string& rDotExec = "dot");
203 
204 
205 
206 /**
207  * Create a temp file, length 0
208  *
209  * @return
210  * Name of temp file
211  */
212 extern FAUDES_API std::string CreateTempFile(void);
213 
214 
215 /**
216  * Extract directory from (full) path; i.e., remove the last
217  * separator and anything thereafer.
218  *
219  * This is a compatible left-over from pre-v2.32. As of v2.32.
220  * We now take care that out internal representation is in
221  * posix style, i.e., case (2) should not happen.
222  *
223  * @param rFullPath
224  * Full name of file eg (1) "/home/friend/data/generator.gen"
225  * Full name of file eg (2) "C:\data\project\generator.gen"
226  * @return
227  * Directory eg (1) "/home/friend/data"
228  * Directory eg (2) "C:\data\project"
229  */
230 extern FAUDES_API std::string ExtractDirectory(const std::string& rFullPath);
231 
232 /**
233  * Extract file name from full path.
234  *
235  * @param rFullName
236  * Full path of file eg "/home/friend/data/generator.gen"
237  * @return
238  * Filename "generator.gen"
239  */
240 extern FAUDES_API std::string ExtractFilename(const std::string& rFullName);
241 
242 /**
243  * Extract file basename from full path. This version also
244  * removes the last suffix.
245  *
246  * @param rFullName
247  * Full path of file eg "/home/friend/data/generator.gen"
248  * @return
249  * Filename "generator"
250  */
251 extern FAUDES_API std::string ExtractBasename(const std::string& rFullName);
252 
253 /**
254  * Extract extension from full path, i.e. ontain the last suffix.
255  *
256  * @param rFullName
257  * Full path of file eg "/home/friend/data/generator.gen"
258  * @return
259  * Extension "gen"
260  */
261 extern FAUDES_API std::string ExtractSuffix(const std::string& rFullName);
262 
263 /**
264  * Prepend one path before another. Specifically, insert a
265  * path seperator if necessary.
266  *
267  * @param rLeft
268  * Directory eg "/home/friend/data"
269  * @param rRight
270  * File eg "generator.gen"
271  * @return
272  * Path eg "/home/friend/data/generator.gen"
273  */
274 extern FAUDES_API std::string PrependPath(const std::string& rLeft, const std::string& rRight);
275 
276 /**
277  * Test existence of file
278  *
279  * @param rFilename
280  * Name of file to test
281  * @return
282  * True <> can open file for reading
283  */
284 extern FAUDES_API bool FileExists(const std::string& rFilename);
285 
286 /**
287  * Delete file
288  *
289  * @param rFilename
290  * Name of file to delete
291  * @return
292  * True <> could delete the file
293  */
294 extern FAUDES_API bool FileDelete(const std::string& rFilename);
295 
296 /**
297  * Copy file
298  *
299  * @param rFromFile
300  * Name of source file
301  * @param rToFile
302  * Name of dest file
303  * @return
304  * True <> operation ok
305  */
306 extern FAUDES_API bool FileCopy(const std::string& rFromFile, const std::string& rToFile);
307 
308 /**
309  * Test existence of directory
310  *
311  * @param rDirectory
312  * Name of file to test
313  * @return
314  * True <> can open directory for reading
315  */
316 extern FAUDES_API bool DirectoryExists(const std::string& rDirectory);
317 
318 
319 /**
320  * Read the contents of the specified directors.
321  *
322  * @param rDirectory
323  * Directory eg "/home/friend/data"
324  * @return
325  * List of files, e.g. "gen1.gen gen2.gen data subdir"
326  */
327 extern FAUDES_API std::set< std::string > ReadDirectory(const std::string& rDirectory);
328 
329 
330 /**
331  * Console Out
332  *
333  * All console out messages (errors, progress report etc) are
334  * meant to use the global ConsoleOut instance gConsoleOut, presumably
335  * using the convenience macro FAUDES_WRITE_CONSOLE(). The default ConsoleOut::G()
336  * provides optional redirection to a named file by G()->ConsoleOut.ToFile("filename").
337  * libFAUDES itself does not set/respect verbosity levels for its diagnostic
338  * output, this feature is implemented to support console applications.
339  *
340  * The main motivation of the entire construct is to support gui applications that may
341  * grab all console output by 1) deriving a specialised class from ConsoleOut and 2)
342  * redirection by ConsoleOut::G()->Redirect(derived_class_instance).
343  */
345 public:
346  /** Acess static instance */
347  static ConsoleOut* G(void);
348  /** Write a std::string message (optional progress report and verbosity) */
349  virtual void Write(const std::string& message,long int cntnow=0, long int cntdone=0, int verb=1);
350  /** Redirect to file */
351  void ToFile(const std::string& filename);
352  /** Query filename */
353  const std::string& Filename(void) { return mFilename;};
354  /** Redirect */
355  void Redirect(ConsoleOut* out);
356  /** Verbosity */
357  void Verb(int verb) {mVerb=verb;};
358  int Verb() { return mVerb;};
359 protected:
360  /** Constructor */
361  ConsoleOut(void);
362  /** Destructor */
363  virtual ~ConsoleOut(void);
364  /** Writing hook. Re-implement this function in order to grab all output */
365  virtual void DoWrite(const std::string& message,long int cntnow=0, long int cntdone=0, int verb=1);
366 private:
367  /** Private output stream */
368  std::ofstream* pStream;
369  /** Private record file name */
370  std::string mFilename;
371  /** Mute flag */
372  int mVerb;
373  /** Redirect */
375  /** Private static instance */
377 };
378 
379 
380 /** API wrapper Print at verbosity */
381 extern FAUDES_API void Print(int v, const std::string& message);
382 extern FAUDES_API void Print(const std::string& message);
383 extern FAUDES_API void Verbosity(int v);
384 extern FAUDES_API int Verbosity(void);
385 
386 /**
387  * Debugging counter. Counts items as specified by the type string and reports
388  * sums on exit. You must define the macro FAUDES_DEBUG_CODE to get a report.
389  *
390  * Technical note: we use the somewhat winded static member construct to
391  * guarantee that our member variables have been constructed befor actual
392  * counting occurs. This is neccessary since some faudes types might have a
393  * static instance and, hence, may be consructed rather early.
394  *
395  */
397  public:
398  static void Inc(const std::string& rTypeName);
399  static void Dec(const std::string& rTypeName);
400  static void Init(void);
401  static std::map< std::string, long int >* mspMax;
402  static std::map< std::string, long int >* mspCount;
403  private:
404  static bool msDone;
405  ObjectCount(void);
406 };
407 
408 
409 /**
410  * Test Protocol.
411  * Sets the filename for the test protocol by
412  * - removing any path specififucation,
413  * - replacing "." by "_",
414  * - appending ".prot", and finaly
415  * - prepending "tmp_".
416  * The function returns the filename except for the
417  * "tmp_" prefix. The latter is considered the nominal
418  * protocol output (aka reference protocol).
419  *
420  * Note: only the first invocation of this functions actually sets
421  * the protocol file. Further invocations are ignored, but can be
422  * used to query the reference protocol.
423  *
424  * @param rSource
425  * Source file to protocol
426  * @return
427  * Filename with nominal protocol.
428  *
429  */
430 extern FAUDES_API std::string TestProtocol(const std::string& rSource);
431 
432 
433 /**
434  * Test Protocol.
435  * This function dumps the specified data to the protocol file for
436  * the purpose of later comparison with a refernce value.
437  * If the protocol file has not been set up, this
438  * function does nothing; see also TestProtocol(const std::string&.
439  *
440  * @param rMessage
441  * Informal identifyer of the test
442  * @param rData
443  * Formal result of the test case
444  * @param core
445  * Whether to record full token io or statistics only.
446  *
447  */
448 extern FAUDES_API void TestProtocol(const std::string& rMessage, const Type& rData, bool core=false);
449 
450 /**
451  * Test Protocol.
452  * Specialized version for boolean test 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, bool data);
462 
463 /**
464  * Test Protocol.
465  * Specialized version for integer test data.
466  * See also TestProtocol(const std::string&, const Type&, bool);
467  *
468  * @param rMessage
469  * Informal identifyer of the test
470  * @param data
471  * Test data
472  *
473  */
474 extern FAUDES_API void TestProtocol(const std::string& rMessage, long int data);
475 
476 
477 /**
478  * Test Protocol.
479  * Specialized version for string data.
480  * See also TestProtocol(const std::string&, const Type&, bool);
481  *
482  * @param rMessage
483  * Informal identifyer of the test
484  * @param data
485  * Test data
486  *
487  */
488 extern FAUDES_API void TestProtocol(const std::string& rMessage, const std::string& data);
489 
490 
491 /**
492  * Test Protocol.
493  * Perform a comparison of the recent protocol file and the
494  * corresponding reference. Returns true if the test passes.
495  *
496  * Note: this function closes the current protocol.
497  *
498  * @return
499  * True <=> test past
500  */
501 extern FAUDES_API bool TestProtocol(void);
502 
503 
504 /** Test protocol record macro ("mangle" filename for platform independance)*/
505 #define FAUDES_TEST_DUMP(mes,dat) { \
506  TestProtocol(__FILE__); \
507  std::string fname= __FILE__; \
508  std::replace(fname.begin(),fname.end(),'\\','/'); \
509  fname=ExtractFilename(fname); \
510  std::stringstream sstr; \
511  sstr << mes << " [at " << fname << ":" << __LINE__ << "]" ; \
512  TestProtocol(sstr.str(),dat); }
513 
514 /** Test protocol diff macro */
515 #define FAUDES_TEST_DIFF() { if(!TestProtocol()) { \
516  FAUDES_WRITE_CONSOLE("FAUDES_TEST_DIFF: sensed test case error in " << __FILE__); exit(0);} }
517 
518 
519 /** Algorithm loop callback
520  *
521  * Set a callback function for libFAUDES algorithms. Applications
522  * are meant to use this interface to terminate an algorithm on user
523  * request. libFAUDES algorithms are meant to throw an execption when
524  * the callback function returns true. See also void LoopCallback(void).
525  *
526  * @param pBreakFnct
527  *
528  */
529 extern FAUDES_API void LoopCallback(bool (*pBreakFnct)(void));
530 
531  /** Algorithm loop callback
532  *
533  * Calls the loop callback function and throws an exception if it
534  * returns true.
535  *
536  * @exception
537  * Break on appliation request (id 110)
538  *
539  */
540 extern FAUDES_API void LoopCallback(void);
541 
542 
543 
544 } // namespace faudes
545 
546 
547 #endif
548 
#define FD_MAXCONTAINERNAME
#define FAUDES_API
Definition: cfl_platform.h:85
ConsoleOut * pInstance
Definition: cfl_utils.h:374
std::ofstream * pStream
Definition: cfl_utils.h:368
static ConsoleOut * smpInstance
Definition: cfl_utils.h:376
const std::string & Filename(void)
Definition: cfl_utils.h:353
void Verb(int verb)
Definition: cfl_utils.h:357
std::string mFilename
Definition: cfl_utils.h:370
static bool msDone
Definition: cfl_utils.h:404
static std::map< std::string, long int > * mspCount
Definition: cfl_utils.h:402
static std::map< std::string, long int > * mspMax
Definition: cfl_utils.h:401
uint32_t Idx
std::string VersionString()
Definition: cfl_utils.cpp:141
std::string ExtractDirectory(const std::string &rFullPath)
Definition: cfl_utils.cpp:297
bool DotReady(const std::string &rDotExec)
Definition: cfl_utils.cpp:233
void LoopCallback(bool pBreak(void))
Definition: cfl_utils.cpp:693
std::string PrependPath(const std::string &rLeft, const std::string &rRight)
Definition: cfl_utils.cpp:344
Idx ToIdx(const std::string &rString)
Definition: cfl_utils.cpp:100
void ProcessDot(const std::string &rDotFile, const std::string &rOutFile, const std::string &rOutFormat, const std::string &rDotExec)
Definition: cfl_utils.cpp:177
std::string CreateTempFile(void)
Definition: cfl_utils.cpp:248
bool FileCopy(const std::string &rFromFile, const std::string &rToFile)
Definition: cfl_utils.cpp:438
std::string PluginsString()
Definition: cfl_utils.cpp:146
std::string ExpandString(const std::string &rString, unsigned int len)
Definition: cfl_utils.cpp:80
std::string BuildString()
Definition: cfl_utils.cpp:160
bool FileDelete(const std::string &rFilename)
Definition: cfl_utils.cpp:433
std::string ToStringFloat(Float number)
Definition: cfl_utils.cpp:64
std::string ExtractFilename(const std::string &rFullPath)
Definition: cfl_utils.cpp:306
std::string ToStringInteger16(Int number)
Definition: cfl_utils.cpp:54
double Float
std::string ContributorsString()
Definition: cfl_utils.cpp:151
std::string ToStringInteger(Int number)
Definition: cfl_utils.cpp:43
std::set< std::string > ReadDirectory(const std::string &rDirectory)
Definition: cfl_utils.cpp:389
void Verbosity(int v)
Definition: cfl_utils.cpp:504
std::string StringSubstitute(const std::string &rString, const std::string &rFrom, const std::string &rTo)
Definition: cfl_utils.cpp:121
std::string ToLowerCase(const std::string &rString)
Definition: cfl_utils.cpp:111
void Print(int v, const std::string &message)
Definition: cfl_utils.cpp:491
std::string ExtractBasename(const std::string &rFullPath)
Definition: cfl_utils.cpp:315
bool DirectoryExists(const std::string &rDirectory)
Definition: cfl_utils.cpp:373
std::string TestProtocol(const std::string &rSource)
Definition: cfl_utils.cpp:578
bool FileExists(const std::string &rFilename)
Definition: cfl_utils.cpp:426
std::string ExtractSuffix(const std::string &rFullPath)
Definition: cfl_utils.cpp:329
std::string CollapsString(const std::string &rString, unsigned int len)
Definition: cfl_utils.cpp:91
long int Int

libFAUDES 2.33l --- 2025.09.16 --- c++ api documentaion by doxygen