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.

279 lines
8.2 KiB

  1. //+-------------------------------------------------------------------------
  2. // Microsoft OLE
  3. // Copyright (C) Microsoft Corporation, 1992 - 1996.
  4. // All rights reserved.
  5. //
  6. // File: ilkbdf.cxx
  7. //
  8. // Contents: Implementation for docfile based on ILockBytes class.
  9. //
  10. // Classes: ilkbdf.cxx
  11. //
  12. // Functions: GenerateVirtualDF
  13. // GenerateVirtualDFRoot
  14. //
  15. // History: 3-Aug-96 Narindk Created
  16. //--------------------------------------------------------------------------
  17. #include <dfheader.hxx>
  18. #pragma hdrstop
  19. #include "ilkbhdr.hxx"
  20. //----------------------------------------------------------------------------
  21. // Member: GenerateVirtualDF
  22. //
  23. // Synopsis: Creates a DocFile based on ILockBytes, size of which is
  24. // based on the ChanceDocFile created prior to this.
  25. //
  26. // Arguments: [pChanceDF] - Pointer to ChanceDocFile tree
  27. // [ppDocRoot] - Returned root of DocFile tree
  28. //
  29. // Returns: HRESULT
  30. //
  31. // History: Narindk 31-July-96 Created
  32. //
  33. // Notes: This function differs fro base class GenerateVirtualDF in
  34. // the way that it creates a ILockBytes instance and then call
  35. // GenerateVirtualDFRoot on that custom ILockBytes instance.
  36. //
  37. // This function calls GenerateVirtualDFRoot to generate docfile
  38. // tree's root and GrowDFTree to generate rest of the
  39. // tree. If the function succeeds, it returns pointer to root
  40. // of DocFile generated in ppDocRoot parameter.
  41. // - Get seed from ChanceDocFile tree and construct DG_INTEGER &
  42. // DG_UNICODE objects.
  43. // - Get the modes for creating various storages/streams from thw
  44. // ChanceDocFile tree.
  45. // - Get name of rootdocfile, if given, from chancedocfile tree,
  46. // else generate a random docfile name.
  47. // - Create an instance of custom ILockBytes and initialize the
  48. // same.
  49. // - Call GenerateDFRoot passing it also the custom ILOckBytes
  50. // generated above
  51. // - Call GrowDFTree
  52. // - If successful, assign root of new DF in *ppDocRoot.
  53. //---------------------------------------------------------------------------
  54. HRESULT ILockBytesDF::GenerateVirtualDF(
  55. ChanceDF *pChanceDF,
  56. VirtualCtrNode **ppvcnRoot)
  57. {
  58. HRESULT hr = S_OK;
  59. LPTSTR ptszName = NULL;
  60. CFileBytes *pCFileBytes = NULL;
  61. DH_FUNCENTRY(&hr, DH_LVL_DFLIB, _TEXT("GenerateILockBytesDF"));
  62. DH_VDATEPTRIN(pChanceDF, ChanceDF) ;
  63. DH_ASSERT(NULL != pChanceDF);
  64. DH_ASSERT(NULL != ppvcnRoot);
  65. if(S_OK == hr)
  66. {
  67. *ppvcnRoot = NULL;
  68. // Create a DataGen obj of type DG_INTEGER that will allow us to fill
  69. // count parameters of DocFile tree components, excepting those
  70. // which we got from already created ChanceDocFile tree. Use the
  71. // same seed value as was used in creation of ChanceDocFile tree.
  72. // Get the value of seed used to create ChanceDocFile tree and store it.
  73. _ulSeed = pChanceDF->GetSeed();
  74. _pdgi = new(NullOnFail) DG_INTEGER(_ulSeed);
  75. if (NULL == _pdgi)
  76. {
  77. hr = E_OUTOFMEMORY;
  78. }
  79. }
  80. if(S_OK == hr)
  81. {
  82. // Create a new DataGen object to create random UNICODE strings.
  83. _pgdu = new(NullOnFail) DG_STRING(_ulSeed);
  84. if (NULL == _pgdu)
  85. {
  86. hr = E_OUTOFMEMORY;
  87. }
  88. }
  89. if(S_OK == hr)
  90. {
  91. // Get the value of different creation modes.
  92. _dwRootMode = pChanceDF->GetRootMode();
  93. _dwStgMode = pChanceDF->GetStgMode();
  94. _dwStmMode = pChanceDF->GetStmMode();
  95. // Get user provided name for DocFile, if any
  96. ptszName = pChanceDF->GetDocFileName();
  97. if(NULL != ptszName)
  98. {
  99. _ptszName = new TCHAR[_tcslen(ptszName)+1];
  100. if (_ptszName == NULL)
  101. {
  102. hr = E_OUTOFMEMORY;
  103. }
  104. else
  105. {
  106. _tcscpy(_ptszName, ptszName);
  107. }
  108. }
  109. else
  110. {
  111. // Create a random file name for this root.
  112. hr = GenerateRandomName(_pgdu, MINLENGTH, MAXLENGTH, &_ptszName);
  113. DH_HRCHECK(hr, TEXT("GenerateRandomName")) ;
  114. }
  115. }
  116. // Make new custom ILockBytes
  117. if (S_OK == hr)
  118. {
  119. pCFileBytes = new CFileBytes();
  120. if(NULL == pCFileBytes)
  121. {
  122. hr = E_OUTOFMEMORY;
  123. }
  124. }
  125. // Initialize new custom ILockBytes
  126. if (S_OK == hr)
  127. {
  128. hr = pCFileBytes->Init(_ptszName, OF_CREATE|OF_READWRITE);
  129. DH_HRCHECK(hr, TEXT("CFileBytes::Init")) ;
  130. }
  131. if (S_OK == hr)
  132. {
  133. // Generates the root DocFile tree.
  134. hr = GenerateVirtualDFRoot(pChanceDF->GetChanceDFRoot(), pCFileBytes);
  135. DH_HRCHECK(hr, TEXT("GenerateDFRoot")) ;
  136. }
  137. if (S_OK == hr)
  138. {
  139. // Generate DF tree based on the ChanceDF tree.
  140. hr = GrowVirtualDFTree(pChanceDF->GetChanceDFRoot(), _pvcnRoot);
  141. DH_HRCHECK(hr, TEXT("GrowDFTree")) ;
  142. }
  143. // Fill the out parameter
  144. if(S_OK == hr)
  145. {
  146. _pCFileBytes = pCFileBytes;
  147. *ppvcnRoot = _pvcnRoot;
  148. }
  149. return hr;
  150. }
  151. //----------------------------------------------------------------------------
  152. // Member: ILockBytesDF::GenerateRoot, protected
  153. //
  154. // Synopsis: Creates the root stg for the DocFile
  155. //
  156. // Arguments: [pcnRoot] - Pointer to root of ChanceDocFile tree
  157. // [pCFileBytes] - Pointer to custom ILockBytes
  158. //
  159. // Returns: HRESULT
  160. //
  161. // History: Narindk 31-July-96 Created
  162. //
  163. // Notes: This function differs from base class's GenerateVirtualDF in
  164. // the way that it calls VirtualCtrNode's CreateRootOnCustomILock
  165. // Bytes method to generate the docfile, rather than CreateRoot.
  166. // Thereby the root docfile is created upon custom ILockBytes
  167. // rather than default OLE provided ILockBytes.
  168. //
  169. // - Create VirtualCtrNode and Initialize it based on ChanceNode
  170. // data.
  171. // - Create real IStorage corresponding to this node
  172. // - Creates VirtualStmNodes/IStreams corresponding to this stg
  173. // node, if required.
  174. //---------------------------------------------------------------------------
  175. HRESULT ILockBytesDF::GenerateVirtualDFRoot(
  176. ChanceNode *pcnRoot,
  177. CFileBytes *pCFileBytes)
  178. {
  179. HRESULT hr = S_OK;
  180. DH_VDATEPTRIN(pcnRoot, ChanceNode) ;
  181. DH_VDATEPTRIN(pCFileBytes, CFileBytes) ;
  182. DH_FUNCENTRY(&hr, DH_LVL_DFLIB, _TEXT("::GenerateDFRoot"));
  183. DH_ASSERT(NULL != pcnRoot);
  184. DH_ASSERT(NULL != pCFileBytes);
  185. // Generate VirtualCtrNode for the root node.
  186. if(S_OK == hr)
  187. {
  188. _pvcnRoot = new VirtualCtrNode();
  189. if (NULL == _pvcnRoot)
  190. {
  191. hr = E_OUTOFMEMORY;
  192. }
  193. }
  194. if(S_OK == hr)
  195. {
  196. hr = _pvcnRoot->Init(
  197. _ptszName,
  198. pcnRoot->GetChanceNodeStgCount(),
  199. pcnRoot->GetChanceNodeStmCount());
  200. DH_HRCHECK(hr, TEXT("VirtualCtrNode::Init")) ;
  201. }
  202. // Call StgCreateDocfileOnILockBytes to create a corresponding Root Storage
  203. // on disk.
  204. if(S_OK == hr)
  205. {
  206. hr = _pvcnRoot->CreateRootOnCustomILockBytes(
  207. _dwRootMode | STGM_CREATE,
  208. pCFileBytes);
  209. DH_HRCHECK(hr, TEXT("StgCreateDocfileOnILockBytes")) ;
  210. }
  211. if ((S_OK == hr) && (0 != pcnRoot->GetChanceNodeStmCount()))
  212. {
  213. hr = AppendVirtualStmNodesToVirtualCtrNode(
  214. pcnRoot->GetChanceNodeStmCount(),
  215. _pvcnRoot,
  216. pcnRoot->GetChanceNodeStmMinSize(),
  217. pcnRoot->GetChanceNodeStmMaxSize());
  218. DH_HRCHECK(hr, TEXT("AppendStmToStg")) ;
  219. }
  220. return hr;
  221. }