stringStack.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 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. #ifndef _STRINGSTACK_H_
  23. #define _STRINGSTACK_H_
  24. #ifndef _STRINGFUNCTIONS_H_
  25. #include "core/strings/stringFunctions.h"
  26. #endif
  27. #ifndef _STRINGTABLE_H_
  28. #include "core/stringTable.h"
  29. #endif
  30. #ifndef _CONSOLE_H_
  31. #include "console/console.h"
  32. #endif
  33. /// Core stack for interpreter operations.
  34. ///
  35. /// This class provides some powerful semantics for working with strings, and is
  36. /// used heavily by the console interpreter.
  37. struct StringStack
  38. {
  39. enum {
  40. MaxStackDepth = 1024,
  41. MaxArgs = 20,
  42. ReturnBufferSpace = 512
  43. };
  44. char *mBuffer;
  45. U32 mBufferSize;
  46. const char *mArgV[MaxArgs];
  47. U32 mFrameOffsets[MaxStackDepth];
  48. U32 mStartOffsets[MaxStackDepth];
  49. U32 mNumFrames;
  50. U32 mArgc;
  51. U32 mStart;
  52. U32 mLen;
  53. U32 mStartStackSize;
  54. U32 mFunctionOffset;
  55. U32 mArgBufferSize;
  56. char *mArgBuffer;
  57. void validateBufferSize(U32 size)
  58. {
  59. if(size > mBufferSize)
  60. {
  61. mBufferSize = size + 2048;
  62. mBuffer = (char *) dRealloc(mBuffer, mBufferSize);
  63. }
  64. }
  65. void validateArgBufferSize(U32 size)
  66. {
  67. if(size > mArgBufferSize)
  68. {
  69. mArgBufferSize = size + 2048;
  70. mArgBuffer = (char *) dRealloc(mArgBuffer, mArgBufferSize);
  71. }
  72. }
  73. StringStack()
  74. {
  75. mBufferSize = 0;
  76. mBuffer = NULL;
  77. mArgBufferSize = 0;
  78. mArgBuffer = NULL;
  79. mNumFrames = 0;
  80. mStart = 0;
  81. mLen = 0;
  82. mStartStackSize = 0;
  83. mFunctionOffset = 0;
  84. validateBufferSize(8192);
  85. validateArgBufferSize(2048);
  86. dMemset(mBuffer, '\0', mBufferSize);
  87. dMemset(mArgBuffer, '\0', mArgBufferSize);
  88. }
  89. ~StringStack()
  90. {
  91. if( mBuffer )
  92. dFree( mBuffer );
  93. if( mArgBuffer )
  94. dFree( mArgBuffer );
  95. }
  96. /// Set the top of the stack to be an integer value.
  97. void setIntValue(U32 i)
  98. {
  99. validateBufferSize(mStart + 32);
  100. dSprintf(mBuffer + mStart, 32, "%d", i);
  101. mLen = dStrlen(mBuffer + mStart);
  102. }
  103. /// Set the top of the stack to be a float value.
  104. void setFloatValue(F64 v)
  105. {
  106. validateBufferSize(mStart + 32);
  107. dSprintf(mBuffer + mStart, 32, "%g", v);
  108. mLen = dStrlen(mBuffer + mStart);
  109. }
  110. /// Return a temporary buffer we can use to return data.
  111. char* getReturnBuffer(U32 size)
  112. {
  113. AssertFatal(Con::isMainThread(), "Manipulating return buffer from a secondary thread!");
  114. validateArgBufferSize(size);
  115. return mArgBuffer;
  116. }
  117. /// Return a buffer we can use for arguments.
  118. ///
  119. /// This updates the function offset.
  120. char *getArgBuffer(U32 size)
  121. {
  122. AssertFatal(Con::isMainThread(), "Manipulating console arg buffer from a secondary thread!");
  123. validateBufferSize(mStart + mFunctionOffset + size);
  124. char *ret = mBuffer + mStart + mFunctionOffset;
  125. mFunctionOffset += size;
  126. return ret;
  127. }
  128. /// Clear the function offset.
  129. void clearFunctionOffset()
  130. {
  131. //Con::printf("StringStack mFunctionOffset = 0 (from %i)", mFunctionOffset);
  132. mFunctionOffset = 0;
  133. }
  134. /// Set a string value on the top of the stack.
  135. void setStringValue(const char *s)
  136. {
  137. if(!s)
  138. {
  139. mLen = 0;
  140. mBuffer[mStart] = 0;
  141. return;
  142. }
  143. mLen = dStrlen(s);
  144. validateBufferSize(mStart + mLen + 2);
  145. dStrcpy(mBuffer + mStart, s);
  146. }
  147. /// Get the top of the stack, as a StringTableEntry.
  148. ///
  149. /// @note Don't free this memory!
  150. inline StringTableEntry getSTValue()
  151. {
  152. return StringTable->insert(mBuffer + mStart);
  153. }
  154. /// Get an integer representation of the top of the stack.
  155. inline U32 getIntValue()
  156. {
  157. return dAtoi(mBuffer + mStart);
  158. }
  159. /// Get a float representation of the top of the stack.
  160. inline F64 getFloatValue()
  161. {
  162. return dAtof(mBuffer + mStart);
  163. }
  164. /// Get a string representation of the top of the stack.
  165. ///
  166. /// @note This returns a pointer to the actual top of the stack, be careful!
  167. inline const char *getStringValue()
  168. {
  169. return mBuffer + mStart;
  170. }
  171. inline const char *getPreviousStringValue()
  172. {
  173. return mBuffer + mStartOffsets[mStartStackSize-1];
  174. }
  175. /// Advance the start stack, placing a zero length string on the top.
  176. ///
  177. /// @note You should use StringStack::push, not this, if you want to
  178. /// properly push the stack.
  179. void advance()
  180. {
  181. mStartOffsets[mStartStackSize++] = mStart;
  182. mStart += mLen;
  183. mLen = 0;
  184. }
  185. /// Advance the start stack, placing a single character, null-terminated strong
  186. /// on the top.
  187. ///
  188. /// @note You should use StringStack::push, not this, if you want to
  189. /// properly push the stack.
  190. void advanceChar(char c)
  191. {
  192. mStartOffsets[mStartStackSize++] = mStart;
  193. mStart += mLen;
  194. mBuffer[mStart] = c;
  195. mBuffer[mStart+1] = 0;
  196. mStart += 1;
  197. mLen = 0;
  198. }
  199. /// Push the stack, placing a zero-length string on the top.
  200. void push()
  201. {
  202. advanceChar(0);
  203. }
  204. inline void setLen(U32 newlen)
  205. {
  206. mLen = newlen;
  207. }
  208. /// Pop the start stack.
  209. void rewind()
  210. {
  211. mStart = mStartOffsets[--mStartStackSize];
  212. mLen = dStrlen(mBuffer + mStart);
  213. }
  214. // Terminate the current string, and pop the start stack.
  215. void rewindTerminate()
  216. {
  217. mBuffer[mStart] = 0;
  218. mStart = mStartOffsets[--mStartStackSize];
  219. mLen = dStrlen(mBuffer + mStart);
  220. }
  221. /// Compare 1st and 2nd items on stack, consuming them in the process,
  222. /// and returning true if they matched, false if they didn't.
  223. U32 compare()
  224. {
  225. // Figure out the 1st and 2nd item offsets.
  226. U32 oldStart = mStart;
  227. mStart = mStartOffsets[--mStartStackSize];
  228. // Compare current and previous strings.
  229. U32 ret = !dStricmp(mBuffer + mStart, mBuffer + oldStart);
  230. // Put an empty string on the top of the stack.
  231. mLen = 0;
  232. mBuffer[mStart] = 0;
  233. return ret;
  234. }
  235. void pushFrame()
  236. {
  237. //Con::printf("StringStack pushFrame [frame=%i, start=%i]", mNumFrames, mStartStackSize);
  238. mFrameOffsets[mNumFrames++] = mStartStackSize;
  239. mStartOffsets[mStartStackSize++] = mStart;
  240. mStart += ReturnBufferSpace;
  241. validateBufferSize(0);
  242. }
  243. void popFrame()
  244. {
  245. //Con::printf("StringStack popFrame [frame=%i, start=%i]", mNumFrames, mStartStackSize);
  246. mStartStackSize = mFrameOffsets[--mNumFrames];
  247. mStart = mStartOffsets[mStartStackSize];
  248. mLen = 0;
  249. }
  250. void clearFrames()
  251. {
  252. //Con::printf("StringStack clearFrames");
  253. mNumFrames = 0;
  254. mStart = 0;
  255. mLen = 0;
  256. mStartStackSize = 0;
  257. mFunctionOffset = 0;
  258. }
  259. /// Get the arguments for a function call from the stack.
  260. void getArgcArgv(StringTableEntry name, U32 *argc, const char ***in_argv, bool popStackFrame = false);
  261. };
  262. // New console value stack
  263. class ConsoleValueStack
  264. {
  265. enum {
  266. MaxStackDepth = 1024,
  267. MaxArgs = 20,
  268. ReturnBufferSpace = 512
  269. };
  270. public:
  271. ConsoleValueStack();
  272. ~ConsoleValueStack();
  273. void pushVar(ConsoleValue *variable);
  274. void pushValue(ConsoleValue &value);
  275. ConsoleValue* reserveValues(U32 numValues);
  276. bool reserveValues(U32 numValues, ConsoleValueRef *values);
  277. ConsoleValue* pop();
  278. ConsoleValue *pushString(const char *value);
  279. ConsoleValue *pushStackString(const char *value);
  280. ConsoleValue *pushUINT(U32 value);
  281. ConsoleValue *pushFLT(float value);
  282. void pushFrame();
  283. void popFrame();
  284. void resetFrame();
  285. void clearFrames();
  286. void getArgcArgv(StringTableEntry name, U32 *argc, ConsoleValueRef **in_argv, bool popStackFrame = false);
  287. ConsoleValue mStack[MaxStackDepth];
  288. U32 mStackFrames[MaxStackDepth];
  289. U32 mFrame;
  290. U32 mStackPos;
  291. ConsoleValueRef mArgv[MaxArgs];
  292. };
  293. extern StringStack STR;
  294. extern ConsoleValueStack CSTK;
  295. #endif