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