Utility.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "platform/platformGL.h"
  24. #include "console/consoleTypes.h"
  25. #include "console/console.h"
  26. #include "math/mRandom.h"
  27. #include "2d/sceneobject/SceneObject.h"
  28. #include "2d/core/Utility.h"
  29. //-----------------------------------------------------------------------------
  30. ConsoleType( b2AABB, Typeb2AABB, sizeof(b2AABB), "" )
  31. ConsoleGetType( Typeb2AABB )
  32. {
  33. // Fetch AABB.
  34. const b2AABB* pAABB = (b2AABB*)dptr;
  35. // Format AABB.
  36. char* pBuffer = Con::getReturnBuffer(64);
  37. dSprintf(pBuffer, 64, "%.5g %.5g", pAABB->lowerBound.x, pAABB->lowerBound.y, pAABB->upperBound.x, pAABB->upperBound.y );
  38. return pBuffer;
  39. }
  40. ConsoleSetType( Typeb2AABB )
  41. {
  42. // Fetch AABB.
  43. b2AABB* pAABB = (b2AABB*)dptr;
  44. // "lowerX lowerY upperX upperY".
  45. if( argc == 1 )
  46. {
  47. if ( dSscanf(argv[0], "%g %g %g %g", &(pAABB->lowerBound.x), &(pAABB->lowerBound.y), &(pAABB->upperBound.x), &(pAABB->upperBound.y) ) == 4 )
  48. return;
  49. }
  50. // "lowerX,lowerY,upperX,upperY".
  51. else if( argc == 4 )
  52. {
  53. pAABB->lowerBound.Set( dAtof(argv[0]), dAtof(argv[1] ) );
  54. pAABB->upperBound.Set( dAtof(argv[2]), dAtof(argv[3] ) );
  55. return;
  56. }
  57. // Reset the AABB.
  58. pAABB->lowerBound.SetZero();
  59. pAABB->upperBound.SetZero();
  60. // Warn.
  61. Con::printf("Typeb2AABB must be set as { lowerX, lowerY, upperX, upperY } or \"lowerX lowerY upperX upperY\"");
  62. }
  63. //-----------------------------------------------------------------------------
  64. namespace Utility
  65. {
  66. //-----------------------------------------------------------------------------
  67. // Return a string containing the common elements of two space-separated strings of elements.
  68. //-----------------------------------------------------------------------------
  69. ConsoleFunction( t2dGetCommonElements, const char*, 3, 3, "(set1, set2) - Returns the common elements in two sets.")
  70. {
  71. if (argc != 3)
  72. {
  73. Con::warnf("t2dGetCommonElements - Invalid number of parameters!");
  74. return NULL;
  75. }
  76. // Grab the element count of the first set.
  77. const U32 elementCount1 = Utility::mGetStringElementCount(argv[1]);
  78. // Make sure we get at least one number.
  79. if (elementCount1 < 1)
  80. {
  81. return NULL;
  82. }
  83. // Grab the element count of the second set.
  84. const U32 elementCount2 = Utility::mGetStringElementCount(argv[2]);
  85. // Make sure we get at least one number.
  86. if (elementCount2 < 1)
  87. {
  88. return NULL;
  89. }
  90. char* buffer = Con::getReturnBuffer(dStrlen(argv[1]) + 1);
  91. buffer[0] = '\0';
  92. bool first = true;
  93. // Individual elements assumed to be 1024 or less in length
  94. char element[1024];
  95. // Check for common elements
  96. for (U32 i = 0; i < elementCount1; i++)
  97. {
  98. dStrcpy(element, Utility::mGetStringElement(argv[1], i, true));
  99. for (U32 j = 0; j < elementCount2; j++)
  100. {
  101. if (!dStrcmp(element, Utility::mGetStringElement(argv[2], j, true)))
  102. {
  103. if (!first)
  104. dStrcat(buffer, " ");
  105. else
  106. first = false;
  107. dStrcat(buffer, element);
  108. }
  109. }
  110. }
  111. return buffer;
  112. }
  113. //-----------------------------------------------------------------------------
  114. const char* mGetFirstNonWhitespace( const char* inString )
  115. {
  116. // Search for first non-whitespace.
  117. while(*inString == ' ' || *inString == '\n' || *inString == '\t')
  118. inString++;
  119. // Return found point.
  120. return inString;
  121. }
  122. //------------------------------------------------------------------------------
  123. // NOTE:- You must verify that elements (index/index+1) are valid first!
  124. Vector2 mGetStringElementVector( const char* inString, const U32 index )
  125. {
  126. const U32 elementCount = Utility::mGetStringElementCount(inString);
  127. if ((index + 1) >= elementCount )
  128. {
  129. const F32 element = dAtof(mGetStringElement(inString,index));
  130. return Vector2(element, element);
  131. }
  132. // Get String Element Vector.
  133. return Vector2( dAtof(mGetStringElement(inString,index)), dAtof(Utility::mGetStringElement(inString,index+1)) );
  134. }
  135. //------------------------------------------------------------------------------
  136. // NOTE:- You must verify that elements (index/index+1/index+2) are valid first!
  137. VectorF mGetStringElementVector3D( const char* inString, const U32 index )
  138. {
  139. if ((index + 2) >= Utility::mGetStringElementCount(inString))
  140. return VectorF(0.0f, 0.0f, 0.0f);
  141. // Get String Element Vector.
  142. return VectorF( dAtof(mGetStringElement(inString,index)), dAtof(Utility::mGetStringElement(inString,index+1)), dAtof(Utility::mGetStringElement(inString,index+2)) );
  143. }
  144. //-----------------------------------------------------------------------------
  145. const char* mGetStringElement( const char* inString, const U32 index, const bool copyBuffer )
  146. {
  147. // Non-whitespace chars.
  148. static const char* set = " \t\n";
  149. U32 wordCount = 0;
  150. U8 search = 0;
  151. const char* pWordStart = NULL;
  152. // End of string?
  153. if ( *inString != 0 )
  154. {
  155. // No, so search string.
  156. while( *inString )
  157. {
  158. // Get string element.
  159. search = *inString;
  160. // End of string?
  161. if ( search == 0 )
  162. break;
  163. // Move to next element.
  164. inString++;
  165. // Search for separators.
  166. for( U32 i = 0; set[i]; i++ )
  167. {
  168. // Found one?
  169. if( search == set[i] )
  170. {
  171. // Yes...
  172. search = 0;
  173. break;
  174. }
  175. }
  176. // Found a separator?
  177. if ( search == 0 )
  178. continue;
  179. // Found are word?
  180. if ( wordCount == index )
  181. {
  182. // Yes, so mark it.
  183. pWordStart = inString-1;
  184. }
  185. // We've found a non-separator.
  186. wordCount++;
  187. // Search for end of non-separator.
  188. while( 1 )
  189. {
  190. // Get string element.
  191. search = *inString;
  192. // End of string?
  193. if ( search == 0 )
  194. break;
  195. // Move to next element.
  196. inString++;
  197. // Search for separators.
  198. for( U32 i = 0; set[i]; i++ )
  199. {
  200. // Found one?
  201. if( search == set[i] )
  202. {
  203. // Yes...
  204. search = 0;
  205. break;
  206. }
  207. }
  208. // Found separator?
  209. if ( search == 0 )
  210. break;
  211. }
  212. // Have we found our word?
  213. if ( pWordStart )
  214. {
  215. // Yes, so return position if not copying buffer.
  216. if ( !copyBuffer )
  217. return pWordStart;
  218. // Result Buffer.
  219. static char buffer[4096];
  220. // Calculate word length.
  221. const U32 length = inString - pWordStart - ((*inString)?1:0);
  222. // Copy Word.
  223. dStrncpy( buffer, pWordStart, length);
  224. buffer[length] = '\0';
  225. // Return Word.
  226. return buffer;
  227. }
  228. // End of string?
  229. if ( *inString == 0 )
  230. {
  231. // Bah!
  232. break;
  233. }
  234. }
  235. }
  236. // Sanity!
  237. AssertFatal( false, "Utility::mGetStringElement() - Couldn't find specified string element!" );
  238. // Didn't find it
  239. return StringTable->EmptyString;
  240. }
  241. //-----------------------------------------------------------------------------
  242. U32 mGetStringElementCount( const char* inString )
  243. {
  244. // Non-whitespace chars.
  245. static const char* set = " \t\n";
  246. // End of string.
  247. if ( *inString == 0 )
  248. return 0;
  249. U32 wordCount = 0;
  250. U8 search = 0;
  251. // Search String.
  252. while( *inString )
  253. {
  254. // Get string element.
  255. search = *inString;
  256. // End of string?
  257. if ( search == 0 )
  258. break;
  259. // Move to next element.
  260. inString++;
  261. // Search for separators.
  262. for( U32 i = 0; set[i]; i++ )
  263. {
  264. // Found one?
  265. if( search == set[i] )
  266. {
  267. // Yes...
  268. search = 0;
  269. break;
  270. }
  271. }
  272. // Found a separator?
  273. if ( search == 0 )
  274. continue;
  275. // We've found a non-separator.
  276. wordCount++;
  277. // Search for end of non-separator.
  278. while( 1 )
  279. {
  280. // Get string element.
  281. search = *inString;
  282. // End of string?
  283. if ( search == 0 )
  284. break;
  285. // Move to next element.
  286. inString++;
  287. // Search for separators.
  288. for( U32 i = 0; set[i]; i++ )
  289. {
  290. // Found one?
  291. if( search == set[i] )
  292. {
  293. // Yes...
  294. search = 0;
  295. break;
  296. }
  297. }
  298. // Found Separator?
  299. if ( search == 0 )
  300. break;
  301. }
  302. // End of string?
  303. if ( *inString == 0 )
  304. {
  305. // Bah!
  306. break;
  307. }
  308. }
  309. // We've finished.
  310. return wordCount;
  311. }
  312. } // Namespace Utility