PageRenderTime 38ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/MediaSage/ExternalAPIs/MediaInfo/ZenLib/Source/ZenLib/Ztring.h

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