Tulip  5.0.0
Large graphs analysis and drawing
TlpTools.h
1 /*
2  *
3  * This file is part of Tulip (www.tulip-software.org)
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 NULL
50  * 3. at that point, the Tulip paths will be retrieved from the path of the loaded Tulip shared library
51  * (you must dispose of a standard Tulip installation for that feature to work).
52  * 4. a fallback value of 'C:/Tulip/lib/' on windows, or '/usr/local/lib/' on Unix.
53  */
54 extern TLP_SCOPE void initTulipLib(const char* appDirPath = NULL);
55 
56 /**
57  * @ingroup Plugins
58  *
59  * @brief Demangles the name of a C++ class
60  * @param className The mangled name of a class
61  * @param hideTlp a flag to indicate if the 'tlp::' prefix
62  * @return string The demangled name of a Tulip C++ class.
63  */
64 TLP_SCOPE std::string demangleClassName(const char *className,
65  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] if max is negative
121  */
122 TLP_SCOPE int randomInteger(int max);
123 
124 /**
125  * @brief Returns a random unsigned integer in the range [0, max]
126  */
127 TLP_SCOPE unsigned int randomUnsignedInteger(unsigned int max);
128 
129 /**
130  * @brief Returns a random double in the range [0, max]
131  */
132 TLP_SCOPE double randomDouble(double max = 1.0);
133 
134 /**
135  * @brief Cross-platform function to stat a path on a filesystem. Its purpose is to support
136  * paths on windows platform containing non-ascii characters.
137  * @param pathname an utf-8 encoded string containing the path name to stat
138  * @param buf a pointer to a tlp_stat_t structure (a typedef for struct stat)
139  */
140 TLP_SCOPE int statPath(const std::string &pathname, tlp_stat_t *buf);
141 
142 /**
143  * @brief Cross-platform function to get an input file stream. Its purpose is to support
144  * paths on windows platform containing non-ascii characters.
145  * @param filename an utf-8 encoded string containing the path of the file to open for performing input operations
146  * @param open_mode the stream opening mode flags (bitwise combination of std::ios_base::openmode constants).
147  */
148 TLP_SCOPE std::istream *getInputFileStream(const std::string &filename, std::ios_base::openmode open_mode = std::ios::in);
149 
150 /**
151  * @brief Cross-platform function to get an output file stream. Its purpose is to support
152  * paths on windows platform containing non-ascii characters.
153  * @param filename an utf-8 encoded string containing the path of the file to open for performing output operations
154  * @param open_mode the stream opening mode flags (bitwise combination of std::ios_base::openmode constants).
155  */
156 TLP_SCOPE std::ostream *getOutputFileStream(const std::string &filename, std::ios_base::openmode open_mode = std::ios::out);
157 
158 ///@cond DOXYGEN_HIDDEN
159 // Gui test mode
160 TLP_SCOPE bool inGuiTestingMode();
161 
162 TLP_SCOPE void setGuiTestingMode(bool);
163 ///@endcond
164 
165 }
166 #endif
double randomDouble(double max=1.0)
Returns a random double in the range [0, max].
void initTulipLib(const char *appDirPath=NULL)
Initializes the Tulip library. Looks for the Tulip plug-ins directory. The plug-ins directory can be ...
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...
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.