stringStack.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. validateBufferSize(8192);
  41. validateArgBufferSize(2048);
  42. dMemset(mBuffer, '\0', mBufferSize);
  43. dMemset(mArgBuffer, '\0', mArgBufferSize);
  44. }
  45. StringStack::~StringStack()
  46. {
  47. if( mBuffer )
  48. dFree( mBuffer );
  49. if( mArgBuffer )
  50. dFree( mArgBuffer );
  51. }
  52. void StringStack::validateBufferSize(U32 size)
  53. {
  54. if(size > mBufferSize)
  55. {
  56. mBufferSize = size + 2048;
  57. mBuffer = (char *) dRealloc(mBuffer, mBufferSize);
  58. }
  59. }
  60. void StringStack::validateArgBufferSize(U32 size)
  61. {
  62. if(size > mArgBufferSize)
  63. {
  64. mArgBufferSize = size + 2048;
  65. mArgBuffer = (char *) dRealloc(mArgBuffer, mArgBufferSize);
  66. }
  67. }
  68. void StringStack::setIntValue(U32 i)
  69. {
  70. validateBufferSize(mStart + 32);
  71. dSprintf(mBuffer + mStart, 32, "%d", i);
  72. mLen = dStrlen(mBuffer + mStart);
  73. }
  74. void StringStack::setFloatValue(F64 v)
  75. {
  76. validateBufferSize(mStart + 32);
  77. dSprintf(mBuffer + mStart, 32, "%.9g", v);
  78. mLen = dStrlen(mBuffer + mStart);
  79. }
  80. char *StringStack::getReturnBuffer(U32 size)
  81. {
  82. if(size > ReturnBufferSpace)
  83. {
  84. AssertFatal(Con::isMainThread(), "Manipulating return buffer from a secondary thread!");
  85. validateArgBufferSize(size);
  86. return mArgBuffer;
  87. }
  88. else
  89. {
  90. validateBufferSize(mStart + size);
  91. return mBuffer + mStart;
  92. }
  93. }
  94. char *StringStack::getArgBuffer(U32 size)
  95. {
  96. AssertFatal(Con::isMainThread(), "Manipulating console arg buffer from a secondary thread!");
  97. validateBufferSize(mStart + mFunctionOffset + size);
  98. char *ret = mBuffer + mStart + mFunctionOffset;
  99. mFunctionOffset += size;
  100. return ret;
  101. }
  102. void StringStack::clearFunctionOffset()
  103. {
  104. //Con::printf("StringStack mFunctionOffset = 0 (from %i)", mFunctionOffset);
  105. mFunctionOffset = 0;
  106. }
  107. void StringStack::setStringValue(const char *s)
  108. {
  109. if(!s)
  110. {
  111. mLen = 0;
  112. mBuffer[mStart] = 0;
  113. return;
  114. }
  115. mLen = dStrlen(s);
  116. validateBufferSize(mStart + mLen + 2);
  117. dStrcpy(mBuffer + mStart, s, mBufferSize - mStart);
  118. }
  119. void StringStack::advance()
  120. {
  121. mStartOffsets[mStartStackSize++] = mStart;
  122. mStart += mLen;
  123. mLen = 0;
  124. }
  125. void StringStack::advanceChar(char c)
  126. {
  127. mStartOffsets[mStartStackSize++] = mStart;
  128. mStart += mLen;
  129. mBuffer[mStart] = c;
  130. mBuffer[mStart+1] = 0;
  131. mStart += 1;
  132. mLen = 0;
  133. }
  134. void StringStack::push()
  135. {
  136. advanceChar(0);
  137. }
  138. void StringStack::rewind()
  139. {
  140. mStart = mStartOffsets[--mStartStackSize];
  141. mLen = dStrlen(mBuffer + mStart);
  142. }
  143. void StringStack::rewindTerminate()
  144. {
  145. mBuffer[mStart] = 0;
  146. mStart = mStartOffsets[--mStartStackSize];
  147. mLen = dStrlen(mBuffer + mStart);
  148. }
  149. U32 StringStack::compare()
  150. {
  151. // Figure out the 1st and 2nd item offsets.
  152. U32 oldStart = mStart;
  153. mStart = mStartOffsets[--mStartStackSize];
  154. // Compare current and previous strings.
  155. U32 ret = !dStricmp(mBuffer + mStart, mBuffer + oldStart);
  156. // Put an empty string on the top of the stack.
  157. mLen = 0;
  158. mBuffer[mStart] = 0;
  159. return ret;
  160. }