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.

337 lines
7.9 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(PWCHAR pwszFrom);
  47. inline void Set(PCHAR pszFrom);
  48. inline void Cat(PCHAR pszPlus);
  49. inline operator PCHAR ();
  50. inline operator PCHAR () const;
  51. inline void operator =(PWCHAR pwszFrom)
  52. {
  53. Set(pwszFrom);
  54. };
  55. inline void operator =(PCHAR pszFrom)
  56. {
  57. Set(pszFrom);
  58. };
  59. inline void operator +=(PCHAR pszPlus)
  60. {
  61. Cat(pszPlus);
  62. };
  63. private:
  64. DWORD _cBuffer;
  65. PCHAR pchBuffer; // buffer ptr;
  66. CHAR _achBuffer[MAX_CHAR_BUFFER_SIZE];
  67. };
  68. //+---------------------------------------------------------------------------
  69. //
  70. // Member: CCHARBuffer::CCHARBuffer, inline public
  71. //
  72. // Synopsis: Constructor
  73. //
  74. // Arguments: [cBuffer] -- desired buffer length.
  75. //
  76. // History: 02-17-93 SethuR Created
  77. //
  78. // Notes:
  79. //
  80. //----------------------------------------------------------------------------
  81. inline CCHARBuffer::CCHARBuffer(ULONG cBuffer) :
  82. pchBuffer(NULL),
  83. _cBuffer(cBuffer)
  84. {
  85. if (_cBuffer > MAX_CHAR_BUFFER_SIZE)
  86. {
  87. pchBuffer = new CHAR[_cBuffer];
  88. }
  89. else if (_cBuffer > 0)
  90. {
  91. pchBuffer = _achBuffer;
  92. }
  93. }
  94. //+---------------------------------------------------------------------------
  95. //
  96. // Member: CCHARBuffer::~CCHARBuffer, inline public
  97. //
  98. // Synopsis: Destructor
  99. //
  100. // History: 02-17-93 SethuR Created
  101. //
  102. // Notes:
  103. //
  104. //----------------------------------------------------------------------------
  105. inline CCHARBuffer::~CCHARBuffer()
  106. {
  107. if (_cBuffer > MAX_CHAR_BUFFER_SIZE)
  108. {
  109. delete pchBuffer;
  110. }
  111. }
  112. //+---------------------------------------------------------------------------
  113. //
  114. // Member: CCHARBuffer::Size, inline public
  115. //
  116. // Synopsis: Retrieve the size of the buffer
  117. //
  118. // Returns: the size of the buffer as a DWORD
  119. //
  120. // History: 02-17-93 SethuR Created
  121. //
  122. // Notes:
  123. //
  124. //----------------------------------------------------------------------------
  125. inline DWORD CCHARBuffer::Size()
  126. {
  127. return _cBuffer;
  128. }
  129. //+---------------------------------------------------------------------------
  130. //
  131. // Member: CCHARBuffer::ReAlloc, inline public
  132. //
  133. // Synopsis: Reallocates the buffer to accomdate the newly specified size
  134. //
  135. // Arguments: [cBuffer] -- the desired buffer size
  136. //
  137. // Returns: the ptr to the buffer (PCHAR)
  138. //
  139. // History: 02-17-93 SethuR Created
  140. //
  141. // Notes:
  142. //
  143. //----------------------------------------------------------------------------
  144. inline PCHAR CCHARBuffer::ReAlloc(DWORD cBuffer)
  145. {
  146. if (_cBuffer > MAX_CHAR_BUFFER_SIZE)
  147. {
  148. delete pchBuffer;
  149. }
  150. if ((_cBuffer = cBuffer) > MAX_CHAR_BUFFER_SIZE)
  151. {
  152. pchBuffer = new CHAR[_cBuffer];
  153. }
  154. else if (_cBuffer > 0)
  155. {
  156. pchBuffer = _achBuffer;
  157. }
  158. else if (_cBuffer == 0)
  159. {
  160. pchBuffer = NULL;
  161. }
  162. return pchBuffer;
  163. }
  164. //+---------------------------------------------------------------------------
  165. //
  166. // Member: CCHARBuffer::operator PCHAR (), inline public
  167. //
  168. // Synopsis: casting operator to accomdate syntactic sugaring
  169. //
  170. // Returns: the ptr to the buffer (PCHAR)
  171. //
  172. // History: 02-17-93 SethuR Created
  173. //
  174. // Notes:
  175. //
  176. //----------------------------------------------------------------------------
  177. inline CCHARBuffer::operator PCHAR ()
  178. {
  179. return (PCHAR)pchBuffer;
  180. }
  181. inline CCHARBuffer::operator PCHAR () const
  182. {
  183. return (PCHAR)pchBuffer;
  184. }
  185. //+---------------------------------------------------------------------------
  186. //
  187. // Member: CCHARBuffer::Set, inline public
  188. //
  189. // Synopsis: Copies the string to internal buffer. Reallocates
  190. // in internal buffer, if necessary
  191. //
  192. // Arguments: [pwszFrom] -- Pointer to the string
  193. //
  194. // Returns: -none-
  195. //
  196. // History: 07-28-94 AlokS Created
  197. //
  198. // Notes:
  199. //
  200. //----------------------------------------------------------------------------
  201. inline VOID CCHARBuffer::Set(PWCHAR pwszFrom)
  202. {
  203. if (pwszFrom==NULL)
  204. {
  205. if (_cBuffer > MAX_CHAR_BUFFER_SIZE)
  206. {
  207. delete pchBuffer;
  208. }
  209. _cBuffer=0;
  210. pchBuffer = NULL;
  211. }
  212. else if (*pwszFrom)
  213. {
  214. DWORD len = wcslen(pwszFrom)+1;
  215. if (len > _cBuffer)
  216. {
  217. (void)ReAlloc (len);
  218. }
  219. // Now copy
  220. wcstombs(pchBuffer, pwszFrom, len);
  221. }
  222. else
  223. {
  224. *pchBuffer='\0';
  225. }
  226. return;
  227. }
  228. //+---------------------------------------------------------------------------
  229. //
  230. // Member: CCHARBuffer::Set, inline public
  231. //
  232. // Synopsis: Copies the string to internal buffer. Reallocates
  233. // in internal buffer, if necessary
  234. //
  235. // Arguments: [pszFrom] -- Pointer to the string
  236. //
  237. // Returns: -none-
  238. //
  239. // History: 07-28-94 AlokS Created
  240. //
  241. // Notes:
  242. //
  243. //----------------------------------------------------------------------------
  244. inline VOID CCHARBuffer::Set(PCHAR pszFrom)
  245. {
  246. if (pszFrom==NULL)
  247. {
  248. if (_cBuffer > MAX_CHAR_BUFFER_SIZE)
  249. {
  250. delete pchBuffer;
  251. }
  252. _cBuffer=0;
  253. pchBuffer = NULL;
  254. }
  255. else if (*pszFrom)
  256. {
  257. DWORD len = strlen(pszFrom)+1;
  258. if ( len > _cBuffer)
  259. {
  260. (void)ReAlloc (len);
  261. }
  262. // Now copy
  263. memcpy(pchBuffer, pszFrom, len);
  264. }
  265. else
  266. {
  267. *pchBuffer=L'\0';
  268. }
  269. return;
  270. }
  271. //+---------------------------------------------------------------------------
  272. //
  273. // Member: CCHARBuffer::Cat, inline public
  274. //
  275. // Synopsis: Concatnates the string to internal buffer. Reallocates
  276. // in internal buffer, if necessary
  277. //
  278. // Arguments: [pszPlus] -- Pointer to the string
  279. //
  280. // Returns: -none-
  281. //
  282. // History: 07-28-94 AlokS Created
  283. //
  284. // Notes:
  285. //
  286. //----------------------------------------------------------------------------
  287. inline VOID CCHARBuffer::Cat(PCHAR pszFrom)
  288. {
  289. DWORD len1 = strlen(pchBuffer),
  290. len2 = strlen(pszFrom),
  291. len3 = len1+len2 + 1;
  292. if ( len3 > MAX_CHAR_BUFFER_SIZE)
  293. {
  294. PCHAR ptr = new CHAR [len3];
  295. memcpy(ptr, pchBuffer, len1);
  296. if (_cBuffer > MAX_CHAR_BUFFER_SIZE)
  297. {
  298. delete pchBuffer;
  299. }
  300. pchBuffer = ptr;
  301. }
  302. memcpy( ((LPWSTR)(pchBuffer)+len1), pszFrom, (len2+1) * sizeof(CHAR));
  303. _cBuffer = len3;
  304. }
  305. #endif // __CBUFFER_H__