String.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. //========================================================================
  2. // String.cpp : Defines some useful string utility functions
  3. //
  4. // Part of the GameCode4 Application
  5. //
  6. // GameCode4 is the sample application that encapsulates much of the source code
  7. // discussed in "Game Coding Complete - 4th Edition" by Mike McShaffry and David
  8. // "Rez" Graham, published by Charles River Media.
  9. // ISBN-10: 1133776574 | ISBN-13: 978-1133776574
  10. //
  11. // If this source code has found it's way to you, and you think it has helped you
  12. // in any way, do the authors a favor and buy a new copy of the book - there are
  13. // detailed explanations in it that compliment this code well. Buy a copy at Amazon.com
  14. // by clicking here:
  15. // http://www.amazon.com/gp/product/1133776574/ref=olp_product_details?ie=UTF8&me=&seller=
  16. //
  17. // There's a companion web site at http://www.mcshaffry.com/GameCode/
  18. //
  19. // The source code is managed and maintained through Google Code:
  20. // http://code.google.com/p/gamecode4/
  21. //
  22. // (c) Copyright 2012 Michael L. McShaffry and David Graham
  23. //
  24. // This program is free software; you can redistribute it and/or
  25. // modify it under the terms of the GNU Lesser GPL v3
  26. // as published by the Free Software Foundation.
  27. //
  28. // This program is distributed in the hope that it will be useful,
  29. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  30. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  31. // http://www.gnu.org/licenses/lgpl-3.0.txt for more details.
  32. //
  33. // You should have received a copy of the GNU Lesser GPL v3
  34. // along with this program; if not, write to the Free Software
  35. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  36. //
  37. //========================================================================
  38. //========================================================================
  39. //
  40. // String manipulation routines
  41. //
  42. // Not described in the book, but very useful for printing multiline text
  43. // Used in ..\Win32\Fonts.cpp
  44. //========================================================================
  45. #include "../Unix/GameCodeStd.h"
  46. #include "String.h"
  47. using std::string;
  48. // The following function was found on http://xoomer.virgilio.it/acantato/dev/wildcard/wildmatch.html, where it was attributed to
  49. // the C/C++ Users Journal, written by Mike Cornelison. It is a little ugly, but it is FAST. Use this as an excercise in not reinventing the
  50. // wheel, even if you see gotos.
  51. bool WildcardMatch(const char *pat, const char *str) {
  52. int i, star;
  53. new_segment:
  54. star = 0;
  55. if (*pat == '*') {
  56. star = 1;
  57. do { pat++; } while (*pat == '*'); /* enddo */
  58. } /* endif */
  59. test_match:
  60. for (i = 0; pat[i] && (pat[i] != '*'); i++) {
  61. //if (mapCaseTable[str[i]] != mapCaseTable[pat[i]]) {
  62. if (str[i] != pat[i]) {
  63. if (!str[i]) return 0;
  64. if ((pat[i] == '?') && (str[i] != '.')) continue;
  65. if (!star) return 0;
  66. str++;
  67. goto test_match;
  68. }
  69. }
  70. if (pat[i] == '*') {
  71. str += i;
  72. pat += i;
  73. goto new_segment;
  74. }
  75. if (!str[i]) return 1;
  76. if (i && pat[i - 1] == '*') return 1;
  77. if (!star) return 0;
  78. str++;
  79. goto test_match;
  80. }
  81. string ToStr(int num, int base)
  82. {
  83. char str[MAX_DIGITS_IN_INT];
  84. memset(str,0,MAX_DIGITS_IN_INT);
  85. switch (base)
  86. {
  87. case 16:
  88. snprintf(str, MAX_DIGITS_IN_INT, "0x%X", num);
  89. break;
  90. default:
  91. snprintf(str, MAX_DIGITS_IN_INT, "%d", num);
  92. }
  93. return (string(str));
  94. } // end ToStr()
  95. string ToStr(unsigned int num, int base)
  96. {
  97. char str[MAX_DIGITS_IN_INT];
  98. memset(str, 0, MAX_DIGITS_IN_INT);
  99. switch (base)
  100. {
  101. case 16:
  102. snprintf(str, MAX_DIGITS_IN_INT, "0x%X", num);
  103. break;
  104. default:
  105. snprintf(str, MAX_DIGITS_IN_INT, "%u", num);
  106. }
  107. return (string(str));
  108. }
  109. string ToStr(unsigned long num, int base)
  110. {
  111. char str[MAX_DIGITS_IN_INT];
  112. memset(str,0,MAX_DIGITS_IN_INT);
  113. switch (base)
  114. {
  115. case 16:
  116. snprintf(str, MAX_DIGITS_IN_INT, "0x%lX", num);
  117. break;
  118. default:
  119. snprintf(str, MAX_DIGITS_IN_INT, "%lu", num);
  120. }
  121. return (string(str));
  122. }
  123. string ToStr(float num)
  124. {
  125. char str[64]; // I'm sure this is overkill
  126. memset(str,0,64);
  127. snprintf(str, 64, "%f", num);
  128. return (string(str));
  129. }
  130. string ToStr(double num)
  131. {
  132. char str[64]; // I'm sure this is overkill
  133. memset(str,0,64);
  134. snprintf(str, 64, "%lf", num);
  135. return (string(str));
  136. }
  137. string ToStr(bool val)
  138. {
  139. return (string( (val == true ? "true" : "false") ));
  140. }
  141. string ToStr(const glm::vec3& vec)
  142. {
  143. return string("(" + ToStr(vec.x) + "," + ToStr(vec.y) + "," + ToStr(vec.z) + ")");
  144. }
  145. //---------------------------------------------------------------------------------------------------------------------
  146. // This is basically like the Perl split() function. It splits str into substrings by cutting it at each delimiter.
  147. // The result is stored in vec.
  148. //---------------------------------------------------------------------------------------------------------------------
  149. void Split(const std::string& str, StringVec& vec, char delimiter)
  150. {
  151. vec.clear();
  152. size_t strLen = str.size();
  153. if (strLen == 0)
  154. return;
  155. size_t startIndex = 0;
  156. size_t indexOfDel = str.find_first_of(delimiter, startIndex);
  157. while (indexOfDel != std::string::npos)
  158. {
  159. vec.push_back(str.substr(startIndex, indexOfDel-startIndex));
  160. startIndex=indexOfDel + 1;
  161. if (startIndex >= strLen)
  162. break;
  163. indexOfDel = str.find_first_of(delimiter, startIndex);
  164. }
  165. if(startIndex < strLen)
  166. vec.push_back(str.substr(startIndex));
  167. }
  168. unsigned long
  169. HashedString::hash_name( char const * pIdentStr )
  170. {
  171. // Relatively simple hash of arbitrary text string into a
  172. // 32-bit identifier Output value is
  173. // input-valid-deterministic, but no guarantees are made
  174. // about the uniqueness of the output per-input
  175. //
  176. // Input value is treated as lower-case to cut down on false
  177. // separations cause by human mistypes. Sure, it could be
  178. // construed as a programming error to mix up your cases, and
  179. // it cuts down on permutations, but in Real World Usage
  180. // making this text case-sensitive will likely just lead to
  181. // Pain and Suffering.
  182. //
  183. // This code lossely based upon the adler32 checksum by Mark
  184. // Adler and published as part of the zlib compression
  185. // library sources.
  186. // largest prime smaller than 65536
  187. unsigned long BASE = 65521L;
  188. // NMAX is the largest n such that 255n(n+1)/2 +
  189. // (n+1)(BASE-1) <= 2^32-1
  190. unsigned long NMAX = 5552;
  191. #define DO1(buf,i) {s1 += tolower(buf[i]); s2 += s1;}
  192. #define DO2(buf,i) DO1(buf,i); DO1(buf,i+1);
  193. #define DO4(buf,i) DO2(buf,i); DO2(buf,i+2);
  194. #define DO8(buf,i) DO4(buf,i); DO4(buf,i+4);
  195. #define DO16(buf) DO8(buf,0); DO8(buf,8);
  196. if (pIdentStr == NULL)
  197. return 0;
  198. unsigned long s1 = 0;
  199. unsigned long s2 = 0;
  200. for ( size_t len = strlen( pIdentStr ); len > 0 ; )
  201. {
  202. unsigned long k = len < NMAX ? len : NMAX;
  203. len -= k;
  204. while (k >= 16)
  205. {
  206. DO16(pIdentStr);
  207. pIdentStr += 16;
  208. k -= 16;
  209. }
  210. if (k != 0) do
  211. {
  212. s1 += tolower( *pIdentStr++ );
  213. s2 += s1;
  214. } while (--k);
  215. s1 %= BASE;
  216. s2 %= BASE;
  217. }
  218. #pragma warning(push)
  219. #pragma warning(disable : 4312)
  220. return (s2 << 16) | s1;
  221. #pragma warning(pop)
  222. #undef DO1
  223. #undef DO2
  224. #undef DO4
  225. #undef DO8
  226. #undef DO16
  227. }