stringStack.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. #include <stdio.h>
  23. #include "console/consoleInternal.h"
  24. #include "console/stringStack.h"
  25. StringStack::StringStack()
  26. {
  27. mBufferSize = 0;
  28. mBuffer = NULL;
  29. mArgBufferSize = 0;
  30. mArgBuffer = NULL;
  31. for (U32 i = 0; i < MaxArgs; i++)
  32. mArgV[i] = "";
  33. dMemset(mFrameOffsets, 0, sizeof(mFrameOffsets));
  34. dMemset(mStartOffsets, 0, sizeof(mStartOffsets));
  35. mNumFrames = 0;
  36. mArgc = 0;
  37. mStart = 0;
  38. mLen = 0;
  39. mStartStackSize = 0;
  40. mFunctionOffset = 0;
  41. validateBufferSize(8192);
  42. validateArgBufferSize(2048);
  43. dMemset(mBuffer, '\0', mBufferSize);
  44. dMemset(mArgBuffer, '\0', mArgBufferSize);
  45. }
  46. StringStack::~StringStack()
  47. {
  48. if( mBuffer )
  49. dFree( mBuffer );
  50. if( mArgBuffer )
  51. dFree( mArgBuffer );
  52. }
  53. void StringStack::validateBufferSize(U32 size)
  54. {
  55. if(size > mBufferSize)
  56. {
  57. mBufferSize = size + 2048;
  58. mBuffer = (char *) dRealloc(mBuffer, mBufferSize);
  59. }
  60. }
  61. void StringStack::validateArgBufferSize(U32 size)
  62. {
  63. if(size > mArgBufferSize)
  64. {
  65. mArgBufferSize = size + 2048;
  66. mArgBuffer = (char *) dRealloc(mArgBuffer, mArgBufferSize);
  67. }
  68. }
  69. void StringStack::setIntValue(U32 i)
  70. {
  71. validateBufferSize(mStart + 32);
  72. dSprintf(mBuffer + mStart, 32, "%d", i);
  73. mLen = dStrlen(mBuffer + mStart);
  74. }
  75. void StringStack::setFloatValue(F64 v)
  76. {
  77. validateBufferSize(mStart + 32);
  78. dSprintf(mBuffer + mStart, 32, "%g", v);
  79. mLen = dStrlen(mBuffer + mStart);
  80. }
  81. char *StringStack::getReturnBuffer(U32 size)
  82. {
  83. if(size > ReturnBufferSpace)
  84. {
  85. AssertFatal(Con::isMainThread(), "Manipulating return buffer from a secondary thread!");
  86. validateArgBufferSize(size);
  87. return mArgBuffer;
  88. }
  89. else
  90. {
  91. validateBufferSize(mStart + size);
  92. return mBuffer + mStart;
  93. }
  94. }
  95. char *StringStack::getArgBuffer(U32 size)
  96. {
  97. AssertFatal(Con::isMainThread(), "Manipulating console arg buffer from a secondary thread!");
  98. validateBufferSize(mStart + mFunctionOffset + size);
  99. char *ret = mBuffer + mStart + mFunctionOffset;
  100. mFunctionOffset += size;
  101. return ret;
  102. }
  103. void StringStack::clearFunctionOffset()
  104. {
  105. //Con::printf("StringStack mFunctionOffset = 0 (from %i)", mFunctionOffset);
  106. mFunctionOffset = 0;
  107. }
  108. void StringStack::setStringValue(const char *s)
  109. {
  110. if(!s)
  111. {
  112. mLen = 0;
  113. mBuffer[mStart] = 0;
  114. return;
  115. }
  116. mLen = dStrlen(s);
  117. validateBufferSize(mStart + mLen + 2);
  118. dStrcpy(mBuffer + mStart, s, mBufferSize - mStart);
  119. }
  120. void StringStack::advance()
  121. {
  122. mStartOffsets[mStartStackSize++] = mStart;
  123. mStart += mLen;
  124. mLen = 0;
  125. }
  126. void StringStack::advanceChar(char c)
  127. {
  128. mStartOffsets[mStartStackSize++] = mStart;
  129. mStart += mLen;
  130. mBuffer[mStart] = c;
  131. mBuffer[mStart+1] = 0;
  132. mStart += 1;
  133. mLen = 0;
  134. }
  135. void StringStack::push()
  136. {
  137. advanceChar(0);
  138. }
  139. void StringStack::rewind()
  140. {
  141. mStart = mStartOffsets[--mStartStackSize];
  142. mLen = dStrlen(mBuffer + mStart);
  143. }
  144. void StringStack::rewindTerminate()
  145. {
  146. mBuffer[mStart] = 0;
  147. mStart = mStartOffsets[--mStartStackSize];
  148. mLen = dStrlen(mBuffer + mStart);
  149. }
  150. U32 StringStack::compare()
  151. {
  152. // Figure out the 1st and 2nd item offsets.
  153. U32 oldStart = mStart;
  154. mStart = mStartOffsets[--mStartStackSize];
  155. // Compare current and previous strings.
  156. U32 ret = !dStricmp(mBuffer + mStart, mBuffer + oldStart);
  157. // Put an empty string on the top of the stack.
  158. mLen = 0;
  159. mBuffer[mStart] = 0;
  160. return ret;
  161. }
  162. void StringStack::pushFrame()
  163. {
  164. //Con::printf("StringStack pushFrame [frame=%i, start=%i]", mNumFrames, mStartStackSize);
  165. mFrameOffsets[mNumFrames++] = mStartStackSize;
  166. mStartOffsets[mStartStackSize++] = mStart;
  167. mStart += ReturnBufferSpace;
  168. validateBufferSize(0);
  169. }
  170. void StringStack::popFrame()
  171. {
  172. //Con::printf("StringStack popFrame [frame=%i, start=%i]", mNumFrames, mStartStackSize);
  173. mStartStackSize = mFrameOffsets[--mNumFrames];
  174. mStart = mStartOffsets[mStartStackSize];
  175. mLen = 0;
  176. }
  177. void StringStack::clearFrames()
  178. {
  179. //Con::printf("StringStack clearFrames");
  180. mNumFrames = 0;
  181. mStart = 0;
  182. mLen = 0;
  183. mStartStackSize = 0;
  184. mFunctionOffset = 0;
  185. }