cpplocate  1.0.0.a8d87a94e3ab
C++ Locator Library
utils.h
Go to the documentation of this file.
1 
2 #pragma once
3 
4 
5 #include <string>
6 #include <vector>
7 
8 #include <cpplocate/cpplocate_api.h>
9 
10 
11 namespace cpplocate
12 {
13 
14 
15 class ModuleInfo;
16 
17 
18 namespace utils
19 {
20 
21 
22 /**
23 * @brief
24 * Replace all occurences of a substring in string
25 *
26 * @param[in,out] str
27 * String
28 * @param[in] original
29 * Original string
30 * @param[in] substitute
31 * Substitute string
32 */
33 CPPLOCATE_API void replace(std::string & str, const std::string & original, const std::string & substitute);
34 
35 /**
36 * @brief
37 * Remove spaces at the beginning and the end of a string
38 *
39 * @param[in,out] str
40 * String
41 */
42 CPPLOCATE_API void trim(std::string & str);
43 
44 /**
45 * @brief
46 * Remove spaces and trailing slash/backslash from path
47 *
48 * @param[in] path
49 * Path
50 *
51 * @return
52 * Trimmed path
53 */
54 CPPLOCATE_API std::string trimPath(const std::string & str);
55 
56 /**
57 * @brief
58 * Remove spaces and trailing slash/backslash from path
59 *
60 * @param[in] path
61 * Path
62 *
63 * @return
64 * Trimmed path
65 */
66 CPPLOCATE_API std::string trimPath(std::string && str);
67 
68 /**
69 * @brief
70 * Convert path into unified form (replace '\' with '/')
71 *
72 * @param[in] path
73 * Path
74 *
75 * @return
76 * Path with forward slashes
77 */
78 CPPLOCATE_API std::string unifiedPath(const std::string & path);
79 
80 /**
81 * @brief
82 * Convert path into unified form (replace '\' with '/')
83 *
84 * @param[in] path
85 * Path
86 *
87 * @return
88 * Path with forward slashes
89 */
90 CPPLOCATE_API std::string unifiedPath(std::string && path);
91 
92 /**
93 * @brief
94 * Cut away filename portion of a path, get path to directory
95 *
96 * @param[in] fullpath
97 * Path (e.g., '/path/to/file.txt')
98 *
99 * @return
100 * Directory path (e.g., '/path/to')
101 */
102 CPPLOCATE_API std::string getDirectoryPath(const std::string & fullpath);
103 
104 /**
105 * @brief
106 * Get position right after given substring (searched from right to left)
107 *
108 * @param[in] str
109 * Full string
110 * @param[in] substr
111 * Substring to search for
112 *
113 * @return
114 * Position after substring if substring was found, else string::npos
115 */
116 CPPLOCATE_API size_t posAfterString(const std::string & str, const std::string & substr);
117 
118 /**
119 * @brief
120 * Get system base path for path to library or executable
121 *
122 * @param[in] path
123 * Path to library or executable (e.g., '/usr/bin/myapp')
124 *
125 * @return
126 * System path path (e.g., '/usr')
127 *
128 * @remarks
129 * This function returns the base path if the given file
130 * is a system path. Otherwise, it returns an empty string.
131 *
132 * Examples:
133 * '/usr/bin/myapp' -> '/usr'
134 * '/usr/local/bin/myapp' -> '/usr/local'
135 * '/usr/lib/mylib.so' -> '/usr'
136 * '/usr/lib64/mylib.so' -> '/usr'
137 * '/usr/local/lib64/mylib.so' -> '/usr/local'
138 * '/crosscompile/armv4/usr/lib/mylib.so.2' -> '/crosscompile/armv4/usr'
139 */
140 CPPLOCATE_API std::string getSystemBasePath(const std::string & path);
141 
142 /**
143 * @brief
144 * Split string into array of strings
145 *
146 * @param[in] str
147 * Input string
148 * @param[in] delim
149 * Delimiter used to split the string
150 * @param[out] values
151 * Output vector
152 */
153 CPPLOCATE_API void split(const std::string & str, char delim, std::vector<std::string> & values);
154 
155 /**
156 * @brief
157 * Join array of strings into a single string
158 *
159 * @param[out] values
160 * Input vector
161 * @param[in] delim
162 * Delimiter used to join the string
163 *
164 * @return
165 * Joined string
166 */
167 CPPLOCATE_API std::string join(const std::vector<std::string> & values, const std::string & delim);
168 
169 /**
170 * @brief
171 * Split list of paths separated by : or ; into list of paths
172 *
173 * @param[in] paths
174 * Input string
175 * @param[out] values
176 * Output vector
177 *
178 * @remarks
179 * On windows, the separator ';' is used, ':' on all other systems
180 */
181 CPPLOCATE_API void getPaths(const std::string & str, std::vector<std::string> & values);
182 
183 /**
184 * @brief
185 * Get value of environment variable
186 *
187 * @param[in] name
188 * Name of environment variable
189 *
190 * @return
191 * Value of the environment variable
192 */
193 CPPLOCATE_API std::string getEnv(const std::string & name);
194 
195 /**
196 * @brief
197 * Check if file or directory exists
198 *
199 * @param[in] path
200 * Path to file or directory
201 *
202 * @return
203 * 'true' if it exists, else 'false'
204 */
205 CPPLOCATE_API bool fileExists(const std::string & path);
206 
207 /**
208 * @brief
209 * Load module information file
210 *
211 * @param[in] directory
212 * Path to directory
213 * @param[in] name
214 * Module name
215 * @param[out] info
216 * Module information
217 *
218 * @return
219 * 'true' if module could be loaded successfully, else 'false'
220 *
221 * @remarks
222 * The filename is constructed as <directory>/<name>.modinfo
223 */
224 CPPLOCATE_API bool loadModule(const std::string & directory, const std::string & name, ModuleInfo & info);
225 
226 
227 } // namespace utils
228 
229 } // namespace cpplocate
CPPLOCATE_API std::string join(const std::vector< std::string > &values, const std::string &delim)
Join array of strings into a single string.
CPPLOCATE_API bool fileExists(const std::string &path)
Check if file or directory exists.
CPPLOCATE_API std::string unifiedPath(const std::string &path)
Convert path into unified form (replace &#39;\&#39; with &#39;/&#39;)
CPPLOCATE_API std::string trimPath(const std::string &str)
Remove spaces and trailing slash/backslash from path.
CPPLOCATE_API void getPaths(const std::string &str, std::vector< std::string > &values)
Split list of paths separated by : or ; into list of paths.
CPPLOCATE_API std::string getDirectoryPath(const std::string &fullpath)
Cut away filename portion of a path, get path to directory.
CPPLOCATE_API void split(const std::string &str, char delim, std::vector< std::string > &values)
Split string into array of strings.
CPPLOCATE_API bool loadModule(const std::string &directory, const std::string &name, ModuleInfo &info)
Load module information file.
CPPLOCATE_API std::string getEnv(const std::string &name)
Get value of environment variable.
CPPLOCATE_API size_t posAfterString(const std::string &str, const std::string &substr)
Get position right after given substring (searched from right to left)
Description of a module, containing key/value pairs.
Definition: ModuleInfo.h:20
CPPLOCATE_API void replace(std::string &str, const std::string &original, const std::string &substitute)
Replace all occurences of a substring in string.
CPPLOCATE_API std::string getSystemBasePath(const std::string &path)
Get system base path for path to library or executable.
CPPLOCATE_API void trim(std::string &str)
Remove spaces at the beginning and the end of a string.
Definition: cpplocate.h:10