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