OWLNext    7.0
Borland's Object Windows Library for the modern age
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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
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
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//
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//
156{
158
159 return StreamI->Read(pv, cb, read);
160}
161
162//
163//
164//
167{
169
170 return StreamI->Write(pv, cb, written);
171}
172
173//
174//
175//
183
184//
185//
186//
189{
190 return StreamI->SetSize(*(ULARGE_INTEGER*)&newSize);
191}
192
193//
194//
195//
198{
200
201 return StreamI->CopyTo(stream.GetIStream(), *(ULARGE_INTEGER*)&cb, (ULARGE_INTEGER*)read,
203}
204
205//
206//
207//
213
214//
215//
216//
219{
220 return StreamI->Revert();
221}
222
223//
224//
225//
231
232//
233//
234//
240
241//
242//
243//
251
252//
253//
254//
257{
259
260 return StreamI->Clone(stream);
261}
262
263//----------------------------------------------------------------------------
264// TOcStorage
265
266//
267//
268//
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;
286 }
287 else {
288 hr = ::StgOpenStorage(OleStr(fileName), 0, mode, 0, 0, &StorageI);
289 }
292}
293
294//
295//
296//
298{
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;
312 }
313 else {
314 hr = ::StgOpenStorageOnILockBytes(lkbyt, 0, // IStorage* priority???
315 mode, 0, 0, &StorageI);
316 }
319}
320
321//
322//
323//
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//
396
397//
398//
399//
402{
403 return StorageI->MoveElementTo(OleStr(name), dest.GetIStorage(), OleStr(newName), flags);
404}
405
406//
407//
408//
414
415//
416//
417//
420{
421 return StorageI->Revert();
422}
423
424//
425//
426//
432
433//
434//
435//
438{
440
441 return StorageI->DestroyElement(OleStr(name));
442}
443
444//
445//
446//
452
453//
454//
455//
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//
467{
468 return StorageI->SetClass(clsid);
469}
470
471//
472//
473//
479
480//
481//
482//
490
491//
492//
493//
496{
497 if (!newPath)
498 return HR_INVALIDARG;
499
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//
519
520//
521//
522//
525{
527
528 return StorageI->OpenStream(OleStr(name), rsrvd1, mode, rsrvd2, stream);
529}
530
531//
532//
533//
541
542//
543//
544//
552
553//
554//
555//
558{
560
561 return ::StgIsStorageFile(OleStr(name));
562}
563
564//
565//
566//
569{
571
572 return ::StgIsStorageILockBytes(lkbyt);
573}
574
575//
576//
577//
579TOcStorage::SetTimes(LPCTSTR name, FILETIME const * ctime, FILETIME const * atime, FILETIME const * mtime)
580{
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
#define DIAG_DEFINE_GROUP_INIT(f, g, e, l)
Definition checks.h:429
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
#define _T(x)
Definition cygwin.h:51
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
Object Windows Library (OWLNext Core)
Definition animctrl.h:22
__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
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
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