stringStack.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. #ifndef _STRINGSTACK_H_
  23. #define _STRINGSTACK_H_
  24. #include "platform/platform.h"
  25. #include "console/console.h"
  26. #include "console/compiler.h"
  27. #include "string/stringTable.h"
  28. /// Core stack for interpreter operations.
  29. ///
  30. /// This class provides some powerful semantics for working with strings, and is
  31. /// used heavily by the console interpreter.
  32. struct StringStack
  33. {
  34. enum {
  35. MaxStackDepth = 1024,
  36. MaxArgs = 20,
  37. ReturnBufferSpace = 512
  38. };
  39. char *mBuffer;
  40. U32 mBufferSize;
  41. const char *mArgV[MaxArgs];
  42. U32 mFrameOffsets[MaxStackDepth];
  43. U32 mStartOffsets[MaxStackDepth];
  44. U32 mNumFrames;
  45. U32 mArgc;
  46. U32 mStart;
  47. U32 mLen;
  48. U32 mStartStackSize;
  49. U32 mFunctionOffset;
  50. U32 mArgBufferSize;
  51. char *mArgBuffer;
  52. void validateBufferSize(U32 size)
  53. {
  54. if(size > mBufferSize)
  55. {
  56. mBufferSize = size + 2048;
  57. mBuffer = (char *) dRealloc(mBuffer, mBufferSize);
  58. }
  59. }
  60. void validateArgBufferSize(U32 size)
  61. {
  62. if(size > mArgBufferSize)
  63. {
  64. mArgBufferSize = size + 2048;
  65. mArgBuffer = (char *) dRealloc(mArgBuffer, mArgBufferSize);
  66. }
  67. }
  68. StringStack()
  69. {
  70. mBufferSize = 0;
  71. mBuffer = NULL;
  72. mNumFrames = 0;
  73. mStart = 0;
  74. mLen = 0;
  75. mStartStackSize = 0;
  76. mFunctionOffset = 0;
  77. validateBufferSize(8192);
  78. validateArgBufferSize(2048);
  79. }
  80. /// Set the top of the stack to be an integer value.
  81. void setIntValue(U32 i)
  82. {
  83. validateBufferSize(mStart + 32);
  84. dSprintf(mBuffer + mStart, 32, "%d", i);
  85. mLen = dStrlen(mBuffer + mStart);
  86. }
  87. /// Set the top of the stack to be a float value.
  88. void setFloatValue(F64 v)
  89. {
  90. validateBufferSize(mStart + 32);
  91. dSprintf(mBuffer + mStart, 32, "%.9g", v);
  92. mLen = dStrlen(mBuffer + mStart);
  93. }
  94. /// Return a temporary buffer we can use to return data.
  95. ///
  96. /// @note This clobbers anything in our buffers!
  97. char *getReturnBuffer(U32 size)
  98. {
  99. if(size > ReturnBufferSpace)
  100. {
  101. validateArgBufferSize(size);
  102. return mArgBuffer;
  103. }
  104. else
  105. {
  106. validateBufferSize(mStart + size);
  107. return mBuffer + mStart;
  108. }
  109. }
  110. /// Return a buffer we can use for arguments.
  111. ///
  112. /// This updates the function offset.
  113. char *getArgBuffer(U32 size)
  114. {
  115. validateBufferSize(mStart + mFunctionOffset + size);
  116. char *ret = mBuffer + mStart + mFunctionOffset;
  117. mFunctionOffset += size;
  118. return ret;
  119. }
  120. /// Clear the function offset.
  121. void clearFunctionOffset()
  122. {
  123. mFunctionOffset = 0;
  124. }
  125. /// Set a string value on the top of the stack.
  126. void setStringValue(const char *s)
  127. {
  128. if(!s)
  129. {
  130. mLen = 0;
  131. mBuffer[mStart] = 0;
  132. return;
  133. }
  134. mLen = dStrlen(s);
  135. validateBufferSize(mStart + mLen + 2);
  136. dStrcpy(mBuffer + mStart, s);
  137. }
  138. /// Get the top of the stack, as a StringTableEntry.
  139. ///
  140. /// @note Don't free this memory!
  141. inline StringTableEntry getSTValue()
  142. {
  143. return StringTable->insert(mBuffer + mStart);
  144. }
  145. /// Get an integer representation of the top of the stack.
  146. inline U32 getIntValue()
  147. {
  148. return dAtoi(mBuffer + mStart);
  149. }
  150. /// Get a float representation of the top of the stack.
  151. inline F64 getFloatValue()
  152. {
  153. return dAtof(mBuffer + mStart);
  154. }
  155. /// Get a string representation of the top of the stack.
  156. ///
  157. /// @note This returns a pointer to the actual top of the stack, be careful!
  158. inline const char *getStringValue()
  159. {
  160. return mBuffer + mStart;
  161. }
  162. /// Advance the start stack, placing a zero length string on the top.
  163. ///
  164. /// @note You should use StringStack::push, not this, if you want to
  165. /// properly push the stack.
  166. void advance()
  167. {
  168. mStartOffsets[mStartStackSize++] = mStart;
  169. mStart += mLen;
  170. mLen = 0;
  171. }
  172. /// Advance the start stack, placing a single character, null-terminated strong
  173. /// on the top.
  174. ///
  175. /// @note You should use StringStack::push, not this, if you want to
  176. /// properly push the stack.
  177. void advanceChar(char c)
  178. {
  179. mStartOffsets[mStartStackSize++] = mStart;
  180. mStart += mLen;
  181. mBuffer[mStart] = c;
  182. mBuffer[mStart+1] = 0;
  183. mStart += 1;
  184. mLen = 0;
  185. }
  186. /// Push the stack, placing a zero-length string on the top.
  187. void push()
  188. {
  189. advanceChar(0);
  190. }
  191. inline void setLen(U32 newlen)
  192. {
  193. mLen = newlen;
  194. }
  195. /// Pop the start stack.
  196. void rewind()
  197. {
  198. mStart = mStartOffsets[--mStartStackSize];
  199. mLen = dStrlen(mBuffer + mStart);
  200. }
  201. // Terminate the current string, and pop the start stack.
  202. void rewindTerminate()
  203. {
  204. mBuffer[mStart] = 0;
  205. mStart = mStartOffsets[--mStartStackSize];
  206. mLen = dStrlen(mBuffer + mStart);
  207. }
  208. /// Compare 1st and 2nd items on stack, consuming them in the process,
  209. /// and returning true if they matched, false if they didn't.
  210. U32 compare()
  211. {
  212. // Figure out the 1st and 2nd item offsets.
  213. U32 oldStart = mStart;
  214. mStart = mStartOffsets[--mStartStackSize];
  215. // Compare current and previous strings.
  216. U32 ret = !dStricmp(mBuffer + mStart, mBuffer + oldStart);
  217. // Put an empty string on the top of the stack.
  218. mLen = 0;
  219. mBuffer[mStart] = 0;
  220. return ret;
  221. }
  222. void pushFrame()
  223. {
  224. mFrameOffsets[mNumFrames++] = mStartStackSize;
  225. mStartOffsets[mStartStackSize++] = mStart;
  226. mStart += ReturnBufferSpace;
  227. validateBufferSize(0);
  228. }
  229. void popFrame()
  230. {
  231. mStartStackSize = mFrameOffsets[--mNumFrames];
  232. mStart = mStartOffsets[mStartStackSize];
  233. mLen = 0;
  234. }
  235. /// Get the arguments for a function call from the stack.
  236. void getArgcArgv(StringTableEntry name, U32 *argc, const char ***in_argv, bool popStackFrame = false);
  237. };
  238. #endif