xref: /trunk/main/sot/source/sdstor/stgstrms.hxx (revision 297a844a)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef _STGSTRMS_HXX
25 #define _STGSTRMS_HXX
26 
27 #ifndef _TOOLS_STREAM_HXX
28 #include <tools/stream.hxx>
29 #endif
30 
31 class StgIo;
32 class StgStrm;
33 class StgPage;
34 class StgDirEntry;
35 
36 // The FAT class performs FAT operations on an underlying storage stream.
37 // This stream is either the physical FAT stream (bPhys == sal_True ) or a normal
38 // storage stream, which then holds the FAT for small data allocations.
39 
40 class StgFAT
41 {										// FAT allocator
42 	StgStrm& rStrm;						// underlying stream
43 	sal_Int32 nMaxPage;						// highest page allocated so far
44 	short nPageSize;					// physical page size
45 	short nEntries;						// FAT entries per page
46 	short nOffset;						// current offset within page
47 	sal_Int32 nLimit;						// search limit recommendation
48 	sal_Bool  bPhys;						// sal_True: physical FAT
49 	StgPage* GetPhysPage( sal_Int32 nPage );
50 	sal_Bool  MakeChain( sal_Int32 nStart, sal_Int32 nPages );
51 	sal_Bool  InitNew( sal_Int32 nPage1 );
52 public:
53 	StgFAT( StgStrm& rStrm, sal_Bool bMark );
54 	sal_Int32 FindBlock( sal_Int32& nPages );
55 	sal_Int32 GetNextPage( sal_Int32 nPg );
56 	sal_Int32 AllocPages( sal_Int32 nStart, sal_Int32 nPages );
57 	sal_Bool  FreePages( sal_Int32 nStart, sal_Bool bAll );
GetMaxPage()58 	sal_Int32 GetMaxPage() { return nMaxPage; }
SetLimit(sal_Int32 n)59 	void  SetLimit( sal_Int32 n ) { nLimit = n; }
60 };
61 
62 // The base stream class provides basic functionality for seeking
63 // and accessing the data on a physical basis. It uses the built-in
64 // FAT class for the page allocations.
65 
66 class StgStrm {							// base class for all streams
67 protected:
68 	StgIo& rIo;							// I/O system
69 	StgFAT* pFat;						// FAT stream for allocations
70 	StgDirEntry* pEntry;				// dir entry (for ownership)
71 	sal_Int32 nStart;						// 1st data page
72 	sal_Int32 nSize;						// stream size in bytes
73 	sal_Int32 nPos;							// current byte position
74 	sal_Int32 nPage;						// current logical page
75 	short nOffset;						// offset into current page
76 	short nPageSize;					// logical page size
77 	sal_Bool  Copy( sal_Int32 nFrom, sal_Int32 nBytes );
78 	StgStrm( StgIo& );
79 public:
80 	virtual ~StgStrm();
GetIo()81 	StgIo&  GetIo()		{ return rIo;	 }
GetPos()82 	sal_Int32   GetPos()	{ return nPos;   }
GetStart()83 	sal_Int32	GetStart()	{ return nStart; }
GetSize()84 	sal_Int32	GetSize()	{ return nSize;	 }
GetPage()85 	sal_Int32   GetPage()	{ return nPage;  }
GetPageSize()86 	short   GetPageSize() { return nPageSize; }
87 	sal_Int32	GetPages();
GetOffset()88 	short	GetOffset()	{ return nOffset;}
89 	void    SetEntry( StgDirEntry& );
90 	virtual sal_Bool SetSize( sal_Int32 );
91 	virtual sal_Bool Pos2Page( sal_Int32 nBytePos );
Read(void *,sal_Int32)92 	virtual sal_Int32 Read( void*, sal_Int32 ) 		  { return 0; }
Write(const void *,sal_Int32)93 	virtual sal_Int32 Write( const void*, sal_Int32 ) { return 0; }
94 	virtual StgPage* GetPhysPage( sal_Int32 nBytePos, sal_Bool bForce = sal_False );
IsSmallStrm()95 	virtual sal_Bool IsSmallStrm() { return sal_False; }
96 };
97 
98 // The FAT stream class provides physical access to the master FAT.
99 // Since this access is implemented as a StgStrm, we can use the
100 // FAT allocator.
101 
102 class StgFATStrm : public StgStrm {		// the master FAT stream
103 	virtual sal_Bool Pos2Page( sal_Int32 nBytePos );
104 	sal_Bool  SetPage( short, sal_Int32 );
105 public:
106 	StgFATStrm( StgIo& );
~StgFATStrm()107     virtual ~StgFATStrm() {}
108     using StgStrm::GetPage;
109 	sal_Int32 GetPage( short, sal_Bool, sal_uInt16 *pnMasterAlloc = 0);
110 	virtual sal_Bool SetSize( sal_Int32 );
111 	virtual StgPage* GetPhysPage( sal_Int32 nBytePos, sal_Bool bForce = sal_False );
112 };
113 
114 // The stream has a size increment which normally is 1, but which can be
115 // set to any value is you want the size to be incremented by certain values.
116 
117 class StgDataStrm : public StgStrm		// a physical data stream
118 {
119 	short nIncr;						// size adjust increment
120 	void Init( sal_Int32 nBgn, sal_Int32 nLen );
121 public:
122 	StgDataStrm( StgIo&, sal_Int32 nBgn, sal_Int32 nLen=-1 );
123 	StgDataStrm( StgIo&, StgDirEntry& );
124 	void* GetPtr( sal_Int32 nPos, sal_Bool bForce, sal_Bool bDirty );
SetIncrement(short n)125 	void SetIncrement( short n ) { nIncr = n ; }
126 	virtual sal_Bool SetSize( sal_Int32 );
127 	virtual sal_Int32 Read( void*, sal_Int32 );
128 	virtual sal_Int32 Write( const void*, sal_Int32 );
129 };
130 
131 // The small stream class provides access to streams with a size < 4096 bytes.
132 // This stream is a StgStream containing small pages. The FAT for this stream
133 // is also a StgStream. The start of the FAT is in the header at DataRootPage,
134 // the stream itself is pointed to by the root entry (it holds start & size).
135 
136 class StgSmallStrm : public StgStrm		// a logical data stream
137 {
138 	StgStrm* pData;						// the data stream
139 	void Init( sal_Int32 nBgn, sal_Int32 nLen );
140 public:
141 	StgSmallStrm( StgIo&, sal_Int32 nBgn, sal_Int32 nLen );
142 	StgSmallStrm( StgIo&, StgDirEntry& );
143 	virtual sal_Int32 Read( void*, sal_Int32 );
144 	virtual sal_Int32 Write( const void*, sal_Int32 );
IsSmallStrm()145 	virtual sal_Bool IsSmallStrm() { return sal_True; }
146 };
147 
148 class StgTmpStrm : public SvMemoryStream
149 {
150 	String aName;
151 	SvFileStream* pStrm;
152     using SvMemoryStream::GetData;
153 	virtual sal_uLong GetData( void* pData, sal_uLong nSize );
154 	virtual sal_uLong PutData( const void* pData, sal_uLong nSize );
155 	virtual sal_uLong SeekPos( sal_uLong nPos );
156 	virtual void FlushData();
157 
158 public:
159 	StgTmpStrm( sal_uLong=16 );
160    ~StgTmpStrm();
161 	sal_Bool Copy( StgTmpStrm& );
162 	void SetSize( sal_uLong );
163     sal_uLong GetSize() const;
164 };
165 
166 #endif
167