Tulip  5.2.1
Large graphs analysis and drawing
TlpTools.h
1 /*
2  *
3  * This file is part of Tulip (http://tulip.labri.fr)
4  *
5  * Authors: David Auber and the Tulip development Team
6  * from LaBRI, University of Bordeaux
7  *
8  * Tulip is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation, either version 3
11  * of the License, or (at your option) any later version.
12  *
13  * Tulip is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU General Public License for more details.
17  *
18  */
19 
20 #ifndef _TLPTOOLS_H
21 #define _TLPTOOLS_H
22 
23 #include <iostream>
24 #include <climits>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <tulip/tulipconf.h>
28 
29 #ifdef WIN32
30 typedef struct _stat tlp_stat_t;
31 #else
32 typedef struct stat tlp_stat_t;
33 #endif
34 
35 namespace tlp {
36 extern TLP_SCOPE const char PATH_DELIMITER;
37 extern TLP_SCOPE std::string TulipLibDir;
38 extern TLP_SCOPE std::string TulipPluginsPath;
39 extern TLP_SCOPE std::string TulipBitmapDir;
40 extern TLP_SCOPE std::string TulipShareDir;
41 
42 /**
43  * @ingroup Plugins
44  *
45  * @brief Initializes the Tulip library.
46  * Looks for the Tulip plug-ins directory.
47  * The plug-ins directory can be defined in different ways, given by order of prevalence :
48  * 1. the TLP_DIR environment variable, if it has a value
49  * 2. the appDirPath parameter, if it is not nullptr
50  * 3. at that point, the Tulip paths will be retrieved from the path of the loaded Tulip shared
51  * library
52  * (you must dispose of a standard Tulip installation for that feature to work).
53  * 4. a fallback value of 'C:/Tulip/lib/' on windows, or '/usr/local/lib/' on Unix.
54  */
55 extern TLP_SCOPE void initTulipLib(const char *appDirPath = nullptr);
56 
57 /**
58  * @ingroup Plugins
59  *
60  * @brief Demangles the name of a C++ class
61  * @param className The mangled name of a class
62  * @param hideTlp a flag to indicate if the 'tlp::' prefix
63  * @return string The demangled name of a Tulip C++ class.
64  */
65 TLP_SCOPE std::string demangleClassName(const char *className, bool hideTlp = false);
66 
67 /**
68  * @ingroup Plugins
69  *
70  * @brief Demangles the name of a C++ class defined in the tlp namespace.
71  * @param className The mangled name of a class
72  * @return string The demangled name of a Tulip C++ class
73  * without the tlp:: prefix
74  */
75 inline std::string demangleTlpClassName(const char *className) {
76  return demangleClassName(className, true);
77 }
78 
79 /**
80  * @brief Returns an istream to read from a gzipped file (uses gzstream lib).
81  * The stream has to be deleted after use.
82  * @param name The name of the file to read from.
83  * @param open_mode The mode to open the file with. Defaults to std::ios::in.
84  * @return istream gzipped input stream from a file.
85  */
86 TLP_SCOPE std::istream *getIgzstream(const std::string &name, int open_mode = std::ios::in);
87 
88 /**
89  * @brief Returns an ostream to write to a gzipped file (uses gzstream lib).
90  * The stream has to be deleted after use.
91  * @warning Don't forget to check the stream with ios::bad()!
92  * @param name The name of the file to write to.
93  * @param open_mode The mode to open the file with. Defaults to std::ios::out.
94  * @return ostream gzipped output stream to a file.
95  */
96 TLP_SCOPE std::ostream *getOgzstream(const std::string &name, int open_mode = std::ios::out);
97 
98 /**
99  * @brief Gives the value of the seed used for further initialization
100  * of a random sequence (with further calls to rand() or random()).
101  * @param seed the value of the seed.
102  * Set seed to UINT_MAX if you need a random choice of the seed.
103  */
104 TLP_SCOPE void setSeedOfRandomSequence(unsigned int seed = UINT_MAX);
105 
106 /**
107  * @brief Returns the value of the seed used for further initialization
108  * of a random sequence
109  */
110 TLP_SCOPE unsigned int getSeedOfRandomSequence();
111 
112 /**
113  * @brief Initializes a random sequence with the seed previously set
114  * Further calls to rand() or random() will return the elements of
115  * that sequence
116  */
117 TLP_SCOPE void initRandomSequence();
118 
119 /**
120  * @brief Returns a random integer in the range [0, max] if max is positive or in the range [max, 0]
121  * if max is negative
122  */
123 TLP_SCOPE int randomInteger(int max);
124 
125 /**
126  * @brief Returns a random unsigned integer in the range [0, max]
127  */
128 TLP_SCOPE unsigned int randomUnsignedInteger(unsigned int max);
129 
130 /**
131  * @brief Returns a random double in the range [0, max]
132  */
133 TLP_SCOPE double randomDouble(double max = 1.0);
134 
135 /**
136  * @brief Cross-platform function to stat a path on a filesystem. Its purpose is to support
137  * paths on windows platform containing non-ascii characters.
138  * @param pathname an utf-8 encoded string containing the path name to stat
139  * @param buf a pointer to a tlp_stat_t structure (a typedef for struct stat)
140  */
141 TLP_SCOPE int statPath(const std::string &pathname, tlp_stat_t *buf);
142 
143 /**
144  * @brief Cross-platform function to get an input file stream. Its purpose is to support
145  * paths on windows platform containing non-ascii characters.
146  * @param filename an utf-8 encoded string containing the path of the file to open for performing
147  * input operations
148  * @param open_mode the stream opening mode flags (bitwise combination of std::ios_base::openmode
149  * constants).
150  */
151 TLP_SCOPE std::istream *getInputFileStream(const std::string &filename,
152  std::ios_base::openmode open_mode = std::ios::in);
153 
154 /**
155  * @brief Cross-platform function to get an output file stream. Its purpose is to support
156  * paths on windows platform containing non-ascii characters.
157  * @param filename an utf-8 encoded string containing the path of the file to open for performing
158  * output operations
159  * @param open_mode the stream opening mode flags (bitwise combination of std::ios_base::openmode
160  * constants).
161  */
162 TLP_SCOPE std::ostream *getOutputFileStream(const std::string &filename,
163  std::ios_base::openmode open_mode = std::ios::out);
164 
165 ///@cond DOXYGEN_HIDDEN
166 // Gui test mode
167 TLP_SCOPE bool inGuiTestingMode();
168 
169 TLP_SCOPE void setGuiTestingMode(bool);
170 ///@endcond
171 } // namespace tlp
172 #endif
double randomDouble(double max=1.0)
Returns a random double in the range [0, max].
int statPath(const std::string &pathname, tlp_stat_t *buf)
Cross-platform function to stat a path on a filesystem. Its purpose is to support paths on windows pl...
std::ostream * getOutputFileStream(const std::string &filename, std::ios_base::openmode open_mode=std::ios::out)
Cross-platform function to get an output file stream. Its purpose is to support paths on windows plat...
void initTulipLib(const char *appDirPath=nullptr)
Initializes the Tulip library. Looks for the Tulip plug-ins directory. The plug-ins directory can be ...
int randomInteger(int max)
Returns a random integer in the range [0, max] if max is positive or in the range [max...
unsigned int randomUnsignedInteger(unsigned int max)
Returns a random unsigned integer in the range [0, max].
unsigned int getSeedOfRandomSequence()
Returns the value of the seed used for further initialization of a random sequence.
std::string demangleTlpClassName(const char *className)
Demangles the name of a C++ class defined in the tlp namespace.
Definition: TlpTools.h:75
std::istream * getInputFileStream(const std::string &filename, std::ios_base::openmode open_mode=std::ios::in)
Cross-platform function to get an input file stream. Its purpose is to support paths on windows platf...
std::istream * getIgzstream(const std::string &name, int open_mode=std::ios::in)
Returns an istream to read from a gzipped file (uses gzstream lib). The stream has to be deleted afte...
void initRandomSequence()
Initializes a random sequence with the seed previously set Further calls to rand() or random() will r...
std::ostream * getOgzstream(const std::string &name, int open_mode=std::ios::out)
Returns an ostream to write to a gzipped file (uses gzstream lib). The stream has to be deleted after...
void setSeedOfRandomSequence(unsigned int seed=UINT_MAX)
Gives the value of the seed used for further initialization of a random sequence (with further calls ...
std::string demangleClassName(const char *className, bool hideTlp=false)
Demangles the name of a C++ class.