Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

262 lines
6.6 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1999.
  5. //
  6. // File: szbuffer.cxx
  7. //
  8. // Contents: simple class for a string buffer that dynamically reallocates
  9. // space for itself as necessary
  10. //
  11. // Classes: CSzBuffer
  12. //
  13. // History: 4-22-96 stevebl Created
  14. //
  15. //----------------------------------------------------------------------------
  16. #include "becls.hxx"
  17. #pragma hdrstop
  18. #include "szbuffer.h"
  19. #define INCREMENT_BUFFER 256
  20. #define INITIAL_BUFFER (INCREMENT_BUFFER * 2);
  21. //+---------------------------------------------------------------------------
  22. //
  23. // Member: CSzBuffer::CSzBuffer
  24. //
  25. // Synopsis: constructor
  26. //
  27. // Arguments: [sz] - data for initial string (may be NULL)
  28. //
  29. // History: 4-22-96 stevebl Created
  30. //
  31. // Notes: Throughout this class the actual size of the buffer allocated
  32. // is determined by the formula
  33. // INITIAL_BUFFER + INCREMENT_BUFFER * n
  34. // where n is calculated to give the value closest to (but not
  35. // less than) the number of bytes required.
  36. // The allocated buffer is never shrunk, only grown as needed.
  37. // This keeps allocations and memory moves to a minimum.
  38. //
  39. //----------------------------------------------------------------------------
  40. CSzBuffer::CSzBuffer(const char * sz)
  41. {
  42. if (sz)
  43. {
  44. cchLength = (int) strlen(sz);
  45. cchBufSize = INITIAL_BUFFER;
  46. while ((cchLength + 1) > cchBufSize)
  47. cchBufSize += INCREMENT_BUFFER;
  48. szData = new char[cchBufSize];
  49. strcpy(szData,sz);
  50. }
  51. else
  52. {
  53. cchLength = 0;
  54. cchBufSize = INITIAL_BUFFER;
  55. szData = new char[cchBufSize];
  56. szData[0] = 0;
  57. }
  58. }
  59. //+---------------------------------------------------------------------------
  60. //
  61. // Member: CSzBuffer::CSzBuffer
  62. //
  63. // Synopsis: default constructor
  64. //
  65. // History: 4-22-96 stevebl Created
  66. //
  67. //----------------------------------------------------------------------------
  68. CSzBuffer::CSzBuffer()
  69. {
  70. cchLength = 0;
  71. cchBufSize = INITIAL_BUFFER;
  72. szData = new char[cchBufSize];
  73. szData[0] = 0;
  74. }
  75. //+---------------------------------------------------------------------------
  76. //
  77. // Member: CSzBuffer::~CSzBuffer
  78. //
  79. // Synopsis: destructor
  80. //
  81. // History: 4-22-96 stevebl Created
  82. //
  83. //----------------------------------------------------------------------------
  84. CSzBuffer::~CSzBuffer()
  85. {
  86. delete [] szData;
  87. }
  88. //+---------------------------------------------------------------------------
  89. //
  90. // Member: CSzBuffer::Set
  91. //
  92. // Synopsis: resets buffer and (optionally) initializes it
  93. //
  94. // Arguments: [sz] - data for initial string (may be NULL)
  95. //
  96. // Returns: nothing
  97. //
  98. // History: 4-22-96 stevebl Created
  99. //
  100. // Notes: as mentioned above, the size of the actual buffer does not
  101. // shrink
  102. //
  103. //----------------------------------------------------------------------------
  104. void CSzBuffer::Set(const char * sz)
  105. {
  106. cchLength = 0;
  107. szData[0] = 0;
  108. Append(sz);
  109. }
  110. //+---------------------------------------------------------------------------
  111. //
  112. // Member: CSzBuffer::Append
  113. //
  114. // Synopsis: adds data to the end of the buffer
  115. //
  116. // Arguments: [sz] - string data to add
  117. //
  118. // Returns: nothing
  119. //
  120. // History: 4-22-96 stevebl Created
  121. //
  122. //----------------------------------------------------------------------------
  123. void CSzBuffer::Append(const char * sz)
  124. {
  125. if (sz)
  126. {
  127. int cchLenSz = (int) strlen(sz);
  128. int cchNew = cchLenSz + cchLength;
  129. if ((cchNew + 1) > cchBufSize)
  130. {
  131. while ((cchNew + 1) > cchBufSize)
  132. cchBufSize += INCREMENT_BUFFER;
  133. char * szNew = new char[cchBufSize];
  134. strcpy(szNew,szData);
  135. delete [] szData;
  136. szData=szNew;
  137. }
  138. strcpy(&szData[cchLength], sz);
  139. cchLength = cchNew;
  140. }
  141. }
  142. //+---------------------------------------------------------------------------
  143. //
  144. // Member: CSzBuffer::Prepend
  145. //
  146. // Synopsis: adds data to the front of the buffer
  147. //
  148. // Arguments: [sz] - string to add
  149. //
  150. // Returns: nothing
  151. //
  152. // History: 4-22-96 stevebl Created
  153. //
  154. //----------------------------------------------------------------------------
  155. void CSzBuffer::Prepend(const char * sz)
  156. {
  157. if (sz)
  158. {
  159. int cchLenSz = (int) strlen(sz);
  160. int cchNew = cchLenSz + cchLength;
  161. if ((cchNew + 1) > cchBufSize)
  162. {
  163. while ((cchNew + 1) > cchBufSize)
  164. cchBufSize += INCREMENT_BUFFER;
  165. char * szNew = new char[cchBufSize];
  166. strcpy(szNew,szData);
  167. delete [] szData;
  168. szData=szNew;
  169. }
  170. memmove(&szData[cchLenSz], szData, cchLength);
  171. memmove(szData, sz, cchLenSz);
  172. szData[ cchNew ] = 0;
  173. cchLength = cchNew;
  174. }
  175. }
  176. //+---------------------------------------------------------------------------
  177. //
  178. // Member: CSzBuffer::Append
  179. //
  180. // Synopsis: adds a decimal integer to the end of the buffer
  181. //
  182. // Arguments: [l] - value of integer to add
  183. //
  184. // Returns: nothing
  185. //
  186. // History: 4-22-96 stevebl Created
  187. //
  188. //----------------------------------------------------------------------------
  189. void CSzBuffer::Append(const long l)
  190. {
  191. char sz[50];
  192. sprintf(sz, "%d", l);
  193. Append(sz);
  194. }
  195. //+---------------------------------------------------------------------------
  196. //
  197. // Member: CSzBuffer::Prepend
  198. //
  199. // Synopsis: adds a decimal integer to the front of the buffer
  200. //
  201. // Arguments: [l] - value of integer to add
  202. //
  203. // Returns: nothing
  204. //
  205. // History: 4-22-96 stevebl Created
  206. //
  207. //----------------------------------------------------------------------------
  208. void CSzBuffer::Prepend(const long l)
  209. {
  210. char sz[50];
  211. sprintf(sz, "%d", l);
  212. Prepend(sz);
  213. }
  214. //+---------------------------------------------------------------------------
  215. //
  216. // Member: CSzBuffer::GetData
  217. //
  218. // Returns: pointer to the string in the buffer
  219. //
  220. // History: 4-22-96 stevebl Created
  221. //
  222. //----------------------------------------------------------------------------
  223. char * CSzBuffer::GetData()
  224. {
  225. return szData;
  226. }
  227. //+---------------------------------------------------------------------------
  228. //
  229. // Member: CSzBuffer::GetLength
  230. //
  231. // Returns: length (in chars) of the string in the buffer
  232. //
  233. // History: 4-22-96 stevebl Created
  234. //
  235. //----------------------------------------------------------------------------
  236. int CSzBuffer::GetLength()
  237. {
  238. return cchLength;
  239. }