xref: /aoo41x/main/sot/source/sdstor/stgstrms.hxx (revision cdf0e10c)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir #ifndef _STGSTRMS_HXX
29*cdf0e10cSrcweir #define _STGSTRMS_HXX
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #ifndef _TOOLS_STREAM_HXX
32*cdf0e10cSrcweir #include <tools/stream.hxx>
33*cdf0e10cSrcweir #endif
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir class StgIo;
36*cdf0e10cSrcweir class StgStrm;
37*cdf0e10cSrcweir class StgPage;
38*cdf0e10cSrcweir class StgDirEntry;
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir // The FAT class performs FAT operations on an underlying storage stream.
41*cdf0e10cSrcweir // This stream is either the physical FAT stream (bPhys == sal_True ) or a normal
42*cdf0e10cSrcweir // storage stream, which then holds the FAT for small data allocations.
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir class StgFAT
45*cdf0e10cSrcweir {										// FAT allocator
46*cdf0e10cSrcweir 	StgStrm& rStrm;						// underlying stream
47*cdf0e10cSrcweir 	sal_Int32 nMaxPage;						// highest page allocated so far
48*cdf0e10cSrcweir 	short nPageSize;					// physical page size
49*cdf0e10cSrcweir 	short nEntries;						// FAT entries per page
50*cdf0e10cSrcweir 	short nOffset;						// current offset within page
51*cdf0e10cSrcweir 	sal_Int32 nLimit;						// search limit recommendation
52*cdf0e10cSrcweir 	sal_Bool  bPhys;						// sal_True: physical FAT
53*cdf0e10cSrcweir 	StgPage* GetPhysPage( sal_Int32 nPage );
54*cdf0e10cSrcweir 	sal_Bool  MakeChain( sal_Int32 nStart, sal_Int32 nPages );
55*cdf0e10cSrcweir 	sal_Bool  InitNew( sal_Int32 nPage1 );
56*cdf0e10cSrcweir public:
57*cdf0e10cSrcweir 	StgFAT( StgStrm& rStrm, sal_Bool bMark );
58*cdf0e10cSrcweir 	sal_Int32 FindBlock( sal_Int32& nPages );
59*cdf0e10cSrcweir 	sal_Int32 GetNextPage( sal_Int32 nPg );
60*cdf0e10cSrcweir 	sal_Int32 AllocPages( sal_Int32 nStart, sal_Int32 nPages );
61*cdf0e10cSrcweir 	sal_Bool  FreePages( sal_Int32 nStart, sal_Bool bAll );
62*cdf0e10cSrcweir 	sal_Int32 GetMaxPage() { return nMaxPage; }
63*cdf0e10cSrcweir 	void  SetLimit( sal_Int32 n ) { nLimit = n; }
64*cdf0e10cSrcweir };
65*cdf0e10cSrcweir 
66*cdf0e10cSrcweir // The base stream class provides basic functionality for seeking
67*cdf0e10cSrcweir // and accessing the data on a physical basis. It uses the built-in
68*cdf0e10cSrcweir // FAT class for the page allocations.
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir class StgStrm {							// base class for all streams
71*cdf0e10cSrcweir protected:
72*cdf0e10cSrcweir 	StgIo& rIo;							// I/O system
73*cdf0e10cSrcweir 	StgFAT* pFat;						// FAT stream for allocations
74*cdf0e10cSrcweir 	StgDirEntry* pEntry;				// dir entry (for ownership)
75*cdf0e10cSrcweir 	sal_Int32 nStart;						// 1st data page
76*cdf0e10cSrcweir 	sal_Int32 nSize;						// stream size in bytes
77*cdf0e10cSrcweir 	sal_Int32 nPos;							// current byte position
78*cdf0e10cSrcweir 	sal_Int32 nPage;						// current logical page
79*cdf0e10cSrcweir 	short nOffset;						// offset into current page
80*cdf0e10cSrcweir 	short nPageSize;					// logical page size
81*cdf0e10cSrcweir 	sal_Bool  Copy( sal_Int32 nFrom, sal_Int32 nBytes );
82*cdf0e10cSrcweir 	StgStrm( StgIo& );
83*cdf0e10cSrcweir public:
84*cdf0e10cSrcweir 	virtual ~StgStrm();
85*cdf0e10cSrcweir 	StgIo&  GetIo()		{ return rIo;	 }
86*cdf0e10cSrcweir 	sal_Int32   GetPos()	{ return nPos;   }
87*cdf0e10cSrcweir 	sal_Int32	GetStart()	{ return nStart; }
88*cdf0e10cSrcweir 	sal_Int32	GetSize()	{ return nSize;	 }
89*cdf0e10cSrcweir 	sal_Int32   GetPage()	{ return nPage;  }
90*cdf0e10cSrcweir 	short   GetPageSize() { return nPageSize; }
91*cdf0e10cSrcweir 	sal_Int32	GetPages();
92*cdf0e10cSrcweir 	short	GetOffset()	{ return nOffset;}
93*cdf0e10cSrcweir 	void    SetEntry( StgDirEntry& );
94*cdf0e10cSrcweir 	virtual sal_Bool SetSize( sal_Int32 );
95*cdf0e10cSrcweir 	virtual sal_Bool Pos2Page( sal_Int32 nBytePos );
96*cdf0e10cSrcweir 	virtual sal_Int32 Read( void*, sal_Int32 ) 		  { return 0; }
97*cdf0e10cSrcweir 	virtual sal_Int32 Write( const void*, sal_Int32 ) { return 0; }
98*cdf0e10cSrcweir 	virtual StgPage* GetPhysPage( sal_Int32 nBytePos, sal_Bool bForce = sal_False );
99*cdf0e10cSrcweir 	virtual sal_Bool IsSmallStrm() { return sal_False; }
100*cdf0e10cSrcweir };
101*cdf0e10cSrcweir 
102*cdf0e10cSrcweir // The FAT stream class provides physical access to the master FAT.
103*cdf0e10cSrcweir // Since this access is implemented as a StgStrm, we can use the
104*cdf0e10cSrcweir // FAT allocator.
105*cdf0e10cSrcweir 
106*cdf0e10cSrcweir class StgFATStrm : public StgStrm {		// the master FAT stream
107*cdf0e10cSrcweir 	virtual sal_Bool Pos2Page( sal_Int32 nBytePos );
108*cdf0e10cSrcweir 	sal_Bool  SetPage( short, sal_Int32 );
109*cdf0e10cSrcweir public:
110*cdf0e10cSrcweir 	StgFATStrm( StgIo& );
111*cdf0e10cSrcweir     virtual ~StgFATStrm() {}
112*cdf0e10cSrcweir     using StgStrm::GetPage;
113*cdf0e10cSrcweir 	sal_Int32 GetPage( short, sal_Bool, sal_uInt16 *pnMasterAlloc = 0);
114*cdf0e10cSrcweir 	virtual sal_Bool SetSize( sal_Int32 );
115*cdf0e10cSrcweir 	virtual StgPage* GetPhysPage( sal_Int32 nBytePos, sal_Bool bForce = sal_False );
116*cdf0e10cSrcweir };
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir // The stream has a size increment which normally is 1, but which can be
119*cdf0e10cSrcweir // set to any value is you want the size to be incremented by certain values.
120*cdf0e10cSrcweir 
121*cdf0e10cSrcweir class StgDataStrm : public StgStrm		// a physical data stream
122*cdf0e10cSrcweir {
123*cdf0e10cSrcweir 	short nIncr;						// size adjust increment
124*cdf0e10cSrcweir 	void Init( sal_Int32 nBgn, sal_Int32 nLen );
125*cdf0e10cSrcweir public:
126*cdf0e10cSrcweir 	StgDataStrm( StgIo&, sal_Int32 nBgn, sal_Int32 nLen=-1 );
127*cdf0e10cSrcweir 	StgDataStrm( StgIo&, StgDirEntry* );
128*cdf0e10cSrcweir 	void* GetPtr( sal_Int32 nPos, sal_Bool bForce, sal_Bool bDirty );
129*cdf0e10cSrcweir 	void SetIncrement( short n ) { nIncr = n ; }
130*cdf0e10cSrcweir 	virtual sal_Bool SetSize( sal_Int32 );
131*cdf0e10cSrcweir 	virtual sal_Int32 Read( void*, sal_Int32 );
132*cdf0e10cSrcweir 	virtual sal_Int32 Write( const void*, sal_Int32 );
133*cdf0e10cSrcweir };
134*cdf0e10cSrcweir 
135*cdf0e10cSrcweir // The small stream class provides access to streams with a size < 4096 bytes.
136*cdf0e10cSrcweir // This stream is a StgStream containing small pages. The FAT for this stream
137*cdf0e10cSrcweir // is also a StgStream. The start of the FAT is in the header at DataRootPage,
138*cdf0e10cSrcweir // the stream itself is pointed to by the root entry (it holds start & size).
139*cdf0e10cSrcweir 
140*cdf0e10cSrcweir class StgSmallStrm : public StgStrm		// a logical data stream
141*cdf0e10cSrcweir {
142*cdf0e10cSrcweir 	StgStrm* pData;						// the data stream
143*cdf0e10cSrcweir 	void Init( sal_Int32 nBgn, sal_Int32 nLen );
144*cdf0e10cSrcweir public:
145*cdf0e10cSrcweir 	StgSmallStrm( StgIo&, sal_Int32 nBgn, sal_Int32 nLen );
146*cdf0e10cSrcweir 	StgSmallStrm( StgIo&, StgDirEntry* );
147*cdf0e10cSrcweir 	virtual sal_Int32 Read( void*, sal_Int32 );
148*cdf0e10cSrcweir 	virtual sal_Int32 Write( const void*, sal_Int32 );
149*cdf0e10cSrcweir 	virtual sal_Bool IsSmallStrm() { return sal_True; }
150*cdf0e10cSrcweir };
151*cdf0e10cSrcweir 
152*cdf0e10cSrcweir class StgTmpStrm : public SvMemoryStream
153*cdf0e10cSrcweir {
154*cdf0e10cSrcweir 	String aName;
155*cdf0e10cSrcweir 	SvFileStream* pStrm;
156*cdf0e10cSrcweir     using SvMemoryStream::GetData;
157*cdf0e10cSrcweir 	virtual sal_uLong GetData( void* pData, sal_uLong nSize );
158*cdf0e10cSrcweir 	virtual sal_uLong PutData( const void* pData, sal_uLong nSize );
159*cdf0e10cSrcweir 	virtual sal_uLong SeekPos( sal_uLong nPos );
160*cdf0e10cSrcweir 	virtual void FlushData();
161*cdf0e10cSrcweir 
162*cdf0e10cSrcweir public:
163*cdf0e10cSrcweir 	StgTmpStrm( sal_uLong=16 );
164*cdf0e10cSrcweir    ~StgTmpStrm();
165*cdf0e10cSrcweir 	sal_Bool Copy( StgTmpStrm& );
166*cdf0e10cSrcweir 	void SetSize( sal_uLong );
167*cdf0e10cSrcweir     sal_uLong GetSize() const;
168*cdf0e10cSrcweir };
169*cdf0e10cSrcweir 
170*cdf0e10cSrcweir #endif
171