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.

825 lines
20 KiB

  1. /*++
  2. 1998 Seagate Software, Inc. All rights reserved
  3. Module Name:
  4. wsbstrg.cpp
  5. Abstract:
  6. This component is an object representations of the STRING standard type. It
  7. is both a persistable and collectable.
  8. Author:
  9. Chuck Bardeen [cbardeen] 29-Oct-1996
  10. Revision History:
  11. --*/
  12. #include "stdafx.h"
  13. #include "wsbstrg.h"
  14. HRESULT
  15. CWsbString::CompareToString(
  16. IN OLECHAR* value,
  17. OUT SHORT* pResult
  18. )
  19. /*++
  20. Implements:
  21. IWsbString::CompareToString
  22. --*/
  23. {
  24. HRESULT hr = E_FAIL;
  25. SHORT result;
  26. WsbTraceIn(OLESTR("CWsbString::CompareToString"), OLESTR("value = <%ls>"), value);
  27. // Comapre the two strings, and do the null checking in case the clib
  28. // can't handle it. If there are two valid strings, then use this objects
  29. // isCaseDependent flag to determine how to compare the values.
  30. if (0 == value) {
  31. if (m_value == 0) {
  32. result = 0;
  33. } else {
  34. result = 1;
  35. }
  36. } else {
  37. if (m_value == 0) {
  38. result = -1;
  39. } else {
  40. if (m_isCaseDependent) {
  41. result = (SHORT)wcscmp(m_value, value);
  42. }
  43. else {
  44. result = (SHORT)_wcsicmp(m_value, value);
  45. }
  46. }
  47. }
  48. // If the aren't equal, then return false.
  49. if (result != 0) {
  50. hr = S_FALSE;
  51. }
  52. else {
  53. hr = S_OK;
  54. }
  55. // If they asked for the relative value back, then return it to them.
  56. if (0 != pResult) {
  57. *pResult = result;
  58. }
  59. WsbTraceOut(OLESTR("CWsbString::CompareToString"), OLESTR("hr = <%ls>, result = <%d>"), WsbHrAsString(hr), result);
  60. return(hr);
  61. }
  62. HRESULT
  63. CWsbString::CompareToIString(
  64. IN IWsbString* pString,
  65. OUT SHORT* pResult
  66. )
  67. /*++
  68. Implements:
  69. IWsbString::CompareToIString
  70. --*/
  71. {
  72. HRESULT hr = E_FAIL;
  73. CWsbStringPtr value;
  74. WsbTraceIn(OLESTR("CWsbString::CompareToIString"), OLESTR(""));
  75. try {
  76. // Did they give us a valid item to compare to?
  77. WsbAssert(0 != pString, E_POINTER);
  78. // Get it's value and compare them.
  79. WsbAffirmHr(pString->GetString(&value, 0));
  80. hr = CompareToString(value, pResult);
  81. } WsbCatch(hr);
  82. WsbTraceOut(OLESTR("CWsbString::CompareToIString"), OLESTR("hr = <%ls>, result = <%ls>"), WsbHrAsString(hr), WsbPtrToShortAsString(pResult));
  83. return(hr);
  84. }
  85. HRESULT
  86. CWsbString::CompareTo(
  87. IN IUnknown* pCollectable,
  88. OUT SHORT* pResult
  89. )
  90. /*++
  91. Implements:
  92. IWsbCollectable::CompareTo
  93. --*/
  94. {
  95. HRESULT hr = E_FAIL;
  96. IWsbString* pString;
  97. WsbTraceIn(OLESTR("CWsbString::CompareTo"), OLESTR(""));
  98. try {
  99. // Did they give us a valid item to compare to?
  100. WsbAssert(0 != pCollectable, E_POINTER);
  101. // We need the IWsbString interface to get the value of the object.
  102. WsbAffirmHr(pCollectable->QueryInterface(IID_IWsbString, (void**) &pString));
  103. hr = CompareToIString(pString, pResult);
  104. } WsbCatch(hr);
  105. WsbTraceOut(OLESTR("CWsbString::CompareTo"), OLESTR("hr = <%ls>, result = <%ls>"), WsbHrAsString(hr), WsbPtrToShortAsString(pResult));
  106. return(hr);
  107. }
  108. HRESULT
  109. CWsbString::FinalConstruct(
  110. void
  111. )
  112. /*++
  113. Implements:
  114. CComObjectRoot::FinalConstruct
  115. --*/
  116. {
  117. HRESULT hr = S_OK;
  118. try {
  119. WsbAffirmHr(CWsbObject::FinalConstruct());
  120. m_isCaseDependent = TRUE;
  121. } WsbCatch(hr);
  122. return(hr);
  123. }
  124. HRESULT
  125. CWsbString::GetClassID(
  126. OUT CLSID* pClsid
  127. )
  128. /*++
  129. Implements:
  130. IPersist::GetClassID
  131. --*/
  132. {
  133. HRESULT hr = S_OK;
  134. WsbTraceIn(OLESTR("CWsbString::GetClassID"), OLESTR(""));
  135. try {
  136. WsbAssert(0 != pClsid, E_POINTER);
  137. *pClsid = CLSID_CWsbString;
  138. } WsbCatch(hr);
  139. WsbTraceOut(OLESTR("CWsbString::GetClassID"), OLESTR("hr = <%ls>, CLSID = <%ls>"), WsbHrAsString(hr), WsbGuidAsString(*pClsid));
  140. return(hr);
  141. }
  142. HRESULT
  143. CWsbString::GetSizeMax(
  144. OUT ULARGE_INTEGER* pcbSize
  145. )
  146. /*++
  147. Implements:
  148. IPersistStream::GetSizeMax
  149. --*/
  150. {
  151. HRESULT hr = S_OK;
  152. WsbTraceIn(OLESTR("CWsbString::GetSizeMax"), OLESTR(""));
  153. try {
  154. WsbAssert(0 != pcbSize, E_POINTER);
  155. pcbSize->QuadPart = WsbPersistSizeOf(BOOL) + WsbPersistSizeOf(ULONG) + WsbPersistSize((wcslen(m_value) + 1) * sizeof(OLECHAR));
  156. } WsbCatch(hr);
  157. WsbTraceOut(OLESTR("CWsbString::GetSizeMax"), OLESTR("hr = <%ls>, Size = <%ls>"), WsbHrAsString(hr), WsbPtrToUliAsString(pcbSize));
  158. return(hr);
  159. }
  160. HRESULT
  161. CWsbString::GetString(
  162. OUT OLECHAR** pValue,
  163. IN ULONG bufferSize
  164. )
  165. /*++
  166. Implements:
  167. IWsbString::GetString
  168. --*/
  169. {
  170. HRESULT hr = S_OK;
  171. WsbTraceIn(OLESTR("CWsbString::GetString"), OLESTR(""));
  172. try {
  173. WsbAssert(0 != pValue, E_POINTER);
  174. WsbAffirmHr(m_value.CopyTo(pValue, bufferSize));
  175. } WsbCatch(hr);
  176. WsbTraceOut(OLESTR("CWsbString::GetString"), OLESTR("hr = <%ls>, value = <%ls>"), WsbHrAsString(hr), m_value);
  177. return(hr);
  178. }
  179. HRESULT
  180. CWsbString::GetStringAndCase(
  181. OUT OLECHAR** pValue,
  182. OUT BOOL* pIsCaseDependent,
  183. IN ULONG bufferSize
  184. )
  185. /*++
  186. Implements:
  187. IWsbString::GetStringAndCase
  188. --*/
  189. {
  190. HRESULT hr = S_OK;
  191. WsbTraceIn(OLESTR("CWsbString::GetString"), OLESTR(""));
  192. try {
  193. WsbAssert(0 != pValue, E_POINTER);
  194. WsbAssert(0 != pIsCaseDependent, E_POINTER);
  195. WsbAffirmHr(m_value.CopyTo(pValue, bufferSize));
  196. *pIsCaseDependent = m_isCaseDependent;
  197. } WsbCatch(hr);
  198. WsbTraceOut(OLESTR("CWsbString::GetString"), OLESTR("hr = <%ls>, value = <%ls>"), WsbHrAsString(hr), m_value);
  199. return(hr);
  200. }
  201. HRESULT
  202. CWsbString::IsCaseDependent(
  203. void
  204. )
  205. /*++
  206. Implements:
  207. IWsbString::IsCaseDependent
  208. --*/
  209. {
  210. WsbTraceIn(OLESTR("CWsbString::IsCaseDependent"), OLESTR(""));
  211. WsbTraceOut(OLESTR("CWsbString::IsCaseDependent"), OLESTR("isCaseDependent = <%ls>"), WsbBoolAsString(m_isCaseDependent));
  212. return(m_isCaseDependent ? S_OK : S_FALSE);
  213. }
  214. HRESULT
  215. CWsbString::Load(
  216. IN IStream* pStream
  217. )
  218. /*++
  219. Implements:
  220. IPersistStream::Load
  221. --*/
  222. {
  223. HRESULT hr = S_OK;
  224. WsbTraceIn(OLESTR("CWsbString::Load"), OLESTR(""));
  225. try {
  226. WsbAssert(0 != pStream, E_POINTER);
  227. // First get CaseDependent flag.
  228. WsbAffirmHr(WsbLoadFromStream(pStream, &m_isCaseDependent));
  229. // Now get the string.
  230. WsbAffirmHr(WsbLoadFromStream(pStream, &m_value, 0));
  231. } WsbCatch(hr);
  232. WsbTraceOut(OLESTR("CWsbString::Load"), OLESTR("hr = <%ls>, value = <%ls>"), WsbHrAsString(hr), WsbPtrToStringAsString(&m_value));
  233. return(hr);
  234. }
  235. HRESULT
  236. CWsbString::Save(
  237. IN IStream* pStream,
  238. IN BOOL clearDirty
  239. )
  240. /*++
  241. Implements:
  242. IPersistStream::Save().
  243. --*/
  244. {
  245. HRESULT hr = S_OK;
  246. WsbTraceIn(OLESTR("CWsbString::Save"), OLESTR("clearDirty = <%ls>"), WsbBoolAsString(clearDirty));
  247. try {
  248. WsbAssert(0 != pStream, E_POINTER);
  249. // First save the CaseDependent flag.
  250. WsbAffirmHr(WsbSaveToStream(pStream, m_isCaseDependent));
  251. // Now save the string.
  252. WsbAffirmHr(WsbSaveToStream(pStream, (OLECHAR*)m_value));
  253. // If we got it saved and we were asked to clear the dirty bit, then
  254. // do so now.
  255. if (clearDirty) {
  256. m_isDirty = FALSE;
  257. }
  258. } WsbCatch(hr);
  259. WsbTraceOut(OLESTR("CWsbString::Save"), OLESTR("hr = <%ls>"), WsbHrAsString(hr));
  260. return(hr);
  261. }
  262. HRESULT
  263. CWsbString::SetIsCaseDependent(
  264. BOOL isCaseDependent
  265. )
  266. /*++
  267. Implements:
  268. IWsbString::SetIsCaseDependent
  269. --*/
  270. {
  271. WsbTraceIn(OLESTR("CWsbString::SetIsCaseDependent"), OLESTR("value = <%ls>"), WsbBoolAsString(isCaseDependent));
  272. m_isDirty = TRUE;
  273. m_isCaseDependent = isCaseDependent;
  274. WsbTraceOut(OLESTR("CWsbString::SetIsCaseDependent"), OLESTR(""));
  275. return(S_OK);
  276. }
  277. HRESULT
  278. CWsbString::SetString(
  279. IN OLECHAR* value
  280. )
  281. /*++
  282. Implements:
  283. IWsbString::SetString
  284. --*/
  285. {
  286. HRESULT hr = S_OK;
  287. WsbTraceIn(OLESTR("CWsbString::SetString"), OLESTR("value = <%ls>"), WsbPtrToStringAsString(&value));
  288. try {
  289. m_value = value;
  290. m_isDirty = TRUE;
  291. } WsbCatch(hr);
  292. WsbTraceOut(OLESTR("CWsbString::SetString"), OLESTR("hr = <%ls>"), WsbHrAsString(hr));
  293. return(hr);
  294. }
  295. HRESULT
  296. CWsbString::SetStringAndCase(
  297. IN OLECHAR* value,
  298. IN BOOL isCaseDependent
  299. )
  300. /*++
  301. Implements:
  302. IWsbString::SetStringAndCase
  303. --*/
  304. {
  305. HRESULT hr = S_OK;
  306. WsbTraceIn(OLESTR("CWsbString::SetStringAndCase"), OLESTR("value = <%ls>, isCaseDependent = <%ls>"), WsbPtrToStringAsString(&value), WsbBoolAsString(isCaseDependent));
  307. try {
  308. m_value = value;
  309. m_isDirty = TRUE;
  310. m_isCaseDependent = isCaseDependent;
  311. } WsbCatch(hr);
  312. WsbTraceOut(OLESTR("CWsbString::SetStringAndCase"), OLESTR("hr = <%ls>"), WsbHrAsString(hr));
  313. return(hr);
  314. }
  315. HRESULT
  316. CWsbString::Test(
  317. OUT USHORT* passed,
  318. OUT USHORT* failed
  319. )
  320. /*++
  321. Implements:
  322. IWsbTestable::Test
  323. --*/
  324. {
  325. *passed = 0;
  326. *failed = 0;
  327. HRESULT hr = S_OK;
  328. #if !defined(WSB_NO_TEST)
  329. CComPtr<IWsbString> pString1;
  330. CComPtr<IWsbString> pString2;
  331. // CComPtr<IPersistFile> pFile1;
  332. // CComPtr<IPersistFile> pFile2;
  333. OLECHAR* value = NULL;
  334. BOOL isCaseDependent;
  335. SHORT result;
  336. WsbTraceIn(OLESTR("CWsbString::Test"), OLESTR(""));
  337. try {
  338. // Get the pString interface.
  339. hr = S_OK;
  340. try {
  341. WsbAffirmHr(((IUnknown*) (IWsbString*) this)->QueryInterface(IID_IWsbString, (void**) &pString1));
  342. // Set the bool to a value, and see if it is returned.
  343. hr = S_OK;
  344. try {
  345. WsbAffirmHr(pString1->SetString(OLESTR("Test Case")));
  346. WsbAffirmHr(pString1->GetString(&value, 0));
  347. WsbAssert(wcscmp(value, OLESTR("Test Case")) == 0, E_FAIL);
  348. } WsbCatch(hr);
  349. if (hr == S_OK) {
  350. (*passed)++;
  351. } else {
  352. (*failed)++;
  353. }
  354. // Set the case dependence flag.
  355. hr = S_OK;
  356. try {
  357. WsbAffirmHr(pString1->SetIsCaseDependent(FALSE));
  358. WsbAssert(pString1->IsCaseDependent() == S_FALSE, E_FAIL);
  359. } WsbCatch(hr);
  360. if (hr == S_OK) {
  361. (*passed)++;
  362. } else {
  363. (*failed)++;
  364. }
  365. hr = S_OK;
  366. try {
  367. WsbAffirmHr(pString1->SetIsCaseDependent(TRUE));
  368. WsbAssert(pString1->IsCaseDependent() == S_OK, E_FAIL);
  369. } WsbCatch(hr);
  370. if (hr == S_OK) {
  371. (*passed)++;
  372. } else {
  373. (*failed)++;
  374. }
  375. // Set Both string and case
  376. hr = S_OK;
  377. try {
  378. WsbAffirmHr(pString1->SetStringAndCase(OLESTR("Both"), FALSE));
  379. WsbAffirmHr(pString1->GetStringAndCase(&value, &isCaseDependent, 0));
  380. WsbAssert((wcscmp(value, OLESTR("Both")) == 0) && (isCaseDependent == FALSE), E_FAIL);
  381. } WsbCatch(hr);
  382. if (hr == S_OK) {
  383. (*passed)++;
  384. } else {
  385. (*failed)++;
  386. }
  387. // Create another instance and test the comparisson methods:
  388. try {
  389. WsbAffirmHr(CoCreateInstance(CLSID_CWsbString, NULL, CLSCTX_ALL, IID_IWsbString, (void**) &pString2));
  390. // Check the default values.
  391. hr = S_OK;
  392. try {
  393. WsbAffirmHr(pString2->GetStringAndCase(&value, &isCaseDependent, 0));
  394. WsbAssert((wcscmp(value, OLESTR("")) == 0) && isCaseDependent, E_FAIL);
  395. } WsbCatch(hr);
  396. if (hr == S_OK) {
  397. (*passed)++;
  398. } else {
  399. (*failed)++;
  400. }
  401. // IsEqual()
  402. hr = S_OK;
  403. try {
  404. WsbAffirmHr(pString1->SetStringAndCase(OLESTR("HiJk"), TRUE));
  405. WsbAffirmHr(pString2->SetStringAndCase(OLESTR("HiJk"), FALSE));
  406. WsbAssert(pString1->IsEqual(pString2) == S_OK, E_FAIL);
  407. } WsbCatch(hr);
  408. if (hr == S_OK) {
  409. (*passed)++;
  410. } else {
  411. (*failed)++;
  412. }
  413. hr = S_OK;
  414. try {
  415. WsbAffirmHr(pString1->SetStringAndCase(OLESTR("HiJk"), TRUE));
  416. WsbAffirmHr(pString2->SetStringAndCase(OLESTR("HIJK"), FALSE));
  417. WsbAssert(pString1->IsEqual(pString2) == S_FALSE, E_FAIL);
  418. } WsbCatch(hr);
  419. if (hr == S_OK) {
  420. (*passed)++;
  421. } else {
  422. (*failed)++;
  423. }
  424. hr = S_OK;
  425. try {
  426. WsbAffirmHr(pString1->SetStringAndCase(OLESTR("HiJk"), FALSE));
  427. WsbAffirmHr(pString2->SetStringAndCase(OLESTR("HiJk"), TRUE));
  428. WsbAssert(pString1->IsEqual(pString2) == S_OK, E_FAIL);
  429. } WsbCatch(hr);
  430. if (hr == S_OK) {
  431. (*passed)++;
  432. } else {
  433. (*failed)++;
  434. }
  435. hr = S_OK;
  436. try {
  437. WsbAffirmHr(pString1->SetStringAndCase(OLESTR("HiJk"), FALSE));
  438. WsbAffirmHr(pString2->SetStringAndCase(OLESTR("HIJK"), TRUE));
  439. WsbAssert(pString1->IsEqual(pString2) == S_OK, E_FAIL);
  440. } WsbCatch(hr);
  441. if (hr == S_OK) {
  442. (*passed)++;
  443. } else {
  444. (*failed)++;
  445. }
  446. // CompareTo()
  447. hr = S_OK;
  448. try {
  449. WsbAffirmHr(pString1->SetStringAndCase(OLESTR("HiJk"), TRUE));
  450. WsbAffirmHr(pString2->SetString(OLESTR("HiJk")));
  451. WsbAssert((pString1->CompareTo(pString2, &result) == S_OK) && (0 == result), E_FAIL);
  452. } WsbCatch(hr);
  453. if (hr == S_OK) {
  454. (*passed)++;
  455. } else {
  456. (*failed)++;
  457. }
  458. hr = S_OK;
  459. try {
  460. WsbAffirmHr(pString1->SetStringAndCase(OLESTR("AABC"), TRUE));
  461. WsbAffirmHr(pString2->SetString(OLESTR("ABCC")));
  462. WsbAssert((pString1->CompareTo(pString2, &result) == S_FALSE) && (-1 == result), E_FAIL);
  463. } WsbCatch(hr);
  464. if (hr == S_OK) {
  465. (*passed)++;
  466. } else {
  467. (*failed)++;
  468. }
  469. hr = S_OK;
  470. try {
  471. WsbAffirmHr(pString1->SetStringAndCase(OLESTR("BBC"), TRUE));
  472. WsbAffirmHr(pString2->SetString(OLESTR("ABCC")));
  473. WsbAssert((pString1->CompareTo(pString2, &result) == S_FALSE) && (1 == result), E_FAIL);
  474. } WsbCatch(hr);
  475. if (hr == S_OK) {
  476. (*passed)++;
  477. } else {
  478. (*failed)++;
  479. }
  480. hr = S_OK;
  481. try {
  482. WsbAffirmHr(pString1->SetStringAndCase(OLESTR("abcc"), TRUE));
  483. WsbAffirmHr(pString2->SetString(OLESTR("ABCC")));
  484. WsbAssert((pString1->CompareTo(pString2, &result) == S_FALSE) && (1 == result), E_FAIL);
  485. } WsbCatch(hr);
  486. if (hr == S_OK) {
  487. (*passed)++;
  488. } else {
  489. (*failed)++;
  490. }
  491. hr = S_OK;
  492. try {
  493. WsbAffirmHr(pString1->SetStringAndCase(OLESTR("abcc"), FALSE));
  494. WsbAffirmHr(pString2->SetString(OLESTR("ABCC")));
  495. WsbAssert((pString1->CompareTo(pString2, &result) == S_OK) && (0 == result), E_FAIL);
  496. } WsbCatch(hr);
  497. if (hr == S_OK) {
  498. (*passed)++;
  499. } else {
  500. (*failed)++;
  501. }
  502. #ifdef STRG_PERSIST_FILE
  503. // TODO? Open the file and convert it to a stream?
  504. // Try out the persistence stuff.
  505. hr = S_OK;
  506. try {
  507. WsbAffirmHr(pString1->QueryInterface(IID_IPersistFile, (void**) &pFile1));
  508. WsbAffirmHr(pString2->QueryInterface(IID_IPersistFile, (void**) &pFile2));
  509. // The item should be dirty.
  510. hr = S_OK;
  511. try {
  512. WsbAffirmHr(pString2->SetStringAndCase(OLESTR("The quick brown fox."), TRUE));
  513. WsbAssert(pFile2->IsDirty() == S_OK, E_FAIL);
  514. } WsbCatch(hr);
  515. if (hr == S_OK) {
  516. (*passed)++;
  517. } else {
  518. (*failed)++;
  519. }
  520. // Save the item, and remember.
  521. hr = S_OK;
  522. try {
  523. WsbAffirmHr(pFile2->Save(OLESTR("c:\\WsbTests\\WsbString.tst"), TRUE));
  524. } WsbCatch(hr);
  525. if (hr == S_OK) {
  526. (*passed)++;
  527. } else {
  528. (*failed)++;
  529. }
  530. // It shouldn't be dirty.
  531. hr = S_OK;
  532. try {
  533. WsbAssert(pFile2->IsDirty() == S_FALSE, E_FAIL);
  534. } WsbCatch(hr);
  535. if (hr == S_OK) {
  536. (*passed)++;
  537. } else {
  538. (*failed)++;
  539. }
  540. // Try reading it in to another object.
  541. hr = S_OK;
  542. try {
  543. WsbAffirmHr(pString1->SetStringAndCase(OLESTR("jumped over the lazy dog."), FALSE));
  544. WsbAffirmHr(pFile1->Load(OLESTR("c:\\WsbTests\\WsbString.tst"), 0));
  545. WsbAssert(pString1->CompareToString(OLESTR("The quick brown fox."), NULL) == S_OK, E_FAIL);
  546. WsbAssert(pString1->IsCaseDependent() == S_OK, E_FAIL);
  547. } WsbCatch(hr);
  548. if (hr == S_OK) {
  549. (*passed)++;
  550. } else {
  551. (*failed)++;
  552. }
  553. } WsbCatch(hr);
  554. if (hr == S_OK) {
  555. (*passed)++;
  556. } else {
  557. (*failed)++;
  558. }
  559. #endif
  560. } WsbCatch(hr);
  561. if (hr == S_OK) {
  562. (*passed)++;
  563. } else {
  564. (*failed)++;
  565. }
  566. } WsbCatch(hr);
  567. if (hr == S_OK) {
  568. (*passed)++;
  569. } else {
  570. (*failed)++;
  571. }
  572. // Tally up the results
  573. if (*failed) {
  574. hr = S_FALSE;
  575. } else {
  576. hr = S_OK;
  577. }
  578. } WsbCatch(hr);
  579. // If we used the temporary string buffer, then free it now.
  580. if (0 != value) {
  581. WsbFree(value);
  582. }
  583. WsbTraceOut(OLESTR("CWsbString::Test"), OLESTR("hr = <%ls>"), WsbHrAsString(hr));
  584. #endif
  585. return(hr);
  586. }