Leaked source code of windows server 2003
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.

245 lines
6.1 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1992.
  5. //
  6. // File: cbuffer.h
  7. //
  8. // Contents: CHAR buffer definitions
  9. //
  10. // History: 02-16-93 SethuR -- Implemented
  11. // 07-28-94 AlokS -- Added more methods
  12. // 12-09-97 MilanS -- Ported to Exchange
  13. // Notes:
  14. //
  15. //--------------------------------------------------------------------------
  16. #ifndef __CBUFFER_H__
  17. #define __CBUFFER_H__
  18. //+---------------------------------------------------------------------
  19. //
  20. // Class: CCHARBuffer
  21. //
  22. // Purpose: A CHAR buffer
  23. //
  24. // History:
  25. //
  26. // Notes: Very often we encounter the case in string manipulation wherein
  27. // the length of the string is less than some value most of the time
  28. // (99%). However, in order to reliably with the very rare case we
  29. // are forced to either allocate the string on the heap or alternatively
  30. // go through some bizarre code that avoids the heap allocation in the
  31. // common case. This class is an abstraction of a WCHAR buffer and its
  32. // implementation is an attempt at hiding the detail from all clients.
  33. //
  34. // As it is designed it is an ideal candidate for a temporary buffer
  35. // for string manipulation.
  36. //
  37. //----------------------------------------------------------------------
  38. #define MAX_CHAR_BUFFER_SIZE 260 // long enough to cover all path names
  39. class CCHARBuffer
  40. {
  41. public:
  42. inline CCHARBuffer(ULONG cwBuffer = 0);
  43. inline ~CCHARBuffer();
  44. inline DWORD Size();
  45. inline PCHAR ReAlloc(DWORD cwBuffer = MAX_CHAR_BUFFER_SIZE);
  46. inline void Set(PCHAR pszFrom);
  47. inline operator PCHAR ();
  48. inline operator PCHAR () const;
  49. inline void operator =(PCHAR pszFrom)
  50. {
  51. Set(pszFrom);
  52. };
  53. private:
  54. DWORD _cBuffer;
  55. PCHAR pchBuffer; // buffer ptr;
  56. CHAR _achBuffer[MAX_CHAR_BUFFER_SIZE];
  57. };
  58. //+---------------------------------------------------------------------------
  59. //
  60. // Member: CCHARBuffer::CCHARBuffer, inline public
  61. //
  62. // Synopsis: Constructor
  63. //
  64. // Arguments: [cBuffer] -- desired buffer length.
  65. //
  66. // History: 02-17-93 SethuR Created
  67. //
  68. // Notes:
  69. //
  70. //----------------------------------------------------------------------------
  71. inline CCHARBuffer::CCHARBuffer(ULONG cBuffer) :
  72. pchBuffer(NULL),
  73. _cBuffer(cBuffer)
  74. {
  75. if (_cBuffer > MAX_CHAR_BUFFER_SIZE)
  76. {
  77. pchBuffer = new CHAR[_cBuffer];
  78. }
  79. else if (_cBuffer > 0)
  80. {
  81. pchBuffer = _achBuffer;
  82. }
  83. }
  84. //+---------------------------------------------------------------------------
  85. //
  86. // Member: CCHARBuffer::~CCHARBuffer, inline public
  87. //
  88. // Synopsis: Destructor
  89. //
  90. // History: 02-17-93 SethuR Created
  91. //
  92. // Notes:
  93. //
  94. //----------------------------------------------------------------------------
  95. inline CCHARBuffer::~CCHARBuffer()
  96. {
  97. if (_cBuffer > MAX_CHAR_BUFFER_SIZE)
  98. {
  99. delete pchBuffer;
  100. }
  101. }
  102. //+---------------------------------------------------------------------------
  103. //
  104. // Member: CCHARBuffer::Size, inline public
  105. //
  106. // Synopsis: Retrieve the size of the buffer
  107. //
  108. // Returns: the size of the buffer as a DWORD
  109. //
  110. // History: 02-17-93 SethuR Created
  111. //
  112. // Notes:
  113. //
  114. //----------------------------------------------------------------------------
  115. inline DWORD CCHARBuffer::Size()
  116. {
  117. return _cBuffer;
  118. }
  119. //+---------------------------------------------------------------------------
  120. //
  121. // Member: CCHARBuffer::ReAlloc, inline public
  122. //
  123. // Synopsis: Reallocates the buffer to accomdate the newly specified size
  124. //
  125. // Arguments: [cBuffer] -- the desired buffer size
  126. //
  127. // Returns: the ptr to the buffer (PCHAR)
  128. //
  129. // History: 02-17-93 SethuR Created
  130. //
  131. // Notes:
  132. //
  133. //----------------------------------------------------------------------------
  134. inline PCHAR CCHARBuffer::ReAlloc(DWORD cBuffer)
  135. {
  136. if (_cBuffer > MAX_CHAR_BUFFER_SIZE)
  137. {
  138. delete pchBuffer;
  139. }
  140. if ((_cBuffer = cBuffer) > MAX_CHAR_BUFFER_SIZE)
  141. {
  142. pchBuffer = new CHAR[_cBuffer];
  143. }
  144. else if (_cBuffer > 0)
  145. {
  146. pchBuffer = _achBuffer;
  147. }
  148. else if (_cBuffer == 0)
  149. {
  150. pchBuffer = NULL;
  151. }
  152. return pchBuffer;
  153. }
  154. //+---------------------------------------------------------------------------
  155. //
  156. // Member: CCHARBuffer::operator PCHAR (), inline public
  157. //
  158. // Synopsis: casting operator to accomdate syntactic sugaring
  159. //
  160. // Returns: the ptr to the buffer (PCHAR)
  161. //
  162. // History: 02-17-93 SethuR Created
  163. //
  164. // Notes:
  165. //
  166. //----------------------------------------------------------------------------
  167. inline CCHARBuffer::operator PCHAR ()
  168. {
  169. return (PCHAR)pchBuffer;
  170. }
  171. inline CCHARBuffer::operator PCHAR () const
  172. {
  173. return (PCHAR)pchBuffer;
  174. }
  175. //+---------------------------------------------------------------------------
  176. //
  177. // Member: CCHARBuffer::Set, inline public
  178. //
  179. // Synopsis: Copies the string to internal buffer. Reallocates
  180. // in internal buffer, if necessary
  181. //
  182. // Arguments: [pszFrom] -- Pointer to the string
  183. //
  184. // Returns: -none-
  185. //
  186. // History: 07-28-94 AlokS Created
  187. //
  188. // Notes:
  189. //
  190. //----------------------------------------------------------------------------
  191. inline VOID CCHARBuffer::Set(PCHAR pszFrom)
  192. {
  193. if (pszFrom==NULL)
  194. {
  195. if (_cBuffer > MAX_CHAR_BUFFER_SIZE)
  196. {
  197. delete pchBuffer;
  198. }
  199. _cBuffer=0;
  200. pchBuffer = NULL;
  201. }
  202. else if (*pszFrom)
  203. {
  204. DWORD len = strlen(pszFrom)+1;
  205. if ( len > _cBuffer)
  206. {
  207. (void)ReAlloc (len);
  208. }
  209. // Now copy
  210. if (pchBuffer != NULL) // In case ReAlloc fails
  211. memcpy(pchBuffer, pszFrom, len);
  212. }
  213. else
  214. {
  215. *pchBuffer=L'\0';
  216. }
  217. return;
  218. }
  219. #endif // __CBUFFER_H__