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