Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 : #ifndef _STREAM_HXX
20 : #define _STREAM_HXX
21 :
22 : #include <limits>
23 : #include "tools/toolsdllapi.h"
24 : #include <tools/solar.h>
25 : #include <tools/string.hxx>
26 : #include <tools/errinf.hxx>
27 : #include <tools/ref.hxx>
28 : #include <tools/rtti.hxx>
29 : #include <rtl/string.hxx>
30 :
31 : class FileCopier;
32 : class StreamData;
33 :
34 23394 : inline rtl_TextEncoding GetStoreCharSet( rtl_TextEncoding eEncoding )
35 : {
36 23394 : if ( eEncoding == RTL_TEXTENCODING_ISO_8859_1 )
37 0 : return RTL_TEXTENCODING_MS_1252;
38 : else
39 23394 : return eEncoding;
40 : }
41 :
42 : // StreamTypes
43 :
44 : typedef sal_uInt16 StreamMode;
45 :
46 : // read, write, create,... options
47 : #define STREAM_READ 0x0001 ///< allow read accesses
48 : #define STREAM_WRITE 0x0002 ///< allow write accesses
49 : // file i/o
50 : #define STREAM_NOCREATE 0x0004 ///< 1 == Dont create file
51 : #define STREAM_TRUNC 0x0008 ///< Truncate _existing_ file to zero length
52 : #define STREAM_COPY_ON_SYMLINK 0x0010 ///< copy-on-write for symlinks (UNX)
53 :
54 : #define STREAM_READWRITEBITS (STREAM_READ | STREAM_WRITE | \
55 : STREAM_NOCREATE | STREAM_TRUNC)
56 :
57 : // sharing options
58 : #define STREAM_SHARE_DENYNONE 0x0100
59 : #define STREAM_SHARE_DENYREAD 0x0200 // overrides denynone
60 : #define STREAM_SHARE_DENYWRITE 0x0400 // overrides denynone
61 : #define STREAM_SHARE_DENYALL 0x0800 // overrides denyread,write,none
62 :
63 : #define STREAM_SHAREBITS (STREAM_SHARE_DENYNONE | STREAM_SHARE_DENYREAD |\
64 : STREAM_SHARE_DENYWRITE | STREAM_SHARE_DENYALL)
65 :
66 : #define STREAM_READWRITE (STREAM_READ | STREAM_WRITE)
67 : #define STREAM_SHARE_DENYREADWRITE (STREAM_SHARE_DENYREAD | STREAM_SHARE_DENYWRITE)
68 :
69 : #define STREAM_STD_READ (STREAM_READ | STREAM_SHARE_DENYNONE | STREAM_NOCREATE)
70 : #define STREAM_STD_WRITE (STREAM_WRITE | STREAM_SHARE_DENYALL)
71 : #define STREAM_STD_READWRITE (STREAM_READWRITE | STREAM_SHARE_DENYALL)
72 :
73 : #define STREAM_SEEK_TO_BEGIN 0L
74 : #define STREAM_SEEK_TO_END ULONG_MAX
75 :
76 : #define NUMBERFORMAT_INT_BIGENDIAN (sal_uInt16)0x0000
77 : #define NUMBERFORMAT_INT_LITTLEENDIAN (sal_uInt16)0xFFFF
78 :
79 : #define COMPRESSMODE_NONE (sal_uInt16)0x0000
80 : #define COMPRESSMODE_ZBITMAP (sal_uInt16)0x0001
81 : #define COMPRESSMODE_NATIVE (sal_uInt16)0x0010
82 :
83 : #define STREAM_IO_DONTKNOW 0
84 : #define STREAM_IO_READ 1
85 : #define STREAM_IO_WRITE 2
86 :
87 : #define ID_STREAM 1
88 : #define ID_FILESTREAM 2
89 : #define ID_MEMORYSTREAM 3
90 : #define ID_SHAREDMEMORYSTREAM 4
91 : #define ID_STORAGESTREAM 5
92 : #define ID_PERSISTSTREAM 6
93 :
94 : class SvStream;
95 :
96 : typedef SvStream& (*SvStrPtr)( SvStream& );
97 :
98 : inline SvStream& operator<<( SvStream& rStr, SvStrPtr f );
99 :
100 : // SvLockBytes
101 :
102 : enum LockType {};
103 :
104 : struct SvLockBytesStat
105 : {
106 : sal_Size nSize;
107 :
108 488 : SvLockBytesStat() : nSize(0) {}
109 : };
110 :
111 : enum SvLockBytesStatFlag { SVSTATFLAG_DEFAULT };
112 :
113 : class TOOLS_DLLPUBLIC SvLockBytes: public virtual SvRefBase
114 : {
115 : SvStream * m_pStream;
116 : sal_Bool m_bOwner;
117 : sal_Bool m_bSync;
118 :
119 : protected:
120 : void close();
121 :
122 : public:
123 : TYPEINFO();
124 :
125 5278 : SvLockBytes() : m_pStream(0), m_bOwner(sal_False), m_bSync(sal_False) {}
126 :
127 0 : SvLockBytes(SvStream * pTheStream, sal_Bool bTheOwner = sal_False) :
128 0 : m_pStream(pTheStream), m_bOwner(bTheOwner), m_bSync(sal_False) {}
129 :
130 4925 : virtual ~SvLockBytes() { close(); }
131 :
132 5278 : virtual const SvStream * GetStream() const { return m_pStream; }
133 :
134 7267 : virtual void SetSynchronMode(sal_Bool bTheSync = sal_True) { m_bSync = bTheSync; }
135 26982 : virtual sal_Bool IsSynchronMode() const { return m_bSync; }
136 :
137 : virtual ErrCode ReadAt(sal_Size nPos, void * pBuffer, sal_Size nCount,
138 : sal_Size * pRead) const;
139 : virtual ErrCode WriteAt(sal_Size nPos, const void * pBuffer, sal_Size nCount,
140 : sal_Size * pWritten);
141 :
142 : virtual ErrCode Flush() const;
143 :
144 : virtual ErrCode SetSize(sal_Size nSize);
145 :
146 : virtual ErrCode Stat(SvLockBytesStat * pStat, SvLockBytesStatFlag) const;
147 : };
148 :
149 28 : SV_DECL_IMPL_REF(SvLockBytes);
150 :
151 : // SvOpenLockBytes
152 :
153 0 : class TOOLS_DLLPUBLIC SvOpenLockBytes: public SvLockBytes
154 : {
155 : public:
156 : TYPEINFO();
157 :
158 0 : SvOpenLockBytes() : SvLockBytes(0, sal_False) {}
159 : SvOpenLockBytes(SvStream * pStream, sal_Bool bOwner):
160 : SvLockBytes(pStream, bOwner) {}
161 :
162 : virtual ErrCode FillAppend(const void * pBuffer, sal_Size nCount,
163 : sal_Size * pWritten) = 0;
164 :
165 : virtual sal_Size Tell() const = 0;
166 :
167 : virtual sal_Size Seek(sal_Size nPos) = 0;
168 :
169 : virtual void Terminate() = 0;
170 : };
171 :
172 : SV_DECL_IMPL_REF(SvOpenLockBytes);
173 :
174 : // SvAsyncLockBytes
175 :
176 : class SvAsyncLockBytes: public SvOpenLockBytes
177 : {
178 : sal_Size m_nSize;
179 : sal_Bool m_bTerminated;
180 :
181 : public:
182 : TYPEINFO();
183 :
184 : SvAsyncLockBytes(SvStream * pStream, sal_Bool bOwner):
185 : SvOpenLockBytes(pStream, bOwner), m_nSize(0), m_bTerminated(sal_False) {}
186 :
187 : virtual ErrCode ReadAt(sal_Size nPos, void * pBuffer, sal_Size nCount,
188 : sal_Size * pRead) const;
189 : virtual ErrCode WriteAt(sal_Size nPos, const void * pBuffer, sal_Size nCount,
190 : sal_Size * pWritten);
191 :
192 : virtual ErrCode FillAppend(const void * pBuffer, sal_Size nCount,
193 : sal_Size * pWritten);
194 :
195 : virtual sal_Size Tell() const { return m_nSize; }
196 :
197 : virtual sal_Size Seek(sal_Size nPos);
198 :
199 : virtual void Terminate() { m_bTerminated = sal_True; }
200 : };
201 :
202 : SV_DECL_IMPL_REF(SvAsyncLockBytes);
203 :
204 : // SvStream
205 :
206 : class TOOLS_DLLPUBLIC SvStream
207 : {
208 : private:
209 : // LockBytes Interface
210 : void* pImp; // unused
211 : SvLockBytesRef xLockBytes; // Default implementation
212 : sal_Size nActPos;
213 :
214 : // Puffer-Verwaltung
215 : sal_uInt8* pRWBuf; // Points to read/write buffer
216 : sal_uInt8* pBufPos; // pRWBuf + nBufActualPos
217 : sal_uInt16 nBufSize; // Allocated size of buffer
218 : sal_uInt16 nBufActualLen; // Length of used segment of puffer
219 : // = nBufSize, if EOF did not occur
220 : sal_uInt16 nBufActualPos; // current position in buffer (0..nBufSize-1)
221 : sal_uInt16 nBufFree; // number of free slots in buffer to IO of type eIOMode
222 : unsigned int eIOMode : 2;// STREAM_IO_*
223 :
224 : // Error codes, conversion, compression, ...
225 : int bIsDirty : 1; // sal_True: Stream != buffer content
226 : int bIsConsistent : 1;// sal_False: Buffer contains data, which were
227 : // NOT allowed to be written by PutData
228 : // into the derived stream (cf. PutBack)
229 : int bSwap : 1;
230 : int bIsEof : 1;
231 : sal_uInt32 nError;
232 : sal_uInt16 nNumberFormatInt;
233 : sal_uInt16 nCompressMode;
234 : LineEnd eLineDelimiter;
235 : CharSet eStreamCharSet;
236 :
237 : // Encryption
238 : rtl::OString m_aCryptMaskKey;// aCryptMaskKey.getLength != 0 -> Encryption used
239 : unsigned char nCryptMask;
240 :
241 : // Userdata
242 : long nVersion; // for external use
243 :
244 : // helper methods
245 : TOOLS_DLLPRIVATE void ImpInit();
246 :
247 : SvStream ( const SvStream& rStream ); // not implemented
248 : SvStream& operator=( const SvStream& rStream ); // not implemented
249 :
250 : protected:
251 : sal_Size nBufFilePos;///< File position of pBuf[0]
252 : sal_uInt16 eStreamMode;
253 : sal_Bool bIsWritable;
254 :
255 : virtual sal_Size GetData( void* pData, sal_Size nSize );
256 : virtual sal_Size PutData( const void* pData, sal_Size nSize );
257 : virtual sal_Size SeekPos( sal_Size nPos );
258 : virtual void FlushData();
259 : virtual void SetSize( sal_Size nSize );
260 :
261 : void ClearError();
262 : void ClearBuffer();
263 :
264 : // encrypt and write in blocks
265 : sal_Size CryptAndWriteBuffer( const void* pStart, sal_Size nLen );
266 : sal_Bool EncryptBuffer( void* pStart, sal_Size nLen );
267 :
268 : void SyncSvStream( sal_Size nNewStreamPos ); ///< SvStream <- Medium
269 : void SyncSysStream(); ///< SvStream -> Medium
270 :
271 : public:
272 : SvStream();
273 : SvStream( SvLockBytes *pLockBytes);
274 : virtual ~SvStream();
275 :
276 28 : SvLockBytes* GetLockBytes() const { return xLockBytes; }
277 :
278 2633784 : sal_uInt32 GetError() const { return ERRCODE_TOERROR(nError); }
279 8039 : sal_uInt32 GetErrorCode() const { return nError; }
280 :
281 : void SetError( sal_uInt32 nErrorCode );
282 : virtual void ResetError();
283 :
284 : void SetNumberFormatInt( sal_uInt16 nNewFormat );
285 9629 : sal_uInt16 GetNumberFormatInt() const { return nNumberFormatInt; }
286 : /// Enable/disable swapping of endians, may be needed for Unicode import/export
287 : inline void SetEndianSwap( sal_Bool bVal );
288 : /// returns status of endian swap flag
289 0 : sal_Bool IsEndianSwap() const { return 0 != bSwap; }
290 :
291 10 : void SetCompressMode( sal_uInt16 nNewMode )
292 10 : { nCompressMode = nNewMode; }
293 2587 : sal_uInt16 GetCompressMode() const { return nCompressMode; }
294 :
295 : void SetCryptMaskKey(const rtl::OString& rCryptMaskKey);
296 : const rtl::OString& GetCryptMaskKey() const { return m_aCryptMaskKey; }
297 :
298 47 : void SetStreamCharSet( CharSet eCharSet )
299 47 : { eStreamCharSet = eCharSet; }
300 108913 : CharSet GetStreamCharSet() const { return eStreamCharSet; }
301 :
302 1 : void SetLineDelimiter( LineEnd eLineEnd )
303 1 : { eLineDelimiter = eLineEnd; }
304 0 : LineEnd GetLineDelimiter() const { return eLineDelimiter; }
305 :
306 : SvStream& operator>>( sal_uInt16& rUInt16 );
307 : SvStream& operator>>( sal_uInt32& rUInt32 );
308 : SvStream& operator>>( sal_uInt64& rUInt64 );
309 : SvStream& operator>>( sal_Int16& rInt16 );
310 : SvStream& operator>>( sal_Int32& rInt32 );
311 : SvStream& operator>>( sal_Int64& rInt64 );
312 :
313 : SvStream& operator>>( signed char& rChar );
314 : SvStream& operator>>( char& rChar );
315 : SvStream& operator>>( unsigned char& rChar );
316 : SvStream& operator>>( float& rFloat );
317 : SvStream& operator>>( double& rDouble );
318 : SvStream& operator>>( SvStream& rStream );
319 :
320 : SvStream& operator<<( sal_uInt16 nUInt16 );
321 : SvStream& operator<<( sal_uInt32 nUInt32 );
322 : SvStream& operator<<( sal_uInt64 nuInt64 );
323 : SvStream& operator<<( sal_Int16 nInt16 );
324 : SvStream& operator<<( sal_Int32 nInt32 );
325 : SvStream& operator<<( sal_Int64 nInt64 );
326 :
327 : SvStream& operator<<( signed char nChar );
328 : SvStream& operator<<( char nChar );
329 : SvStream& operator<<( unsigned char nChar );
330 : SvStream& operator<<( float nFloat );
331 : SvStream& operator<<( const double& rDouble );
332 : SvStream& operator<<( const char* pBuf );
333 : SvStream& operator<<( const unsigned char* pBuf );
334 : SvStream& operator<<( SvStream& rStream );
335 :
336 : SvStream& WriteNumber( sal_uInt32 nUInt32 );
337 : SvStream& WriteNumber( sal_Int32 nInt32 );
338 :
339 : sal_Size Read( void* pData, sal_Size nSize );
340 : sal_Size Write( const void* pData, sal_Size nSize );
341 : sal_Size Seek( sal_Size nPos );
342 : sal_Size SeekRel( sal_sSize nPos );
343 1075346 : sal_Size Tell() const { return nBufFilePos+nBufActualPos; }
344 : // length between current (Tell()) pos and end of stream
345 : virtual sal_Size remainingSize();
346 : void Flush();
347 1829330 : sal_Bool IsEof() const { return bIsEof; }
348 : // next Tell() <= nSize
349 : sal_Bool SetStreamSize( sal_Size nSize );
350 :
351 : /** Read a line of bytes.
352 :
353 : @param nMaxBytesToRead
354 : Maximum of bytes to read, if line is longer it will be
355 : truncated.
356 :
357 : @note NOTE that the default is one character less than STRING_MAXLEN to
358 : prevent problems after conversion to String that may be lurking
359 : in various places doing something like
360 : @code
361 : for (sal_uInt16 i=0; i < aString.Len(); ++i)
362 : @endcode
363 : causing endless loops ...
364 : */
365 : sal_Bool ReadLine( rtl::OString& rStr, sal_Int32 nMaxBytesToRead = 0xFFFE );
366 : sal_Bool WriteLine( const rtl::OString& rStr );
367 :
368 : /** Read a line of bytes.
369 :
370 : @param nMaxBytesToRead
371 : Maximum of bytes to read, if line is longer it will be
372 : truncated.
373 :
374 : @note NOTE that the default is one character less than STRING_MAXLEN to
375 : prevent problems after conversion to String that may be lurking
376 : in various places doing something like
377 : @code
378 : for (sal_uInt16 i=0; i < aString.Len(); ++i)
379 : @endcode
380 : causing endless loops ...
381 : */
382 : sal_Bool ReadByteStringLine( rtl::OUString& rStr, rtl_TextEncoding eSrcCharSet,
383 : sal_Int32 nMaxBytesToRead = 0xFFFE );
384 : sal_Bool ReadByteStringLine( String& rStr, rtl_TextEncoding eSrcCharSet );
385 : sal_Bool WriteByteStringLine( const String& rStr, rtl_TextEncoding eDestCharSet );
386 :
387 : /// Switch to no endian swapping and write 0xfeff
388 : sal_Bool StartWritingUnicodeText();
389 :
390 : /** If eReadBomCharSet==RTL_TEXTENCODING_DONTKNOW: read 16bit, if 0xfeff do
391 : nothing (UTF-16), if 0xfffe switch endian swapping (UTF-16), if 0xefbb
392 : or 0xbbef read another byte and check for UTF-8. If no UTF-* BOM was
393 : detected put all read bytes back. This means that if 2 bytes were read
394 : it was an UTF-16 BOM, if 3 bytes were read it was an UTF-8 BOM. There
395 : is no UTF-7, UTF-32 or UTF-EBCDIC BOM detection!
396 :
397 : If eReadBomCharSet!=RTL_TEXTENCODING_DONTKNOW: only read a BOM of that
398 : encoding and switch endian swapping if UTF-16 and 0xfffe. */
399 : sal_Bool StartReadingUnicodeText( rtl_TextEncoding eReadBomCharSet );
400 :
401 : /** Read a line of Unicode.
402 :
403 : @param nMaxCodepointsToRead
404 : Maximum of codepoints (UCS-2 or UTF-16 pairs, not bytes) to
405 : read, if line is longer it will be truncated.
406 :
407 : @note NOTE that the default is one character less than STRING_MAXLEN to
408 : prevent problems after conversion to String that may be lurking in
409 : various places doing something like
410 : @code
411 : for (sal_uInt16 i=0; i < aString.Len(); ++i)
412 : @endcode
413 : causing endless loops ...
414 : */
415 : sal_Bool ReadUniStringLine( rtl::OUString& rStr, sal_Int32 nMaxCodepointsToRead = 0xFFFE );
416 : /** Read a 32bit length prefixed sequence of utf-16 if
417 : eSrcCharSet==RTL_TEXTENCODING_UNICODE, otherwise read a 16bit length
418 : prefixed sequence of bytes and convert from eSrcCharSet */
419 : rtl::OUString ReadUniOrByteString(rtl_TextEncoding eSrcCharSet);
420 : /** Write a 32bit length prefixed sequence of utf-16 if
421 : eSrcCharSet==RTL_TEXTENCODING_UNICODE, otherwise convert to eSrcCharSet
422 : and write a 16bit length prefixed sequence of bytes */
423 : SvStream& WriteUniOrByteString( const rtl::OUString& rStr, rtl_TextEncoding eDestCharSet );
424 :
425 : /** Read a line of Unicode if eSrcCharSet==RTL_TEXTENCODING_UNICODE,
426 : otherwise read a line of Bytecode and convert from eSrcCharSet
427 :
428 : @param nMaxCodepointsToRead
429 : Maximum of codepoints (2 bytes if Unicode, bytes if not
430 : Unicode) to read, if line is longer it will be truncated.
431 :
432 : @note NOTE that the default is one character less than STRING_MAXLEN to
433 : prevent problems after conversion to String that may be lurking in
434 : various places doing something like
435 : @code
436 : for (sal_uInt16 i=0; i < aString.Len(); ++i)
437 : @endcode
438 : causing endless loops ...
439 : */
440 : sal_Bool ReadUniOrByteStringLine( rtl::OUString& rStr, rtl_TextEncoding eSrcCharSet,
441 : sal_Int32 nMaxCodepointsToRead = 0xFFFE );
442 : /** Write a sequence of Unicode characters if
443 : eDestCharSet==RTL_TEXTENCODING_UNICODE, otherwise write a sequence of
444 : Bytecodes converted to eDestCharSet */
445 : sal_Bool WriteUnicodeOrByteText( const String& rStr, rtl_TextEncoding eDestCharSet );
446 22 : sal_Bool WriteUnicodeOrByteText( const String& rStr )
447 22 : { return WriteUnicodeOrByteText( rStr, GetStreamCharSet() ); }
448 :
449 : /** Write a Unicode character if eDestCharSet==RTL_TEXTENCODING_UNICODE,
450 : otherwise write as Bytecode converted to eDestCharSet.
451 :
452 : This may result in more than one byte being written if a multi byte
453 : encoding (e.g. UTF7, UTF8) is chosen. */
454 : sal_Bool WriteUniOrByteChar( sal_Unicode ch, rtl_TextEncoding eDestCharSet );
455 0 : sal_Bool WriteUniOrByteChar( sal_Unicode ch )
456 0 : { return WriteUniOrByteChar( ch, GetStreamCharSet() ); }
457 :
458 : void SetBufferSize( sal_uInt16 nBufSize );
459 39 : sal_uInt16 GetBufferSize() const { return nBufSize; }
460 :
461 : void RefreshBuffer();
462 : SvStream& PutBack( char aCh );
463 :
464 103 : sal_Bool IsWritable() const { return bIsWritable; }
465 : StreamMode GetStreamMode() const { return eStreamMode; }
466 : virtual sal_uInt16 IsA() const;
467 :
468 2297 : long GetVersion() { return nVersion; }
469 2451 : void SetVersion( long n ) { nVersion = n; }
470 :
471 : friend SvStream& operator<<( SvStream& rStr, SvStrPtr f ); // for Manips
472 :
473 : /// end of input seen during previous i/o operation
474 694756 : bool eof() const { return bIsEof; }
475 :
476 : /// stream is broken
477 694633 : bool bad() const { return GetError() != 0; }
478 :
479 : /** Get state
480 :
481 : If the state is good() the previous i/o operation succeeded.
482 :
483 : If the state is good(), the next input operation might succeed;
484 : otherwise, it will fail.
485 :
486 : Applying an input operation to a stream that is not in the good() state
487 : is a null operation as far as the variable being read into is concerned.
488 :
489 : If we try to read into a variable v and the operation fails, the value
490 : of v should be unchanged,
491 : */
492 694743 : bool good() const { return !(eof() || bad()); }
493 : };
494 :
495 196481 : inline SvStream& operator<<( SvStream& rStr, SvStrPtr f )
496 : {
497 196481 : (*f)(rStr);
498 196481 : return rStr;
499 : }
500 :
501 0 : inline void SvStream::SetEndianSwap( sal_Bool bVal )
502 : {
503 : #ifdef OSL_BIGENDIAN
504 : SetNumberFormatInt( bVal ? NUMBERFORMAT_INT_LITTLEENDIAN : NUMBERFORMAT_INT_BIGENDIAN );
505 : #else
506 0 : SetNumberFormatInt( bVal ? NUMBERFORMAT_INT_BIGENDIAN : NUMBERFORMAT_INT_LITTLEENDIAN );
507 : #endif
508 0 : }
509 :
510 : TOOLS_DLLPUBLIC SvStream& endl( SvStream& rStr );
511 : /// same as endl() but Unicode
512 : TOOLS_DLLPUBLIC SvStream& endlu( SvStream& rStr );
513 : /// call endlu() if eStreamCharSet==RTL_TEXTECODING_UNICODE otherwise endl()
514 : TOOLS_DLLPUBLIC SvStream& endlub( SvStream& rStr );
515 :
516 : /// Attempt to read nUnits 8bit units to an OString, returned rtl::OString's
517 : /// length is number of units successfully read
518 : TOOLS_DLLPUBLIC rtl::OString read_uInt8s_ToOString(SvStream& rStrm,
519 : sal_Size nUnits);
520 :
521 : /// Attempt to read nUnits 8bit units to an OUString
522 57 : TOOLS_DLLPUBLIC inline rtl::OUString read_uInt8s_ToOUString(SvStream& rStrm,
523 : sal_Size nUnits, rtl_TextEncoding eEnc)
524 : {
525 57 : return rtl::OStringToOUString(read_uInt8s_ToOString(rStrm, nUnits), eEnc);
526 : }
527 :
528 : /// Attempt to read nUnits 16bit units to an OUString, returned
529 : /// rtl::OUString's length is number of units successfully read
530 : TOOLS_DLLPUBLIC rtl::OUString read_uInt16s_ToOUString(SvStream& rStrm,
531 : sal_Size nUnits);
532 :
533 : /// Attempt to read a pascal-style length (of type prefix) prefixed sequence of
534 : /// 16bit units to an OUString, returned rtl::OString's length is number of
535 : /// units successfully read.
536 : template<typename prefix>
537 23199 : rtl::OUString read_lenPrefixed_uInt16s_ToOUString(SvStream& rStrm)
538 : {
539 23199 : prefix nUnits = 0;
540 23199 : rStrm >> nUnits;
541 23199 : return read_uInt16s_ToOUString(rStrm, nUnits);
542 : }
543 :
544 : /// Attempt to write a prefixed sequence of nUnits 16bit units from an OUString,
545 : /// returned value is number of bytes written
546 : TOOLS_DLLPUBLIC sal_Size write_uInt16s_FromOUString(SvStream& rStrm,
547 : const rtl::OUString& rStr, sal_Size nUnits);
548 :
549 0 : TOOLS_DLLPUBLIC inline sal_Size write_uInt16s_FromOUString(SvStream& rStrm,
550 : const rtl::OUString& rStr)
551 : {
552 0 : return write_uInt16s_FromOUString(rStrm, rStr, rStr.getLength());
553 : }
554 :
555 : namespace streamdetail
556 : {
557 : /// Attempt to write a pascal-style length (of type prefix) prefixed
558 : /// sequence of units from a string-type, returned value is number of bytes
559 : /// written (including byte-count of prefix)
560 : template<typename prefix, typename S, sal_Size (*writeOper)(SvStream&, const S&, sal_Size)>
561 81058 : sal_Size write_lenPrefixed_seq_From_str(SvStream& rStrm, const S &rStr)
562 : {
563 81058 : sal_Size nWritten = 0;
564 81058 : prefix nUnits = std::min<sal_Size>(rStr.getLength(), std::numeric_limits<prefix>::max());
565 : SAL_WARN_IF(static_cast<sal_Size>(nUnits) != static_cast<sal_Size>(rStr.getLength()),
566 : "tools.stream",
567 : "string too long for prefix count to fit in output type");
568 81058 : rStrm << nUnits;
569 81058 : if (rStrm.good())
570 : {
571 81058 : nWritten += sizeof(prefix);
572 81058 : nWritten += writeOper(rStrm, rStr, nUnits);
573 : }
574 81058 : return nWritten;
575 : }
576 : }
577 :
578 : /// Attempt to write a pascal-style length (of type prefix) prefixed sequence
579 : /// of 16bit units from an OUString, returned value is number of bytes written
580 : /// (including byte-count of prefix)
581 22207 : template<typename prefix> sal_Size write_lenPrefixed_uInt16s_FromOUString(SvStream& rStrm,
582 : const rtl::OUString &rStr)
583 : {
584 22207 : return streamdetail::write_lenPrefixed_seq_From_str<prefix, rtl::OUString, write_uInt16s_FromOUString>(rStrm, rStr);
585 : }
586 :
587 : /// Attempt to read 8bit units to an OString until a zero terminator is
588 : /// encountered, returned rtl::OString's length is number of units *definitely*
589 : /// successfully read, check SvStream::good() to see if null terminator was
590 : /// sucessfully read
591 : TOOLS_DLLPUBLIC rtl::OString read_zeroTerminated_uInt8s_ToOString(SvStream& rStrm);
592 :
593 : /// Attempt to read 8bit units assuming source encoding eEnc to an OUString
594 : /// until a zero terminator is encountered. Check SvStream::good() to see if
595 : /// null terminator was sucessfully read
596 : TOOLS_DLLPUBLIC rtl::OUString read_zeroTerminated_uInt8s_ToOUString(SvStream& rStrm, rtl_TextEncoding eEnc);
597 :
598 : /// Attempt to read a pascal-style length (of type prefix) prefixed sequence of
599 : /// 8bit units to an OString, returned rtl::OString's length is number of units
600 : /// successfully read.
601 : template<typename prefix>
602 16652 : rtl::OString read_lenPrefixed_uInt8s_ToOString(SvStream& rStrm)
603 : {
604 16652 : prefix nUnits = 0;
605 16652 : rStrm >> nUnits;
606 16652 : return read_uInt8s_ToOString(rStrm, nUnits);
607 : }
608 :
609 : /// Attempt to read a pascal-style length (of type prefix) prefixed sequence of
610 : /// 8bit units to an OUString
611 : template<typename prefix>
612 8 : rtl::OUString read_lenPrefixed_uInt8s_ToOUString(SvStream& rStrm,
613 : rtl_TextEncoding eEnc)
614 : {
615 8 : return rtl::OStringToOUString(read_lenPrefixed_uInt8s_ToOString<prefix>(rStrm), eEnc);
616 : }
617 :
618 : /// Attempt to write a prefixed sequence of nUnits 8bit units from an OString,
619 : /// returned value is number of bytes written
620 58871 : TOOLS_DLLPUBLIC inline sal_Size write_uInt8s_FromOString(SvStream& rStrm, const rtl::OString& rStr,
621 : sal_Size nUnits)
622 : {
623 58871 : return rStrm.Write(rStr.getStr(), nUnits);
624 : }
625 :
626 0 : TOOLS_DLLPUBLIC inline sal_Size write_uInt8s_FromOString(SvStream& rStrm, const rtl::OString& rStr)
627 : {
628 0 : return write_uInt8s_FromOString(rStrm, rStr, rStr.getLength());
629 : }
630 :
631 : /// Attempt to write a pascal-style length (of type prefix) prefixed sequence
632 : /// of 8bit units from an OString, returned value is number of bytes written
633 : /// (including byte-count of prefix)
634 58851 : template<typename prefix> sal_Size write_lenPrefixed_uInt8s_FromOString(SvStream& rStrm,
635 : const rtl::OString &rStr)
636 : {
637 58851 : return streamdetail::write_lenPrefixed_seq_From_str<prefix, rtl::OString, write_uInt8s_FromOString>(rStrm, rStr);
638 : }
639 :
640 : /// Attempt to write a pascal-style length (of type prefix) prefixed sequence
641 : /// of 8bit units from an OUString, returned value is number of bytes written
642 : /// (including byte-count of prefix)
643 888 : template<typename prefix> sal_Size write_lenPrefixed_uInt8s_FromOUString(SvStream& rStrm,
644 : const rtl::OUString &rStr, rtl_TextEncoding eEnc)
645 : {
646 888 : return write_lenPrefixed_uInt8s_FromOString<prefix>(rStrm, rtl::OUStringToOString(rStr, eEnc));
647 : }
648 :
649 : // FileStream
650 :
651 : class TOOLS_DLLPUBLIC SvFileStream : public SvStream
652 : {
653 : friend class FileCopier;
654 :
655 : private:
656 : StreamData* pInstanceData;
657 : String aFilename;
658 : sal_uInt16 nLockCounter;
659 : sal_Bool bIsOpen;
660 : sal_uInt32 GetFileHandle() const;
661 :
662 : // Forbidden and not implemented.
663 : SvFileStream (const SvFileStream&);
664 : SvFileStream & operator= (const SvFileStream&);
665 :
666 : sal_Bool LockRange( sal_Size nByteOffset, sal_Size nBytes );
667 : sal_Bool UnlockRange( sal_Size nByteOffset, sal_Size nBytes );
668 : sal_Bool LockFile();
669 : sal_Bool UnlockFile();
670 :
671 : protected:
672 : virtual sal_Size GetData( void* pData, sal_Size nSize );
673 : virtual sal_Size PutData( const void* pData, sal_Size nSize );
674 : virtual sal_Size SeekPos( sal_Size nPos );
675 : virtual void SetSize( sal_Size nSize );
676 : virtual void FlushData();
677 :
678 : public:
679 : // Switches to Read StreamMode on failed attempt of Write opening
680 : SvFileStream( const String& rFileName, StreamMode eOpenMode );
681 : SvFileStream();
682 : ~SvFileStream();
683 :
684 : virtual void ResetError();
685 :
686 : void Open( const String& rFileName, StreamMode eOpenMode );
687 : void Close();
688 38 : sal_Bool IsOpen() const { return bIsOpen; }
689 : sal_Bool IsLocked() const { return ( nLockCounter!=0 ); }
690 : virtual sal_uInt16 IsA() const;
691 :
692 18 : const String& GetFileName() const { return aFilename; }
693 : };
694 :
695 : // MemoryStream
696 :
697 : class TOOLS_DLLPUBLIC SvMemoryStream : public SvStream
698 : {
699 : // Forbidden and not implemented.
700 : SvMemoryStream (const SvMemoryStream&);
701 : SvMemoryStream & operator= (const SvMemoryStream&);
702 :
703 : friend class SvCacheStream;
704 0 : sal_Size GetSize() const { return nSize; }
705 :
706 : protected:
707 : sal_Size nSize;
708 : sal_Size nResize;
709 : sal_Size nPos;
710 : sal_Size nEndOfData;
711 : sal_uInt8* pBuf;
712 : sal_Bool bOwnsData;
713 :
714 : virtual sal_Size GetData( void* pData, sal_Size nSize );
715 : virtual sal_Size PutData( const void* pData, sal_Size nSize );
716 : virtual sal_Size SeekPos( sal_Size nPos );
717 : virtual void SetSize( sal_Size nSize );
718 : virtual void FlushData();
719 :
720 : /// AllocateMemory must update pBuf accordingly
721 : /// - pBuf: Address of new block
722 : virtual sal_Bool AllocateMemory( sal_Size nSize );
723 :
724 : /// ReAllocateMemory must update the following variables:
725 : /// - pBuf: Address of new block
726 : /// - nEndOfData: Set to nNewSize-1L , if outside of block
727 : /// Set to 0 , if new block size is 0 bytes
728 : /// - nSize: New block size
729 : /// - nPos: Set to 0 if position outside of block
730 : virtual sal_Bool ReAllocateMemory( long nDiff );
731 :
732 : /// Is called when this stream allocated the buffer or the buffer is
733 : /// resized. FreeMemory may need to NULLify handles in derived classes.
734 : virtual void FreeMemory();
735 :
736 : SvMemoryStream(void*) { } // for sub-classes
737 :
738 : public:
739 : SvMemoryStream( void* pBuf, sal_Size nSize, StreamMode eMode);
740 : SvMemoryStream( sal_Size nInitSize=512, sal_Size nResize=64 );
741 : ~SvMemoryStream();
742 :
743 : virtual void ResetError();
744 :
745 5 : sal_Size GetEndOfData() const { return nEndOfData; }
746 5345 : const void* GetData() { Flush(); return pBuf; }
747 : operator const void*() { Flush(); return pBuf; }
748 : virtual sal_uInt16 IsA() const;
749 :
750 : void* SwitchBuffer( sal_Size nInitSize=512, sal_Size nResize=64 );
751 : void* SetBuffer( void* pBuf, sal_Size nSize,
752 : sal_Bool bOwnsData=sal_True, sal_Size nEOF=0 );
753 :
754 32 : void ObjectOwnsMemory( sal_Bool bOwn ) { bOwnsData = bOwn; }
755 : sal_Bool IsObjectMemoryOwner() { return bOwnsData; }
756 23 : void SetResizeOffset( sal_Size nNewResize ) { nResize = nNewResize; }
757 : sal_Size GetResizeOffset() const { return nResize; }
758 0 : virtual sal_Size remainingSize() { return GetSize() - Tell(); }
759 : };
760 :
761 : /** Data Copy Stream
762 :
763 : This class is the foundation for all classes, using SvData
764 : (SO2\DTRANS.HXX/CXX) for transportation (e.g., graphics).
765 : */
766 11376 : class TOOLS_DLLPUBLIC SvDataCopyStream
767 : {
768 : public:
769 : // repeated execution of Load or Assign is allowed
770 : TYPEINFO();
771 10801 : virtual ~SvDataCopyStream(){}
772 : virtual void Load( SvStream & ) = 0;
773 : virtual void Save( SvStream & ) = 0;
774 : virtual void Assign( const SvDataCopyStream & );
775 : };
776 :
777 : #endif
778 :
779 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|