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.

364 lines
9.2 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. if (_cwBuffer > MAX_CHAR_BUFFER_SIZE)
  85. {
  86. _pwchBuffer = new WCHAR[_cwBuffer];
  87. }
  88. else if (_cwBuffer > 0)
  89. {
  90. _pwchBuffer = _awchBuffer;
  91. }
  92. }
  93. //+---------------------------------------------------------------------------
  94. //
  95. // Member: CWCHARBuffer::~CWCHARBuffer, inline public
  96. //
  97. // Synopsis: Destructor
  98. //
  99. // History: 02-17-93 SethuR Created
  100. //
  101. // Notes:
  102. //
  103. //----------------------------------------------------------------------------
  104. inline CWCHARBuffer::~CWCHARBuffer()
  105. {
  106. if (_cwBuffer > MAX_CHAR_BUFFER_SIZE &&
  107. _pwchBuffer != NULL &&
  108. _pwchBuffer != _awchBuffer)
  109. {
  110. delete _pwchBuffer;
  111. }
  112. _cwBuffer=0;
  113. _pwchBuffer = NULL;
  114. }
  115. //+---------------------------------------------------------------------------
  116. //
  117. // Member: CWCHARBuffer::Size, inline public
  118. //
  119. // Synopsis: Retrieve the size of the buffer
  120. //
  121. // Returns: the size of the buffer as a DWORD
  122. //
  123. // History: 02-17-93 SethuR Created
  124. //
  125. // Notes:
  126. //
  127. //----------------------------------------------------------------------------
  128. inline DWORD CWCHARBuffer::Size()
  129. {
  130. return _cwBuffer;
  131. }
  132. //+---------------------------------------------------------------------------
  133. //
  134. // Member: CWCHARBuffer::ReAlloc, inline public
  135. //
  136. // Synopsis: Reallocates the buffer to accomdate the newly specified size
  137. //
  138. // Arguments: [cwBuffer] -- the desired buffer size
  139. //
  140. // Returns: the ptr to the buffer (PWCHAR)
  141. //
  142. // History: 02-17-93 SethuR Created
  143. //
  144. // Notes:
  145. //
  146. //----------------------------------------------------------------------------
  147. inline PWCHAR CWCHARBuffer::ReAlloc(DWORD cwBuffer)
  148. {
  149. if (_cwBuffer > MAX_CHAR_BUFFER_SIZE &&
  150. _pwchBuffer != NULL &&
  151. _pwchBuffer != _awchBuffer)
  152. {
  153. delete _pwchBuffer;
  154. }
  155. _cwBuffer=0;
  156. _pwchBuffer = NULL;
  157. if ((_cwBuffer = cwBuffer) > MAX_CHAR_BUFFER_SIZE)
  158. {
  159. _pwchBuffer = new WCHAR[_cwBuffer];
  160. }
  161. else if (_cwBuffer > 0)
  162. {
  163. _pwchBuffer = _awchBuffer;
  164. }
  165. else if (_cwBuffer == 0)
  166. {
  167. _pwchBuffer = NULL;
  168. }
  169. return _pwchBuffer;
  170. }
  171. //+---------------------------------------------------------------------------
  172. //
  173. // Member: CWCHARBuffer::operator PWCHAR (), inline public
  174. //
  175. // Synopsis: casting operator to accomdate syntactic sugaring
  176. //
  177. // Returns: the ptr to the buffer (PWCHAR)
  178. //
  179. // History: 02-17-93 SethuR Created
  180. //
  181. // Notes:
  182. //
  183. //----------------------------------------------------------------------------
  184. inline CWCHARBuffer::operator PWCHAR ()
  185. {
  186. return (PWCHAR)_pwchBuffer;
  187. }
  188. inline CWCHARBuffer::operator PWCHAR () const
  189. {
  190. return (PWCHAR)_pwchBuffer;
  191. }
  192. //+---------------------------------------------------------------------------
  193. //
  194. // Member: CWCHARBuffer::Set, inline public
  195. //
  196. // Synopsis: Copies the string to internal buffer. Reallocates
  197. // in internal buffer, if necessary
  198. //
  199. // Arguments: [pwszFrom] -- Pointer to the string
  200. //
  201. // Returns: -none-
  202. //
  203. // History: 07-28-94 AlokS Created
  204. //
  205. // Notes:
  206. //
  207. //----------------------------------------------------------------------------
  208. inline VOID CWCHARBuffer::Set(PWCHAR pwszFrom)
  209. {
  210. if (pwszFrom==NULL)
  211. {
  212. if (_cwBuffer > MAX_CHAR_BUFFER_SIZE &&
  213. _pwchBuffer != NULL &&
  214. _pwchBuffer != _awchBuffer)
  215. {
  216. delete _pwchBuffer;
  217. }
  218. _cwBuffer=0;
  219. _pwchBuffer = NULL;
  220. }
  221. else if (*pwszFrom)
  222. {
  223. DWORD len = wcslen(pwszFrom)+1;
  224. if (len > _cwBuffer)
  225. {
  226. (void)ReAlloc (len);
  227. }
  228. if (_pwchBuffer != NULL) {
  229. // Now copy
  230. memcpy(_pwchBuffer, pwszFrom, sizeof(WCHAR)*len);
  231. }
  232. }
  233. else
  234. {
  235. if (_pwchBuffer != NULL) {
  236. *_pwchBuffer=L'\0';
  237. }
  238. }
  239. return;
  240. }
  241. //+---------------------------------------------------------------------------
  242. //
  243. // Member: CWCHARBuffer::Set, inline public
  244. //
  245. // Synopsis: Copies the string to internal buffer. Reallocates
  246. // in internal buffer, if necessary
  247. //
  248. // Arguments: [pszFrom] -- Pointer to the string
  249. //
  250. // Returns: -none-
  251. //
  252. // History: 07-28-94 AlokS Created
  253. //
  254. // Notes:
  255. //
  256. //----------------------------------------------------------------------------
  257. inline VOID CWCHARBuffer::Set(PCHAR pszFrom)
  258. {
  259. if (pszFrom==NULL)
  260. {
  261. if (_cwBuffer > MAX_CHAR_BUFFER_SIZE &&
  262. _pwchBuffer != NULL &&
  263. _pwchBuffer != _awchBuffer)
  264. {
  265. delete _pwchBuffer;
  266. }
  267. _cwBuffer=0;
  268. _pwchBuffer = NULL;
  269. }
  270. else if (*pszFrom)
  271. {
  272. DWORD len = strlen(pszFrom)+1;
  273. if ( len > _cwBuffer)
  274. {
  275. (void)ReAlloc (len);
  276. }
  277. if (_pwchBuffer != NULL) {
  278. // Now copy
  279. mbstowcs(_pwchBuffer, pszFrom, len);
  280. }
  281. }
  282. else
  283. {
  284. if (_pwchBuffer != NULL) {
  285. *_pwchBuffer=L'\0';
  286. }
  287. }
  288. return;
  289. }
  290. //+---------------------------------------------------------------------------
  291. //
  292. // Member: CWCHARBuffer::Cat, inline public
  293. //
  294. // Synopsis: Concatnates the string to internal buffer. Reallocates
  295. // in internal buffer, if necessary
  296. //
  297. // Arguments: [pwszPlus] -- Pointer to the string
  298. //
  299. // Returns: -none-
  300. //
  301. // History: 07-28-94 AlokS Created
  302. //
  303. // Notes:
  304. //
  305. //----------------------------------------------------------------------------
  306. inline VOID CWCHARBuffer::Cat(PWCHAR pwszFrom)
  307. {
  308. DWORD len1 = wcslen(_pwchBuffer),
  309. len2 = wcslen(pwszFrom),
  310. len3 = len1+len2 + 1;
  311. if ( len3 > MAX_CHAR_BUFFER_SIZE)
  312. {
  313. PWCHAR ptr = new WCHAR [len3];
  314. if (ptr != NULL && _pwchBuffer != NULL) {
  315. memcpy(ptr, _pwchBuffer, len1*sizeof(WCHAR));
  316. }
  317. if (_cwBuffer > MAX_CHAR_BUFFER_SIZE &&
  318. _pwchBuffer != NULL &&
  319. _pwchBuffer != _awchBuffer)
  320. {
  321. delete _pwchBuffer;
  322. }
  323. _cwBuffer = len3;
  324. _pwchBuffer = ptr;
  325. }
  326. if (_pwchBuffer != NULL) {
  327. memcpy( ((LPWSTR)(_pwchBuffer)+len1), pwszFrom, (len2+1) * sizeof(WCHAR));
  328. }
  329. }
  330. #endif // __WCBUFFER_HXX__