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.

370 lines
9.0 KiB

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