xref: /trunk/main/tools/inc/tools/stream.hxx (revision 5a41e572)
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 #ifndef _STREAM_HXX
24 #define _STREAM_HXX
25 
26 #include "tools/toolsdllapi.h"
27 #include <tools/solar.h>
28 #include <tools/string.hxx>
29 #ifndef _EINF_HXX
30 #include <tools/errinf.hxx>
31 #endif
32 #include <tools/ref.hxx>
33 #include <tools/rtti.hxx>
34 
35 class FileCopier;
36 class StreamData;
37 
38 // ------------------------
39 // - FileFormat-Functions -
40 // ------------------------
41 
GetStoreCharSet(rtl_TextEncoding eEncoding)42 inline rtl_TextEncoding GetStoreCharSet( rtl_TextEncoding eEncoding )
43 {
44 	if ( eEncoding == RTL_TEXTENCODING_ISO_8859_1 )
45 		return RTL_TEXTENCODING_MS_1252;
46 	else
47 		return eEncoding;
48 }
49 
50 // ---------------
51 // - StreamTypes -
52 // ---------------
53 
54 typedef sal_uInt16 StreamMode;
55 
56 // read, write, create,... options
57 #define STREAM_READ 					0x0001	// allow read accesses
58 #define STREAM_WRITE					0x0002	// allow write accesses
59 // file i/o
60 #define STREAM_NOCREATE 				0x0004	// 1 == Dont create file
61 #define STREAM_TRUNC					0x0008	// Truncate _existing_ file to zero length
62 #define STREAM_COPY_ON_SYMLINK			0x0010	// copy-on-write for symlinks (UNX)
63 
64 #define STREAM_READWRITEBITS			(STREAM_READ | STREAM_WRITE | \
65 										 STREAM_NOCREATE | STREAM_TRUNC)
66 
67 // sharing options
68 #define STREAM_SHARE_DENYNONE			0x0100
69 #define STREAM_SHARE_DENYREAD			0x0200	// overrides denynone
70 #define STREAM_SHARE_DENYWRITE		0x0400	// overrides denynone
71 #define STREAM_SHARE_DENYALL			0x0800	// overrides denyread,write,none
72 
73 #define STREAM_SHAREBITS				(STREAM_SHARE_DENYNONE | STREAM_SHARE_DENYREAD |\
74 										 STREAM_SHARE_DENYWRITE | STREAM_SHARE_DENYALL)
75 
76 #define STREAM_READWRITE				(STREAM_READ | STREAM_WRITE)
77 #define STREAM_SHARE_DENYREADWRITE		(STREAM_SHARE_DENYREAD | STREAM_SHARE_DENYWRITE)
78 
79 #define STREAM_STD_READ 				(STREAM_READ | STREAM_SHARE_DENYNONE | STREAM_NOCREATE)
80 #define STREAM_STD_WRITE				(STREAM_WRITE | STREAM_SHARE_DENYALL)
81 #define STREAM_STD_READWRITE			(STREAM_READWRITE | STREAM_SHARE_DENYALL)
82 
83 #define STREAM_SEEK_TO_BEGIN			0L
84 #define STREAM_SEEK_TO_END				ULONG_MAX
85 
86 #define NUMBERFORMAT_INT_BIGENDIAN		(sal_uInt16)0x0000
87 #define NUMBERFORMAT_INT_LITTLEENDIAN	(sal_uInt16)0xFFFF
88 
89 #define COMPRESSMODE_FULL			(sal_uInt16)0xFFFF
90 #define COMPRESSMODE_NONE			(sal_uInt16)0x0000
91 #define COMPRESSMODE_ZBITMAP			(sal_uInt16)0x0001
92 #define COMPRESSMODE_NATIVE 			(sal_uInt16)0x0010
93 
94 #define JUSTIFY_RIGHT				0x00
95 #define JUSTIFY_LEFT				0x01
96 
97 #define STREAM_IO_DONTKNOW			0
98 #define STREAM_IO_READ				1
99 #define STREAM_IO_WRITE 				2
100 
101 #define ID_STREAM						1
102 #define ID_FILESTREAM					2
103 #define ID_MEMORYSTREAM 				3
104 #define ID_SHAREDMEMORYSTREAM			4
105 #define ID_STORAGESTREAM				5
106 #define ID_PERSISTSTREAM				6
107 
108 class SvStream;
109 typedef SvStream& (*SvStrPtr)( SvStream& );
110 
111 // forward declaration with internal linkage
112 inline SvStream& operator<<( SvStream& rStr, SvStrPtr f );
113 
114 // ---------------
115 // - SvLockBytes -
116 // ---------------
117 
118 enum LockType {};
119 
120 struct SvLockBytesStat
121 {
122 	sal_Size nSize;
123 
SvLockBytesStatSvLockBytesStat124 	SvLockBytesStat(): nSize(0) {}
125 };
126 
127 enum SvLockBytesStatFlag { SVSTATFLAG_DEFAULT };
128 
129 class TOOLS_DLLPUBLIC SvLockBytes: public virtual SvRefBase
130 {
131 	SvStream * m_pStream;
132 	sal_Bool m_bOwner;
133 	sal_Bool m_bSync;
134 
135 protected:
136 	void close();
137 
138 public:
139 	TYPEINFO();
140 
SvLockBytes()141 	SvLockBytes(): m_pStream(0), m_bOwner(sal_False), m_bSync(sal_False) {}
142 
SvLockBytes(SvStream * pTheStream,sal_Bool bTheOwner=sal_False)143 	SvLockBytes(SvStream * pTheStream, sal_Bool bTheOwner = sal_False):
144 		m_pStream(pTheStream), m_bOwner(bTheOwner), m_bSync(sal_False) {}
145 
~SvLockBytes()146 	virtual ~SvLockBytes() { close(); }
147 
GetStream() const148 	virtual const SvStream * GetStream() const { return m_pStream; }
149 
SetSynchronMode(sal_Bool bTheSync=sal_True)150 	virtual void SetSynchronMode(sal_Bool bTheSync = sal_True) { m_bSync = bTheSync; }
151 
IsSynchronMode() const152 	virtual sal_Bool IsSynchronMode() const { return m_bSync; }
153 
154 	virtual ErrCode ReadAt(sal_Size nPos, void * pBuffer, sal_Size nCount,
155 						   sal_Size * pRead) const;
156 
157 	virtual ErrCode WriteAt(sal_Size nPos, const void * pBuffer, sal_Size nCount,
158 							sal_Size * pWritten);
159 
160 	virtual ErrCode Flush() const;
161 
162 	virtual ErrCode SetSize(sal_Size nSize);
163 
164 	virtual ErrCode LockRegion(sal_Size, sal_Size, LockType);
165 
166 	virtual ErrCode UnlockRegion(sal_Size, sal_Size, LockType);
167 
168 	virtual ErrCode Stat(SvLockBytesStat * pStat, SvLockBytesStatFlag) const;
169 };
170 
171 SV_DECL_IMPL_REF(SvLockBytes);
172 
173 // -------------------
174 // - SvOpenLockBytes -
175 // -------------------
176 
177 class TOOLS_DLLPUBLIC SvOpenLockBytes: public SvLockBytes
178 {
179 public:
180 	TYPEINFO();
181 
SvOpenLockBytes()182 	SvOpenLockBytes(): SvLockBytes(0, sal_False) {}
183 
SvOpenLockBytes(SvStream * pStream,sal_Bool bOwner)184 	SvOpenLockBytes(SvStream * pStream, sal_Bool bOwner):
185 		SvLockBytes(pStream, bOwner) {}
186 
187 	virtual ErrCode FillAppend(const void * pBuffer, sal_Size nCount,
188 							   sal_Size * pWritten) = 0;
189 
190 	virtual sal_Size Tell() const = 0;
191 
192 	virtual sal_Size Seek(sal_Size nPos) = 0;
193 
194 	virtual void Terminate() = 0;
195 };
196 
197 SV_DECL_IMPL_REF(SvOpenLockBytes);
198 
199 // --------------------
200 // - SvAsyncLockBytes -
201 // --------------------
202 
203 class SvAsyncLockBytes: public SvOpenLockBytes
204 {
205 	sal_Size m_nSize;
206 	sal_Bool m_bTerminated;
207 
208 public:
209 	TYPEINFO();
210 
SvAsyncLockBytes(SvStream * pStream,sal_Bool bOwner)211 	SvAsyncLockBytes(SvStream * pStream, sal_Bool bOwner):
212 		SvOpenLockBytes(pStream, bOwner), m_nSize(0), m_bTerminated(sal_False) {}
213 
214 	virtual ErrCode ReadAt(sal_Size nPos, void * pBuffer, sal_Size nCount,
215 						   sal_Size * pRead) const;
216 
217 	virtual ErrCode WriteAt(sal_Size nPos, const void * pBuffer, sal_Size nCount,
218 							sal_Size * pWritten);
219 
220 	virtual ErrCode FillAppend(const void * pBuffer, sal_Size nCount,
221 							   sal_Size * pWritten);
222 
Tell() const223 	virtual sal_Size Tell() const { return m_nSize; }
224 
225 	virtual sal_Size Seek(sal_Size nPos);
226 
Terminate()227 	virtual void Terminate() { m_bTerminated = sal_True; }
228 };
229 
230 SV_DECL_IMPL_REF(SvAsyncLockBytes);
231 
232 // ----------
233 // - Stream -
234 // ----------
235 
236 class TOOLS_DLLPUBLIC SvStream
237 {
238 private:
239 	// LockBytes Interface
240 	void*		pImp;			// unused
241 	SvLockBytesRef	xLockBytes; 	// Default Implmentierung
242 	sal_Size		nActPos;		//
243 
244 	// Puffer-Verwaltung
245 	sal_uInt8*	pRWBuf; 		// Zeigt auf Read/Write-Puffer
246 	sal_uInt8*	pBufPos;		// pRWBuf + nBufActualPos
247 	sal_uInt16	nBufSize;		// Allozierte Groesse des Puffers
248 	sal_uInt16	nBufActualLen;	// Laenge des beschriebenen Teils des Puffers
249 									// Entspricht nBufSize, wenn EOF nicht
250 									// ueberschritten wurde
251 	sal_uInt16	nBufActualPos;	// aktuelle Position im Puffer (0..nBufSize-1)
252 	sal_uInt16	nBufFree;		// freier Platz im Puffer fuer IO vom Typ eIOMode
253 	unsigned int	eIOMode:2;		// STREAM_IO_*
254 
255 	// Error-Codes, Konvertierung, Komprimierung, ...
256 	int 			bIsDirty:1; 	// sal_True: Stream != Pufferinhalt
257 	int 			bIsConsistent:1;// sal_False: Buffer enthaelt Daten, die NICHT
258 									// per PutData in den abgeleiteten Stream
259 									// geschrieben werden duerfen (siehe PutBack)
260 	int 			bSwap:1;
261 	int 			bIsEof:1;
262 	sal_uInt32	nError;
263 	sal_uInt16	nNumberFormatInt;
264 	sal_uInt16	nCompressMode;
265 	LineEnd 		eLineDelimiter;
266 	CharSet 		eStreamCharSet;
267 //	CharSet 		eTargetCharSet;
268 
269 	// Verschluesselung
270 	ByteString		aKey;			// aKey.Len != 0  -> Verschluesselung
271 	unsigned char	nCryptMask;
272 
273 	// Formatierung von Strings
274 	char			cFiller;
275 	sal_uInt8			nRadix;
276 	sal_uInt8			nPrecision;
277 	sal_uInt8			nWidth;
278 	sal_uInt8			nPrintfParams;
279 	sal_uInt8			nJustification;
280 	ByteString		aFormatString;
281 
282 	// Userdata
283 	long			nVersion;		// for external use
284 
285 	// Hilfsmethoden
286 	void			CreateFormatString();
287 	TOOLS_DLLPRIVATE void			ImpInit();
288 
289 	 				SvStream ( const SvStream& rStream ); // not implemented
290 	SvStream&		operator=( const SvStream& rStream ); // not implemented
291 
292 protected:
293 	sal_Size			nBufFilePos;	// Fileposition von pBuf[0]
294 	sal_uInt16			eStreamMode;
295 	sal_Bool			bIsWritable;
296 
297 	virtual sal_Size	GetData( void* pData, sal_Size nSize );
298 	virtual sal_Size	PutData( const void* pData, sal_Size nSize );
299 	virtual sal_Size	SeekPos( sal_Size nPos );
300 	virtual void	FlushData();
301 	virtual void	SetSize( sal_Size nSize );
302 
303 	void			ClearError();
304 	void			ClearBuffer();
305 
306 	// verschluesselt & schreibt blockweise
307 	sal_Size			CryptAndWriteBuffer( const void* pStart, sal_Size nLen );
308 	sal_Bool			EncryptBuffer( void* pStart, sal_Size nLen );
309 
310 	void			SyncSvStream( sal_Size nNewStreamPos ); // SvStream <- Medium
311 	void			SyncSysStream(); // SvStream -> Medium
312 
313 public:
314 					SvStream();
315 					SvStream( SvLockBytes *pLockBytes);
316 	virtual 		~SvStream();
317 
318 	ErrCode 		SetLockBytes( SvLockBytesRef& rBytes );
GetLockBytes() const319 	SvLockBytes*	GetLockBytes() const { return xLockBytes; }
320 
GetError() const321 	sal_uInt32	GetError() const { return ERRCODE_TOERROR(nError); }
GetErrorCode() const322 	sal_uInt32	GetErrorCode() const { return nError; }
323 
324 	void			SetError( sal_uInt32 nErrorCode );
325 	virtual void	ResetError();
326 
327 	void			SetNumberFormatInt( sal_uInt16 nNewFormat );
GetNumberFormatInt() const328 	sal_uInt16			GetNumberFormatInt() const { return nNumberFormatInt; }
329 					/// Enable/disable swapping of endians, may be needed for Unicode import/export
330 	inline void		SetEndianSwap( sal_Bool bVal );
331 					// returns status of endian swap flag
IsEndianSwap() const332 	sal_Bool			IsEndianSwap() const { return 0 != bSwap; }
333 
SetCompressMode(sal_uInt16 nNewMode)334 	void			SetCompressMode( sal_uInt16 nNewMode )
335 						{ nCompressMode = nNewMode; }
GetCompressMode() const336 	sal_uInt16			GetCompressMode() const { return nCompressMode; }
337 
338 	void			SetKey( const ByteString& rKey );
GetKey() const339 	const ByteString&	GetKey() const { return aKey; }
340 
SetStreamCharSet(CharSet eCharSet)341 	void			SetStreamCharSet( CharSet eCharSet )
342 						{ eStreamCharSet = eCharSet; }
GetStreamCharSet() const343 	CharSet 		GetStreamCharSet() const { return eStreamCharSet; }
344 //	void			SetTargetCharSet( CharSet eCharSet )
345 //						{ eTargetCharSet = eCharSet; }
346 //	CharSet 		GetTargetCharSet() const { return eTargetCharSet; }
347 
SetLineDelimiter(LineEnd eLineEnd)348 	void			SetLineDelimiter( LineEnd eLineEnd )
349 						{ eLineDelimiter = eLineEnd; }
GetLineDelimiter() const350 	LineEnd 		GetLineDelimiter() const { return eLineDelimiter; }
351 
352 	SvStream&		operator>>( sal_uInt16& rUInt16 );
353 	SvStream&		operator>>( sal_uInt32& rUInt32 );
354 	SvStream&		operator>>( long& rLong );
355 	SvStream&		operator>>( short& rShort );
356 	SvStream&		operator>>( int& rInt );
357 	SvStream&		operator>>( signed char& rChar );
358 	SvStream&		operator>>( char& rChar );
359 	SvStream&		operator>>( unsigned char& rChar );
360 	SvStream&		operator>>( float& rFloat );
361 	SvStream&		operator>>( double& rDouble );
362 #ifdef ENABLE_BYTESTRING_STREAM_OPERATORS
operator >>(ByteString & rString)363 	SvStream&		operator>>( ByteString& rString ) { return ReadByteString(rString); }
364 #endif
365 #ifdef ENABLE_STRING_STREAM_OPERATORS
operator >>(UniString & rString)366 	SvStream&		operator>>( UniString& rString ) { return ReadByteString(rString); }
367 #endif
368 	SvStream&		operator>>( SvStream& rStream );
369 
370 	SvStream&		operator<<( sal_uInt16 nUInt16 );
371 	SvStream&		operator<<( sal_uInt32 nUInt32 );
372 	SvStream&		operator<<( long nLong );
373 	SvStream&		operator<<( short nShort );
374 	SvStream&		operator<<( int nInt );
375 	SvStream&		operator<<( signed char nChar );
376 	SvStream&		operator<<( char nChar );
377 	SvStream&		operator<<( unsigned char nChar );
378 	SvStream&		operator<<( float nFloat );
379 	SvStream&		operator<<( const double& rDouble );
380 	SvStream&		operator<<( const char* pBuf );
381 	SvStream&		operator<<( const unsigned char* pBuf );
382 #ifdef ENABLE_BYTESTRING_STREAM_OPERATORS
operator <<(const ByteString & rString)383 	SvStream&		operator<<( const ByteString& rString ) { return WriteByteString( rString ); }
384 #endif
385 #ifdef ENABLE_STRING_STREAM_OPERATORS
operator <<(const UniString & rString)386 	SvStream&		operator<<( const UniString& rString ) { return WriteByteString(rString); }
387 #endif
388 	SvStream&		operator<<( SvStream& rStream );
389 
390 	SvStream&		ReadByteString( UniString& rStr, rtl_TextEncoding eSrcCharSet );
ReadByteString(UniString & rStr)391 	SvStream&		ReadByteString( UniString& rStr ) { return ReadByteString( rStr, GetStreamCharSet() ); }
392 	SvStream&       ReadByteString( ByteString& rStr );
393 	SvStream&		WriteByteString( const UniString& rStr, rtl_TextEncoding eDestCharSet );
WriteByteString(const UniString & rStr)394 	SvStream&		WriteByteString( const UniString& rStr ) { return WriteByteString( rStr, GetStreamCharSet() ); }
395 	SvStream&       WriteByteString( const ByteString& rStr );
396 
SetRadix(sal_uInt8 nRad)397 	void			SetRadix( sal_uInt8 nRad )
398 						{ nRadix = nRad; CreateFormatString(); }
GetRadix() const399 	sal_uInt8			GetRadix() const { return nRadix; }
SetPrecision(sal_uInt8 nPrec)400 	void			SetPrecision( sal_uInt8 nPrec )
401 						{ nPrecision = nPrec; CreateFormatString(); }
GetPrecision() const402 	sal_uInt8			GetPrecision() const { return nPrecision; }
SetWidth(sal_uInt8 nWid)403 	void			SetWidth( sal_uInt8 nWid)
404 						{ nWidth = nWid; CreateFormatString(); }
GetWidth() const405 	sal_uInt8			GetWidth() const { return nWidth; }
SetFiller(char cFil)406 	void			SetFiller( char cFil )
407 						{ cFiller = cFil; CreateFormatString(); }
GetFiller() const408 	char			GetFiller() const { return cFiller; }
SetJustification(sal_uInt8 nJus)409 	void			SetJustification( sal_uInt8 nJus )
410 						 { nJustification = nJus; CreateFormatString(); }
GetJustification() const411 	sal_uInt8			GetJustification() const { return nJustification; }
412 
413 	SvStream&		ReadNumber( short& rShort );
414 	SvStream&		ReadNumber( sal_uInt16& rUInt16 );
415 	SvStream&		ReadNumber( long& rLong );
416 	SvStream&		ReadNumber( sal_uInt32& rUInt32 );
417 	SvStream&		ReadNumber( int& rInt );
418 	SvStream&		ReadNumber( float& rFloat );
419 	SvStream&		ReadNumber( double& rDouble );
420 
421 	SvStream&		WriteNumber( short nShort );
422 	SvStream&		WriteNumber( sal_uInt16 nUInt16 );
423 	SvStream&		WriteNumber( long nLong );
424 	SvStream&		WriteNumber( sal_uInt32 nUInt32 );
425 	SvStream&		WriteNumber( int nInt );
426 	SvStream&		WriteNumber( float nFloat );
427 	SvStream&		WriteNumber( const double& rDouble );
428 
429 	sal_Size		Read( void* pData, sal_Size nSize );
430 	sal_Size		Write( const void* pData, sal_Size nSize );
431 	sal_Size		Seek( sal_Size nPos );
432 	sal_Size		SeekRel( sal_sSize nPos );
Tell() const433 	sal_Size		Tell() const { return nBufFilePos+nBufActualPos;  }
434 	void			Flush();
IsEof() const435 	sal_Bool		IsEof() const { return bIsEof; }
436 	// next Tell() <= nSize
437 	sal_Bool		SetStreamSize( sal_Size nSize );
438 
439 				/// Read in the stream to a zero character and put all
440 				/// read chracters in the Bytestring. The String interface
441 				/// convert the BytString with the given encoding to a String
442 	sal_Bool		ReadCString( ByteString& rStr );
443 	sal_Bool 		ReadCString( String& rStr, rtl_TextEncoding eToEncode );
ReadCString(String & rStr)444 	sal_Bool 		ReadCString( String& rStr ) { return ReadCString( rStr, GetStreamCharSet()); }
445 
446 	sal_Bool		ReadLine( ByteString& rStr );
447 	sal_Bool		WriteLine( const ByteString& rStr );
448 	sal_Bool		WriteLines( const ByteString& rStr );
449 
450 	sal_Bool		ReadByteStringLine( String& rStr, rtl_TextEncoding eSrcCharSet );
ReadByteStringLine(String & rStr)451 	sal_Bool		ReadByteStringLine( String& rStr ) { return ReadByteStringLine( rStr, GetStreamCharSet()); }
452 	sal_Bool		WriteByteStringLine( const String& rStr, rtl_TextEncoding eDestCharSet );
WriteByteStringLine(const String & rStr)453 	sal_Bool		WriteByteStringLine( const String& rStr ) { return WriteByteStringLine( rStr, GetStreamCharSet()); }
454 	sal_Bool		WriteByteStringLines( const String& rStr, rtl_TextEncoding eDestCharSet );
WriteByteStringLines(const String & rStr)455 	sal_Bool		WriteByteStringLines( const String& rStr ) { return WriteByteStringLine( rStr, GetStreamCharSet()); }
456 
457 				/// Switch to no endian swapping and write 0xfeff
458 	sal_Bool		StartWritingUnicodeText();
459 
460                 /** If eReadBomCharSet==RTL_TEXTENCODING_DONTKNOW: read 16bit,
461                     if 0xfeff do nothing (UTF-16), if 0xfffe switch endian
462                     swapping (UTF-16), if 0xefbb or 0xbbef read another byte
463                     and check for UTF-8. If no UTF-* BOM was detected put all
464                     read bytes back. This means that if 2 bytes were read it
465                     was an UTF-16 BOM, if 3 bytes were read it was an UTF-8
466                     BOM. There is no UTF-7, UTF-32 or UTF-EBCDIC BOM detection!
467 
468                     If eReadBomCharSet!=RTL_TEXTENCODING_DONTKNOW: only read a
469                     BOM of that encoding and switch endian swapping if UTF-16
470                     and 0xfffe.
471                  */
472     sal_Bool		StartReadingUnicodeText( rtl_TextEncoding eReadBomCharSet );
473 
474 				/// Read a line of Unicode
475 	sal_Bool		ReadUniStringLine( String& rStr );
476 				/// Read a line of Unicode if eSrcCharSet==RTL_TEXTENCODING_UNICODE,
477 				/// otherwise read a line of Bytecode and convert from eSrcCharSet
478 	sal_Bool		ReadUniOrByteStringLine( String& rStr, rtl_TextEncoding eSrcCharSet );
ReadUniOrByteStringLine(String & rStr)479 	sal_Bool		ReadUniOrByteStringLine( String& rStr )
480 					{ return ReadUniOrByteStringLine( rStr, GetStreamCharSet() ); }
481 				/// Write a sequence of Unicode characters
482 	sal_Bool		WriteUnicodeText( const String& rStr );
483 				/// Write a sequence of Unicode characters if eDestCharSet==RTL_TEXTENCODING_UNICODE,
484 				/// otherwise write a sequence of Bytecodes converted to eDestCharSet
485 	sal_Bool		WriteUnicodeOrByteText( const String& rStr, rtl_TextEncoding eDestCharSet );
WriteUnicodeOrByteText(const String & rStr)486 	sal_Bool		WriteUnicodeOrByteText( const String& rStr )
487 					{ return WriteUnicodeOrByteText( rStr, GetStreamCharSet() ); }
488 				/// Write a line of Unicode and append line end (endlu())
489 	sal_Bool		WriteUniStringLine( const String& rStr );
490 				/// Write multiple lines of Unicode (with CovertLineEnd) and append line end (endlu())
491 	sal_Bool		WriteUniStringLines( const String& rStr );
492 				/// Write a line of Unicode if eDestCharSet==RTL_TEXTENCODING_UNICODE,
493 				/// otherwise write a line of Bytecode converted to eDestCharSet
494 	sal_Bool		WriteUniOrByteStringLine( const String& rStr, rtl_TextEncoding eDestCharSet );
WriteUniOrByteStringLine(const String & rStr)495 	sal_Bool		WriteUniOrByteStringLine( const String& rStr )
496 					{ return WriteUniOrByteStringLine( rStr, GetStreamCharSet() ); }
497 				/// Write multiple lines of Unicode if eDestCharSet==RTL_TEXTENCODING_UNICODE,
498 				/// otherwise write multiple lines of Bytecode converted to eDestCharSet,
499 				/// CovertLineEnd is applied.
500 	sal_Bool		WriteUniOrByteStringLines( const String& rStr, rtl_TextEncoding eDestCharSet );
WriteUniOrByteStringLines(const String & rStr)501 	sal_Bool		WriteUniOrByteStringLines( const String& rStr )
502 					{ return WriteUniOrByteStringLines( rStr, GetStreamCharSet() ); }
503 				/// Write a Unicode character if eDestCharSet==RTL_TEXTENCODING_UNICODE,
504 				/// otherwise write as Bytecode converted to eDestCharSet.
505 				/// This may result in more than one byte being written
506 				/// if a multi byte encoding (e.g. UTF7, UTF8) is chosen.
507 	sal_Bool		WriteUniOrByteChar( sal_Unicode ch, rtl_TextEncoding eDestCharSet );
WriteUniOrByteChar(sal_Unicode ch)508 	sal_Bool		WriteUniOrByteChar( sal_Unicode ch )
509 					{ return WriteUniOrByteChar( ch, GetStreamCharSet() ); }
510 
511                 /** Read a CSV (comma separated values) data line using
512                     ReadUniOrByteStringLine().
513 
514                     @param bEmbeddedLineBreak
515                     If sal_True and a line-break occurs inside a field of data,
516                     a line feed LF '\n' and the next line are appended. Repeats
517                     until a line-break is not in a field. A field is determined
518                     by delimiting rFieldSeparators and optionally surrounded by
519                     a pair of cFieldQuote characters. For a line-break to be
520                     within a field, the field content MUST be surrounded by
521                     cFieldQuote characters, and the opening cFieldQuote MUST be
522                     at the very start of a line or follow right behind a field
523                     separator with no extra characters in between. Anything,
524                     including field separators and escaped quotes (by doubling
525                     them, or preceding them with a backslash if
526                     bAllowBackslashEscape==sal_True) may appear in a quoted
527                     field.
528 
529                     If bEmbeddedLineBreak==sal_False, nothing is parsed and the
530                     string returned is simply one ReadUniOrByteStringLine().
531 
532                     @param rFieldSeparators
533                     A list of characters that each may act as a field separator.
534 
535                     @param cFieldQuote
536                     The quote character used.
537 
538                     @param bAllowBackslashEscape
539                     If sal_True, an embedded quote character inside a quoted
540                     field may also be escaped with a preceding backslash.
541                     Normally, quotes are escaped by doubling them.
542 
543                     @return
544                     sal_True if no stream error.
545 
546                     @ATTENTION
547                     Note that the string returned may be truncated even inside
548                     a quoted field if STRING_MAXLEN was reached. There
549                     currently is no way to exactly determine the conditions,
550                     whether this was at a line end, or whether open quotes
551                     would have closed the field before the line end, as even a
552                     ReadUniOrByteStringLine() may return prematurely but the
553                     stream was positioned ahead until the real end of line.
554                     Additionally, due to character encoding conversions, string
555                     length and bytes read don't necessarily match, and
556                     resyncing to a previous position matching the string's
557                     length isn't always possible. As a result, a logical line
558                     with embedded line breaks and more than STRING_MAXLEN
559                     characters will be spoiled, and a subsequent ReadCsvLine()
560                     may start under false preconditions.
561                   */
562     sal_Bool        ReadCsvLine( String& rStr, sal_Bool bEmbeddedLineBreak,
563                         const String& rFieldSeparators, sal_Unicode cFieldQuote,
564                         sal_Bool bAllowBackslashEscape = sal_False);
565 
566 	void			SetBufferSize( sal_uInt16 nBufSize );
GetBufferSize() const567 	sal_uInt16	GetBufferSize() const { return nBufSize; }
568 
569 	void			RefreshBuffer();
570 	SvStream&		PutBack( char aCh );
571 	void			EatWhite();
572 
IsWritable() const573 	sal_Bool			IsWritable() const { return bIsWritable; }
GetStreamMode() const574 	StreamMode		GetStreamMode() const { return eStreamMode; }
575 	virtual sal_uInt16	IsA() const;
576 
GetVersion()577 	long			GetVersion() { return nVersion; }
SetVersion(long n)578 	void			SetVersion( long n ) { nVersion = n; }
579 
580 	/** Add a mark to indicate to which position in the stream you want to be
581 		able to seek back.
582 
583 		@descr	If you set a mark at nPos, you indicate to the stream that you
584 		won't issue seek operations to any position 'before' nPos.  This can
585 		be exploited by 'transient streams' that do not permanently store
586 		their data; they can discard any buffered data up to nPos.
587 
588 		@descr	However, if the stream is already positioned past nPos, this
589 		method is not guaranteed to have the desired effect.  A 'transient
590 		stream' may have already discarded the buffered data at nPos, so that
591 		a seek operation to nPos will fail nonetheless.
592 
593 		@descr	There can be more than one mark for a stream, indicating that
594 		you want to be able to seek back in the stream as far as the 'lowest'
595 		off all the marks.	There can even be multiple marks at the same
596 		position (and they must all be individually removed with
597 		RemoveMark()).
598 
599 		@param nPos  The position in the stream at which to add a mark.
600 	 */
601 	virtual void	AddMark(sal_Size nPos);
602 
603 	/** Remove a mark introduced with AddMark().
604 
605 		@descr	If you no longer need to seek back to some position for which
606 		you added a mark, you should remove that mark.	(And a 'transient
607 		stream' that does not permanently store its data can then potentially
608 		discard some of its buffered data.)
609 
610 		@descr	Removing one mark does not have any effects on any other
611 		marks.	Especially, if you have multiple marks at the same position,
612 		you must call this method multiple times to effectively 'unmark' that
613 		position.
614 
615 		@descr	If you specify a position for which there is no mark, this
616 		method simply has no effect.
617 
618 		@param nPos  The position in the stream at which to remove the mark.
619 	 */
620 	virtual void	RemoveMark(sal_Size nPos);
621 
622 	friend SvStream& operator<<( SvStream& rStr, SvStrPtr f ); // fuer Manips
623 };
624 
operator <<(SvStream & rStr,SvStrPtr f)625 inline SvStream& operator<<( SvStream& rStr, SvStrPtr f )
626 {
627 	(*f)(rStr);
628 	return rStr;
629 }
630 
ReadNumber(short & rShort)631 inline SvStream& SvStream::ReadNumber( short& rShort )
632 {
633 	long nTmp;
634 	ReadNumber( nTmp );
635 	rShort = (short)nTmp;
636 	return *this;
637 }
638 
ReadNumber(sal_uInt16 & rUShort)639 inline SvStream& SvStream::ReadNumber( sal_uInt16& rUShort )
640 {
641 	sal_uInt32 nTmp;
642 	ReadNumber( nTmp );
643 	rUShort = (sal_uInt16)nTmp;
644 	return *this;
645 }
646 
ReadNumber(int & rInt)647 inline SvStream& SvStream::ReadNumber( int& rInt )
648 {
649 	long nTmp;
650 	ReadNumber( nTmp );
651 	rInt = (int)nTmp;
652 	return *this;
653 }
654 
655 /*
656 inline SvStream& SvStream::ReadNumber( unsigned int& rUInt )
657 {
658 	sal_uIntPtr nTmp;
659 	ReadNumber( nTmp );
660 	rUInt = (unsigned int)nTmp;
661 	return *this;
662 }
663 */
664 
ReadNumber(float & rFloat)665 inline SvStream& SvStream::ReadNumber( float& rFloat )
666 {
667 	double nTmp;
668 	ReadNumber( nTmp );
669 	rFloat = (float)nTmp;
670 	return *this;
671 }
672 
WriteNumber(short nShort)673 inline SvStream& SvStream::WriteNumber( short nShort )
674 {
675 	WriteNumber( (long)nShort );
676 	return *this;
677 }
678 
WriteNumber(sal_uInt16 nUShort)679 inline SvStream& SvStream::WriteNumber( sal_uInt16 nUShort )
680 {
681 	WriteNumber( (sal_uInt32)nUShort );
682 	return *this;
683 }
684 
WriteNumber(int nInt)685 inline SvStream& SvStream::WriteNumber( int nInt )
686 {
687 	WriteNumber( (long)nInt );
688 	return *this;
689 }
690 
691 /*
692 inline SvStream& SvStream::WriteNumber( unsigned int nUInt )
693 {
694 	WriteNumber( (sal_uIntPtr)nUInt );
695 	return *this;
696 }
697 */
698 
WriteNumber(float nFloat)699 inline SvStream& SvStream::WriteNumber( float nFloat )
700 {
701 	double nTemp = nFloat;
702 	WriteNumber( nTemp );
703 	return *this;
704 }
705 
SetEndianSwap(sal_Bool bVal)706 inline void SvStream::SetEndianSwap( sal_Bool bVal )
707 {
708 #ifdef OSL_BIGENDIAN
709 	SetNumberFormatInt( bVal ? NUMBERFORMAT_INT_LITTLEENDIAN : NUMBERFORMAT_INT_BIGENDIAN );
710 #else
711 	SetNumberFormatInt( bVal ? NUMBERFORMAT_INT_BIGENDIAN : NUMBERFORMAT_INT_LITTLEENDIAN );
712 #endif
713 }
714 
715 TOOLS_DLLPUBLIC SvStream& endl( SvStream& rStr );
716 /// same as endl() but Unicode
717 TOOLS_DLLPUBLIC SvStream& endlu( SvStream& rStr );
718 /// call endlu() if eStreamCharSet==RTL_TEXTECODING_UNICODE otherwise endl()
719 TOOLS_DLLPUBLIC SvStream& endlub( SvStream& rStr );
720 
721 // --------------
722 // - FileStream -
723 // --------------
724 
725 class TOOLS_DLLPUBLIC SvFileStream : public SvStream
726 {
727 	friend class ImpEaMgr;
728 	friend class CORmFileStream;
729     friend class FileCopier;
730 
731 private:
732 	StreamData* 		pInstanceData;
733 	String			aFilename;
734 	sal_uInt16		nLockCounter;
735 	sal_Bool			bIsOpen;
736 	sal_uIntPtr		GetFileHandle() const;
737 
738 	// Forbidden and not implemented.
739 	SvFileStream (const SvFileStream&);
740 	SvFileStream & operator= (const SvFileStream&);
741 
742 protected:
743 
744 	virtual sal_Size	GetData( void* pData, sal_Size nSize );
745 	virtual sal_Size	PutData( const void* pData, sal_Size nSize );
746 	virtual sal_Size	SeekPos( sal_Size nPos );
747 	virtual void	SetSize( sal_Size nSize );
748 	virtual void	FlushData();
749 
750 public:
751 					// Schaltet bei fehlgeschlagenem Schreiboeffnen auf Lesen zurueck
752 					SvFileStream( const String& rFileName, StreamMode eOpenMode );
753 					SvFileStream();
754 					~SvFileStream();
755 
756 	virtual void	ResetError();
757 
758 	sal_Bool			LockRange( sal_Size nByteOffset, sal_Size nBytes );
759 	sal_Bool			UnlockRange( sal_Size nByteOffset, sal_Size nBytes );
760 	sal_Bool			LockFile();
761 	sal_Bool			UnlockFile();
762 
763 	void			Open( const String& rFileName, StreamMode eOpenMode );
764 	void			Close();
765 	void			ReOpen(); // Aufruf nach Close, FilePointer == 0
IsOpen() const766 	sal_Bool			IsOpen() const { return bIsOpen; }
IsLocked() const767 	sal_Bool			IsLocked() const { return ( nLockCounter!=0 ); }
768 	virtual sal_uInt16	IsA() const;
769 
GetFileName() const770 	const String&	GetFileName() const { return aFilename; }
771 };
772 
773 // ----------------
774 // - MemoryStream -
775 // ----------------
776 
777 class TOOLS_DLLPUBLIC SvMemoryStream : public SvStream
778 {
779 	// Forbidden and not implemented.
780 	SvMemoryStream (const SvMemoryStream&);
781 	SvMemoryStream & operator= (const SvMemoryStream&);
782 
783     friend class SvCacheStream;
GetSize() const784     sal_Size            GetSize() const { return nSize; }
785 
786 protected:
787 	sal_Size			nSize;
788 	sal_Size			nResize;
789 	sal_Size			nPos;
790 	sal_Size			nEndOfData;
791 	sal_uInt8*			pBuf;
792 	sal_Bool			bOwnsData;
793 
794 	virtual sal_Size	GetData( void* pData, sal_Size nSize );
795 	virtual sal_Size	PutData( const void* pData, sal_Size nSize );
796 	virtual sal_Size	SeekPos( sal_Size nPos );
797 	virtual void	SetSize( sal_Size nSize );
798 	virtual void	FlushData();
799 
800 	// AllocateMemory muss folgende Variable mitpflegen:
801 	// - pBuf: Adresse des neuen Blocks
802 	virtual sal_Bool	AllocateMemory( sal_Size nSize );
803 
804 	// ReAllocateMemory muss folgende Variablen mitpflegen:
805 	// - pBuf: Adresse des neuen Blocks
806 	// - nEndOfData: Muss auf nNewSize-1L gesetzt werden, wenn ausserhalb des Blocks
807 	//				 Muss auf 0 gesetzt werden, wenn neuer Block 0 Byte gross
808 	// - nSize: Neue Groesse des Blocks
809 	// - nPos: Muss auf 0 gesetzt werden, wenn ausserhalb des Blocks
810 	virtual sal_Bool	ReAllocateMemory( long nDiff );
811 
812 	// wird aufgerufen, wenn dem Stream der Speicher gehoert oder wenn
813 	// der Speicher in der Groesse veraendert wird.
814 	// FreeMemory muss folgende Variablen mitpflegen:
815 	// - in abgeleiteten Klassen muessen ggf. Handles genullt werden
816 	virtual void	FreeMemory();
817 
SvMemoryStream(void *)818 					SvMemoryStream(void*) { }	// Fuer unsere Subklassen
819 
820 public:
821 					SvMemoryStream( void* pBuf, sal_Size nSize, StreamMode eMode);
822 					SvMemoryStream( sal_Size nInitSize=512, sal_Size nResize=64 );
823 					~SvMemoryStream();
824 
825 	virtual void	ResetError();
826 
GetEndOfData() const827     sal_Size        GetEndOfData() const { return nEndOfData; }
GetData()828 	const void* 	GetData() { Flush(); return pBuf; }
829 	operator const	void*() { Flush(); return pBuf; }
830 	virtual sal_uInt16	IsA() const;
831 
832 	void*			SwitchBuffer( sal_Size nInitSize=512, sal_Size nResize=64 );
833 	void*			SetBuffer( void* pBuf, sal_Size nSize,
834 							   sal_Bool bOwnsData=sal_True, sal_Size nEOF=0 );
835 
ObjectOwnsMemory(sal_Bool bOwn)836 	void			ObjectOwnsMemory( sal_Bool bOwn ) { bOwnsData = bOwn; }
IsObjectMemoryOwner()837 	sal_Bool			IsObjectMemoryOwner() { return bOwnsData; }
SetResizeOffset(sal_Size nNewResize)838 	void			SetResizeOffset( sal_Size nNewResize ) { nResize = nNewResize; }
GetResizeOffset() const839 	sal_Size			GetResizeOffset() const { return nResize; }
840 };
841 
842 // --------------------
843 // - SvDataCopyStream -
844 // --------------------
845 
846 // AB 10.5.1996: Diese Klasse bildet die Basis fuer Klassen, die mittels
847 // SvData (SO2\DTRANS.HXX/CXX) transportiert werden sollen, z.B. Graphik
848 // Die abgeleiteten Klassen muessen die virtuellen Funktionen ueberladen.
849 
850 class TOOLS_DLLPUBLIC SvDataCopyStream
851 {
852 public:
853 	/*-----------------MM 30.04.96 11:01-----------------
854 	 mehrfaches Aufrufen von Load und Assign erlaubt
855 	--------------------------------------------------*/
856 					TYPEINFO();
~SvDataCopyStream()857 	virtual 		~SvDataCopyStream(){}
858 	virtual void	Load( SvStream & ) = 0;
859 	virtual void	Save( SvStream & ) = 0;
860 	virtual void	Assign( const SvDataCopyStream & );
861 };
862 
863 #endif // _STREAM_HXX
864