ZenLib
Ztring.h
Go to the documentation of this file.
1 /* Copyright (c) MediaArea.net SARL. All Rights Reserved.
2  *
3  * Use of this source code is governed by a zlib-style license that can
4  * be found in the License.txt file in the root of the source tree.
5  */
6 
7 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
8 //
9 // More methods for std::(w)string
10 //
11 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
12 
13 //---------------------------------------------------------------------------
14 #ifndef ZenLib_ZtringH
15 #define ZenLib_ZtringH
16 //---------------------------------------------------------------------------
17 
18 //---------------------------------------------------------------------------
19 #include "ZenLib/Utils.h"
20 #include <string>
21 #include <sstream>
22 //---------------------------------------------------------------------------
23 
24 namespace ZenLib
25 {
26 
27 //---------------------------------------------------------------------------
28 typedef std::basic_string<Char, std::char_traits<Char>, std::allocator<Char> > tstring;
29 //---------------------------------------------------------------------------
30 
31 //---------------------------------------------------------------------------
32 /// @brief Options for Ztring methods
34 {
36  Ztring_Rounded = 1, ///< if >.5, upper, else lower
37  Ztring_CaseSensitive = 2, ///< Case sensitive ("A" and "a" are different)
38  Ztring_AddLastItem = 4, ///< if Begin is found and End is not found, return between Begin and end of string
39  Ztring_Recursive = 8, ///< Do all strings
40  Ztring_NoZero =16 ///> Doesn't keep Zero in the float number
41 };
42 
43 //---------------------------------------------------------------------------
44 
45 //***************************************************************************
46 /// @brief String manipulation (based on std::(w)string)
47 //***************************************************************************
48 
49 class Ztring : public tstring //for details about undocumented methods see http://www.sgi.com/tech/stl/basic_string.html
50 {
51 public :
52  //Constructor/destructor
53  Ztring () : tstring(){};
54  Ztring (const tstring& str) : tstring(str){};
55  Ztring (const tstring& str, size_type pos, size_type n=npos) : tstring(str, pos, n){};
56  Ztring (const Char* s, size_type n) : tstring(s, n){};
57  Ztring (const Char* s) : tstring(s){};
58  Ztring (size_type n, Char c) : tstring(n, c){};
59  #ifdef UNICODE
60  Ztring (const char* S) : tstring(){From_UTF8(S);};
61  Ztring (const char* S, size_type n) : tstring(){From_UTF8(S, 0, n);};
62  #endif //UNICODE
63 
64  //Operators
65  ///Same as [], but resize the string if Pos doesn't exist yet
66  Char &operator () (size_type Pos);
67 
68  //Assign
69  bool Assign_FromFile (const Ztring &FileName);
70 
71  //Conversions - From
72  #ifndef WSTRING_MISSING
73  /// @brief convert an Unicode encoded string into Ztring
74  Ztring& From_Unicode (const std::wstring &S) {return From_Unicode(S.c_str());};
75  #endif //WSTRING_MISSING
76  /// @brief convert an Unicode encoded string into Ztring
77  Ztring& From_Unicode (const wchar_t *S);
78  /// @brief convert an Unicode encoded string into Ztring
79  Ztring& From_Unicode (const wchar_t *S, size_type Start, size_type Length);
80  /// @brief convert an Unicode encoded string into Ztring
81  Ztring& From_Unicode (const wchar_t *S, size_type Length) {return From_Unicode(S, 0, Length);};
82  /// @brief convert an UTF-8 encoded string into Ztring
83  Ztring& From_UTF8 (const std::string &S) {return From_UTF8(S.c_str());};
84  /// @brief convert an UTF-8 encoded string into Ztring
85  Ztring& From_UTF8 (const char *S);
86  /// @brief convert an UTF-8 encoded string into Ztring
87  Ztring& From_UTF8 (const char *S, size_type Start, size_type Length);
88  /// @brief convert an UTF-8 encoded string into Ztring
89  Ztring& From_UTF8 (const char *S, size_type Length) {return From_UTF8(S, 0, Length);};
90  /// @brief convert an UTF-16 encoded string into Ztring
91  Ztring& From_UTF16 (const char *S);
92  /// @brief convert an UTF-16 encoded string into Ztring
93  Ztring& From_UTF16 (const char *S, size_type Start, size_type Length);
94  /// @brief convert an UTF-16 encoded string into Ztring
95  Ztring& From_UTF16 (const char *S, size_type Length) {return From_UTF16(S, 0, Length);};
96  /// @brief convert an UTF-16BE encoded string into Ztring
97  Ztring& From_UTF16BE (const char *S);
98  /// @brief convert an UTF-16BE encoded string into Ztring
99  Ztring& From_UTF16BE (const char *S, size_type Start, size_type Length);
100  /// @brief convert an UTF-16BE encoded string into Ztring
101  Ztring& From_UTF16BE (const char *S, size_type Length) {return From_UTF16BE(S, 0, Length);};
102  /// @brief convert an UTF-16LE encoded string into Ztring
103  Ztring& From_UTF16LE (const char *S);
104  /// @brief convert an UTF-16LE encoded string into Ztring
105  Ztring& From_UTF16LE (const char *S, size_type Start, size_type Length);
106  /// @brief convert an UTF-16LE encoded string into Ztring
107  Ztring& From_UTF16LE (const char *S, size_type Length) {return From_UTF16LE(S, 0, Length);};
108  /// @brief convert an Locael encoded string into Ztring
109  Ztring& From_Local (const std::string &S) {return From_Local(S.c_str());};
110  /// @brief convert an Local encoded string into Ztring
111  Ztring& From_Local (const char *S);
112  /// @brief convert an Local encoded string into Ztring
113  Ztring& From_Local (const char *S, size_type Start, size_type Length);
114  /// @brief convert an Local encoded string into Ztring
115  Ztring& From_Local (const char *S, size_type Length) {return From_Local(S, 0, Length);};
116 
117  /// @brief convert an ISO-8859-1 encoded string into Ztring
118  Ztring& From_ISO_8859_1 (const char *S);
119  /// @brief convert an ISO-8859-1 encoded string into Ztring
120  Ztring& From_ISO_8859_1 (const char *S, size_type Start, size_type Length);
121  /// @brief convert an ISO-8859-1 encoded string into Ztring
122  Ztring& From_ISO_8859_1 (const char *S, size_type Length) {return From_ISO_8859_1(S, 0, Length);};
123 
124  /// @brief convert an ISO-8859-2 encoded string into Ztring
125  Ztring& From_ISO_8859_2 (const char *S);
126  /// @brief convert an ISO-8859-1 encoded string into Ztring
127  Ztring& From_ISO_8859_2 (const char *S, size_type Start, size_type Length);
128  /// @brief convert an ISO-8859-1 encoded string into Ztring
129  Ztring& From_ISO_8859_2 (const char *S, size_type Length) {return From_ISO_8859_2(S, 0, Length);};
130 
131  /// @brief convert an 16 byte GUID into Ztring
132  Ztring& From_GUID (const int128u S);
133  /// @brief convert an 16 byte UUID into Ztring
134  Ztring& From_UUID (const int128u S);
135  /// @brief convert an 4 Character Code into Ztring
136  Ztring& From_CC4 (const char *S) {return From_Local(S, 0, 4);};
137  /// @brief convert an 4 Character Code into Ztring
138  Ztring& From_CC4 (const int8u *S) {return From_Local((const char*)S, 0, 4);};
139  /// @brief convert an 4 Character Code into Ztring
140  Ztring& From_CC4 (const int32u S);
141  /// @brief convert an 2 Character Code into Ztring
142  Ztring& From_CC3 (const char *S) {return From_Local(S, 0, 3);};
143  /// @brief convert an 4 Character Code into Ztring
144  Ztring& From_CC3 (const int8u *S) {return From_Local((const char*)S, 0, 3);};
145  /// @brief convert an 4 Character Code into Ztring
146  Ztring& From_CC3 (const int32u S);
147  /// @brief convert an 2 Character Code into Ztring
148  Ztring& From_CC2 (const char *S) {return From_CC2(ZenLib::CC2(S));};
149  /// @brief convert an 2 Character Code into Ztring
150  Ztring& From_CC2 (const int8u *S) {return From_CC2(ZenLib::CC2(S));};
151  /// @brief convert an 2 Character Code into Ztring
152  Ztring& From_CC2 (const int16u S);
153  /// @brief convert an 1 Character Code into Ztring
154  Ztring& From_CC1 (const char *S) {return From_CC1(ZenLib::CC1(S));};
155  /// @brief convert an 1 Character Code into Ztring
156  Ztring& From_CC1 (const int8u *S) {return From_CC1(ZenLib::CC1(S));};
157  /// @brief convert an 1 Character Code into Ztring
158  Ztring& From_CC1 (const int8u S);
159  /// @brief convert number into Ztring
160  Ztring& From_Number (const int8s, int8u Radix=10);
161  /// @brief convert number into Ztring
162  Ztring& From_Number (const int8u, int8u Radix=10);
163  /// @brief convert number into Ztring
164  Ztring& From_Number (const int16s, int8u Radix=10);
165  /// @brief convert number into Ztring
166  Ztring& From_Number (const int16u, int8u Radix=10);
167  /// @brief convert number into Ztring
168  Ztring& From_Number (const int32s, int8u Radix=10);
169  /// @brief convert number into Ztring
170  Ztring& From_Number (const int32u, int8u Radix=10);
171  /// @brief convert number into Ztring
172  Ztring& From_Number (const int64s, int8u Radix=10);
173  /// @brief convert number into Ztring
174  Ztring& From_Number (const int64u, int8u Radix=10);
175  /// @brief convert number into Ztring
176  Ztring& From_Number (const int128u, int8u Radix=10);
177  /// @brief convert number into Ztring
178  Ztring& From_Number (const float32, int8u AfterComma=3, ztring_t Options=Ztring_Nothing);
179  /// @brief convert number into Ztring
180  Ztring& From_Number (const float64, int8u AfterComma=3, ztring_t Options=Ztring_Nothing);
181  /// @brief convert number into Ztring
182  Ztring& From_Number (const float80, int8u AfterComma=3, ztring_t Options=Ztring_Nothing);
183  #ifdef SIZE_T_IS_LONG
184  /// @brief convert number into Ztring
185  Ztring& From_Number (const size_t, int8u Radix=10);
186  #endif //SIZE_T_IS_LONG
187  /// @brief convert number (BCD coded) into Ztring
188  Ztring& From_BCD (const int8u);
189  /// @brief convert count of milliseconds into a readable and sortable string
190  Ztring& Duration_From_Milliseconds (const int64s Milliseconds);
191  /// @deprecated replaced by the int64s version
192  Ztring& Duration_From_Milliseconds (const int64u Milliseconds);
193  /// @brief convert count of seconds since 1601 into a readable and sortable string
194  Ztring& Date_From_Milliseconds_1601 (const int64u Milliseconds);
195  /// @brief convert count of seconds since 1601 into a readable and sortable string
196  Ztring& Date_From_Seconds_1601 (const int64u Seconds);
197  /// @brief convert count of seconds since 1970 into a readable and sortable string
198  Ztring& Date_From_Seconds_1904 (const int64u Seconds);
199  /// @brief convert count of seconds since 1970 into a readable and sortable string
200  Ztring& Date_From_Seconds_1970 (const int32u Seconds);
201  /// @brief convert count of seconds since 1970 into a readable and sortable string (in local time)
202  Ztring& Date_From_Seconds_1970_Local (const int32u Seconds);
203  /// @brief convert a free formated string into a readable and sortable string
204  Ztring& Date_From_String (const char* Date, size_type Value_Size=Error);
205  /// @brief convert numbers into a readable and sortable string
206  Ztring& Date_From_Numbers (const int8u Year, const int8u Month, const int8u Day, const int8u Hour, const int8u Minute, const int8u Second);
207 
208  //Conversions - To
209  #ifndef WSTRING_MISSING
210  /// @brief Convert into Unicode chars
211  /// @return the string corresponding \n
212  std::wstring To_Unicode () const;
213  #endif //WSTRING_MISSING
214  /// @brief Convert into char* (UTF-8 encoded)
215  /// @return the string corresponding \n
216  std::string To_UTF8 () const;
217  /// @brief Convert into char* (Local encoded)
218  /// @return the string corresponding \n
219  std::string To_Local () const;
220  /// @brief Convert into 16 byte UUID number
221  /// @return the value corresponding \n
222  /// 0 if there is a problem
223  int128u To_UUID () const;
224  /// @brief Convert into a 4 Character Code
225  /// @return the value corresponding \n
226  /// 0 if there is a problem
227  int32u To_CC4 () const;
228  /// @brief Convert into Int (8 bits)
229  /// @return the value corresponding \n
230  /// 0 if there is a problem
231  int8s To_int8s (int8u Radix=10, ztring_t Options=Ztring_Rounded) const;
232  /// @brief Convert into unsigned Int (8 bits)
233  /// @return the value corresponding
234  /// 0 if there is a problem
235  int8u To_int8u (int8u Radix=10, ztring_t Options=Ztring_Rounded) const;
236  /// @brief Convert into Int (16 bits)
237  /// @return the value corresponding \n
238  /// 0 if there is a problem
239  int16s To_int16s (int8u Radix=10, ztring_t Options=Ztring_Rounded) const;
240  /// @brief Convert into unsigned Int (16 bits)
241  /// @return the value corresponding
242  /// 0 if there is a problem
243  int16u To_int16u (int8u Radix=10, ztring_t Options=Ztring_Rounded) const;
244  /// @brief Convert into Int (32 bits)
245  /// @return the value corresponding \n
246  /// 0 if there is a problem
247  int32s To_int32s (int8u Radix=10, ztring_t Options=Ztring_Rounded) const;
248  /// @brief Convert into unsigned Int (32 bits)
249  /// @return the value corresponding
250  /// 0 if there is a problem
251  int32u To_int32u (int8u Radix=10, ztring_t Options=Ztring_Rounded) const;
252  /// @brief Convert into Int (64 bits)
253  /// @return the value corresponding \n
254  /// 0 if there is a problem
255  int64s To_int64s (int8u Radix=10, ztring_t Options=Ztring_Rounded) const;
256  /// @brief Convert into unsigned Int (64 bits)
257  /// @return the value corresponding \n
258  /// 0 if there is a problem
259  int64u To_int64u (int8u Radix=10, ztring_t Options=Ztring_Rounded) const;
260  /// @brief Convert into unsigned Int (64 bits)
261  /// @warning only hexadecimal and no rounding are currenlty supported \n
262  /// @return the value corresponding \n
263  /// 0 if there is a problem
264  int128u To_int128u (int8u Radix=10, ztring_t Options=Ztring_Rounded) const;
265  /// @brief Convert into float
266  /// @return the value corresponding \n
267  /// 0 if there is a problem
268  float32 To_float32 (ztring_t Options=Ztring_Nothing) const;
269  float64 To_float64 (ztring_t Options=Ztring_Nothing) const;
270  float80 To_float80 (ztring_t Options=Ztring_Nothing) const;
271 
272  //Static versions
273  static Ztring ToZtring_From_Local(const std::string &S) {return Ztring().From_Local(S);};
274  static Ztring ToZtring_From_Local(const char *S) {return Ztring().From_Local(S);};
275  static Ztring ToZtring_From_Local(const char *S, size_type Start, size_type Length) {return Ztring().From_Local(S, Start, Length);};
276  static Ztring ToZtring_From_Local(const char *S, size_type Length) {return Ztring().From_Local(S, Length);};
277  static Ztring ToZtring_From_CC4 (const char *S) {return Ztring().From_CC4(S);};
278  static Ztring ToZtring_From_CC4 (const int8u *S) {return Ztring().From_CC4(S);};
279  static Ztring ToZtring_From_CC4 (const int32u S) {return Ztring().From_CC4(S);};
280  static Ztring ToZtring_From_CC3 (const char *S) {return Ztring().From_CC3(S);};
281  static Ztring ToZtring_From_CC3 (const int8u *S) {return Ztring().From_CC3(S);};
282  static Ztring ToZtring_From_CC3 (const int32u S) {return Ztring().From_CC3(S);};
283  static Ztring ToZtring_From_CC2 (const char *S) {return Ztring().From_CC2(S);};
284  static Ztring ToZtring_From_CC2 (const int8u *S) {return Ztring().From_CC2(S);};
285  static Ztring ToZtring_From_CC2 (const int16u S) {return Ztring().From_CC2(S);};
286  static Ztring ToZtring_From_CC1 (const char *S) {return Ztring().From_CC1(S);};
287  static Ztring ToZtring_From_CC1 (const int8u *S) {return Ztring().From_CC1(S);};
288  static Ztring ToZtring_From_CC1 (const int8u S) {return Ztring().From_CC1(S);};
289  static Ztring ToZtring (const int8s I, int8u Radix=10) {return Ztring().From_Number(I, Radix);};
290  static Ztring ToZtring (const int8u I, int8u Radix=10) {return Ztring().From_Number(I, Radix);};
291  static Ztring ToZtring (const int16s I, int8u Radix=10) {return Ztring().From_Number(I, Radix);};
292  static Ztring ToZtring (const int16u I, int8u Radix=10) {return Ztring().From_Number(I, Radix);};
293  static Ztring ToZtring (const int32s I, int8u Radix=10) {return Ztring().From_Number(I, Radix);};
294  static Ztring ToZtring (const int32u I, int8u Radix=10) {return Ztring().From_Number(I, Radix);};
295  static Ztring ToZtring (const int64s I, int8u Radix=10) {return Ztring().From_Number(I, Radix);};
296  static Ztring ToZtring (const int64u I, int8u Radix=10) {return Ztring().From_Number(I, Radix);};
297  static Ztring ToZtring (const int128u I, int8u Radix=10) {return Ztring().From_Number(I, Radix);};
298  static Ztring ToZtring (const float32 F, int8u AfterComma=3) {return Ztring().From_Number(F, AfterComma);};
299  static Ztring ToZtring (const float64 F, int8u AfterComma=3) {return Ztring().From_Number(F, AfterComma);};
300  static Ztring ToZtring (const float80 F, int8u AfterComma=3) {return Ztring().From_Number(F, AfterComma);};
301  #ifdef SIZE_T_IS_LONG
302  static Ztring ToZtring (const size_t I, int8u Radix=10) {return Ztring().From_Number(I, Radix);};
303  #endif //SIZE_T_IS_LONG
304 
305  //Edition
306  /// @brief test if it is a number
307  bool IsNumber() const;
308  /// @brief convert into lowercase
310  /// @brief convert into uppercase
312  /// @brief Remove leading whitespaces from a string
313  Ztring &TrimLeft(Char ToTrim=__T(' '));
314  /// @brief Remove trailing whitespaces from a string
315  Ztring &TrimRight(Char ToTrim=__T(' '));
316  /// @brief Remove leading and trailing whitespaces from a string
317  Ztring &Trim(Char ToTrim=__T(' '));
318  /// @brief Quotes a string
319  Ztring &Quote(Char ToTrim=__T('\"'));
320  /// @brief return a string between two strings
321  /// @param Begin First string
322  /// @param End Second string
323  /// @param Pos Position to begin to scan string
324  /// @param Options Options for searching \n
325  /// Available : Ztring_CaseSensitive
326  /// @return The substring \n
327  /// "" if not found
328  Ztring SubString (const tstring &Begin, const tstring &End, size_type Pos=0, ztring_t Options=Ztring_Nothing) const;
329  /// @brief replace a string by another one
330  /// @param ToFind string to find
331  /// @param ToReplace string wich replace the string found
332  /// @param Pos Position to begin to scan string
333  /// @param Options Options for searching \n
334  /// Available : Ztring_CaseSensitive, Ztring_Recursive
335  /// @return The count of replacements
336  size_type FindAndReplace (const tstring &ToFind, const tstring &ReplaceBy, size_type Pos=0, ztring_t Options=Ztring_Nothing); //Remplace une chaine par une autre
337  /// @brief Count the number of occurencies of a string in the string
338  /// @param ToCount string to count
339  /// @param Options Options for count \n
340  /// Available : Ztring_CaseSensitive
341  /// @return the count
342 
343  //Information
344  size_type Count (const Ztring &ToCount, ztring_t Options=Ztring_Nothing) const;
345  /// @brief compare with another string
346  /// @param ToCompare string to compare with
347  /// @param Options Options for comaparing \n
348  /// Available : Ztring_CaseSensitive
349  /// @return The result of comparasion
350  bool Compare (const Ztring &ToCompare, const Ztring &Comparator=__T("=="), ztring_t Options=Ztring_Nothing) const;
351 };
352 
353 } //NameSpace
354 
355 #endif