Commit | Line | Data |
---|---|---|
0e23f538 TJ |
1 | /* |
2 | The software in this package is distributed under the GNU General | |
3 | Public License version 2 (with a special exception described below). | |
4 | ||
5 | A copy of GNU General Public License (GPL) is included in this distribution, | |
6 | in the file COPYING.GPL. | |
7 | ||
8 | As a special exception, if other files instantiate templates or use macros | |
9 | or inline functions from this file, or you compile this file and link it | |
10 | with other works to produce a work based on this file, this file | |
11 | does not by itself cause the resulting work to be covered | |
12 | by the GNU General Public License. | |
13 | ||
14 | However the source code for this file must still be made available | |
15 | in accordance with section (3) of the GNU General Public License. | |
16 | ||
17 | This exception does not invalidate any other reasons why a work based | |
18 | on this file might be covered by the GNU General Public License. | |
19 | */ | |
6a93d84a TJ |
20 | /** @file |
21 | * @brief collection of string tools (/ functions). | |
22 | * | |
23 | * contains a collection of miscellaneous functions for dealing with strings. | |
24 | * | |
25 | * some functions (like trim, lower case, upper case ...) are available in two versions: | |
26 | * - a modifying one (suffix "Mod") which modifies the given string argument in place. | |
27 | * - a non modifying one which take a constant string reference and returns a new string. | |
28 | * . | |
29 | * | |
30 | * | |
31 | * (c) Copyright 2007-2008 by Intra2net AG | |
6a93d84a | 32 | */ |
e93545dd GE |
33 | |
34 | #ifndef __STRINGFUNC_HXX | |
35 | #define __STRINGFUNC_HXX | |
36 | ||
6a93d84a | 37 | #include <list> |
376ec4fa | 38 | #include <vector> |
e93545dd | 39 | #include <string> |
6a93d84a TJ |
40 | #include <sstream> |
41 | #include <stdexcept> | |
b04b4f7e | 42 | #include <sys/types.h> |
6a93d84a | 43 | |
6ab3bc95 RP |
44 | namespace I2n |
45 | { | |
6a93d84a TJ |
46 | |
47 | /* | |
48 | ** some useful constants: | |
49 | */ | |
50 | ||
6ab3bc95 RP |
51 | extern const std::string Whitespaces; |
52 | extern const std::string LineEndings; | |
6a93d84a TJ |
53 | |
54 | ||
55 | /* | |
56 | ** predicates: | |
57 | */ | |
58 | ||
59 | ||
6ab3bc95 | 60 | bool has_prefix(const std::string& str, const std::string& prefix); |
6a93d84a | 61 | |
6ab3bc95 | 62 | bool has_suffix(const std::string& str, const std::string& suffix); |
6a93d84a TJ |
63 | |
64 | ||
65 | /* | |
6ab3bc95 | 66 | ** tool functions(modifying): |
6a93d84a TJ |
67 | */ |
68 | ||
6ab3bc95 | 69 | std::string trim_mod(std::string& str, const std::string& charlist = Whitespaces); |
6a93d84a | 70 | |
6ab3bc95 | 71 | std::string chomp_mod(std::string& str, const std::string& what= LineEndings ); |
6a93d84a | 72 | |
6ab3bc95 | 73 | std::string to_lower_mod(std::string& str); |
6a93d84a | 74 | |
6ab3bc95 | 75 | std::string to_upper_mod(std::string& str); |
6a93d84a TJ |
76 | |
77 | ||
78 | /* | |
79 | ** tool functions (not modifying): | |
80 | */ | |
81 | ||
6ab3bc95 | 82 | std::string trim(const std::string& str, const std::string& charlist = Whitespaces); |
6a93d84a | 83 | |
6ab3bc95 | 84 | std::string chomp(const std::string& str, const std::string& what= LineEndings ); |
6a93d84a | 85 | |
6ab3bc95 | 86 | std::string to_lower(const std::string& str); |
6a93d84a | 87 | |
6ab3bc95 | 88 | std::string to_upper(const std::string& str); |
6a93d84a TJ |
89 | |
90 | ||
6ab3bc95 | 91 | std::string remove_suffix(const std::string& str, const std::string& suffix); |
6a93d84a | 92 | |
6ab3bc95 | 93 | std::string remove_prefix(const std::string& str, const std::string& prefix); |
6a93d84a TJ |
94 | |
95 | ||
96 | ||
97 | /* | |
98 | ** split and join: | |
99 | */ | |
100 | ||
101 | ||
6ab3bc95 RP |
102 | bool pair_split( |
103 | const std::string& str, | |
104 | std::string& key, | |
105 | std::string& value, | |
106 | char delimiter = '='); | |
6a93d84a TJ |
107 | |
108 | ||
6ab3bc95 RP |
109 | void split_string( |
110 | const std::string& str, | |
111 | std::list< std::string >& result, | |
112 | const std::string& delimiter= "\n", | |
113 | bool omit_empty= false, | |
114 | const std::string& trim_list= std::string() | |
6a93d84a TJ |
115 | ); |
116 | ||
6ab3bc95 RP |
117 | std::list< std::string > split_string( |
118 | const std::string& str, | |
119 | const std::string& delimiter = "\n", | |
120 | bool omit_empty= false, | |
121 | const std::string& trim_list= std::string() | |
6a93d84a TJ |
122 | ); |
123 | ||
124 | ||
6ab3bc95 RP |
125 | std::string join_string( |
126 | const std::list< std::string >& parts, | |
127 | const std::string& delimiter = "\n" | |
6a93d84a TJ |
128 | ); |
129 | ||
376ec4fa CH |
130 | std::string join_string( |
131 | const std::vector< std::string >& parts, | |
132 | const std::string& delimiter = "\n" | |
133 | ); | |
134 | ||
6a93d84a TJ |
135 | |
136 | /* | |
137 | ** conversions: | |
138 | */ | |
139 | ||
140 | ||
6ab3bc95 | 141 | std::string convert_binary_to_hex(const std::string&str, bool upper_case_digits= false); |
6a93d84a | 142 | |
6ab3bc95 | 143 | std::string convert_hex_to_binary(const std::string& str) throw(std::runtime_error); |
6a93d84a TJ |
144 | |
145 | ||
6a93d84a TJ |
146 | /* |
147 | ** "type conversions": | |
148 | */ | |
149 | ||
150 | ||
151 | /** | |
152 | * convert a datatype @a T to a string via string stream. | |
153 | * | |
154 | * @param s the string which should be converted to @a T. | |
155 | * @return the value of type T. | |
156 | */ | |
157 | template< | |
6ab3bc95 | 158 | class T |
6a93d84a | 159 | > |
6ab3bc95 | 160 | T string_to(const std::string& s) |
6a93d84a | 161 | { |
6ab3bc95 RP |
162 | std::istringstream istr(s); |
163 | T result; | |
164 | istr >> result; | |
165 | return result; | |
166 | } // eo string_to(const std::string&) | |
6a93d84a TJ |
167 | |
168 | ||
169 | /** | |
170 | * convert a datatype @a T to a string via string stream. | |
171 | * | |
172 | * @param s the string which should be converted to @a T. | |
173 | * @param result the resulting value of type @a T. | |
174 | * @return @a true iff the internal string stream was EOF after the conversion. | |
08f2d184 GE |
175 | * |
176 | * @attention: does not return if the conversion was successful. So check for empty strings before. | |
6a93d84a TJ |
177 | */ |
178 | template< | |
6ab3bc95 | 179 | class T |
6a93d84a | 180 | > |
6ab3bc95 | 181 | bool string_to(const std::string& s, T& result) |
6a93d84a | 182 | { |
6ab3bc95 RP |
183 | std::istringstream istr(s); |
184 | istr >> result; | |
185 | return istr.eof(); | |
186 | } // eo string_to(const std::string&) | |
6a93d84a TJ |
187 | |
188 | ||
189 | /** | |
3bcc713f GE |
190 | * convert string in hexadecimal notation to a datatype @a T |
191 | * supports strings with and without "0x" notation, e.g. 0xff and FF are both valid | |
192 | * | |
193 | * @param s the hex string which should be converted to @a T. | |
194 | * @return the value of type T. | |
195 | */ | |
196 | template< | |
197 | class T | |
198 | > | |
199 | T hex_string_to(const std::string& s) | |
200 | { | |
201 | std::istringstream istr(s); | |
202 | T result; | |
203 | istr >> std::hex >> result; | |
204 | return result; | |
205 | } // eo string_to(const std::string&) | |
206 | ||
207 | ||
208 | /** | |
209 | * convert string in hexadecimal notation to a datatype @a T | |
210 | * supports strings with and without "0x" notation, e.g. 0xff and FF are both valid | |
211 | * | |
212 | * @param s the hex string which should be converted to @a T. | |
213 | * @param result the resulting value of type @a T. | |
214 | * @return @a true iff the internal string stream was EOF after the conversion. | |
215 | * | |
216 | * @attention: does not return if the conversion was successful. So check for empty strings before. | |
217 | */ | |
218 | template< | |
219 | class T | |
220 | > | |
221 | bool hex_string_to(const std::string& s, T& result) | |
222 | { | |
223 | std::istringstream istr(s); | |
224 | istr >> std::hex >> result; | |
225 | return istr.eof(); | |
226 | } // eo string_to(const std::string&) | |
227 | ||
228 | ||
229 | /** | |
6a93d84a TJ |
230 | * convert a string to another datatype @a T via string stream. |
231 | * | |
232 | * @param v the value (of type @a T) which should be converted to a string. | |
233 | * @return the resulting string. | |
234 | */ | |
235 | template< | |
6ab3bc95 | 236 | class T |
6a93d84a | 237 | > |
6ab3bc95 | 238 | std::string to_string(const T& v) |
6a93d84a | 239 | { |
6ab3bc95 RP |
240 | std::ostringstream ostr; |
241 | ostr << v; | |
242 | return ostr.str(); | |
243 | } // eo to_string(const T&) | |
244 | ||
245 | ||
246 | } // eo namespace I2n | |
247 | ||
6a93d84a | 248 | |
e5b21dbb | 249 | #if 0 |
6ab3bc95 RP |
250 | std::string to_lower(const std::string &src); |
251 | std::string to_upper(const std::string &src); | |
e5b21dbb RP |
252 | #else |
253 | // compatibility: import lower/upper funcs from I2n: | |
254 | using I2n::to_lower; | |
255 | using I2n::to_upper; | |
256 | #endif | |
e93545dd | 257 | |
81267544 | 258 | |
8f296924 GMF |
259 | enum UnitBase { |
260 | UnitBase1000, // SI decimal, composed by multiples of 1000 (KB, MB, etc.) | |
261 | UnitBase1024 // IEC binary, composed by multiples of 1024 (KiB, MiB, etc. ) | |
81267544 GMF |
262 | }; |
263 | ||
8f296924 GMF |
264 | enum UnitFormat { |
265 | ShortUnitFormat, // B, KB, MB, ... | |
266 | LongUnitFormat // Byte, KByte, MByte, ... | |
d1ea9075 GMF |
267 | }; |
268 | ||
81267544 GMF |
269 | std::string nice_unit_format( |
270 | const int64_t input, | |
ee037ee3 GMF |
271 | const UnitFormat format = ShortUnitFormat, |
272 | const UnitBase base = UnitBase1024 | |
81267544 | 273 | ); |
e93545dd | 274 | |
5cd64148 CH |
275 | std::string nice_unit_format( |
276 | const double input, | |
277 | const UnitFormat format = ShortUnitFormat, | |
278 | const UnitBase base = UnitBase1024 | |
279 | ); | |
280 | ||
e93545dd GE |
281 | bool replace_all(std::string &base, const std::string *ist, const std::string *soll); |
282 | bool replace_all(std::string &base, const char *ist, const char *soll); | |
283 | bool replace_all(std::string &base, const char *ist, const std::string *soll); | |
284 | bool replace_all(std::string &base, const std::string &ist, const char *soll); | |
285 | bool replace_all(std::string &base, const std::string &ist, const std::string &soll); | |
286 | ||
287 | std::string iso_to_utf8(const std::string& isostring); | |
288 | std::string utf8_to_iso(const std::string& utf8string); | |
13cc4db1 | 289 | std::string utf7imap_to_utf8(const std::string &utf7imapstring); |
6a2b6dd1 | 290 | std::string utf8_to_utf7imap(const std::string &utf8string); |
118e216e TJ |
291 | |
292 | std::string strip_html_tags(const std::string &input); | |
293 | std::string smart_html_entities(const std::string &input); | |
294 | std::string html_entities(std::string str); | |
554f813d | 295 | std::string html_entities_to_console(std::string str); |
118e216e | 296 | |
b953bf36 GE |
297 | std::string sanitize_for_logging(const std::string &str, const char replace_with='?'); |
298 | ||
47c07fba GE |
299 | std::string escape(const std::string &s); |
300 | ||
301 | std::string descape(const std::string &s, int startpos, int &endpos); | |
e6da286a | 302 | inline std::string descape(const std::string &s) |
47c07fba | 303 | { |
6ab3bc95 RP |
304 | int endpos; |
305 | return descape(s,0,endpos); | |
47c07fba GE |
306 | } |
307 | ||
308 | std::string escape_shellarg(const std::string &input); | |
309 | ||
e93545dd | 310 | #endif |