fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / sax / source / tools / CachedOutputStream.hxx
blobf11697871ad002425d7d688985f8c87f754ad86d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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 */
10 #ifndef INCLUDED_SAX_SOURCE_TOOLS_CACHEDOUTPUTSTREAM_HXX
11 #define INCLUDED_SAX_SOURCE_TOOLS_CACHEDOUTPUTSTREAM_HXX
13 #include <sal/types.h>
15 #include <com/sun/star/io/XOutputStream.hpp>
16 #include <com/sun/star/uno/Sequence.hxx>
18 #include <cstring>
19 #include <cstdlib>
20 #include <boost/shared_ptr.hpp>
22 namespace sax_fastparser {
24 class ForMergeBase
26 public:
27 virtual ~ForMergeBase() {}
28 virtual void append( const css::uno::Sequence<sal_Int8>& rWhat ) = 0;
31 class CachedOutputStream
33 /// When buffer hits this size, it's written to mxOutputStream
34 static const sal_Int32 mnMaximumSize = 0x10000;
36 /// Output stream, usually writing data into files.
37 css::uno::Reference< css::io::XOutputStream > mxOutputStream;
38 sal_Int32 mnCacheWrittenSize;
39 const css::uno::Sequence<sal_Int8> mpCache;
40 uno_Sequence *pSeq;
41 bool mbWriteToOutStream;
42 /// ForMerge structure is used for sorting elements in Writer
43 boost::shared_ptr< ForMergeBase > mpForMerge;
45 public:
46 CachedOutputStream() : mnCacheWrittenSize(0)
47 , mpCache(mnMaximumSize)
48 , pSeq(mpCache.get())
49 , mbWriteToOutStream(true)
50 , mpForMerge(nullptr)
52 ~CachedOutputStream() {}
54 css::uno::Reference< css::io::XOutputStream > getOutputStream() const
56 return mxOutputStream;
59 void setOutputStream( const css::uno::Reference< css::io::XOutputStream >& xOutputStream )
61 mxOutputStream = xOutputStream;
64 void setOutput( boost::shared_ptr< ForMergeBase > pForMerge )
66 flush();
67 mbWriteToOutStream = false;
68 mpForMerge = pForMerge;
71 void resetOutputToStream()
73 flush();
74 mbWriteToOutStream = true;
75 mpForMerge.reset();
78 /// cache string and if limit is hit, flush
79 void writeBytes( const sal_Int8* pStr, sal_Int32 nLen )
81 // Write when the buffer gets big enough
82 if (mnCacheWrittenSize + nLen > mnMaximumSize)
84 flush();
86 // Writer does some elements sorting, so it can accumulate
87 // pretty big strings in FastSaxSerializer::ForMerge.
88 // In that case, just flush data and write immediately.
89 if (nLen > mnMaximumSize)
91 if (mbWriteToOutStream)
92 mxOutputStream->writeBytes( css::uno::Sequence<sal_Int8>(pStr, nLen) );
93 else
94 mpForMerge->append( css::uno::Sequence<sal_Int8>(pStr, nLen) );
95 return;
99 memcpy(pSeq->elements + mnCacheWrittenSize, pStr, nLen);
100 mnCacheWrittenSize += nLen;
103 /// immediately write buffer into mxOutputStream and clear
104 void flush()
106 // resize the Sequence to written size
107 pSeq->nElements = mnCacheWrittenSize;
108 if (mbWriteToOutStream)
109 mxOutputStream->writeBytes( mpCache );
110 else
111 mpForMerge->append( mpCache );
112 // and next time write to the beginning
113 mnCacheWrittenSize = 0;
119 #endif
121 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */