b91a18fd6b8498dba6a9cab6723f698b3c11b768
[libi2ncommon] / src / stringfunc.hxx
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 */
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
32  */
33
34 #ifndef __STRINGFUNC_HXX
35 #define __STRINGFUNC_HXX
36
37 #include <list>
38 #include <string>
39 #include <sstream>
40 #include <stdexcept>
41 #include <sys/types.h>
42
43 namespace I2n
44 {
45
46 /*
47 ** some useful constants:
48 */
49
50 extern const std::string Whitespaces;
51 extern const std::string LineEndings;
52
53
54 /*
55 ** predicates:
56 */
57
58
59 bool has_prefix(const std::string& str, const std::string& prefix);
60
61 bool has_suffix(const std::string& str, const std::string& suffix);
62
63
64 /*
65 ** tool functions(modifying):
66 */
67
68 std::string trim_mod(std::string& str, const std::string& charlist = Whitespaces);
69
70 std::string chomp_mod(std::string& str, const std::string& what= LineEndings );
71
72 std::string to_lower_mod(std::string& str);
73
74 std::string to_upper_mod(std::string& str);
75
76
77 /*
78 ** tool functions (not modifying):
79 */
80
81 std::string trim(const std::string& str, const std::string& charlist = Whitespaces);
82
83 std::string chomp(const std::string& str, const std::string& what= LineEndings );
84
85 std::string to_lower(const std::string& str);
86
87 std::string to_upper(const std::string& str);
88
89
90 std::string remove_suffix(const std::string& str, const std::string& suffix);
91
92 std::string remove_prefix(const std::string& str, const std::string& prefix);
93
94
95
96 /*
97 ** split and join:
98 */
99
100
101 bool pair_split(
102    const std::string& str,
103    std::string& key,
104    std::string& value,
105    char delimiter = '=');
106
107
108 void split_string(
109    const std::string& str,
110    std::list< std::string >& result,
111    const std::string& delimiter= "\n",
112    bool omit_empty= false,
113    const std::string& trim_list= std::string()
114 );
115
116 std::list< std::string > split_string(
117    const std::string& str,
118    const std::string& delimiter = "\n",
119    bool omit_empty= false,
120    const std::string& trim_list= std::string()
121 );
122
123
124 std::string join_string(
125    const std::list< std::string >& parts,
126    const std::string& delimiter = "\n"
127 );
128
129
130 /*
131 ** conversions:
132 */
133
134
135 std::string convert_binary_to_hex(const std::string&str, bool upper_case_digits= false);
136
137 std::string convert_hex_to_binary(const std::string& str) throw(std::runtime_error);
138
139
140
141 /*
142 ** "type conversions":
143 */
144
145
146 /**
147  * convert a datatype @a T to a string via string stream.
148  *
149  * @param s the string which should be converted to @a T.
150  * @return the value of type T.
151  */
152 template<
153 class T
154 >
155 T string_to(const std::string& s)
156 {
157    std::istringstream istr(s);
158    T result;
159    istr >> result;
160    return result;
161 } // eo string_to(const std::string&)
162
163
164 /**
165  * convert a datatype @a T to a string via string stream.
166  *
167  * @param s the string which should be converted to @a T.
168  * @param result the resulting value of type @a T.
169  * @return @a true iff the internal string stream was EOF after the conversion.
170  *
171  * @attention: does not return if the conversion was successful. So check for empty strings before.
172  */
173 template<
174 class T
175 >
176 bool string_to(const std::string& s, T& result)
177 {
178    std::istringstream istr(s);
179    istr >> result;
180    return istr.eof();
181 } // eo string_to(const std::string&)
182
183
184 /**
185  * convert a string to another datatype @a T via string stream.
186  *
187  * @param v the value (of type @a T) which should be converted to a string.
188  * @return the resulting string.
189  */
190 template<
191 class T
192 >
193 std::string to_string(const T& v)
194 {
195    std::ostringstream ostr;
196    ostr << v;
197    return ostr.str();
198 } // eo to_string(const T&)
199
200
201 } // eo namespace I2n
202
203
204 #if 0
205 std::string to_lower(const std::string &src);
206 std::string to_upper(const std::string &src);
207 #else
208 // compatibility: import lower/upper funcs from I2n:
209 using I2n::to_lower;
210 using I2n::to_upper;
211 #endif
212
213
214 enum UnitBase {
215     UnitBase1000, // SI decimal, composed by multiples of 1000 (KB, MB, etc.)
216     UnitBase1024 // IEC binary, composed by multiples of 1024 (KiB, MiB, etc. )
217 };
218
219 enum UnitFormat {
220     ShortUnitFormat, // B, KB, MB, ...
221     LongUnitFormat // Byte, KByte, MByte, ...
222 };
223
224 std::string nice_unit_format(
225         const int64_t input,
226         const UnitFormat format = ShortUnitFormat,
227         const UnitBase base = UnitBase1024
228 );
229
230 bool replace_all(std::string &base, const std::string *ist, const std::string *soll);
231 bool replace_all(std::string &base, const char *ist, const char *soll);
232 bool replace_all(std::string &base, const char *ist, const std::string *soll);
233 bool replace_all(std::string &base, const std::string &ist, const char *soll);
234 bool replace_all(std::string &base, const std::string &ist, const std::string &soll);
235
236 std::string iso_to_utf8(const std::string& isostring);
237 std::string utf8_to_iso(const std::string& utf8string);
238 std::string utf7imap_to_utf8(const std::string &utf7imapstring);
239 std::string utf8_to_utf7imap(const std::string &utf8string);
240
241 std::string strip_html_tags(const std::string &input);
242 std::string smart_html_entities(const std::string &input);
243 std::string html_entities(std::string str);
244 std::string html_entities_to_console(std::string str);
245
246 std::string escape(const std::string &s);
247
248 std::string descape(const std::string &s, int startpos, int &endpos);
249 inline std::string descape(const std::string &s)
250 {
251    int endpos;
252    return descape(s,0,endpos);
253 }
254
255 std::string escape_shellarg(const std::string &input);
256
257 #endif