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.

341 lines
8.5 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. // 01-18-99 MikeSwa -- Fixed Cat()
  14. // Notes:
  15. //
  16. //--------------------------------------------------------------------------
  17. #ifndef __CBUFFER_H__
  18. #define __CBUFFER_H__
  19. //+---------------------------------------------------------------------
  20. //
  21. // Class: CCHARBuffer
  22. //
  23. // Purpose: A CHAR buffer
  24. //
  25. // History:
  26. //
  27. // Notes: Very often we encounter the case in string manipulation wherein
  28. // the length of the string is less than some value most of the time
  29. // (99%). However, in order to reliably with the very rare case we
  30. // are forced to either allocate the string on the heap or alternatively
  31. // go through some bizarre code that avoids the heap allocation in the
  32. // common case. This class is an abstraction of a WCHAR buffer and its
  33. // implementation is an attempt at hiding the detail from all clients.
  34. //
  35. // As it is designed it is an ideal candidate for a temporary buffer
  36. // for string manipulation.
  37. //
  38. //----------------------------------------------------------------------
  39. #define MAX_CHAR_BUFFER_SIZE 260 // long enough to cover all path names
  40. class CCHARBuffer
  41. {
  42. public:
  43. inline CCHARBuffer(ULONG cwBuffer = 0);
  44. inline ~CCHARBuffer();
  45. inline DWORD Size();
  46. inline PCHAR ReAlloc(DWORD cwBuffer = MAX_CHAR_BUFFER_SIZE);
  47. inline void Set(PWCHAR pwszFrom);
  48. inline void Set(PCHAR pszFrom);
  49. inline BOOL Cat(PCHAR pszPlus);
  50. inline operator PCHAR ();
  51. inline operator PCHAR () const;
  52. inline void operator =(PWCHAR pwszFrom)
  53. {
  54. Set(pwszFrom);
  55. };
  56. inline void operator =(PCHAR pszFrom)
  57. {
  58. Set(pszFrom);
  59. };
  60. private:
  61. DWORD _cBuffer;
  62. PCHAR pchBuffer; // buffer ptr;
  63. CHAR _achBuffer[MAX_CHAR_BUFFER_SIZE];
  64. };
  65. //+---------------------------------------------------------------------------
  66. //
  67. // Member: CCHARBuffer::CCHARBuffer, inline public
  68. //
  69. // Synopsis: Constructor
  70. //
  71. // Arguments: [cBuffer] -- desired buffer length.
  72. //
  73. // History: 02-17-93 SethuR Created
  74. //
  75. // Notes:
  76. //
  77. //----------------------------------------------------------------------------
  78. inline CCHARBuffer::CCHARBuffer(ULONG cBuffer) :
  79. pchBuffer(NULL),
  80. _cBuffer(cBuffer)
  81. {
  82. if (_cBuffer > MAX_CHAR_BUFFER_SIZE)
  83. {
  84. pchBuffer = new CHAR[_cBuffer];
  85. }
  86. else if (_cBuffer > 0)
  87. {
  88. pchBuffer = _achBuffer;
  89. }
  90. }
  91. //+---------------------------------------------------------------------------
  92. //
  93. // Member: CCHARBuffer::~CCHARBuffer, inline public
  94. //
  95. // Synopsis: Destructor
  96. //
  97. // History: 02-17-93 SethuR Created
  98. //
  99. // Notes:
  100. //
  101. //----------------------------------------------------------------------------
  102. inline CCHARBuffer::~CCHARBuffer()
  103. {
  104. if (_cBuffer > MAX_CHAR_BUFFER_SIZE)
  105. {
  106. delete pchBuffer;
  107. }
  108. }
  109. //+---------------------------------------------------------------------------
  110. //
  111. // Member: CCHARBuffer::Size, inline public
  112. //
  113. // Synopsis: Retrieve the size of the buffer
  114. //
  115. // Returns: the size of the buffer as a DWORD
  116. //
  117. // History: 02-17-93 SethuR Created
  118. //
  119. // Notes:
  120. //
  121. //----------------------------------------------------------------------------
  122. inline DWORD CCHARBuffer::Size()
  123. {
  124. return _cBuffer;
  125. }
  126. //+---------------------------------------------------------------------------
  127. //
  128. // Member: CCHARBuffer::ReAlloc, inline public
  129. //
  130. // Synopsis: Reallocates the buffer to accomdate the newly specified size
  131. //
  132. // Arguments: [cBuffer] -- the desired buffer size
  133. //
  134. // Returns: the ptr to the buffer (PCHAR)
  135. //
  136. // History: 02-17-93 SethuR Created
  137. //
  138. // Notes:
  139. //
  140. //----------------------------------------------------------------------------
  141. inline PCHAR CCHARBuffer::ReAlloc(DWORD cBuffer)
  142. {
  143. if (_cBuffer > MAX_CHAR_BUFFER_SIZE)
  144. {
  145. delete pchBuffer;
  146. }
  147. if ((_cBuffer = cBuffer) > MAX_CHAR_BUFFER_SIZE)
  148. {
  149. pchBuffer = new CHAR[_cBuffer];
  150. }
  151. else if (_cBuffer > 0)
  152. {
  153. pchBuffer = _achBuffer;
  154. }
  155. else if (_cBuffer == 0)
  156. {
  157. pchBuffer = NULL;
  158. }
  159. return pchBuffer;
  160. }
  161. //+---------------------------------------------------------------------------
  162. //
  163. // Member: CCHARBuffer::operator PCHAR (), inline public
  164. //
  165. // Synopsis: casting operator to accomdate syntactic sugaring
  166. //
  167. // Returns: the ptr to the buffer (PCHAR)
  168. //
  169. // History: 02-17-93 SethuR Created
  170. //
  171. // Notes:
  172. //
  173. //----------------------------------------------------------------------------
  174. inline CCHARBuffer::operator PCHAR ()
  175. {
  176. return (PCHAR)pchBuffer;
  177. }
  178. inline CCHARBuffer::operator PCHAR () const
  179. {
  180. return (PCHAR)pchBuffer;
  181. }
  182. //+---------------------------------------------------------------------------
  183. //
  184. // Member: CCHARBuffer::Set, inline public
  185. //
  186. // Synopsis: Copies the string to internal buffer. Reallocates
  187. // in internal buffer, if necessary
  188. //
  189. // Arguments: [pwszFrom] -- Pointer to the string
  190. //
  191. // Returns: -none-
  192. //
  193. // History: 07-28-94 AlokS Created
  194. //
  195. // Notes:
  196. //
  197. //----------------------------------------------------------------------------
  198. inline VOID CCHARBuffer::Set(PWCHAR pwszFrom)
  199. {
  200. if (pwszFrom==NULL)
  201. {
  202. if (_cBuffer > MAX_CHAR_BUFFER_SIZE)
  203. {
  204. delete pchBuffer;
  205. }
  206. _cBuffer=0;
  207. pchBuffer = NULL;
  208. }
  209. else if (*pwszFrom)
  210. {
  211. DWORD len = wcslen(pwszFrom)+1;
  212. if (len > _cBuffer)
  213. {
  214. (void)ReAlloc (len);
  215. }
  216. // Now copy
  217. wcstombs(pchBuffer, pwszFrom, len);
  218. }
  219. else
  220. {
  221. *pchBuffer='\0';
  222. }
  223. return;
  224. }
  225. //+---------------------------------------------------------------------------
  226. //
  227. // Member: CCHARBuffer::Set, inline public
  228. //
  229. // Synopsis: Copies the string to internal buffer. Reallocates
  230. // in internal buffer, if necessary
  231. //
  232. // Arguments: [pszFrom] -- Pointer to the string
  233. //
  234. // Returns: -none-
  235. //
  236. // History: 07-28-94 AlokS Created
  237. //
  238. // Notes:
  239. //
  240. //----------------------------------------------------------------------------
  241. inline VOID CCHARBuffer::Set(PCHAR pszFrom)
  242. {
  243. if (pszFrom==NULL)
  244. {
  245. if (_cBuffer > MAX_CHAR_BUFFER_SIZE)
  246. {
  247. delete pchBuffer;
  248. }
  249. _cBuffer=0;
  250. pchBuffer = NULL;
  251. }
  252. else if (*pszFrom)
  253. {
  254. DWORD len = strlen(pszFrom)+1;
  255. if ( len > _cBuffer)
  256. {
  257. (void)ReAlloc (len);
  258. }
  259. // Now copy
  260. memcpy(pchBuffer, pszFrom, len);
  261. }
  262. else
  263. {
  264. *pchBuffer=L'\0';
  265. }
  266. return;
  267. }
  268. //+---------------------------------------------------------------------------
  269. //
  270. // Member: CCHARBuffer::Cat, inline public
  271. //
  272. // Synopsis: Concatnates the string to internal buffer. Reallocates
  273. // in internal buffer, if necessary
  274. //
  275. // Arguments: [pszPlus] -- Pointer to the string
  276. //
  277. // Returns: TRUE on success
  278. // FALSE if memory allocation failed
  279. //
  280. // History: 07-28-94 AlokS Created
  281. // 01-18-99 Mikeswa Fixed AV in checkin allocated ptr
  282. //
  283. // Notes:
  284. //
  285. //----------------------------------------------------------------------------
  286. inline BOOL CCHARBuffer::Cat(PCHAR pszFrom)
  287. {
  288. DWORD len1 = strlen(pchBuffer),
  289. len2 = strlen(pszFrom),
  290. len3 = len1+len2 + 1;
  291. if ( len3 > MAX_CHAR_BUFFER_SIZE)
  292. {
  293. PCHAR ptr = new CHAR [len3];
  294. //Avoid AV'ing
  295. if (!ptr)
  296. return FALSE;
  297. memcpy(ptr, pchBuffer, len1);
  298. if (_cBuffer > MAX_CHAR_BUFFER_SIZE)
  299. {
  300. delete pchBuffer;
  301. }
  302. pchBuffer = ptr;
  303. }
  304. memcpy( ((LPWSTR)(pchBuffer)+len1), pszFrom, (len2+1) * sizeof(CHAR));
  305. _cBuffer = len3;
  306. return TRUE;
  307. }
  308. #endif // __CBUFFER_H__