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