Tulip  5.4.0
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 // The hash_combine function from the boost library
36 // Call it repeatedly to incrementally create a hash value from several variables.
37 
38 // Explanation of the formula from StackOverflow
39 // (http://stackoverflow.com/questions/4948780/magic-numbers-in-boosthash-combine) :
40 // The magic number 0x9e3779b9 = 2^32 / ((1 + sqrt(5)) / 2) is the reciprocal of the golden ratio.
41 // It is supposed to be 32 random bits, where each is equally likely to be 0 or 1, and with no
42 // simple correlation between the bits.
43 // So including this number "randomly" changes each bit of the seed; as you say, this means that
44 // consecutive values will be far apart.
45 // Including the shifted versions of the old seed makes sure that, even if hash_value() has a fairly
46 // small range of values,
47 // differences will soon be spread across all the bits.
48 namespace std {
49 template <class T>
50 inline void tlp_hash_combine(std::size_t &seed, const T &v) {
51  hash<T> hasher;
52  seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
53 }
54 } // namespace std
55 
56 namespace tlp {
57 extern TLP_SCOPE const char PATH_DELIMITER;
58 extern TLP_SCOPE std::string TulipLibDir;
59 extern TLP_SCOPE std::string TulipPluginsPath;
60 extern TLP_SCOPE std::string TulipBitmapDir;
61 extern TLP_SCOPE std::string TulipShareDir;
62 extern TLP_SCOPE bool TulipProgramExiting;
63 
64 /**
65  * @ingroup Plugins
66  *
67  * @brief Initializes the Tulip library.
68  * Looks for the Tulip plug-ins directory.
69  * The plug-ins directory can be defined in different ways, given by order of prevalence :
70  * 1. the TLP_DIR environment variable, if it has a value
71  * 2. the appDirPath parameter, if it is not nullptr
72  * 3. at that point, the Tulip paths will be retrieved from the path of the loaded Tulip shared
73  * library
74  * (you must dispose of a standard Tulip installation for that feature to work).
75  * 4. a fallback value of 'C:/Tulip/lib/' on windows, or '/usr/local/lib/' on Unix.
76  */
77 extern TLP_SCOPE void initTulipLib(const char *appDirPath = nullptr);
78 
79 /**
80  * @ingroup Plugins
81  *
82  * @brief Demangles the name of a C++ class
83  * @param className The mangled name of a class
84  * @param hideTlp a flag to indicate if the 'tlp::' prefix
85  * @return string The demangled name of a Tulip C++ class.
86  */
87 TLP_SCOPE std::string demangleClassName(const char *className, bool hideTlp = false);
88 
89 /**
90  * @ingroup Plugins
91  *
92  * @brief Demangles the name of a C++ class defined in the tlp namespace.
93  * @param className The mangled name of a class
94  * @return string The demangled name of a Tulip C++ class
95  * without the tlp:: prefix
96  */
97 inline std::string demangleTlpClassName(const char *className) {
98  return demangleClassName(className, true);
99 }
100 
101 /**
102  * @brief Returns an istream to read from a gzipped file (uses gzstream lib).
103  * The stream has to be deleted after use.
104  * @param name The name of the file to read from.
105  * @param open_mode The mode to open the file with. Defaults to std::ios::in.
106  * @return istream gzipped input stream from a file.
107  */
108 TLP_SCOPE std::istream *getIgzstream(const std::string &name, int open_mode = std::ios::in);
109 
110 /**
111  * @brief Returns an ostream to write to a gzipped file (uses gzstream lib).
112  * The stream has to be deleted after use.
113  * @warning Don't forget to check the stream with ios::bad()!
114  * @param name The name of the file to write to.
115  * @param open_mode The mode to open the file with. Defaults to std::ios::out.
116  * @return ostream gzipped output stream to a file.
117  */
118 TLP_SCOPE std::ostream *getOgzstream(const std::string &name, int open_mode = std::ios::out);
119 
120 /**
121  * @brief Gives the value of the seed used for further initialization
122  * of a random sequence (with further calls to rand() or random()).
123  * @param seed the value of the seed.
124  * Set seed to UINT_MAX if you need a random choice of the seed.
125  */
126 TLP_SCOPE void setSeedOfRandomSequence(unsigned int seed = UINT_MAX);
127 
128 /**
129  * @brief Returns the value of the seed used for further initialization
130  * of a random sequence
131  */
132 TLP_SCOPE unsigned int getSeedOfRandomSequence();
133 
134 /**
135  * @brief Initializes a random sequence with the seed previously set
136  * Further calls to rand() or random() will return the elements of
137  * that sequence
138  */
139 TLP_SCOPE void initRandomSequence();
140 
141 /**
142  * @brief Returns a random integer in the range [0, max] if max is positive or in the range [max, 0]
143  * if max is negative
144  */
145 TLP_SCOPE int randomInteger(int max);
146 
147 /**
148  * @brief Returns a random unsigned integer in the range [0, max]
149  */
150 TLP_SCOPE unsigned int randomUnsignedInteger(unsigned int max);
151 
152 /**
153  * @brief Returns a random double in the range [0, max]
154  */
155 TLP_SCOPE double randomDouble(double max = 1.0);
156 
157 /**
158  * @brief Cross-platform function to stat a path on a filesystem. Its purpose is to support
159  * paths on windows platform containing non-ascii characters.
160  * @param pathname an utf-8 encoded string containing the path name to stat
161  * @param buf a pointer to a tlp_stat_t structure (a typedef for struct stat)
162  */
163 TLP_SCOPE int statPath(const std::string &pathname, tlp_stat_t *buf);
164 
165 /**
166  * @brief Cross-platform function to get an input file stream. Its purpose is to support
167  * paths on windows platform containing non-ascii characters.
168  * @param filename an utf-8 encoded string containing the path of the file to open for performing
169  * input operations
170  * @param open_mode the stream opening mode flags (bitwise combination of std::ios_base::openmode
171  * constants).
172  */
173 TLP_SCOPE std::istream *getInputFileStream(const std::string &filename,
174  std::ios_base::openmode open_mode = std::ios::in);
175 
176 /**
177  * @brief Cross-platform function to get an output file stream. Its purpose is to support
178  * paths on windows platform containing non-ascii characters.
179  * @param filename an utf-8 encoded string containing the path of the file to open for performing
180  * output operations
181  * @param open_mode the stream opening mode flags (bitwise combination of std::ios_base::openmode
182  * constants).
183  */
184 TLP_SCOPE std::ostream *getOutputFileStream(const std::string &filename,
185  std::ios_base::openmode open_mode = std::ios::out);
186 
187 ///@cond DOXYGEN_HIDDEN
188 // Gui test mode
189 TLP_SCOPE bool inGuiTestingMode();
190 TLP_SCOPE void setGuiTestingMode(bool);
191 // exit handler registration
192 TLP_SCOPE void registerTulipExitHandler();
193 ///@endcond
194 } // namespace tlp
195 #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...
Definition: TlpTools.h:48
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:97
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.