CmString.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /*
  2. -----------------------------------------------------------------------------
  3. This source file is part of OGRE
  4. (Object-oriented Graphics Rendering Engine)
  5. For the latest info, see http://www.ogre3d.org/
  6. Copyright (c) 2000-2011 Torus Knot Software Ltd
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. -----------------------------------------------------------------------------
  23. */
  24. #ifndef _String_H__
  25. #define _String_H__
  26. #include "CmPrerequisitesUtil.h"
  27. // If we're using the GCC 3.1 C++ Std lib
  28. #if CM_COMPILER == CM_COMPILER_GNUC && CM_COMP_VER >= 310 && !defined(STLPORT)
  29. // For gcc 4.3 see http://gcc.gnu.org/gcc-4.3/changes.html
  30. # if CM_COMP_VER >= 430
  31. # include <tr1/unordered_map>
  32. # else
  33. # include <ext/hash_map>
  34. namespace __gnu_cxx
  35. {
  36. template <> struct hash< CamelotEngine::_StringBase >
  37. {
  38. size_t operator()( const CamelotEngine::_StringBase _stringBase ) const
  39. {
  40. /* This is the PRO-STL way, but it seems to cause problems with VC7.1
  41. and in some other cases (although I can't recreate it)
  42. hash<const char*> H;
  43. return H(_stringBase.c_str());
  44. */
  45. /** This is our custom way */
  46. register size_t ret = 0;
  47. for( CamelotEngine::_StringBase::const_iterator it = _stringBase.begin(); it != _stringBase.end(); ++it )
  48. ret = 5 * ret + *it;
  49. return ret;
  50. }
  51. };
  52. }
  53. # endif
  54. #endif
  55. namespace CamelotEngine {
  56. #if CM_WCHAR_T_STRINGS
  57. typedef std::wstring _StringBase;
  58. #else
  59. typedef std::string _StringBase;
  60. #endif
  61. #if CM_WCHAR_T_STRINGS
  62. typedef std::basic_stringstream<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t> > _StringStreamBase;
  63. #else
  64. typedef std::basic_stringstream<char,std::char_traits<char>,std::allocator<char> > _StringStreamBase;
  65. #endif
  66. typedef _StringBase String;
  67. typedef _StringStreamBase StringStream;
  68. typedef StringStream stringstream;
  69. /** \addtogroup Core
  70. * @{
  71. */
  72. /** \addtogroup General
  73. * @{
  74. */
  75. /** Utility class for manipulating Strings. */
  76. class CM_UTILITY_EXPORT StringUtil
  77. {
  78. public:
  79. typedef StringStream StrStreamType;
  80. /** Removes any whitespace characters, be it standard space or
  81. TABs and so on.
  82. @remarks
  83. The user may specify wether they want to trim only the
  84. beginning or the end of the String ( the default action is
  85. to trim both).
  86. */
  87. static void trim( String& str, bool left = true, bool right = true );
  88. /** Returns a StringVector that contains all the substrings delimited
  89. by the characters in the passed <code>delims</code> argument.
  90. @param
  91. delims A list of delimiter characters to split by
  92. @param
  93. maxSplits The maximum number of splits to perform (0 for unlimited splits). If this
  94. parameters is > 0, the splitting process will stop after this many splits, left to right.
  95. */
  96. static vector<String>::type split( const String& str, const String& delims = "\t\n ", unsigned int maxSplits = 0);
  97. /** Returns a StringVector that contains all the substrings delimited
  98. by the characters in the passed <code>delims</code> argument,
  99. or in the <code>doubleDelims</code> argument, which is used to include (normal)
  100. delimeters in the tokenised string. For example, "strings like this".
  101. @param
  102. delims A list of delimiter characters to split by
  103. @param
  104. delims A list of double delimeters characters to tokenise by
  105. @param
  106. maxSplits The maximum number of splits to perform (0 for unlimited splits). If this
  107. parameters is > 0, the splitting process will stop after this many splits, left to right.
  108. */
  109. static vector<String>::type tokenise( const String& str, const String& delims = "\t\n ", const String& doubleDelims = "\"", unsigned int maxSplits = 0);
  110. /** Lower-cases all the characters in the string.
  111. */
  112. static void toLowerCase( String& str );
  113. /** Upper-cases all the characters in the string.
  114. */
  115. static void toUpperCase( String& str );
  116. /** Returns whether the string begins with the pattern passed in.
  117. @param pattern The pattern to compare with.
  118. @param lowerCase If true, the start of the string will be lower cased before
  119. comparison, pattern should also be in lower case.
  120. */
  121. static bool startsWith(const String& str, const String& pattern, bool lowerCase = true);
  122. /** Returns whether the string ends with the pattern passed in.
  123. @param pattern The pattern to compare with.
  124. @param lowerCase If true, the end of the string will be lower cased before
  125. comparison, pattern should also be in lower case.
  126. */
  127. static bool endsWith(const String& str, const String& pattern, bool lowerCase = true);
  128. /** Method for standardising paths - use forward slashes only, end with slash.
  129. */
  130. static String standardisePath( const String &init);
  131. /** Method for splitting a fully qualified filename into the base name
  132. and path.
  133. @remarks
  134. Path is standardised as in standardisePath
  135. */
  136. static void splitFilename(const String& qualifiedName,
  137. String& outBasename, String& outPath);
  138. /** Method for splitting a fully qualified filename into the base name,
  139. extension and path.
  140. @remarks
  141. Path is standardised as in standardisePath
  142. */
  143. static void splitFullFilename(const CamelotEngine::String& qualifiedName,
  144. CamelotEngine::String& outBasename, CamelotEngine::String& outExtention,
  145. CamelotEngine::String& outPath);
  146. /** Method for splitting a filename into the base name
  147. and extension.
  148. */
  149. static void splitBaseFilename(const CamelotEngine::String& fullName,
  150. CamelotEngine::String& outBasename, CamelotEngine::String& outExtention);
  151. /** Simple pattern-matching routine allowing a wildcard pattern.
  152. @param str String to test
  153. @param pattern Pattern to match against; can include simple '*' wildcards
  154. @param caseSensitive Whether the match is case sensitive or not
  155. */
  156. static bool match(const String& str, const String& pattern, bool caseSensitive = true);
  157. /** replace all instances of a sub-string with a another sub-string.
  158. @param source Source string
  159. @param replaceWhat Sub-string to find and replace
  160. @param replaceWithWhat Sub-string to replace with (the new sub-string)
  161. @returns An updated string with the sub-string replaced
  162. */
  163. static const String replaceAll(const String& source, const String& replaceWhat, const String& replaceWithWhat);
  164. /// Constant blank string, useful for returning by ref where local does not exist
  165. static const String BLANK;
  166. };
  167. #if CM_COMPILER == CM_COMPILER_GNUC && CM_COMP_VER >= 310 && !defined(STLPORT)
  168. # if CM_COMP_VER < 430
  169. typedef ::__gnu_cxx::hash< _StringBase > _StringHash;
  170. # else
  171. typedef ::std::tr1::hash< _StringBase > _StringHash;
  172. # endif
  173. #elif CM_COMPILER == CM_COMPILER_MSVC && CM_COMP_VER >= 1600 && !defined(STLPORT) // VC++ 10.0
  174. typedef ::std::tr1::hash< _StringBase > _StringHash;
  175. #elif !defined( _STLP_HASH_FUN_H )
  176. typedef stdext::hash_compare< _StringBase, std::less< _StringBase > > _StringHash;
  177. #else
  178. typedef std::hash< _StringBase > _StringHash;
  179. #endif
  180. /** @} */
  181. /** @} */
  182. /** Converts a float to a String. */
  183. CM_UTILITY_EXPORT String toString(float val, unsigned short precision = 6,
  184. unsigned short width = 0, char fill = ' ',
  185. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  186. /** Converts a Radian to a String. */
  187. CM_UTILITY_EXPORT String toString(Radian val, unsigned short precision = 6,
  188. unsigned short width = 0, char fill = ' ',
  189. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  190. /** Converts a Degree to a String. */
  191. CM_UTILITY_EXPORT String toString(Degree val, unsigned short precision = 6,
  192. unsigned short width = 0, char fill = ' ',
  193. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  194. /** Converts an int to a String. */
  195. CM_UTILITY_EXPORT String toString(int val, unsigned short width = 0,
  196. char fill = ' ',
  197. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  198. #if CM_ARCH_TYPE == CM_ARCHITECTURE_64
  199. /** Converts an unsigned int to a String. */
  200. CM_UTILITY_EXPORT String toString(unsigned int val,
  201. unsigned short width = 0, char fill = ' ',
  202. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  203. /** Converts a size_t to a String. */
  204. CM_UTILITY_EXPORT String toString(size_t val,
  205. unsigned short width = 0, char fill = ' ',
  206. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  207. #if CM_COMPILER == CM_COMPILER_MSVC
  208. /** Converts an unsigned long to a String. */
  209. CM_UTILITY_EXPORT String toString(unsigned long val,
  210. unsigned short width = 0, char fill = ' ',
  211. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  212. #endif
  213. #else
  214. /** Converts a size_t to a String. */
  215. CM_UTILITY_EXPORT String toString(size_t val,
  216. unsigned short width = 0, char fill = ' ',
  217. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  218. /** Converts an unsigned long to a String. */
  219. CM_UTILITY_EXPORT String toString(unsigned long val,
  220. unsigned short width = 0, char fill = ' ',
  221. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  222. /** Converts a long long to a String. */
  223. CM_UTILITY_EXPORT String toString(unsigned long long int val,
  224. unsigned short width = 0, char fill = ' ',
  225. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  226. #endif
  227. /** Converts a long to a String. */
  228. CM_UTILITY_EXPORT String toString(long val,
  229. unsigned short width = 0, char fill = ' ',
  230. std::ios::fmtflags flags = std::ios::fmtflags(0) );
  231. /** Global conversion methods **/
  232. /** Converts a boolean to a String.
  233. @param yesNo If set to true, result is 'yes' or 'no' instead of 'true' or 'false'
  234. */
  235. CM_UTILITY_EXPORT String toString(bool val, bool yesNo = false);
  236. /** Converts a Vector2 to a String.
  237. @remarks
  238. Format is "x y" (i.e. 2x float values, space delimited)
  239. */
  240. CM_UTILITY_EXPORT String toString(const Vector2& val);
  241. /** Converts a Vector3 to a String.
  242. @remarks
  243. Format is "x y z" (i.e. 3x float values, space delimited)
  244. */
  245. CM_UTILITY_EXPORT String toString(const Vector3& val);
  246. /** Converts a Vector4 to a String.
  247. @remarks
  248. Format is "x y z w" (i.e. 4x float values, space delimited)
  249. */
  250. CM_UTILITY_EXPORT String toString(const Vector4& val);
  251. /** Converts a Matrix3 to a String.
  252. @remarks
  253. Format is "00 01 02 10 11 12 20 21 22" where '01' means row 0 column 1 etc.
  254. */
  255. CM_UTILITY_EXPORT String toString(const Matrix3& val);
  256. /** Converts a Matrix4 to a String.
  257. @remarks
  258. Format is "00 01 02 03 10 11 12 13 20 21 22 23 30 31 32 33" where
  259. '01' means row 0 column 1 etc.
  260. */
  261. CM_UTILITY_EXPORT String toString(const Matrix4& val);
  262. /** Converts a Quaternion to a String.
  263. @remarks
  264. Format is "w x y z" (i.e. 4x float values, space delimited)
  265. */
  266. CM_UTILITY_EXPORT String toString(const Quaternion& val);
  267. /** Converts a ColourValue to a String.
  268. @remarks
  269. Format is "r g b a" (i.e. 4x float values, space delimited).
  270. */
  271. CM_UTILITY_EXPORT String toString(const Color& val);
  272. /** Converts a StringVector to a string.
  273. @remarks
  274. Strings must not contain spaces since space is used as a delimiter in
  275. the output.
  276. */
  277. CM_UTILITY_EXPORT String toString(const std::vector<CamelotEngine::String>& val);
  278. /** Converts a String to a float.
  279. @returns
  280. 0.0 if the value could not be parsed, otherwise the float version of the String.
  281. */
  282. CM_UTILITY_EXPORT float parseReal(const String& val, float defaultValue = 0);
  283. /** Converts a String to a whole number.
  284. @returns
  285. 0.0 if the value could not be parsed, otherwise the numeric version of the String.
  286. */
  287. CM_UTILITY_EXPORT int parseInt(const String& val, int defaultValue = 0);
  288. /** Converts a String to a whole number.
  289. @returns
  290. 0.0 if the value could not be parsed, otherwise the numeric version of the String.
  291. */
  292. CM_UTILITY_EXPORT unsigned int parseUnsignedInt(const String& val, unsigned int defaultValue = 0);
  293. /** Converts a String to a whole number.
  294. @returns
  295. 0.0 if the value could not be parsed, otherwise the numeric version of the String.
  296. */
  297. CM_UTILITY_EXPORT long parseLong(const String& val, long defaultValue = 0);
  298. /** Converts a String to a whole number.
  299. @returns
  300. 0.0 if the value could not be parsed, otherwise the numeric version of the String.
  301. */
  302. CM_UTILITY_EXPORT unsigned long parseUnsignedLong(const String& val, unsigned long defaultValue = 0);
  303. /** Converts a String to a boolean.
  304. @remarks
  305. Returns true if case-insensitive match of the start of the string
  306. matches "true", "yes" or "1", false otherwise.
  307. */
  308. CM_UTILITY_EXPORT bool parseBool(const String& val, bool defaultValue = 0);
  309. /** Pareses a StringVector from a string.
  310. @remarks
  311. Strings must not contain spaces since space is used as a delimiter in
  312. the output.
  313. */
  314. CM_UTILITY_EXPORT std::vector<CamelotEngine::String> parseStringVector(const String& val);
  315. /** Checks the String is a valid number value. */
  316. CM_UTILITY_EXPORT bool isNumber(const String& val);
  317. /** @} */
  318. } // namespace CamelotEngine
  319. #endif // _String_H__