1
0

StringW.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. #ifndef _STRINGW_H_WASABI
  2. #define _STRINGW_H_WASABI
  3. #ifdef __cplusplus
  4. /**
  5. This is a very basic string class. It is not trying to be a be-all end-all
  6. string class. It's meant to be just enough functionality to avoid STRDUP().
  7. It is available to use in client apps, but no API in Wasabi depends on its
  8. use by client code, so you can ignore it if you like. ;)
  9. Also note that one of the design goals is to use same storage space as
  10. a char *, which means no virtual methods.
  11. @short Basic string class, to replace STRDUP/FREE
  12. @see StringPrintf
  13. @see DebugString
  14. */
  15. #include <bfc/platform/types.h>
  16. class StringW
  17. {
  18. public:
  19. StringW(const wchar_t *initial_val = NULL);
  20. StringW(const StringW &s);
  21. StringW(const StringW *s);
  22. ~StringW();
  23. /**
  24. Returns the value of the string. It is a pointer to the internal storage
  25. used by the class, so you must not assume the pointer will not change,
  26. because it will.
  27. @see getValueSafe();
  28. @ret The value of the string.
  29. */
  30. const wchar_t *getValue() const { return val; }
  31. const wchar_t *v() const { return getValue(); } // for ease of typing
  32. operator const wchar_t *() const { return getValue(); }
  33. /**
  34. Gets the value of the string, or a safe value if the value is NULL.
  35. @param def_val The value to return if the string's value is NULL. Defaults to "".
  36. @see getValue()
  37. @ret The value of the string, or a safe value if the value is NULL.
  38. */
  39. const wchar_t *getValueSafe(const wchar_t *def_val = L"") const; // returns def_val if NULL
  40. /**
  41. Returns the value of the character at a position in the string. If the
  42. position is invalid, returns -1. Note that unless bounds_check is set to
  43. TRUE, the pos will only be checked for negative values.
  44. @param pos The position of the character to return.
  45. @param bounds_check If TRUE, pos is checked against the length of the string.
  46. @see setChar()
  47. @ret The value of the character at the position in the string, or -1 if the position is invalid.
  48. */
  49. //int getChar(int pos, int bounds_check = FALSE);
  50. /**
  51. Sets the value of the character at a position in the string. Not multibyte UTF-8 safe yet. No lengthwise bounds checking yet.
  52. @param pos The position to set.
  53. @param value The value to set.
  54. @ret The value of the character set, or -1 if pos < 0.
  55. */
  56. //int setChar(int pos, int value);
  57. /**
  58. Sets the value of the string. The given value will be copied.
  59. Can accept NULL. Note that this may cause the
  60. pointer returned by getValue() et al to change.
  61. @param newval The new value of the string, nul-terminated.
  62. @ret The new value of the string.
  63. @see getValue()
  64. */
  65. const wchar_t *setValue(const wchar_t *newval);
  66. /**
  67. Gets the value of the string in the form of a non-const wchar_t *. WARNING: you
  68. don't usually need to call this. If you want to modify the string,
  69. you can generally just use setChar(), or setValue().
  70. @ret The value of the string, casted to a non-const wchar_t *.
  71. @see getValue()
  72. @see setValue();
  73. @see setChar();
  74. */
  75. wchar_t *getNonConstVal() { return const_cast<wchar_t *>(getValue()); }
  76. const wchar_t *operator =(const wchar_t *newval) { return setValue(newval); }
  77. const wchar_t *operator +=(const wchar_t *addval)
  78. {
  79. return cat(addval);
  80. }
  81. const wchar_t *operator +=(wchar_t value);
  82. const wchar_t *operator +=(int value);
  83. const wchar_t *operator +=(GUID guid);
  84. // copy assignment operator
  85. StringW &operator =(const StringW &s)
  86. {
  87. if (this != &s)
  88. setValue(s);
  89. return *this;
  90. }
  91. // comparator operators
  92. //inline int operator ==(const wchar_t *val) const
  93. //{
  94. // if (!val) return isempty();
  95. // return isequal(val);
  96. //}
  97. //inline int operator <(const wchar_t *val) const
  98. //{
  99. // return islessthan(val);
  100. //}
  101. //inline int operator !=(const wchar_t *val) const
  102. //{
  103. // if (!val) return !isempty();
  104. // return !isequal(val);
  105. //}
  106. //inline int operator >(const wchar_t *val) const
  107. //{
  108. // return (!islessthan(val)) && (!isequal(val));
  109. //}
  110. inline StringW operator +(const wchar_t *val)
  111. {
  112. StringW retval = *this;
  113. return retval += val;
  114. }
  115. //inline int operator ==(const StringW &val) const
  116. //{
  117. // return isequal(val);
  118. //}
  119. //inline int operator <(const StringW &val) const
  120. //{
  121. // return islessthan(val);
  122. //}
  123. //inline int operator !=(const StringW &val) const
  124. //{
  125. // return !isequal(val);
  126. //}
  127. //inline int operator >(const StringW &val) const
  128. //{
  129. // return (!islessthan(val)) && (!isequal(val));
  130. //}
  131. //inline StringW operator +(const StringW &val)
  132. //{
  133. // StringW retval = *this;
  134. // return retval += val;
  135. //}
  136. //inline StringW operator +(const wchar_t val)
  137. //{
  138. // StringW retval = *this;
  139. // return retval += val;
  140. //}
  141. /**
  142. Gets the length of the string's value. Note that a 0 length can result from
  143. both a value of NULL and a value of "".
  144. @ret The length of the string's value;
  145. */
  146. size_t len() const;
  147. /**
  148. Returns TRUE if the string's value is either NULL or "".
  149. @ret TRUE if the string's value is either NULL or "", FALSE otherwise.
  150. */
  151. int isempty() const;
  152. /**
  153. Converts entire string to uppercase. Not multibyte UTF-8 safe yet.
  154. @see tolower()
  155. */
  156. void toupper();
  157. /**
  158. Converts entire string to lowercase. Not multibyte UTF-8 safe yet.
  159. @see tolower()
  160. */
  161. void tolower();
  162. /**
  163. Checks string value equality against a nul-terminated wchar_t *.
  164. @param otherval The value to check against. If NULL, will be treated as "".
  165. @ret TRUE if the string matches exactly, FALSE otherwise.
  166. @see iscaseequal()
  167. @see islessthan()
  168. */
  169. int isequal(const wchar_t *otherval) const; // basically !strcmp
  170. /**
  171. Checks string value equality against a nul-terminated wchar_t *, case insensitively. I.e. "Blah" is case equal to "bLaH".
  172. @param otherval The value to check against. If NULL, will be treated as "".
  173. @ret TRUE if the string matches case-insensitively, FALSE otherwise.
  174. @see isequal()
  175. @see islessthan()
  176. */
  177. int iscaseequal(const wchar_t *otherval) const; // basically !strcasecmp
  178. //int islessthan(const wchar_t *otherval) const; // basically strcmp < 0
  179. /**
  180. Changes all instances of a character to another character throughout the
  181. string. Not multibyte UTF-8 aware yet. Note you can use a 'to' of NULL,
  182. but this practice is not encouraged: try to use trunc() or truncateOnChar() instead.
  183. @param from The character value to modify.
  184. @param to The character value to replace with.
  185. @see trunc()
  186. @see truncateOnChar()
  187. */
  188. void changeChar(wchar_t from, wchar_t to);
  189. /**
  190. Truncates the string value at the first given character value found. If fromright==TRUE, searches from the right, otherwise goes left-to-right. Not UTF-8 multibyte aware yet.
  191. Ex:
  192. StringW x("abcd");
  193. x.truncateOnChar('c');
  194. x now contains "ab"
  195. @see changeChar()
  196. */
  197. //void truncateOnChar(int which, int fromright = FALSE);
  198. /**
  199. Gets the last character value (rightmost).
  200. @see getChar()
  201. @ret The rightmost character value, or -1 if string is empty.
  202. */
  203. int lastChar(); // -1 if empty
  204. /**
  205. Executes a standard printf type call and sets the string's value to it.
  206. @ret The new value of the string.
  207. @param format The formatting string to use.
  208. */
  209. const wchar_t *printf(const wchar_t *format, ...);
  210. /**
  211. Concatenates the given value onto the end of the string. NULL will be
  212. treated as "".
  213. @param value The value to concatenate.
  214. @ret The new value of the string.
  215. @see catn()
  216. @see prepend()
  217. */
  218. const wchar_t *cat(const wchar_t *value);
  219. /**
  220. Concatenates a certain number of characters from the given value onto the end of the string. NULL will be treated as "".
  221. @param value The value to concatenate.
  222. @param len How many characters of value to use.
  223. @ret The new value of the string.
  224. @see cat()
  225. @see prepend()
  226. */
  227. const wchar_t *catn(const wchar_t *value, size_t len);
  228. /**
  229. Useful for making directory paths and stuff
  230. adds a string plus a separator character.
  231. i.e.
  232. StringW x = "/usr/";
  233. x.catPostSeparator("bin", '/');
  234. creates "/usr/bin/"
  235. */
  236. const wchar_t *catPostSeparator(const wchar_t *value, const wchar_t separator);
  237. /**
  238. similiar to above, but puts the separator first
  239. i.e.
  240. StringW x = "/usr";
  241. x.catPostSeparator('/' "bin");
  242. creates "/usr/bin"
  243. */
  244. //const wchar_t *catPreSeparator(const wchar_t separator, const wchar_t *value);
  245. /**
  246. Inserts the given string value at the beginning of the string. NULL will be
  247. treated as "".
  248. @param value The value to insert.
  249. @ret The new value of the string.
  250. @see cat()
  251. @see catn()
  252. */
  253. const wchar_t *prepend(const wchar_t *value);
  254. // replaces string with n chars of val or length of val, whichever is less.
  255. const wchar_t *ncpy(const wchar_t *newstr, size_t numchars);
  256. /**
  257. Copies up to maxlen chars from the string into the destination. Differs from
  258. STRNCPY in that it makes sure the destination is always nul-terminated, so
  259. note that maxlen includes the terminating nul.
  260. @param dest The destination to copy to.
  261. @param maxlen How many bytes, at most, to copy.
  262. */
  263. //void strncpyTo(wchar_t *dest, int maxlen);
  264. // -----------------------------------------
  265. // Character based find-n-splice methods --
  266. // "l" and "r" prefixes specify to begin at
  267. // front or back of string:
  268. // Returns index of first found, -1 if not found.
  269. size_t lFindChar(wchar_t findval);
  270. size_t lFindChar(const wchar_t *findval); // a list of chars to search for
  271. //int rFindChar(wchar_t findval);
  272. size_t rFindChar(const wchar_t *findval); // a list of chars to search for
  273. // Splits string at findval. Characters passed by search, including the
  274. // found character, are MOVED to the returned string. If there is no wchar_t
  275. // to be found, the entire string is returned and the called instance is
  276. // left empty. (Makes looped splits very easy).
  277. StringW lSplit(size_t idxval);
  278. //StringW lSplitChar(wchar_t findval);
  279. StringW lSplitChar(const wchar_t *findval);
  280. StringW rSplit(size_t idxval);
  281. //StringW rSplitChar(wchar_t findval);
  282. StringW rSplitChar(const wchar_t *findval);
  283. // Same as split, except the find wchar_t is cut completely.
  284. //StringW lSpliceChar(wchar_t findval);
  285. StringW lSpliceChar(const wchar_t *findval);
  286. //StringW rSpliceChar(wchar_t findval);
  287. StringW rSpliceChar(const wchar_t *findval);
  288. /**
  289. Replaces all occurences of the value specified by 'find' with the value
  290. specified by 'replace'.
  291. @param find The value to find.
  292. @param replace The value to replace with.
  293. @ret The number of replacements that were executed.
  294. */
  295. int replace(const wchar_t *find, const wchar_t *replace);
  296. /**
  297. Replaces fields of same character with 0-padded text representation of an int.
  298. Example: blah$$$$.png becomes blah0000.png
  299. */
  300. int replaceNumericField(int value, wchar_t fieldchar = '\x24');
  301. // UTF8-Aware "Character Based" Methods
  302. /**
  303. Returns how many characters are in the string value. Same as len(), but multibyte UTF-8 aware.
  304. @see len()
  305. @ret Number of logical UTF-8 character in the string.
  306. */
  307. //int numCharacters();
  308. /**
  309. Truncates the length of the string to newlen. If newlen is negative, trims
  310. -newlen characters from the end. Multibyte UTF-8 aware.
  311. @param newlen The new length of the string. If the string is shorter than this, nothing happens. If this value is negative, then the absolute value of newlen is how many characters to trim from the right. I.e. -1 means trim off one character from the end.
  312. @see truncateOnChar()
  313. */
  314. void trunc(int newlen);
  315. // void trim(const wchar_t *whitespace = " \t\r\n", int left = TRUE, int right = TRUE);
  316. /**
  317. Does a vsprintf. Used the same way as printf(), but with a va_list instead of "...".
  318. @param format The format string to use.
  319. @param args The argument list in va_list format.
  320. @ret The number of characters in the final string.
  321. */
  322. size_t va_sprintf(const wchar_t *format, va_list args);
  323. /**
  324. Ensures that the string drops any memory it might have allocated.
  325. */
  326. void purge();
  327. void swap(StringW *); // swaps buffers with another string
  328. void swap(StringW &); // swaps buffers with another string
  329. void own(wchar_t *); // take ownership of a buffer
  330. void AppendPath(const wchar_t *path);
  331. void AppendFolder(const wchar_t *path);
  332. void AddBackslash();
  333. void RemovePath();
  334. void FixSlashes();
  335. protected:
  336. wchar_t * val;
  337. enum { wastage_allowed = 128 };
  338. };
  339. class StringPathCombine : public StringW
  340. {
  341. public:
  342. StringPathCombine(const wchar_t *path, const wchar_t *filename);
  343. };
  344. /**
  345. StringW class with a printf-style constructor. Otherwise identical to StringW.
  346. Also takes some standard types, like int, double, and GUID.
  347. @see GUID
  348. @see StringW
  349. */
  350. class StringPrintfW : public StringW
  351. {
  352. public:
  353. StringPrintfW(const wchar_t *format = NULL, ...);
  354. StringPrintfW(int value);
  355. StringPrintfW(double value);
  356. StringPrintfW(GUID g);
  357. };
  358. #if defined(NDEBUG) && defined(WASABI_NO_RELEASEMODE_DEBUGSTRINGS)
  359. #define DebugStringW __noop
  360. #else
  361. #define DebugStringW _DebugStringW
  362. #endif
  363. class _DebugStringW : public StringW
  364. {
  365. public:
  366. _DebugStringW(const wchar_t *format = NULL, ...);
  367. _DebugStringW(const StringW &s);
  368. _DebugStringW(const StringW *s);
  369. void debugPrint();
  370. };
  371. #if 0
  372. class StringToLower : public StringW
  373. {
  374. public:
  375. StringToLower(const char *val = NULL) : StringW(val)
  376. {
  377. tolower();
  378. }
  379. };
  380. class StringToUpper : public StringW
  381. {
  382. public:
  383. StringToUpper(const char *val = NULL) : StringW(val)
  384. {
  385. toupper();
  386. }
  387. };
  388. //
  389. // Global operator overrides to allow string to take over for
  390. // the use of standard operators with const char pointers as
  391. // left hand operands.
  392. inline int operator ==(const char *v1, const StringW &v2)
  393. {
  394. return v2.isequal(v1);
  395. }
  396. inline int operator !=(const char *v1, const StringW &v2)
  397. {
  398. return !v2.isequal(v1);
  399. }
  400. inline int operator <(const char *v1, const StringW &v2)
  401. {
  402. return !v2.islessthan(v1);
  403. }
  404. inline int operator >(const char *v1, const StringW &v2)
  405. {
  406. return v2.islessthan(v1);
  407. }
  408. #endif
  409. /**
  410. Compares two strings. Generally used with PtrListSorted<>
  411. @see PtrListSorted
  412. */
  413. class StringWComparator
  414. {
  415. public:
  416. // comparator for sorting
  417. static int compareItem(StringW *p1, StringW* p2);
  418. // comparator for searching
  419. static int compareAttrib(const wchar_t *attrib, StringW *item);
  420. };
  421. #endif
  422. #endif // __cplusplus