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