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