str.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /* ScummVM Tools
  2. *
  3. * ScummVM Tools is the legal property of its developers, whose
  4. * names are too numerous to list here. Please refer to the
  5. * COPYRIGHT file distributed with this source distribution.
  6. *
  7. * Additionally this file is based on the ScummVM source code.
  8. * Copyright information for the ScummVM source code is
  9. * available in the COPYRIGHT file of the ScummVM source
  10. * distribution.
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * as published by the Free Software Foundation; either version 2
  15. * of the License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  25. */
  26. #ifndef COMMON_STRING_H
  27. #define COMMON_STRING_H
  28. #include "common/scummsys.h"
  29. #include <string>
  30. namespace Common {
  31. /**
  32. * Simple string class for ScummVM. Provides automatic storage managment,
  33. * and overloads several operators in a 'natural' fashion, mimicking
  34. * the std::string class. Even provides simple iterators.
  35. *
  36. */
  37. class String {
  38. protected:
  39. /**
  40. * Pointer to the actual string storage. Either points to _storage,
  41. * or to a block allocated on the heap via malloc.
  42. */
  43. std::string _str;
  44. public:
  45. /** Construct a new empty string. */
  46. String() = default;
  47. /** Construct a new string from the given NULL-terminated C string. */
  48. String(const char *str);
  49. /** Construct a new string containing exactly len characters read from address str. */
  50. String(const char *str, uint32 len);
  51. /** Construct a new string containing the characters between beginP (including) and endP (excluding). */
  52. String(const char *beginP, const char *endP);
  53. /** Construct a copy of the given string. */
  54. String(const String &str);
  55. /** Construct a string consisting of the given character. */
  56. explicit String(char c);
  57. ~String();
  58. String &operator =(const char *str);
  59. String &operator =(const String &str);
  60. String &operator =(char c);
  61. String &operator +=(const char *str);
  62. String &operator +=(const String &str);
  63. String &operator +=(char c);
  64. bool operator ==(const String &x) const;
  65. bool operator ==(const char *x) const;
  66. bool operator !=(const String &x) const;
  67. bool operator !=(const char *x) const;
  68. bool operator <(const String &x) const;
  69. bool operator <=(const String &x) const;
  70. bool operator >(const String &x) const;
  71. bool operator >=(const String &x) const;
  72. bool equals(const String &x) const;
  73. bool equalsIgnoreCase(const String &x) const;
  74. int compareTo(const String &x) const; // strcmp clone
  75. int compareToIgnoreCase(const String &x) const; // stricmp clone
  76. bool equals(const char *x) const;
  77. bool equalsIgnoreCase(const char *x) const;
  78. int compareTo(const char *x) const; // strcmp clone
  79. int compareToIgnoreCase(const char *x) const; // stricmp clone
  80. bool hasSuffix(const String &x) const;
  81. bool hasSuffix(const char *x) const;
  82. bool hasPrefix(const String &x) const;
  83. bool hasPrefix(const char *x) const;
  84. bool contains(const String &x) const;
  85. bool contains(const char *x) const;
  86. bool contains(char x) const;
  87. /**
  88. * Simple DOS-style pattern matching function (understands * and ? like used in DOS).
  89. * Taken from exult/files/listfiles.cc
  90. *
  91. * Token meaning:
  92. * "*": any character, any amount of times.
  93. * "?": any character, only once.
  94. *
  95. * Example strings/patterns:
  96. * String: monkey.s01 Pattern: monkey.s?? => true
  97. * String: monkey.s101 Pattern: monkey.s?? => false
  98. * String: monkey.s99 Pattern: monkey.s?1 => false
  99. * String: monkey.s101 Pattern: monkey.s* => true
  100. * String: monkey.s99 Pattern: monkey.s*1 => false
  101. *
  102. * @param str Text to be matched against the given pattern.
  103. * @param pat Glob pattern.
  104. * @param ignoreCase Whether to ignore the case when doing pattern match
  105. * @param pathMode Whether to use path mode, i.e., whether slashes must be matched explicitly.
  106. *
  107. * @return true if str matches the pattern, false otherwise.
  108. */
  109. bool matchString(const char *pat, bool ignoreCase = false, bool pathMode = false) const;
  110. bool matchString(const String &pat, bool ignoreCase = false, bool pathMode = false) const;
  111. inline const char *c_str() const { return _str.c_str(); }
  112. inline uint size() const { return uint(_str.size()); }
  113. inline bool empty() const { return (_str.empty() == 0); }
  114. char lastChar() const { return (_str.size() > 0) ? _str[_str.size()-1] : 0; }
  115. char operator[](int idx) const {
  116. assert(idx >= 0 && idx < (int)_str.size());
  117. return _str[idx];
  118. }
  119. /** Set character c at position p, replacing the previous character there. */
  120. void setChar(char c, uint32 p);
  121. /** Insert character c before position p. */
  122. void insertChar(char c, uint32 p);
  123. /** Clears the string, making it empty. */
  124. void clear();
  125. /** Convert all characters in the string to lowercase. */
  126. void toLowercase();
  127. /** Convert all characters in the string to uppercase. */
  128. void toUppercase();
  129. public:
  130. typedef char * iterator;
  131. typedef const char * const_iterator;
  132. iterator begin() {
  133. return &_str[0];
  134. }
  135. iterator end() {
  136. return begin() + size();
  137. }
  138. const_iterator begin() const {
  139. return &_str[0];
  140. }
  141. const_iterator end() const {
  142. return begin() + size();
  143. }
  144. protected:
  145. void initWithCStr(const char *str, uint32 len);
  146. };
  147. // Append two strings to form a new (temp) string
  148. String operator +(const String &x, const String &y);
  149. String operator +(const char *x, const String &y);
  150. String operator +(const String &x, const char *y);
  151. String operator +(const String &x, char y);
  152. String operator +(char x, const String &y);
  153. // Some useful additional comparison operators for Strings
  154. bool operator == (const char *x, const String &y);
  155. bool operator != (const char *x, const String &y);
  156. // Utility functions to remove leading and trailing whitespaces
  157. extern char *ltrim(char *t);
  158. extern char *rtrim(char *t);
  159. extern char *trim(char *t);
  160. /**
  161. * Returns the last component of a given path.
  162. *
  163. * Examples:
  164. * /foo/bar.txt would return 'bar.txt'
  165. * /foo/bar/ would return 'bar'
  166. * /foo/./bar// would return 'bar'
  167. *
  168. * @param path the path of which we want to know the last component
  169. * @param sep character used to separate path components
  170. * @return The last component of the path.
  171. */
  172. Common::String lastPathComponent(const Common::String &path, const char sep);
  173. /**
  174. * Normalize a gien path to a canonical form. In particular:
  175. * - trailing separators are removed: /foo/bar/ -> /foo/bar
  176. * - double separators (= empty components) are removed: /foo//bar -> /foo/bar
  177. * - dot components are removed: /foo/./bar -> /foo/bar
  178. *
  179. * @todo remove double dot components: /foo/baz/../bar -> /foo/bar
  180. *
  181. * @param path the path to normalize
  182. * @param sep the separator token (usually '/' on Unix-style systems, or '\\' on Windows based stuff)
  183. * @return the normalized path
  184. */
  185. Common::String normalizePath(const Common::String &path, const char sep);
  186. /**
  187. * Simple DOS-style pattern matching function (understands * and ? like used in DOS).
  188. * Taken from exult/files/listfiles.cc
  189. *
  190. * Token meaning:
  191. * "*": any character, any amount of times.
  192. * "?": any character, only once.
  193. *
  194. * Example strings/patterns:
  195. * String: monkey.s01 Pattern: monkey.s?? => true
  196. * String: monkey.s101 Pattern: monkey.s?? => false
  197. * String: monkey.s99 Pattern: monkey.s?1 => false
  198. * String: monkey.s101 Pattern: monkey.s* => true
  199. * String: monkey.s99 Pattern: monkey.s*1 => false
  200. *
  201. * @param str Text to be matched against the given pattern.
  202. * @param pat Glob pattern.
  203. * @param ignoreCase Whether to ignore the case when doing pattern match
  204. * @param pathMode Whether to use path mode, i.e., whether slashes must be matched explicitly.
  205. *
  206. * @return true if str matches the pattern, false otherwise.
  207. */
  208. bool matchString(const char *str, const char *pat, bool ignoreCase = false, bool pathMode = false);
  209. } // End of namespace Common
  210. #endif