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.

442 lines
11 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1998
  5. //
  6. // File: docname.cxx
  7. //
  8. // Contents: Storage Client for document
  9. //
  10. // History: 11-22-96 srikants Created
  11. //
  12. //----------------------------------------------------------------------------
  13. #include <pch.cxx>
  14. #pragma hdrstop
  15. #include <docname.hxx>
  16. //+---------------------------------------------------------------------------
  17. //
  18. // Member: CCiCDocName::CCiCDocName
  19. //
  20. // Synopsis: Constructor which takes a path
  21. //
  22. // Arguments: [pwszPath] - Pointer to a NULL terminated path.
  23. //
  24. // History: 11-22-96 srikants Created
  25. //
  26. //----------------------------------------------------------------------------
  27. CCiCDocName::CCiCDocName( WCHAR const * pwszPath )
  28. :_pwszPath(_wszBuffer),
  29. _refCount(1),
  30. _fIsInit(FALSE),
  31. _cwcMax(MAX_PATH-1),
  32. _cwcPath(0)
  33. {
  34. if ( pwszPath )
  35. {
  36. ULONG cwc = wcslen( pwszPath );
  37. SetPath( pwszPath, cwc );
  38. }
  39. }
  40. //+---------------------------------------------------------------------------
  41. //
  42. // Member: CCiCDocName::CCiCDocName
  43. //
  44. // Synopsis: Constructor which takes a path and length
  45. //
  46. // Arguments: [pwszPath] - Pointer to a NULL terminated path.
  47. //
  48. // History: 11-22-96 srikants Created
  49. //
  50. //----------------------------------------------------------------------------
  51. CCiCDocName::CCiCDocName( WCHAR const * pwszPath, ULONG cwc )
  52. :_pwszPath(_wszBuffer),
  53. _refCount(1),
  54. _fIsInit(FALSE),
  55. _cwcMax(MAX_PATH-1),
  56. _cwcPath(0)
  57. {
  58. if ( pwszPath )
  59. SetPath( pwszPath, cwc );
  60. }
  61. //+---------------------------------------------------------------------------
  62. //
  63. // Member: CCiCDocName::SetPath
  64. //
  65. // Synopsis: Stores the given path
  66. //
  67. // Arguments: [pwszPath] - Pointer to the path
  68. // [cwc] - Number of characters in the path.
  69. //
  70. // History: 11-22-96 srikants Created
  71. //
  72. //----------------------------------------------------------------------------
  73. void CCiCDocName::SetPath( WCHAR const * pwszPath, ULONG cwc )
  74. {
  75. if ( cwc && 0 != pwszPath )
  76. {
  77. if ( cwc > _cwcMax )
  78. {
  79. FreeResources();
  80. ULONG cwcMax = cwc + CWC_DELTA;
  81. _pwszPath = new WCHAR [cwcMax+1];
  82. _cwcMax = cwcMax;
  83. }
  84. RtlCopyMemory( _pwszPath, pwszPath, cwc*sizeof(WCHAR) );
  85. _cwcPath = cwc;
  86. }
  87. else
  88. {
  89. _cwcPath = 0;
  90. }
  91. _pwszPath[_cwcPath] = 0;
  92. _fIsInit = TRUE;
  93. }
  94. //+---------------------------------------------------------------------------
  95. //
  96. // Member: CCiCDocName::QueryInterface
  97. //
  98. // Synopsis: Supports IID_IUnknown and IID_ICiCDocName
  99. //
  100. // Arguments: [riid] -
  101. // [ppvObject] -
  102. //
  103. // History: 11-22-96 srikants Created
  104. //
  105. //----------------------------------------------------------------------------
  106. STDMETHODIMP CCiCDocName::QueryInterface(
  107. REFIID riid,
  108. void **ppvObject)
  109. {
  110. Win4Assert( 0 != ppvObject );
  111. *ppvObject = 0;
  112. if ( IID_ICiCDocName == riid )
  113. *ppvObject = (void *)((ICiCDocName *)this);
  114. else if ( IID_IUnknown == riid )
  115. *ppvObject = (void *)((IUnknown *)this);
  116. else
  117. return E_NOINTERFACE;
  118. AddRef();
  119. return S_OK;
  120. } //QueryInterface
  121. //+---------------------------------------------------------------------------
  122. //
  123. // Member: CCiCDocName::AddRef
  124. //
  125. // History: 11-22-96 srikants Created
  126. //
  127. //----------------------------------------------------------------------------
  128. STDMETHODIMP_(ULONG) CCiCDocName::AddRef()
  129. {
  130. return InterlockedIncrement(&_refCount);
  131. } //AddRef
  132. //+---------------------------------------------------------------------------
  133. //
  134. // Member: CCiCDocName::Release
  135. //
  136. // History: 11-22-96 srikants Created
  137. //
  138. //----------------------------------------------------------------------------
  139. STDMETHODIMP_(ULONG) CCiCDocName::Release()
  140. {
  141. Win4Assert( _refCount > 0 );
  142. LONG refCount = InterlockedDecrement(&_refCount);
  143. if ( refCount <= 0 )
  144. delete this;
  145. return (ULONG) refCount;
  146. } //Release
  147. //+---------------------------------------------------------------------------
  148. //
  149. // Member: CCiCDocName::Init
  150. //
  151. // Synopsis: Intiailizes with the given name data. The name must be a
  152. // serialized form of a WIDE-CHAR string. It MUST be a
  153. // NULL terminated string.
  154. //
  155. // Arguments: [pbData] - Pointer to the path.
  156. // [cbData] - Number of valid bytes, including the NULL
  157. // termination.
  158. //
  159. // Returns:
  160. //
  161. // History: 11-26-96 srikants Created
  162. //
  163. //----------------------------------------------------------------------------
  164. STDMETHODIMP CCiCDocName::Init(
  165. const BYTE * pbData,
  166. ULONG cbData )
  167. {
  168. //
  169. // The number of bytes must be even.
  170. //
  171. Win4Assert( (cbData & 0x1) == 0 );
  172. if ( 0 != cbData%sizeof(WCHAR) )
  173. return E_INVALIDARG;
  174. if ( _fIsInit )
  175. return CI_E_ALREADY_INITIALIZED;
  176. WCHAR const * pwszPath = (WCHAR const *) pbData;
  177. ULONG cwc = cbData/sizeof(WCHAR);
  178. //
  179. // The data must be NULL terminated.
  180. //
  181. if ( cwc > 0 && 0 != pwszPath[cwc-1] )
  182. return E_INVALIDARG;
  183. SetPath( pwszPath, cwc-1 );
  184. return S_OK;
  185. } //Init
  186. //+---------------------------------------------------------------------------
  187. //
  188. // Member: CCiCDocName::Set
  189. //
  190. // Synopsis: Initializes this object with the given document name.
  191. //
  192. // Arguments: [pICiCDocName] - Pointer to the source document name.
  193. //
  194. // Returns:
  195. //
  196. // History: 11-26-96 srikants Created
  197. //
  198. //----------------------------------------------------------------------------
  199. STDMETHODIMP CCiCDocName::Set(
  200. const ICiCDocName * pICiCDocName )
  201. {
  202. const CCiCDocName * pRhs = (CCiCDocName *) pICiCDocName;
  203. if ( !pRhs )
  204. return E_INVALIDARG;
  205. if ( _fIsInit )
  206. return CI_E_ALREADY_INITIALIZED;
  207. if ( pRhs->_fIsInit )
  208. SetPath( pRhs->_pwszPath, pRhs->_cwcPath );
  209. else
  210. _fIsInit = FALSE;
  211. return S_OK;
  212. } //Set
  213. //+---------------------------------------------------------------------------
  214. //
  215. // Member: CCiCDocName::Clear
  216. //
  217. // Synopsis: Clears the data. The document name is "invalid" after this.
  218. //
  219. // History: 11-26-96 srikants Created
  220. //
  221. //----------------------------------------------------------------------------
  222. STDMETHODIMP CCiCDocName::Clear()
  223. {
  224. if ( !_fIsInit )
  225. return CI_E_NOT_INITIALIZED;
  226. _cwcPath = 0;
  227. _fIsInit = FALSE;
  228. return S_OK;
  229. } //Clear
  230. //+---------------------------------------------------------------------------
  231. //
  232. // Member: CCiCDocName::IsValid
  233. //
  234. // Synopsis: Tests if the document name is properly initialized or not.
  235. //
  236. // History: 11-26-96 srikants Created
  237. //
  238. //----------------------------------------------------------------------------
  239. STDMETHODIMP CCiCDocName::IsValid(void)
  240. {
  241. if ( _fIsInit )
  242. return S_OK;
  243. else return CI_E_NOT_INITIALIZED;
  244. }
  245. //+---------------------------------------------------------------------------
  246. //
  247. // Member: CCiCDocName::Duplicate
  248. //
  249. // Synopsis: Makes a duplicate copy of the data in this object.
  250. //
  251. // Arguments: [ppICiCDocName] -
  252. //
  253. // History: 11-26-96 srikants Created
  254. //
  255. //----------------------------------------------------------------------------
  256. STDMETHODIMP CCiCDocName::Duplicate(
  257. ICiCDocName ** ppICiCDocName )
  258. {
  259. if ( 0 == ppICiCDocName )
  260. return E_INVALIDARG;
  261. CCiCDocName * pRhs = 0;
  262. SCODE sc = S_OK;
  263. TRY
  264. {
  265. if ( _fIsInit )
  266. pRhs = new CCiCDocName( _pwszPath, _cwcPath );
  267. else
  268. pRhs = new CCiCDocName();
  269. }
  270. CATCH( CException,e )
  271. {
  272. sc = e.GetErrorCode();
  273. }
  274. END_CATCH
  275. *ppICiCDocName = pRhs;
  276. return sc;
  277. }
  278. //+---------------------------------------------------------------------------
  279. //
  280. // Member: CCiCDocName::GetBufSizeNeeded
  281. //
  282. // Synopsis:
  283. //
  284. // Arguments: [pcbName] -
  285. //
  286. // Returns:
  287. //
  288. // Modifies:
  289. //
  290. // History: 11-26-96 srikants Created
  291. //
  292. // Notes:
  293. //
  294. //----------------------------------------------------------------------------
  295. STDMETHODIMP CCiCDocName::GetBufSizeNeeded(
  296. ULONG * pcbName )
  297. {
  298. if ( !_fIsInit )
  299. return CI_E_NOT_INITIALIZED;
  300. Win4Assert( pcbName );
  301. *pcbName = ComputeBufSize();
  302. return S_OK;
  303. }
  304. //+---------------------------------------------------------------------------
  305. //
  306. // Member: CCiCDocName::Get
  307. //
  308. // Synopsis: Serializes the name into the buffers provided.
  309. //
  310. // Arguments: [pbName] - Pointer to the buffer to copy.
  311. // [pcbName] - [in/out] In - Max Bytes to Copy
  312. // [out] - Bytes copied or to be copied.
  313. //
  314. // Returns: S_OK if successful;
  315. // CI_E_BUFFERTOOSMALL if the buffer is not big enough.
  316. //
  317. // History: 11-26-96 srikants Created
  318. //
  319. //----------------------------------------------------------------------------
  320. STDMETHODIMP CCiCDocName::Get(
  321. BYTE * pbName,
  322. ULONG * pcbName )
  323. {
  324. Win4Assert( pcbName );
  325. if ( !_fIsInit )
  326. return CI_E_NOT_INITIALIZED;
  327. ULONG cbToCopy = ComputeBufSize();
  328. if ( *pcbName < cbToCopy )
  329. {
  330. *pcbName = cbToCopy;
  331. return CI_E_BUFFERTOOSMALL;
  332. }
  333. RtlCopyMemory( pbName, _pwszPath, cbToCopy );
  334. *pcbName = cbToCopy;
  335. return S_OK;
  336. }
  337. //+---------------------------------------------------------------------------
  338. //
  339. // Member: CCiCDocName::GetNameBuffer
  340. //
  341. // Synopsis: Returns the internal buffer containing the name.
  342. //
  343. // Arguments: [ppbName] - On output, will point to the name buffer
  344. // [pcbName] - Number of valid bytes in the buffer
  345. //
  346. // Returns: S_OK if successful;
  347. // CI_E_NOT_INITIALIZED if not initialized.
  348. //
  349. // History: 1-21-97 srikants Created
  350. //
  351. // Notes: The buffer should not be written by the caller.
  352. //
  353. //----------------------------------------------------------------------------
  354. STDMETHODIMP CCiCDocName::GetNameBuffer(
  355. BYTE const * * ppbName,
  356. ULONG * pcbName )
  357. {
  358. Win4Assert( pcbName );
  359. Win4Assert( ppbName );
  360. if ( !_fIsInit )
  361. return CI_E_NOT_INITIALIZED;
  362. *ppbName = (BYTE *) _pwszPath;
  363. *pcbName = ComputeBufSize();
  364. return S_OK;
  365. }