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
31namespace faudes {
32
33// forward
34class Type;
35
36/**
37 * integer to string
38 *
39 * @param number
40 * integer
41 *
42 * @return
43 * string
44 */
45extern 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 */
57extern 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 */
68extern 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 */
83extern 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 */
96extern 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 */
111extern 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 */
122extern 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 */
137extern 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 */
146extern FAUDES_API std::string VersionString();
147
148/**
149 * Return FAUDES_PLUGINS as std::string
150 *
151 * @return
152 * std::string with FAUDES_VERSION
153 */
154extern FAUDES_API std::string PluginsString();
155
156/**
157 * Return contributors as std::string
158 *
159 * @return
160 * std::string
161 */
162extern FAUDES_API std::string ContributorsString();
163
164/**
165 * Return contributors as std::string
166 *
167 * @return
168 * std::string
169 */
170extern 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 */
191extern 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 */
202extern 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 */
212extern 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 */
230extern 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 */
240extern 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 */
251extern 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 */
261extern 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 */
274extern 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 */
284extern 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 */
294extern 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 */
306extern 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 */
316extern 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 */
327extern 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 */
345public:
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 bool IsFile(void);
353 /** Query filename */
354 const std::string& Filename(void) { return mFilename;};
355 /** Redirect */
356 void Redirect(ConsoleOut* out);
357 /** Verbosity */
358 void Verb(int verb) {mVerb=verb;};
359 int Verb() { return mVerb;};
360protected:
361 /** Constructor */
362 ConsoleOut(void);
363 /** Destructor */
364 virtual ~ConsoleOut(void);
365 /** Writing hook. Re-implement this function in order to grab all output */
366 virtual void DoWrite(const std::string& message,long int cntnow=0, long int cntdone=0, int verb=1);
367private:
368 /** Private output stream */
369 std::ofstream* pStream;
370 /** Private record file name */
371 std::string mFilename;
372 /** Mute flag */
373 int mVerb;
374 /** Redirect */
376 /** Private static instance */
378};
379
380
381/** API wrapper Print at verbosity */
382extern FAUDES_API void Print(int v, const std::string& message);
383extern FAUDES_API void Print(const std::string& message);
384extern FAUDES_API void Verbosity(int v);
385extern FAUDES_API int Verbosity(void);
386
387/**
388 * Debugging counter. Counts items as specified by the type string and reports
389 * sums on exit. You must define the macro FAUDES_DEBUG_CODE to get a report.
390 *
391 * Technical note: we use the somewhat winded static member construct to
392 * guarantee that our member variables have been constructed befor actual
393 * counting occurs. This is neccessary since some faudes types might have a
394 * static instance and, hence, may be consructed rather early.
395 *
396 */
398 public:
399 static void Inc(const std::string& rTypeName);
400 static void Dec(const std::string& rTypeName);
401 static void Init(void);
402 static std::map< std::string, long int >* mspMax;
403 static std::map< std::string, long int >* mspCount;
404 private:
405 static bool msDone;
406 ObjectCount(void);
407};
408
409
410/**
411 * Test Protocol.
412 * Sets the filename for the test protocol by
413 * - removing any path specififucation,
414 * - replacing "." by "_",
415 * - appending ".prot", and finaly
416 * - prepending "tmp_".
417 * The function returns the filename except for the
418 * "tmp_" prefix. The latter is considered the nominal
419 * protocol output (aka reference protocol).
420 *
421 * Note: only the first invocation of this functions actually sets
422 * the protocol file. Further invocations are ignored, but can be
423 * used to query the reference protocol.
424 *
425 * @param rSource
426 * Source file to protocol
427 * @return
428 * Filename with nominal protocol.
429 *
430 */
431extern FAUDES_API std::string TestProtocol(const std::string& rSource);
432
433
434/**
435 * Test Protocol.
436 * This function dumps the specified data to the protocol file for
437 * the purpose of later comparison with a refernce value.
438 * If the protocol file has not been set up, this
439 * function does nothing; see also TestProtocol(const std::string&.
440 *
441 * @param rMessage
442 * Informal identifyer of the test
443 * @param rData
444 * Formal result of the test case
445 * @param core
446 * Whether to record full token io or statistics only.
447 *
448 */
449extern FAUDES_API void TestProtocol(const std::string& rMessage, const Type& rData, bool core=false);
450
451/**
452 * Test Protocol.
453 * Specialized version for boolean test data.
454 * See also TestProtocol(const std::string&, const Type&, bool);
455 *
456 * @param rMessage
457 * Informal identifyer of the test
458 * @param data
459 * Test data
460 *
461 */
462extern FAUDES_API void TestProtocol(const std::string& rMessage, bool data);
463
464/**
465 * Test Protocol.
466 * Specialized version for integer test data.
467 * See also TestProtocol(const std::string&, const Type&, bool);
468 *
469 * @param rMessage
470 * Informal identifyer of the test
471 * @param data
472 * Test data
473 *
474 */
475extern FAUDES_API void TestProtocol(const std::string& rMessage, long int data);
476
477
478/**
479 * Test Protocol.
480 * Specialized version for string data.
481 * See also TestProtocol(const std::string&, const Type&, bool);
482 *
483 * @param rMessage
484 * Informal identifyer of the test
485 * @param data
486 * Test data
487 *
488 */
489extern FAUDES_API void TestProtocol(const std::string& rMessage, const std::string& data);
490
491
492/**
493 * Test Protocol.
494 * Perform a comparison of the recent protocol file and the
495 * corresponding reference. Returns true if the test passes.
496 *
497 * Note: this function closes the current protocol.
498 *
499 * @return
500 * True <=> test past
501 */
502extern FAUDES_API bool TestProtocol(void);
503
504
505/** Test protocol record macro ("mangle" filename for platform independance)*/
506#define FAUDES_TEST_DUMP(mes,dat) { \
507 TestProtocol(__FILE__); \
508 std::string fname= __FILE__; \
509 std::replace(fname.begin(),fname.end(),'\\','/'); \
510 fname=ExtractFilename(fname); \
511 std::stringstream sstr; \
512 sstr << mes << " [at " << fname << ":" << __LINE__ << "]" ; \
513 TestProtocol(sstr.str(),dat); }
514
515/** Test protocol diff macro */
516#define FAUDES_TEST_DIFF() { if(!TestProtocol()) { \
517 FAUDES_WRITE_CONSOLE("FAUDES_TEST_DIFF: sensed test case error in " << __FILE__); exit(0);} }
518
519
520/** Algorithm loop callback
521 *
522 * Set a callback function for libFAUDES algorithms. Applications
523 * are meant to use this interface to terminate an algorithm on user
524 * request. libFAUDES algorithms are meant to throw an execption when
525 * the callback function returns true. See also void LoopCallback(void).
526 *
527 * @param pBreakFnct
528 *
529 */
530extern FAUDES_API void LoopCallback(bool (*pBreakFnct)(void));
531
532 /** Algorithm loop callback
533 *
534 * Calls the loop callback function and throws an exception if it
535 * returns true.
536 *
537 * @exception
538 * Break on appliation request (id 110)
539 *
540 */
541extern FAUDES_API void LoopCallback(void);
542
543
544
545} // namespace faudes
546
547
548#endif
549
#define FD_MAXCONTAINERNAME
#define FAUDES_API
ConsoleOut * pInstance
Definition cfl_utils.h:375
std::ofstream * pStream
Definition cfl_utils.h:369
static ConsoleOut * smpInstance
Definition cfl_utils.h:377
void Verb(int verb)
Definition cfl_utils.h:358
const std::string & Filename(void)
Definition cfl_utils.h:354
std::string mFilename
Definition cfl_utils.h:371
static bool msDone
Definition cfl_utils.h:405
static std::map< std::string, long int > * mspCount
Definition cfl_utils.h:403
static std::map< std::string, long int > * mspMax
Definition cfl_utils.h:402
uint32_t Idx
std::string VersionString()
std::string ExtractDirectory(const std::string &rFullPath)
bool DotReady(const std::string &rDotExec)
int Verbosity(void)
std::string PrependPath(const std::string &rLeft, const std::string &rRight)
Idx ToIdx(const std::string &rString)
void ProcessDot(const std::string &rDotFile, const std::string &rOutFile, const std::string &rOutFormat, const std::string &rDotExec)
std::string CreateTempFile(void)
bool FileCopy(const std::string &rFromFile, const std::string &rToFile)
void LoopCallback(void)
std::string PluginsString()
std::string ExpandString(const std::string &rString, unsigned int len)
Definition cfl_utils.cpp:80
std::string BuildString()
bool TestProtocol(void)
bool FileDelete(const std::string &rFilename)
std::string ToStringFloat(Float number)
Definition cfl_utils.cpp:64
std::string ExtractFilename(const std::string &rFullPath)
std::string ToStringInteger16(Int number)
Definition cfl_utils.cpp:54
double Float
std::string ContributorsString()
std::string ToStringInteger(Int number)
Definition cfl_utils.cpp:43
std::set< std::string > ReadDirectory(const std::string &rDirectory)
std::string StringSubstitute(const std::string &rString, const std::string &rFrom, const std::string &rTo)
std::string ToLowerCase(const std::string &rString)
void Print(int v, const std::string &message)
std::string ExtractBasename(const std::string &rFullPath)
bool DirectoryExists(const std::string &rDirectory)
bool FileExists(const std::string &rFilename)
std::string ExtractSuffix(const std::string &rFullPath)
std::string CollapsString(const std::string &rString, unsigned int len)
Definition cfl_utils.cpp:91
long int Int

libFAUDES 2.34e --- 2026.03.16 --- c++ api documentaion by doxygen