stringStack.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. validateArgBufferSize(size);
  114. return mArgBuffer;
  115. }
  116. /// Return a buffer we can use for arguments.
  117. ///
  118. /// This updates the function offset.
  119. char *getArgBuffer(U32 size)
  120. {
  121. validateBufferSize(mStart + mFunctionOffset + size);
  122. char *ret = mBuffer + mStart + mFunctionOffset;
  123. mFunctionOffset += size;
  124. return ret;
  125. }
  126. /// Clear the function offset.
  127. void clearFunctionOffset()
  128. {
  129. //Con::printf("StringStack mFunctionOffset = 0 (from %i)", mFunctionOffset);
  130. mFunctionOffset = 0;
  131. }
  132. /// Set a string value on the top of the stack.
  133. void setStringValue(const char *s)
  134. {
  135. if(!s)
  136. {
  137. mLen = 0;
  138. mBuffer[mStart] = 0;
  139. return;
  140. }
  141. mLen = dStrlen(s);
  142. validateBufferSize(mStart + mLen + 2);
  143. dStrcpy(mBuffer + mStart, s);
  144. }
  145. /// Get the top of the stack, as a StringTableEntry.
  146. ///
  147. /// @note Don't free this memory!
  148. inline StringTableEntry getSTValue()
  149. {
  150. return StringTable->insert(mBuffer + mStart);
  151. }
  152. /// Get an integer representation of the top of the stack.
  153. inline U32 getIntValue()
  154. {
  155. return dAtoi(mBuffer + mStart);
  156. }
  157. /// Get a float representation of the top of the stack.
  158. inline F64 getFloatValue()
  159. {
  160. return dAtof(mBuffer + mStart);
  161. }
  162. /// Get a string representation of the top of the stack.
  163. ///
  164. /// @note This returns a pointer to the actual top of the stack, be careful!
  165. inline const char *getStringValue()
  166. {
  167. return mBuffer + mStart;
  168. }
  169. inline const char *getPreviousStringValue()
  170. {
  171. return mBuffer + mStartOffsets[mStartStackSize-1];
  172. }
  173. /// Advance the start stack, placing a zero length string on the top.
  174. ///
  175. /// @note You should use StringStack::push, not this, if you want to
  176. /// properly push the stack.
  177. void advance()
  178. {
  179. mStartOffsets[mStartStackSize++] = mStart;
  180. mStart += mLen;
  181. mLen = 0;
  182. }
  183. /// Advance the start stack, placing a single character, null-terminated strong
  184. /// on the top.
  185. ///
  186. /// @note You should use StringStack::push, not this, if you want to
  187. /// properly push the stack.
  188. void advanceChar(char c)
  189. {
  190. mStartOffsets[mStartStackSize++] = mStart;
  191. mStart += mLen;
  192. mBuffer[mStart] = c;
  193. mBuffer[mStart+1] = 0;
  194. mStart += 1;
  195. mLen = 0;
  196. }
  197. /// Push the stack, placing a zero-length string on the top.
  198. void push()
  199. {
  200. advanceChar(0);
  201. }
  202. inline void setLen(U32 newlen)
  203. {
  204. mLen = newlen;
  205. }
  206. /// Pop the start stack.
  207. void rewind()
  208. {
  209. mStart = mStartOffsets[--mStartStackSize];
  210. mLen = dStrlen(mBuffer + mStart);
  211. }
  212. // Terminate the current string, and pop the start stack.
  213. void rewindTerminate()
  214. {
  215. mBuffer[mStart] = 0;
  216. mStart = mStartOffsets[--mStartStackSize];
  217. mLen = dStrlen(mBuffer + mStart);
  218. }
  219. /// Compare 1st and 2nd items on stack, consuming them in the process,
  220. /// and returning true if they matched, false if they didn't.
  221. U32 compare()
  222. {
  223. // Figure out the 1st and 2nd item offsets.
  224. U32 oldStart = mStart;
  225. mStart = mStartOffsets[--mStartStackSize];
  226. // Compare current and previous strings.
  227. U32 ret = !dStricmp(mBuffer + mStart, mBuffer + oldStart);
  228. // Put an empty string on the top of the stack.
  229. mLen = 0;
  230. mBuffer[mStart] = 0;
  231. return ret;
  232. }
  233. void pushFrame()
  234. {
  235. //Con::printf("StringStack pushFrame [frame=%i, start=%i]", mNumFrames, mStartStackSize);
  236. mFrameOffsets[mNumFrames++] = mStartStackSize;
  237. mStartOffsets[mStartStackSize++] = mStart;
  238. mStart += ReturnBufferSpace;
  239. validateBufferSize(0);
  240. }
  241. void popFrame()
  242. {
  243. //Con::printf("StringStack popFrame [frame=%i, start=%i]", mNumFrames, mStartStackSize);
  244. mStartStackSize = mFrameOffsets[--mNumFrames];
  245. mStart = mStartOffsets[mStartStackSize];
  246. mLen = 0;
  247. }
  248. void clearFrames()
  249. {
  250. //Con::printf("StringStack clearFrames");
  251. mNumFrames = 0;
  252. mStart = 0;
  253. mLen = 0;
  254. mStartStackSize = 0;
  255. mFunctionOffset = 0;
  256. }
  257. /// Get the arguments for a function call from the stack.
  258. void getArgcArgv(StringTableEntry name, U32 *argc, const char ***in_argv, bool popStackFrame = false);
  259. };
  260. // New console value stack
  261. class ConsoleValueStack
  262. {
  263. enum {
  264. MaxStackDepth = 1024,
  265. MaxArgs = 20,
  266. ReturnBufferSpace = 512
  267. };
  268. public:
  269. ConsoleValueStack();
  270. ~ConsoleValueStack();
  271. void pushVar(ConsoleValue *variable);
  272. void pushValue(ConsoleValue &value);
  273. ConsoleValue* pop();
  274. ConsoleValue *pushString(const char *value);
  275. ConsoleValue *pushStackString(const char *value);
  276. ConsoleValue *pushUINT(U32 value);
  277. ConsoleValue *pushFLT(float value);
  278. void pushFrame();
  279. void popFrame();
  280. void resetFrame();
  281. void clearFrames();
  282. void getArgcArgv(StringTableEntry name, U32 *argc, ConsoleValueRef **in_argv, bool popStackFrame = false);
  283. ConsoleValue mStack[MaxStackDepth];
  284. U32 mStackFrames[MaxStackDepth];
  285. U32 mFrame;
  286. U32 mStackPos;
  287. ConsoleValueRef mArgv[MaxArgs];
  288. };
  289. #endif