~vcs-imports/wengophone/trunk

« back to all changes in this revision

Viewing changes to libs/util/util/String.h

  • Committer: tanguy_k
  • Date: 2006-11-15 14:12:35 UTC
  • Revision ID: vcs-imports@canonical.com-20061115141235-6efc6e38eaa40ca0
* (compilation fix) OWBuild: rename portaudio to PORTAUDIO
* Libutil has only one include path now -> faster compilation
* Remove old directories
* (compilation fix) phapi: ph_msession_stopped() was a macro inside phmedia.h, now it is a standard function. Don't know why but it was not compiling under Windows using CMake

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * WengoPhone, a voice over Internet phone
 
3
 * Copyright (C) 2004-2006  Wengo
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or modify
 
6
 * it under the terms of the GNU General Public License as published by
 
7
 * the Free Software Foundation; either version 2 of the License, or
 
8
 * (at your option) any later version.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program; if not, write to the Free Software
 
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
18
 */
 
19
 
 
20
#ifndef OWSTRING_H
 
21
#define OWSTRING_H
 
22
 
 
23
#include <util/owutildll.h>
 
24
 
 
25
#include <string>
 
26
 
 
27
class StringList;
 
28
 
 
29
/**
 
30
 * std::string wrapper/helper.
 
31
 *
 
32
 * Inspired from the class QString from the Qt library.
 
33
 *
 
34
 * @see QString
 
35
 * @see std::string
 
36
 * @see java.lang.String
 
37
 * @author Tanguy Krotoff
 
38
 */
 
39
class String : public std::string {
 
40
public:
 
41
 
 
42
        /**
 
43
         * Null string.
 
44
         *
 
45
         * Rather than to code something like this:
 
46
         * if (myString == "")
 
47
         * it's better to write:
 
48
         * if (myString.empty())
 
49
         * return "" -> return String::null
 
50
         */
 
51
        OWUTIL_API static const char * null;
 
52
 
 
53
        /**
 
54
         * End of line character.
 
55
         *
 
56
         * Equals \n (LF).
 
57
         */
 
58
        OWUTIL_API static const std::string EOL;
 
59
 
 
60
        /**
 
61
         * Windows end of line character.
 
62
         *
 
63
         * Equals \r\n (CRLF).
 
64
         */
 
65
        OWUTIL_API static const std::string WIN_EOL;
 
66
 
 
67
        /**
 
68
         * Unix end of line character.
 
69
         *
 
70
         * Equals \n (LF).
 
71
         */
 
72
        OWUTIL_API static const std::string UNIX_EOL;
 
73
 
 
74
        /**
 
75
         * System end of line character.
 
76
         *
 
77
         * Equals \r\n or \n depending on the system (Windows, Unix...)
 
78
         */
 
79
        OWUTIL_API static const std::string SYSTEM_EOL;
 
80
 
 
81
        OWUTIL_API String() : std::string() { }
 
82
 
 
83
        OWUTIL_API String(const char * str) : std::string(str) { }
 
84
 
 
85
        OWUTIL_API String(const std::string & str) : std::string(str) { }
 
86
 
 
87
        /**
 
88
         * Converts this String to std::string.
 
89
         *
 
90
         * @return the converted String to std::string
 
91
         */
 
92
        OWUTIL_API operator const std::string&() {
 
93
                return *this;
 
94
        }
 
95
 
 
96
        /**
 
97
         * Converts this string to an int.
 
98
         *
 
99
         * @return the string converted to an int or 0 if failed to convert
 
100
         */
 
101
        OWUTIL_API int toInteger() const;
 
102
 
 
103
        /**
 
104
         * Converts this string to a boolean.
 
105
         *
 
106
         * Detects strings: "true", "TRUE", "yes", "YES", "1"
 
107
         *                  "false", "FALSE", "no", "NO", "0"
 
108
         * Be very carefull when using this method, maybe it should throw an Exception.
 
109
         *
 
110
         * @return the string converted to a boolean (return false if failed to convert)
 
111
         */
 
112
        OWUTIL_API bool toBoolean() const;
 
113
 
 
114
        /**
 
115
         * Converts all of the characters in this string to lower case.
 
116
         *
 
117
         * Example:
 
118
         * <pre>
 
119
         * String myString("WengO");
 
120
         * str = myString.toLowerCase();        //str == "wengo"
 
121
         * </pre>
 
122
         *
 
123
         * @return the string converted to lowercase
 
124
         */
 
125
        OWUTIL_API std::string toLowerCase() const;
 
126
 
 
127
        /**
 
128
         * Converts all of the characters in this string to upper case.
 
129
         *
 
130
         * Example:
 
131
         * <pre>
 
132
         * String myString("WengO");
 
133
         * str = myString.toUpperCase();        //str == "WENGO"
 
134
         * </pre>
 
135
         *
 
136
         * @return the string converted to uppercase
 
137
         */
 
138
        OWUTIL_API std::string toUpperCase() const;
 
139
 
 
140
        /**
 
141
         * @param str the string to test
 
142
         * @return true if String begins with str
 
143
         */
 
144
        OWUTIL_API bool beginsWith(const String & str) const;
 
145
 
 
146
        /**
 
147
         * @param str the string to test
 
148
         * @return true if String ends with str
 
149
         */
 
150
        bool endsWith(const String & str) const;
 
151
 
 
152
        /**
 
153
         * Gets the number of occurences of the string str inside this string.
 
154
         *
 
155
         * @param str string to find
 
156
         * @param caseSensitive the search is case sensitive; otherwise the search is case insensitive
 
157
         * @return true if this list contains the specified element
 
158
         */
 
159
        OWUTIL_API bool contains(const std::string & str, bool caseSensitive = true) const;
 
160
 
 
161
        /**
 
162
         * @see contains()
 
163
         */
 
164
        OWUTIL_API bool contains(char ch, bool caseSensitive = true) const;
 
165
 
 
166
        /**
 
167
         * Replaces every occurence of the string before with the string after.
 
168
         *
 
169
         * @param before occurence to find
 
170
         * @param after the string that will replace the string before
 
171
         * @param caseSensitive the search is case sensitive; otherwise the search is case insensitive
 
172
         */
 
173
        OWUTIL_API void replace(const std::string & before, const std::string & after, bool caseSensitive = true);
 
174
 
 
175
        /**
 
176
         * Appends a string onto the end of this string.
 
177
         *
 
178
         * @param str string to append
 
179
         * @return the new string
 
180
         */
 
181
        OWUTIL_API std::string & append(const std::string & str);
 
182
 
 
183
        /**
 
184
         * Removes every occurrence of str in the string.
 
185
         *
 
186
         * @param str to remove in the string
 
187
         */
 
188
        OWUTIL_API void remove(const std::string & str);
 
189
 
 
190
        /**
 
191
         * Gets a string from a number.
 
192
         *
 
193
         * @param number number to convert into a string
 
194
         * @return number converted to a string
 
195
         */
 
196
        OWUTIL_API static std::string fromNumber(int number);
 
197
 
 
198
        /**
 
199
         * Gets a string from a boolean.
 
200
         *
 
201
         * @param boolean boolean to convert into a string
 
202
         * @return boolean converted to a string
 
203
         */
 
204
        OWUTIL_API static std::string fromBoolean(bool boolean);
 
205
 
 
206
        /**
 
207
         * Gets a string from an unsigned int.
 
208
         *
 
209
         * @param number unsigned int to convert into a string
 
210
         * @return unsigned int converted to a string
 
211
         */
 
212
        OWUTIL_API static std::string fromUnsignedInt(unsigned int number);
 
213
 
 
214
        /**
 
215
         * Gets a string from an double.
 
216
         *
 
217
         * @param number double to convert into a string
 
218
         * @return double converted to a string
 
219
         */
 
220
        OWUTIL_API static std::string fromDouble(double number);
 
221
 
 
222
        /**
 
223
         * Gets a string from a long.
 
224
         *
 
225
         * @param number long to convert into a string
 
226
         * @return long converted to a string
 
227
         */
 
228
        OWUTIL_API static std::string fromLong(long number);
 
229
 
 
230
        /**
 
231
         * Gets a string from a long long.
 
232
         *
 
233
         * @param number long long to convert into a string
 
234
         * @return long long converted to a string
 
235
         */
 
236
        OWUTIL_API static std::string fromLongLong(long long number);
 
237
 
 
238
        /**
 
239
         * Gets a string from a unsigned long long.
 
240
         *
 
241
         * @param number unsigned long long to convert into a string
 
242
         * @return unsigned long long converted to a string
 
243
         */
 
244
        OWUTIL_API static std::string fromUnsignedLongLong(unsigned long long number);
 
245
 
 
246
        /**
 
247
         * URL-encodes a string.
 
248
         *
 
249
         * @param url the string to encode
 
250
         * @return a string with all non-alphanumeric characters replaced by their
 
251
         *         URL-encoded equivalent.
 
252
         */
 
253
        OWUTIL_API static std::string encodeUrl(const std::string & url);
 
254
 
 
255
        /**
 
256
         * URL-decodes a string.
 
257
         *
 
258
         * @param url the URL-encoded string to decode
 
259
         * @return a string with all URL-encoded sequences replaced by their
 
260
         *         ASCII equivalent
 
261
         */
 
262
        OWUTIL_API static std::string decodeUrl(const std::string & url);
 
263
 
 
264
        /**
 
265
         * Tokenizes the string with a delimiter of your choice.
 
266
         *
 
267
         * Example:
 
268
         * <pre>
 
269
         * String str("four");
 
270
         * StringList tokens = str.split("");   //tokens = "four"
 
271
         * String str("four roses");
 
272
         * StringList tokens = str.split(" ");  //tokens = "four", "roses"
 
273
         * </pre>
 
274
         *
 
275
         * @param separator string delimiter
 
276
         * @return tokens, strings created by splitting the input string
 
277
         */
 
278
        OWUTIL_API StringList split(const std::string & separator) const;
 
279
 
 
280
        /**
 
281
         * Removes spaces at the beginning and the end of a string.
 
282
         *
 
283
         * @return a cleaned string
 
284
         */
 
285
        OWUTIL_API std::string trim();
 
286
};
 
287
 
 
288
#endif  //OWSTRING_H