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

libFAUDES 2.33h --- 2025.06.18 --- c++ api documentaion by doxygen