home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 May / Pcwk5b98.iso / Borland / Cplus45 / BC45 / OCFSRC.PAK / OCSTORAG.CPP < prev    next >
C/C++ Source or Header  |  1995-08-29  |  12KB  |  581 lines

  1. //
  2. //----------------------------------------------------------------------------
  3. // ObjectComponents
  4. // (C) Copyright 1994 by Borland International, All Rights Reserved
  5. //
  6. //   Implementation of TOcStorage & TOcStream IStorage/IStream encapsulation
  7. //----------------------------------------------------------------------------
  8. #include <ocf/ocfpch.h>
  9. #include <ocf/ocobject.h>
  10. #include <ocf/ocstorag.h>
  11. #include <osl/geometry.h>
  12. #include <iostream.h>
  13.  
  14. DIAG_DEFINE_GROUP(OcExcept, true, 1);
  15.  
  16. // Simple refcount debug assistant
  17. //
  18. #if defined(CHECK_REFCOUNT)
  19. static void RefCountCheck(IStorage far* si) {
  20.   uint32 count = si->AddRef();
  21.   count = si->Release();
  22. }
  23. #else
  24. # define RefCountCheck(si)
  25. #endif
  26.  
  27. //
  28. // Storage sharing mode bit mask
  29. //
  30. #define STGM_SHARE_MASK    (STGM_SHARE_DENY_NONE | STGM_SHARE_DENY_READ| \
  31.                             STGM_SHARE_DENY_WRITE | STGM_SHARE_EXCLUSIVE)
  32.  
  33. //----------------------------------------------------------------------------
  34. // TXObjComp
  35.  
  36.  
  37. TXObjComp::~TXObjComp()
  38. {
  39. }
  40.  
  41. TXObjComp* TXObjComp::Clone()
  42. {
  43.   return new TXObjComp(*this);
  44. }
  45.  
  46. void TXObjComp::Throw()
  47. {
  48.   THROW( *this );
  49. }
  50.  
  51. void TXObjComp::Check(HRESULT stat, TError err, const char far* arg)
  52. {
  53.   if (FAILED(stat))
  54.     Throw(err, stat, arg);
  55. }
  56.  
  57. static char* sObjCompErrMsgs[] = {
  58.   "",                                           // xNoError
  59.   "Could not locate " BOLEDLL,                  // xBOleLoadFail
  60.   "Incompatible version of " BOLEDLL,           // xBOleVersFail
  61.   "Could not obtain BOle ClassMgr",             // xBOleBindFail
  62.   "Document factory [un]registration failed",   // xDocFactoryFail
  63.   "Missing Root IStorage in OcDocument",        // xMissingRootIStorage
  64.   "Internal OcPart creation error",             // xInternalPartError
  65.   "OcPart initialization failure",              // xPartInitError
  66.   "Unable to open/create RootStorage on '%s'",  // xRootStorageOpenError
  67.   "Unable to open/create Storage '%s'",         // xStorageOpenError
  68.   "Unable to open/create Storage on ILockBytes",// xStorageILockError
  69.   "Unable to open/create Stream '%s'"           // xStreamOpenError
  70. };
  71.  
  72. void TXObjComp::Throw(TError err, HRESULT hr, const char far* arg)
  73. {
  74.   if (!InstanceCount) {
  75.     char buf[128+1];
  76.     wsprintf(buf, sObjCompErrMsgs[err], arg);
  77.  
  78.     if (hr != HR_FAIL) {  // generic error, dont bother with message.
  79.       strcat(buf, ": ");
  80.       int len = strlen(buf);
  81.       OleErrorFromCode(hr, buf + len, sizeof buf - len - 2);
  82.     }
  83.     strcat(buf, ".");
  84.  
  85.     WARNX(OcExcept, hr != HR_NOERROR, 0, buf);
  86.     throw TXObjComp(err, buf, hr);
  87.   }
  88. }
  89.  
  90. //----------------------------------------------------------------------------
  91. // TOcStream
  92.  
  93. //
  94. //
  95. //
  96. TOcStream::TOcStream(TOcStorage& storage, const char far* name, bool create, uint32 mode)
  97. {
  98.   // Make sure that the transacted mode is off since streams don't support
  99.   // this mode. Also make sure that the stream is opened in exclusive mode.
  100.   //
  101.   mode &= ~STGM_TRANSACTED;
  102.   mode = (mode & ~STGM_SHARE_MASK) | STGM_SHARE_EXCLUSIVE;
  103.  
  104.   HRESULT hr = storage.OpenStream(name, 0, mode, 0, &StreamI);
  105.   if (!StreamI && create)
  106.     hr = storage.CreateStream(name, mode, 0, 0, &StreamI);
  107.   TXObjComp::Check(hr, TXObjComp::xStreamOpenError, name);
  108. }
  109.  
  110. //
  111. //
  112. //
  113. TOcStream::TOcStream(TOcStream& stream)
  114. {
  115.   stream.Clone(&StreamI);
  116.   // if (!StreamI) throw...?
  117. }
  118.  
  119. //
  120. //
  121. //
  122. TOcStream::TOcStream(IStream* stream)
  123. :
  124.   StreamI(stream)
  125. {
  126. }
  127.  
  128. //
  129. //
  130. //
  131. TOcStream::~TOcStream()
  132. {
  133.   if (StreamI)
  134.     StreamI->Release();
  135. }
  136.  
  137. //
  138. //
  139. //
  140. IStream*
  141. TOcStream::GetIStream()
  142. {
  143.   return StreamI;
  144. }
  145.  
  146. //
  147. //
  148. //
  149. HRESULT
  150. TOcStream::Read(void HUGE* pv, ulong cb, ulong far* read)
  151. {
  152.   PRECONDITION(pv && read && StreamI);
  153.  
  154.   return StreamI->Read(pv, cb, read);
  155. }
  156.  
  157. //
  158. //
  159. //
  160. HRESULT
  161. TOcStream::Write(void const HUGE* pv, ulong cb, ulong far* written)
  162. {
  163.   PRECONDITION(pv && written && StreamI);
  164.  
  165.   return StreamI->Write(pv, cb, written);
  166. }
  167.  
  168. //
  169. //
  170. //
  171. HRESULT
  172. TOcStream::Seek(int64 move, uint32 origin, uint64 far* newPosition)
  173. {
  174.   PRECONDITION(newPosition && StreamI);
  175.  
  176.   return StreamI->Seek(move, origin, (ULARGE_INTEGER*)newPosition);
  177. }
  178.  
  179. //
  180. //
  181. //
  182. HRESULT
  183. TOcStream::SetSize(uint64 newSize)
  184. {
  185.   return StreamI->SetSize(newSize);
  186. }
  187.  
  188. //
  189. //
  190. //
  191. HRESULT
  192. TOcStream::CopyTo(TOcStream& stream, uint64 cb, uint64 far* read, uint64 far* written)
  193. {
  194.   PRECONDITION(read);
  195.  
  196.   return StreamI->CopyTo(stream.GetIStream(), cb, (ULARGE_INTEGER*)read,
  197.                          (ULARGE_INTEGER*)written);
  198. }
  199.  
  200. //
  201. //
  202. //
  203. HRESULT
  204. TOcStream::Commit(uint32 commitFlags)
  205. {
  206.   return StreamI->Commit(commitFlags);
  207. }
  208.  
  209. //
  210. //
  211. //
  212. HRESULT
  213. TOcStream::Revert()
  214. {
  215.   return StreamI->Revert();
  216. }
  217.  
  218. //
  219. //
  220. //
  221. HRESULT
  222. TOcStream::LockRegion(uint64 offset, uint64 cb, uint32 lockType)
  223. {
  224.   return StreamI->LockRegion(offset, cb, lockType);
  225. }
  226.  
  227. //
  228. //
  229. //
  230. HRESULT
  231. TOcStream::UnlockRegion(uint64 offset, uint64 cb, uint32 lockType)
  232. {
  233.   return StreamI->UnlockRegion(offset, cb, lockType);
  234. }
  235.  
  236. //
  237. //
  238. //
  239. HRESULT
  240. TOcStream::Stat(STATSTG far* statstg, uint32 statFlag)
  241. {
  242.   PRECONDITION(statstg);
  243.  
  244.   return StreamI->Stat(statstg, statFlag);
  245. }
  246.  
  247. //
  248. //
  249. //
  250. HRESULT
  251. TOcStream::Clone(IStream far* far* stream)
  252. {
  253.   PRECONDITION(stream);
  254.  
  255.   return StreamI->Clone(stream);
  256. }
  257.  
  258. //----------------------------------------------------------------------------
  259. // TOcStorage
  260.  
  261. //
  262. //
  263. //
  264. TOcStorage::TOcStorage(const char far* fileName, bool create, uint32 mode)
  265. {
  266. //  Parent = 0;
  267.  
  268.   // Fill in the sharing mode based on the access
  269.   //
  270.   if ((mode & STGM_WRITE) || (mode & STGM_READWRITE))
  271.     mode = (mode & ~STGM_SHARE_MASK) | STGM_SHARE_DENY_WRITE;
  272.   else
  273.     mode = (mode & ~STGM_SHARE_MASK) | STGM_SHARE_DENY_NONE;
  274.   
  275.   HRESULT hr;
  276.   if (create) {
  277.     mode |= STGM_CREATE;
  278.     if (!fileName)
  279.       mode |= STGM_DELETEONRELEASE;
  280.     hr = ::StgCreateDocfile(OleStr(fileName), mode, 0, &StorageI);
  281.   }
  282.   else {
  283.     hr = ::StgOpenStorage(OleStr(fileName), 0, mode, 0, 0, &StorageI);
  284.   }
  285.   RefCountCheck(StorageI);
  286.   TXObjComp::Check(hr, TXObjComp::xRootStorageOpenError, fileName);
  287. }
  288.  
  289. //
  290. //
  291. //
  292. TOcStorage::TOcStorage(ILockBytes far* lkbyt, bool create, uint32 mode)
  293. {
  294.   PRECONDITION(lkbyt);
  295.  
  296.   // Fill in the sharing mode based on the access
  297.   //
  298.   if ((mode & STGM_WRITE) || (mode & STGM_READWRITE))
  299.     mode = (mode & ~STGM_SHARE_MASK) | STGM_SHARE_DENY_WRITE;
  300.   else
  301.     mode = (mode & ~STGM_SHARE_MASK) | STGM_SHARE_DENY_NONE;
  302.   
  303.   HRESULT hr;
  304.   if (create) {
  305.     mode |= STGM_CREATE;
  306.     hr = ::StgCreateDocfileOnILockBytes(lkbyt, mode, 0, &StorageI);
  307.   }
  308.   else {
  309.     hr = ::StgOpenStorageOnILockBytes(lkbyt, 0,  // IStorage* priority???
  310.              mode, 0, 0, &StorageI);
  311.   }
  312.   RefCountCheck(StorageI);
  313.   TXObjComp::Check(hr, TXObjComp::xStorageILockError);
  314. }
  315.  
  316. //
  317. //
  318. //
  319. TOcStorage::TOcStorage(TOcStorage& parent, const char far* name, bool create, uint32 mode)
  320. {
  321. //  Parent = &parent;
  322.   mode = (mode & ~STGM_SHARE_MASK) | STGM_SHARE_EXCLUSIVE;
  323.   HRESULT hr;
  324.   hr = parent.OpenStorage(name, 0, mode, 0, 0, &StorageI);
  325.   if (!StorageI && create)
  326.     hr = parent.CreateStorage(name, mode, 0, 0, &StorageI);
  327.  
  328.   RefCountCheck(StorageI);
  329.   TXObjComp::Check(hr, TXObjComp::xStorageOpenError, name);
  330. }
  331.  
  332. //
  333. //
  334. //
  335. TOcStorage::TOcStorage(IStorage* storage)
  336. :
  337.   StorageI(storage)
  338. {
  339.   if (StorageI) {
  340.     StorageI->AddRef();
  341.     RefCountCheck(StorageI);
  342.   }
  343. }
  344.  
  345. //
  346. //
  347. //
  348. TOcStorage::~TOcStorage()
  349. {
  350.   if (StorageI) {
  351.     RefCountCheck(StorageI);
  352.     StorageI->Release();
  353.   }
  354. }
  355.  
  356. //
  357. //
  358. //
  359. ulong
  360. TOcStorage::AddRef()
  361. {
  362.   return StorageI? StorageI->AddRef() : 0;
  363. }
  364.  
  365. //
  366. //
  367. //
  368. ulong
  369. TOcStorage::Release()
  370. {
  371.   return StorageI? StorageI->Release() : 0;
  372. }
  373.  
  374. //
  375. //
  376. //
  377. IStorage*
  378. TOcStorage::GetIStorage()
  379. {
  380.   return StorageI;
  381. }
  382.  
  383. //
  384. //
  385. //
  386. HRESULT
  387. TOcStorage::CopyTo(uint32 ciidExclude, IID const far* rgiidExclude, SNB snbExclude, TOcStorage& dest)
  388. {
  389.   return StorageI->CopyTo(ciidExclude, rgiidExclude, snbExclude, dest.GetIStorage());
  390. }
  391.  
  392. //
  393. //
  394. //
  395. HRESULT
  396. TOcStorage::MoveElementTo(char const far* name, TOcStorage& dest, char const far* newName, uint32 flags)
  397. {
  398.   return StorageI->MoveElementTo(OleStr(name), dest.GetIStorage(), OleStr(newName), flags);
  399. }
  400.  
  401. //
  402. //
  403. //
  404. HRESULT
  405. TOcStorage::Commit(uint32 commitFlags)
  406. {
  407.   return StorageI->Commit(commitFlags);
  408. }
  409.  
  410. //
  411. //
  412. //
  413. HRESULT
  414. TOcStorage::Revert()
  415. {
  416.   return StorageI->Revert();
  417. }
  418.  
  419. //
  420. //
  421. //
  422. HRESULT
  423. TOcStorage::EnumElements(uint32 rsrvd1, void far* rsrvd2, uint32 rsrvd3, IEnumSTATSTG far*far* enm)
  424. {
  425.   return StorageI->EnumElements(rsrvd1, rsrvd2, rsrvd3, enm);
  426. }
  427.  
  428. //
  429. //
  430. //
  431. HRESULT
  432. TOcStorage::DestroyElement(const char far* name)
  433. {
  434.   PRECONDITION(name);
  435.  
  436.   return StorageI->DestroyElement(OleStr(name));
  437. }
  438.  
  439. //
  440. //
  441. //
  442. HRESULT
  443. TOcStorage::RenameElement(const char far* oldName, const char far* newName)
  444. {
  445.   return StorageI->RenameElement(OleStr(oldName), OleStr(newName));
  446. }
  447.  
  448. //
  449. //
  450. //
  451. HRESULT
  452. TOcStorage::SetElementTimes(const char far* name, FILETIME const far* ctime, FILETIME const far* atime, FILETIME const far* mtime)
  453. {
  454.   return StorageI->SetElementTimes(OleStr(name), ctime, atime, mtime);
  455. }
  456.  
  457. //
  458. //
  459. //
  460. HRESULT
  461. TOcStorage::SetClass(const IID far& clsid)
  462. {
  463.   return StorageI->SetClass(clsid);
  464. }
  465.  
  466. //
  467. //
  468. //
  469. HRESULT
  470. TOcStorage::SetStateBits(uint32 stateBits, uint32 mask)
  471. {
  472.   return StorageI->SetStateBits(stateBits, mask);
  473. }
  474.  
  475. //
  476. //
  477. //
  478. HRESULT
  479. TOcStorage::Stat(STATSTG far* statstg, uint32 statFlag)
  480. {
  481.   PRECONDITION(statstg);
  482.  
  483.   return StorageI->Stat(statstg, statFlag);
  484. }
  485.  
  486. //
  487. //
  488. //
  489. HRESULT
  490. TOcStorage::SwitchToFile(const char far* newPath)
  491. {
  492.   if (!newPath)
  493.     return HR_INVALIDARG;
  494.  
  495.   IRootStorage* rootStorageI;
  496.   HRESULT hr = StorageI->QueryInterface(IID_IRootStorage, &(void far*)rootStorageI);
  497.   if (HRSucceeded(hr)) {
  498.     hr = rootStorageI->SwitchToFile(OleStr(const_cast<char far*>(newPath)));
  499.     rootStorageI->Release();
  500.   }
  501.   return hr;
  502. }
  503.  
  504. //
  505. //
  506. //
  507. HRESULT
  508. TOcStorage::CreateStream(const char far* name, uint32 mode, uint32 rsrvd1, uint32 rsrvd2, IStream far* far* stream)
  509. {
  510.   PRECONDITION(name);
  511.  
  512.   return StorageI->CreateStream(OleStr(name), mode, rsrvd1, rsrvd2, stream);
  513. }
  514.  
  515. //
  516. //
  517. //
  518. HRESULT
  519. TOcStorage::OpenStream(const char far* name, void far *rsrvd1, uint32 mode, uint32 rsrvd2, IStream far*far* stream)
  520. {
  521.   PRECONDITION(name);
  522.  
  523.   return StorageI->OpenStream(OleStr(name), rsrvd1, mode, rsrvd2, stream);
  524. }
  525.  
  526. //
  527. //
  528. //
  529. HRESULT
  530. TOcStorage::CreateStorage(const char far* name, uint32 mode, uint32 rsrvd1, uint32 rsrvd2, IStorage far*far* storage)
  531. {
  532.   PRECONDITION(StorageI && name);
  533.  
  534.   return StorageI->CreateStorage(OleStr(name), mode, rsrvd1, rsrvd2, storage);
  535. }
  536.  
  537. //
  538. //
  539. //
  540. HRESULT
  541. TOcStorage::OpenStorage(const char far* name, IStorage far* stgPriority, uint32 mode, SNB snbExclude, uint32 rsrvd, IStorage far*far* storage)
  542. {
  543.   PRECONDITION(name);
  544.  
  545.   return StorageI->OpenStorage(OleStr(name), stgPriority, mode, snbExclude, rsrvd, storage);
  546. }
  547.  
  548. //
  549. //
  550. //
  551. HRESULT
  552. TOcStorage::IsStorageFile(const char far* name)
  553. {
  554.   PRECONDITION(name);
  555.  
  556.   return ::StgIsStorageFile(OleStr(name));
  557. }
  558.  
  559. //
  560. //
  561. //
  562. HRESULT
  563. TOcStorage::IsStorageILockBytes(ILockBytes far* lkbyt)
  564. {
  565.   PRECONDITION(lkbyt);
  566.  
  567.   return ::StgIsStorageILockBytes(lkbyt);
  568. }
  569.  
  570. //
  571. //
  572. //
  573. HRESULT
  574. TOcStorage::SetTimes(char const far* name, FILETIME const far* ctime, FILETIME const far* atime, FILETIME const far* mtime)
  575. {
  576.   PRECONDITION(name);
  577.  
  578.   return ::StgSetTimes(OleStr(name), ctime, atime, mtime);
  579. }
  580.  
  581.