stringStack.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. }
  87. ~StringStack()
  88. {
  89. if( mBuffer )
  90. dFree( mBuffer );
  91. if( mArgBuffer )
  92. dFree( mArgBuffer );
  93. }
  94. /// Set the top of the stack to be an integer value.
  95. void setIntValue(U32 i)
  96. {
  97. validateBufferSize(mStart + 32);
  98. dSprintf(mBuffer + mStart, 32, "%d", i);
  99. mLen = dStrlen(mBuffer + mStart);
  100. }
  101. /// Set the top of the stack to be a float value.
  102. void setFloatValue(F64 v)
  103. {
  104. validateBufferSize(mStart + 32);
  105. dSprintf(mBuffer + mStart, 32, "%g", v);
  106. mLen = dStrlen(mBuffer + mStart);
  107. }
  108. /// Return a temporary buffer we can use to return data.
  109. char* getReturnBuffer(U32 size)
  110. {
  111. validateArgBufferSize(size);
  112. return mArgBuffer;
  113. }
  114. /// Return a buffer we can use for arguments.
  115. ///
  116. /// This updates the function offset.
  117. char *getArgBuffer(U32 size)
  118. {
  119. validateBufferSize(mStart + mFunctionOffset + size);
  120. char *ret = mBuffer + mStart + mFunctionOffset;
  121. mFunctionOffset += size;
  122. return ret;
  123. }
  124. /// Clear the function offset.
  125. void clearFunctionOffset()
  126. {
  127. mFunctionOffset = 0;
  128. }
  129. /// Set a string value on the top of the stack.
  130. void setStringValue(const char *s)
  131. {
  132. if(!s)
  133. {
  134. mLen = 0;
  135. mBuffer[mStart] = 0;
  136. return;
  137. }
  138. mLen = dStrlen(s);
  139. validateBufferSize(mStart + mLen + 2);
  140. dStrcpy(mBuffer + mStart, s);
  141. }
  142. /// Get the top of the stack, as a StringTableEntry.
  143. ///
  144. /// @note Don't free this memory!
  145. inline StringTableEntry getSTValue()
  146. {
  147. return StringTable->insert(mBuffer + mStart);
  148. }
  149. /// Get an integer representation of the top of the stack.
  150. inline U32 getIntValue()
  151. {
  152. return dAtoi(mBuffer + mStart);
  153. }
  154. /// Get a float representation of the top of the stack.
  155. inline F64 getFloatValue()
  156. {
  157. return dAtof(mBuffer + mStart);
  158. }
  159. /// Get a string representation of the top of the stack.
  160. ///
  161. /// @note This returns a pointer to the actual top of the stack, be careful!
  162. inline const char *getStringValue()
  163. {
  164. return mBuffer + mStart;
  165. }
  166. /// Advance the start stack, placing a zero length string on the top.
  167. ///
  168. /// @note You should use StringStack::push, not this, if you want to
  169. /// properly push the stack.
  170. void advance()
  171. {
  172. mStartOffsets[mStartStackSize++] = mStart;
  173. mStart += mLen;
  174. mLen = 0;
  175. }
  176. /// Advance the start stack, placing a single character, null-terminated strong
  177. /// on the top.
  178. ///
  179. /// @note You should use StringStack::push, not this, if you want to
  180. /// properly push the stack.
  181. void advanceChar(char c)
  182. {
  183. mStartOffsets[mStartStackSize++] = mStart;
  184. mStart += mLen;
  185. mBuffer[mStart] = c;
  186. mBuffer[mStart+1] = 0;
  187. mStart += 1;
  188. mLen = 0;
  189. }
  190. /// Push the stack, placing a zero-length string on the top.
  191. void push()
  192. {
  193. advanceChar(0);
  194. }
  195. inline void setLen(U32 newlen)
  196. {
  197. mLen = newlen;
  198. }
  199. /// Pop the start stack.
  200. void rewind()
  201. {
  202. mStart = mStartOffsets[--mStartStackSize];
  203. mLen = dStrlen(mBuffer + mStart);
  204. }
  205. // Terminate the current string, and pop the start stack.
  206. void rewindTerminate()
  207. {
  208. mBuffer[mStart] = 0;
  209. mStart = mStartOffsets[--mStartStackSize];
  210. mLen = dStrlen(mBuffer + mStart);
  211. }
  212. /// Compare 1st and 2nd items on stack, consuming them in the process,
  213. /// and returning true if they matched, false if they didn't.
  214. U32 compare()
  215. {
  216. // Figure out the 1st and 2nd item offsets.
  217. U32 oldStart = mStart;
  218. mStart = mStartOffsets[--mStartStackSize];
  219. // Compare current and previous strings.
  220. U32 ret = !dStricmp(mBuffer + mStart, mBuffer + oldStart);
  221. // Put an empty string on the top of the stack.
  222. mLen = 0;
  223. mBuffer[mStart] = 0;
  224. return ret;
  225. }
  226. void pushFrame()
  227. {
  228. mFrameOffsets[mNumFrames++] = mStartStackSize;
  229. mStartOffsets[mStartStackSize++] = mStart;
  230. mStart += ReturnBufferSpace;
  231. validateBufferSize(0);
  232. }
  233. void popFrame()
  234. {
  235. mStartStackSize = mFrameOffsets[--mNumFrames];
  236. mStart = mStartOffsets[mStartStackSize];
  237. mLen = 0;
  238. }
  239. /// Get the arguments for a function call from the stack.
  240. void getArgcArgv(StringTableEntry name, U32 *argc, const char ***in_argv, bool popStackFrame = false);
  241. };
  242. #endif