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 16 byte GUID into Ztring
141  Ztring& From_GUID (const int128u S);
142  /// @brief convert an 16 byte UUID into Ztring
143  Ztring& From_UUID (const int128u S);
144  /// @brief convert an 4 Character Code into Ztring
145  Ztring& From_CC4 (const char *S) {return From_Local(S, 0, 4);};
146  /// @brief convert an 4 Character Code into Ztring
147  Ztring& From_CC4 (const int8u *S) {return From_Local((const char*)S, 0, 4);};
148  /// @brief convert an 4 Character Code into Ztring
149  Ztring& From_CC4 (const int32u S);
150  /// @brief convert an 2 Character Code into Ztring
151  Ztring& From_CC3 (const char *S) {return From_Local(S, 0, 3);};
152  /// @brief convert an 4 Character Code into Ztring
153  Ztring& From_CC3 (const int8u *S) {return From_Local((const char*)S, 0, 3);};
154  /// @brief convert an 4 Character Code into Ztring
155  Ztring& From_CC3 (const int32u S);
156  /// @brief convert an 2 Character Code into Ztring
157  Ztring& From_CC2 (const char *S) {return From_CC2(ZenLib::CC2(S));};
158  /// @brief convert an 2 Character Code into Ztring
159  Ztring& From_CC2 (const int8u *S) {return From_CC2(ZenLib::CC2(S));};
160  /// @brief convert an 2 Character Code into Ztring
161  Ztring& From_CC2 (const int16u S);
162  /// @brief convert an 1 Character Code into Ztring
163  Ztring& From_CC1 (const char *S) {return From_CC1(ZenLib::CC1(S));};
164  /// @brief convert an 1 Character Code into Ztring
165  Ztring& From_CC1 (const int8u *S) {return From_CC1(ZenLib::CC1(S));};
166  /// @brief convert an 1 Character Code into Ztring
167  Ztring& From_CC1 (const int8u S);
168  /// @brief convert number into Ztring
169  Ztring& From_Number (const int8s, int8u Radix=10);
170  /// @brief convert number into Ztring
171  Ztring& From_Number (const int8u, int8u Radix=10);
172  /// @brief convert number into Ztring
173  Ztring& From_Number (const int16s, int8u Radix=10);
174  /// @brief convert number into Ztring
175  Ztring& From_Number (const int16u, int8u Radix=10);
176  /// @brief convert number into Ztring
177  Ztring& From_Number (const int32s, int8u Radix=10);
178  /// @brief convert number into Ztring
179  Ztring& From_Number (const int32u, int8u Radix=10);
180  /// @brief convert number into Ztring
181  Ztring& From_Number (const int64s, int8u Radix=10);
182  /// @brief convert number into Ztring
183  Ztring& From_Number (const int64u, int8u Radix=10);
184  /// @brief convert number into Ztring
185  Ztring& From_Number (const int128u, int8u Radix=10);
186  /// @brief convert number into Ztring
187  Ztring& From_Number (const float32, int8u AfterComma=3, ztring_t Options=Ztring_Nothing);
188  /// @brief convert number into Ztring
189  Ztring& From_Number (const float64, int8u AfterComma=3, ztring_t Options=Ztring_Nothing);
190  /// @brief convert number into Ztring
191  Ztring& From_Number (const float80, int8u AfterComma=3, ztring_t Options=Ztring_Nothing);
192  #ifdef NEED_SIZET
193  /// @brief convert number into Ztring
194  Ztring& From_Number (const size_t, int8u Radix=10);
195  #endif //NEED_SIZET
196  /// @brief convert number (BCD coded) into Ztring
197  Ztring& From_BCD (const int8u);
198  /// @brief convert count of milliseconds into a readable and sortable string
199  Ztring& Duration_From_Milliseconds (const int64s Milliseconds);
200  /// @deprecated replaced by the int64s version
201  Ztring& Duration_From_Milliseconds (const int64u Milliseconds);
202  /// @brief convert count of seconds since 1601 into a readable and sortable string
203  Ztring& Date_From_Milliseconds_1601 (const int64u Milliseconds);
204  /// @brief convert count of seconds since 1601 into a readable and sortable string
205  Ztring& Date_From_Seconds_1601 (const int64u Seconds);
206  /// @brief convert count of seconds since 1970 into a readable and sortable string
207  Ztring& Date_From_Seconds_1904 (const int64u Seconds);
208  /// @brief convert count of seconds since 1970 into a readable and sortable string
209  Ztring& Date_From_Seconds_1970 (const int32u Seconds);
210  /// @brief convert count of seconds since 1970 into a readable and sortable string (in local time)
211  Ztring& Date_From_Seconds_1970_Local (const int32u Seconds);
212  /// @brief convert a free formated string into a readable and sortable string
213  Ztring& Date_From_String (const char* Date, size_type Value_Size=Error);
214  /// @brief convert numbers into a readable and sortable string
215  Ztring& Date_From_Numbers (const int8u Year, const int8u Month, const int8u Day, const int8u Hour, const int8u Minute, const int8u Second);
216 
217  //Conversions - To
218  #ifndef WSTRING_MISSING
219  /// @brief Convert into Unicode chars
220  /// @return the string corresponding \n
221  std::wstring To_Unicode () const;
222  #endif //WSTRING_MISSING
223  /// @brief Convert into char* (UTF-8 encoded)
224  /// @return the string corresponding \n
225  std::string To_UTF8 () const;
226  /// @brief Convert into char* (Local encoded)
227  /// @return the string corresponding \n
228  std::string To_Local () const;
229  /// @brief Convert into 16 byte UUID number
230  /// @return the value corresponding \n
231  /// 0 if there is a problem
232  int128u To_UUID () const;
233  /// @brief Convert into a 4 Character Code
234  /// @return the value corresponding \n
235  /// 0 if there is a problem
236  int32u To_CC4 () const;
237  /// @brief Convert into Int (8 bits)
238  /// @return the value corresponding \n
239  /// 0 if there is a problem
240  int8s To_int8s (int8u Radix=10, ztring_t Options=Ztring_Rounded) const;
241  /// @brief Convert into unsigned Int (8 bits)
242  /// @return the value corresponding
243  /// 0 if there is a problem
244  int8u To_int8u (int8u Radix=10, ztring_t Options=Ztring_Rounded) const;
245  /// @brief Convert into Int (16 bits)
246  /// @return the value corresponding \n
247  /// 0 if there is a problem
248  int16s To_int16s (int8u Radix=10, ztring_t Options=Ztring_Rounded) const;
249  /// @brief Convert into unsigned Int (16 bits)
250  /// @return the value corresponding
251  /// 0 if there is a problem
252  int16u To_int16u (int8u Radix=10, ztring_t Options=Ztring_Rounded) const;
253  /// @brief Convert into Int (32 bits)
254  /// @return the value corresponding \n
255  /// 0 if there is a problem
256  int32s To_int32s (int8u Radix=10, ztring_t Options=Ztring_Rounded) const;
257  /// @brief Convert into unsigned Int (32 bits)
258  /// @return the value corresponding
259  /// 0 if there is a problem
260  int32u To_int32u (int8u Radix=10, ztring_t Options=Ztring_Rounded) const;
261  /// @brief Convert into Int (64 bits)
262  /// @return the value corresponding \n
263  /// 0 if there is a problem
264  int64s To_int64s (int8u Radix=10, ztring_t Options=Ztring_Rounded) const;
265  /// @brief Convert into unsigned Int (64 bits)
266  /// @return the value corresponding \n
267  /// 0 if there is a problem
268  int64u To_int64u (int8u Radix=10, ztring_t Options=Ztring_Rounded) const;
269  /// @brief Convert into unsigned Int (64 bits)
270  /// @warning only hexadecimal and no rounding are currenlty supported \n
271  /// @return the value corresponding \n
272  /// 0 if there is a problem
273  int128u To_int128u (int8u Radix=10, ztring_t Options=Ztring_Rounded) const;
274  /// @brief Convert into float
275  /// @return the value corresponding \n
276  /// 0 if there is a problem
277  float32 To_float32 (ztring_t Options=Ztring_Nothing) const;
278  float64 To_float64 (ztring_t Options=Ztring_Nothing) const;
279  float80 To_float80 (ztring_t Options=Ztring_Nothing) const;
280 
281  //Static versions
282  static Ztring ToZtring_From_Local(const std::string &S) {return Ztring().From_Local(S);};
283  static Ztring ToZtring_From_Local(const char *S) {return Ztring().From_Local(S);};
284  static Ztring ToZtring_From_Local(const char *S, size_type Start, size_type Length) {return Ztring().From_Local(S, Start, Length);};
285  static Ztring ToZtring_From_Local(const char *S, size_type Length) {return Ztring().From_Local(S, Length);};
286  static Ztring ToZtring_From_CC4 (const char *S) {return Ztring().From_CC4(S);};
287  static Ztring ToZtring_From_CC4 (const int8u *S) {return Ztring().From_CC4(S);};
288  static Ztring ToZtring_From_CC4 (const int32u S) {return Ztring().From_CC4(S);};
289  static Ztring ToZtring_From_CC3 (const char *S) {return Ztring().From_CC3(S);};
290  static Ztring ToZtring_From_CC3 (const int8u *S) {return Ztring().From_CC3(S);};
291  static Ztring ToZtring_From_CC3 (const int32u S) {return Ztring().From_CC3(S);};
292  static Ztring ToZtring_From_CC2 (const char *S) {return Ztring().From_CC2(S);};
293  static Ztring ToZtring_From_CC2 (const int8u *S) {return Ztring().From_CC2(S);};
294  static Ztring ToZtring_From_CC2 (const int16u S) {return Ztring().From_CC2(S);};
295  static Ztring ToZtring_From_CC1 (const char *S) {return Ztring().From_CC1(S);};
296  static Ztring ToZtring_From_CC1 (const int8u *S) {return Ztring().From_CC1(S);};
297  static Ztring ToZtring_From_CC1 (const int8u S) {return Ztring().From_CC1(S);};
298  static Ztring ToZtring (const int8s I, int8u Radix=10) {return Ztring().From_Number(I, Radix);};
299  static Ztring ToZtring (const int8u I, int8u Radix=10) {return Ztring().From_Number(I, Radix);};
300  static Ztring ToZtring (const int16s I, int8u Radix=10) {return Ztring().From_Number(I, Radix);};
301  static Ztring ToZtring (const int16u I, int8u Radix=10) {return Ztring().From_Number(I, Radix);};
302  static Ztring ToZtring (const int32s I, int8u Radix=10) {return Ztring().From_Number(I, Radix);};
303  static Ztring ToZtring (const int32u I, int8u Radix=10) {return Ztring().From_Number(I, Radix);};
304  static Ztring ToZtring (const int64s I, int8u Radix=10) {return Ztring().From_Number(I, Radix);};
305  static Ztring ToZtring (const int64u I, int8u Radix=10) {return Ztring().From_Number(I, Radix);};
306  static Ztring ToZtring (const int128u I, int8u Radix=10) {return Ztring().From_Number(I, Radix);};
307  static Ztring ToZtring (const float32 F, int8u AfterComma=3) {return Ztring().From_Number(F, AfterComma);};
308  static Ztring ToZtring (const float64 F, int8u AfterComma=3) {return Ztring().From_Number(F, AfterComma);};
309  static Ztring ToZtring (const float80 F, int8u AfterComma=3) {return Ztring().From_Number(F, AfterComma);};
310  #ifdef NEED_SIZET
311  static Ztring ToZtring (const size_t I, int8u Radix=10) {return Ztring().From_Number(I, Radix);};
312  #endif //NEED_SIZET
313 
314  //Edition
315  /// @brief test if it is a number
316  bool IsNumber() const;
317  /// @brief convert into lowercase
319  /// @brief convert into uppercase
321  /// @brief Remove leading whitespaces from a string
322  Ztring &TrimLeft(Char ToTrim=_T(' '));
323  /// @brief Remove trailing whitespaces from a string
324  Ztring &TrimRight(Char ToTrim=_T(' '));
325  /// @brief Remove leading and trailing whitespaces from a string
326  Ztring &Trim(Char ToTrim=_T(' '));
327  /// @brief Quotes a string
328  Ztring &Quote(Char ToTrim=_T('\"'));
329  /// @brief return a string between two strings
330  /// @param Begin First string
331  /// @param End Second string
332  /// @param Pos Position to begin to scan string
333  /// @param Options Options for searching \n
334  /// Available : Ztring_CaseSensitive
335  /// @return The substring \n
336  /// "" if not found
337  Ztring SubString (const tstring &Begin, const tstring &End, size_type Pos=0, ztring_t Options=Ztring_Nothing) const;
338  /// @brief replace a string by another one
339  /// @param ToFind string to find
340  /// @param ToReplace string wich replace the string found
341  /// @param Pos Position to begin to scan string
342  /// @param Options Options for searching \n
343  /// Available : Ztring_CaseSensitive, Ztring_Recursive
344  /// @return The count of replacements
345  size_type FindAndReplace (const tstring &ToFind, const tstring &ReplaceBy, size_type Pos=0, ztring_t Options=Ztring_Nothing); //Remplace une chaine par une autre
346  /// @brief Count the number of occurencies of a string in the string
347  /// @param ToCount string to count
348  /// @param Options Options for count \n
349  /// Available : Ztring_CaseSensitive
350  /// @return the count
351 
352  //Information
353  size_type Count (const Ztring &ToCount, ztring_t Options=Ztring_Nothing) const;
354  /// @brief compare with another string
355  /// @param ToCompare string to compare with
356  /// @param Options Options for comaparing \n
357  /// Available : Ztring_CaseSensitive
358  /// @return The result of comparasion
359  bool Compare (const Ztring &ToCompare, const Ztring &Comparator=_T("=="), ztring_t Options=Ztring_Nothing) const;
360 };
361 
362 } //NameSpace
363 
364 #endif
365