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.

510 lines
13 KiB

  1. //-----------------------------------------------------------------------------
  2. //
  3. // File: passtr.inl
  4. // Copyright (C) 1994-1997 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. //
  8. //
  9. //-----------------------------------------------------------------------------
  10. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  11. //
  12. // Default constructor for a Pascal string. Sets the length to zero, with
  13. // no storage.
  14. //
  15. //-----------------------------------------------------------------------------
  16. inline
  17. CPascalString::CPascalString()
  18. {
  19. //
  20. // The string data class is initialized by it's constructor.
  21. //
  22. LTASSERT(m_blbData.GetBlobSize() == 0);
  23. DEBUGONLY(++m_UsageCounter);
  24. }
  25. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  26. //
  27. // Casting operator to convert a CPascalString to a blob.
  28. //
  29. //-----------------------------------------------------------------------------
  30. inline
  31. CPascalString::operator const CLocCOWBlob &(void)
  32. const
  33. {
  34. return m_blbData;
  35. }
  36. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  37. //
  38. // Assignment operator - CPascalString to CPascalString.
  39. //
  40. //-----------------------------------------------------------------------------
  41. inline
  42. const CPascalString & // Allows a=b=c;
  43. CPascalString::operator=(
  44. const CPascalString &pstrSource) // Source string
  45. {
  46. DEBUGONLY(m_StorageCounter -= m_blbData.GetBlobSize());
  47. m_blbData = ((const CLocCOWBlob &)pstrSource);
  48. DEBUGONLY(m_StorageCounter += m_blbData.GetBlobSize());
  49. return *this;
  50. }
  51. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  52. //
  53. // Copy constructor for CPascalString's
  54. //
  55. //-----------------------------------------------------------------------------
  56. inline
  57. CPascalString::CPascalString(
  58. const CPascalString &pstrSource)
  59. {
  60. LTASSERT(pstrSource.m_blbData.GetWriteCount() == 0);
  61. operator=(pstrSource);
  62. DEBUGONLY(++m_UsageCounter);
  63. }
  64. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  65. //
  66. // Assignment operator - Wide character C String to CPascalString. The string
  67. // is COPIED into the CPascalString.
  68. //
  69. //-----------------------------------------------------------------------------
  70. inline
  71. const CPascalString & // Allows a=b=c;
  72. CPascalString::operator=(
  73. const WCHAR *wszSource) // Source, zero terminated string
  74. {
  75. SetString(wszSource, wcslen(wszSource));
  76. return *this;
  77. }
  78. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  79. //
  80. // Appends a CPascalString to the current string.
  81. //
  82. //-----------------------------------------------------------------------------
  83. inline
  84. const CPascalString & // Allows a=b+=c syntax
  85. CPascalString::operator+=(
  86. const CPascalString &pstrTail) // Pascal string to append
  87. {
  88. AppendBuffer(pstrTail, pstrTail.GetStringLength());
  89. return *this;
  90. }
  91. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  92. //
  93. // Append a NUL terminated Unicode string to a Pascal string.
  94. //
  95. //-----------------------------------------------------------------------------
  96. inline
  97. const CPascalString & // Allows a-b+=L"Hi There" syntax
  98. CPascalString::operator+=(
  99. const WCHAR *szTail) // NUL terminated string to append
  100. {
  101. AppendBuffer(szTail, wcslen(szTail));
  102. return *this;
  103. }
  104. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  105. //
  106. // Append a Unicode character to a Pascal string.
  107. //
  108. //-----------------------------------------------------------------------------
  109. inline
  110. const CPascalString & // Allows a-b+=L"Hi There" syntax
  111. CPascalString::operator+=(
  112. const WCHAR wch) // WCHAR to append
  113. {
  114. AppendBuffer(&wch, 1);
  115. return *this;
  116. }
  117. //-----------------------------------------------------------------------------
  118. //
  119. // Comparison function for Pascal strings.
  120. //
  121. //-----------------------------------------------------------------------------
  122. inline
  123. BOOL // TRUE (1) if the same
  124. CPascalString::IsEqualTo(
  125. const CPascalString &pstrOtherString) const // String to compare to
  126. {
  127. return m_blbData == (const CLocCOWBlob &)pstrOtherString;
  128. }
  129. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  130. //
  131. // Operator == version of IsEqualTo
  132. //
  133. //-----------------------------------------------------------------------------
  134. inline
  135. int // TRUE (1) if equal
  136. CPascalString::operator==(
  137. const CPascalString &pstrOtherString) // String to compare
  138. const
  139. {
  140. return IsEqualTo(pstrOtherString);
  141. }
  142. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  143. //
  144. // Operator != - just the negative of IsEqualTo
  145. //
  146. //-----------------------------------------------------------------------------
  147. inline
  148. int // TRUE (1) if *not* equal
  149. CPascalString::operator!=(
  150. const CPascalString &pstrOtherString) const // String to compare
  151. {
  152. return !IsEqualTo(pstrOtherString);
  153. }
  154. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  155. //
  156. // Comparison operator for NUL terminated WCHAR strings.
  157. //
  158. //-----------------------------------------------------------------------------
  159. inline
  160. int
  161. CPascalString::operator==(
  162. const WCHAR *pwch)
  163. const
  164. {
  165. return (wcscmp(*this, pwch) == 0);
  166. }
  167. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  168. //
  169. // Comparison operator for NUL termninated WCHAR strings.
  170. //
  171. //-----------------------------------------------------------------------------
  172. inline int
  173. CPascalString::operator!=(
  174. const WCHAR *pwch)
  175. const
  176. {
  177. return (wcscmp(*this, pwch) != 0);
  178. }
  179. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  180. //
  181. // Is there anything in the string? This is different from a string of zero
  182. // length.
  183. //
  184. //-----------------------------------------------------------------------------
  185. inline
  186. BOOL
  187. CPascalString::IsNull(void)
  188. const
  189. {
  190. return ((const void *)m_blbData == NULL);
  191. }
  192. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  193. //
  194. // Get the length of the pascal string. If the length is zero, there may be
  195. // no storage associated with the string. Use IsNull to check for storage.
  196. //
  197. //-----------------------------------------------------------------------------
  198. inline
  199. UINT // length of the string.
  200. CPascalString::GetStringLength(void) const
  201. {
  202. UINT uiBufferSize;
  203. uiBufferSize = m_blbData.GetBlobSize();
  204. LTASSERT((uiBufferSize % sizeof(WCHAR)) == 0);
  205. return (uiBufferSize != 0 ? (uiBufferSize/sizeof(WCHAR)-1): 0);
  206. }
  207. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  208. //
  209. // Set the length of the pascal string. String contents are not preserved
  210. //
  211. //-----------------------------------------------------------------------------
  212. inline
  213. void // length of the string.
  214. CPascalString::SetStringLength(UINT uNewSize)
  215. {
  216. DEBUGONLY(m_StorageCounter -= m_blbData.GetBlobSize());
  217. m_blbData.SetBlobSize((uNewSize + 1) * sizeof(WCHAR));
  218. DEBUGONLY(m_StorageCounter += m_blbData.GetBlobSize());
  219. *(GetStringPointer() + uNewSize) = L'\0';
  220. ReleaseStringPointer();
  221. }
  222. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  223. //
  224. // Realloc a string - set true size
  225. //
  226. //-----------------------------------------------------------------------------
  227. inline
  228. void // length of the string.
  229. CPascalString::ReallocString(UINT uNewSize)
  230. {
  231. DEBUGONLY(m_StorageCounter -= m_blbData.GetBlobSize());
  232. m_blbData.ReallocBlob((uNewSize + 1) * sizeof(WCHAR));
  233. DEBUGONLY(m_StorageCounter += m_blbData.GetBlobSize());
  234. *(GetStringPointer() + uNewSize) = L'\0';
  235. ReleaseStringPointer();
  236. }
  237. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  238. //
  239. // As an optimization, the user can ask the Pascal string to reserve some
  240. // memory for future growth. This would allow incremental additions to be
  241. // very efficent. The reported size of the string is not changed - only the
  242. // amount of storage reserved for the string.
  243. //
  244. // If the user requests less space than is already allocated, nothing
  245. // happens.
  246. //
  247. //-----------------------------------------------------------------------------
  248. inline
  249. void
  250. CPascalString::ReserveStorage(
  251. UINT nMinSize) // Size (in chars) to reserve for
  252. {
  253. if (nMinSize > GetStringLength())
  254. {
  255. UINT uiCurSize;
  256. uiCurSize = GetStringLength();
  257. ReallocString(nMinSize);
  258. ReallocString(uiCurSize);
  259. }
  260. }
  261. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  262. //
  263. // Get a pointer to the storage for the string. This may be NULL if the
  264. // string has length 0. This pointer should be considered INVALID if any
  265. // other assignment operation is performed on the Pascal string. Calling
  266. // this dis-ables teh COW behavior of the CPascalString.
  267. //
  268. //-----------------------------------------------------------------------------
  269. inline
  270. WCHAR *
  271. CPascalString::GetStringPointer(void)
  272. {
  273. return (WCHAR *)m_blbData.GetPointer();
  274. }
  275. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  276. //
  277. // Anytime you do a GetStringPointer, use ReleaseStringPointer to allow
  278. // the PascalString to revert to COW behavior. Once you call this, the
  279. // pointer from GetStringPointer is INVALID.
  280. //
  281. //-----------------------------------------------------------------------------
  282. inline
  283. void
  284. CPascalString::ReleaseStringPointer(void)
  285. {
  286. m_blbData.ReleasePointer();
  287. }
  288. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  289. //
  290. // Casting operator version of GetString pointer. Cast a CPascalString to
  291. // const WCHAR *, and you get a pointer to the string.
  292. //
  293. //-----------------------------------------------------------------------------
  294. inline
  295. CPascalString::operator const WCHAR *(void) const
  296. {
  297. return (const WCHAR *)(const void *)(m_blbData);
  298. }
  299. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  300. //
  301. // Cleanup on the string. Sets the length to zero, and remove all storage.
  302. // This is different than assigning a NULL string - that is a string of
  303. // length 1, consisting of the NUL (zero) character.
  304. //
  305. //-----------------------------------------------------------------------------
  306. inline
  307. void
  308. CPascalString::ClearString(void)
  309. {
  310. DEBUGONLY(m_StorageCounter -= m_blbData.GetBlobSize());
  311. m_blbData.SetBlobSize(0);
  312. }
  313. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  314. //
  315. // Destructor for a Pascal string. Frees up the current storage. After
  316. // a Pascal string goes out of scope, all pointers to the internal storage
  317. // are invalid.
  318. //
  319. //-----------------------------------------------------------------------------
  320. inline
  321. CPascalString::~CPascalString()
  322. {
  323. LTASSERTONLY(AssertValid());
  324. DEBUGONLY(--m_UsageCounter);
  325. DEBUGONLY(m_StorageCounter -= m_blbData.GetBlobSize());
  326. }
  327. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  328. //
  329. // Serialize for a Pascal string.
  330. //
  331. //-----------------------------------------------------------------------------
  332. inline
  333. void CPascalString::Serialize(CArchive &ar)
  334. {
  335. if (ar.IsStoring())
  336. {
  337. Store(ar);
  338. }
  339. else
  340. {
  341. Load(ar);
  342. }
  343. }
  344. inline
  345. void
  346. CPascalString::Store(
  347. CArchive &ar)
  348. const
  349. {
  350. LTASSERT(ar.IsStoring());
  351. LTASSERTONLY(AssertValid());
  352. //
  353. // HACK HACK HACK
  354. // Emulate Old Espresso 3.0 serialization.
  355. m_blbData.Store(ar);
  356. }
  357. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  358. //
  359. // Helper function - comparison operator for a NUL terminated WCHAR string
  360. // and a CPascalString.
  361. //
  362. //-----------------------------------------------------------------------------
  363. inline
  364. int
  365. operator==(
  366. const WCHAR *pwch,
  367. const CPascalString &pstr)
  368. {
  369. return (wcscmp(pwch, pstr) == 0);
  370. }
  371. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  372. //
  373. // Helper function - comparison operator for a NUL terminated WCHAR string
  374. // and a CPascalString.
  375. //
  376. //-----------------------------------------------------------------------------
  377. inline
  378. int
  379. operator!=(
  380. const WCHAR *pwch,
  381. const CPascalString &pstr)
  382. {
  383. return (wcscmp(pwch, pstr) != 0);
  384. }
  385. inline
  386. int CPascalString::operator!=(
  387. const _bstr_t &bsOther)
  388. const
  389. {
  390. return !(operator==(bsOther));
  391. }
  392. inline
  393. int
  394. operator==(
  395. const _bstr_t &bsOther,
  396. const CPascalString &pstr)
  397. {
  398. return pstr == bsOther;
  399. }
  400. inline
  401. int
  402. operator!=(
  403. const _bstr_t &bsOther,
  404. const CPascalString &pstr)
  405. {
  406. return pstr != bsOther;
  407. }