2014-07-30 19:10:28 +02:00
|
|
|
/*
|
2018-07-19 14:02:37 +02:00
|
|
|
* Copyright (C) 2014-2018 Max Kellermann <max.kellermann@gmail.com>
|
2014-07-30 19:10:28 +02:00
|
|
|
*
|
2018-07-19 14:02:37 +02:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
2014-07-30 19:10:28 +02:00
|
|
|
*
|
2018-07-19 14:02:37 +02:00
|
|
|
* - Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
2014-07-30 19:10:28 +02:00
|
|
|
*
|
2018-07-19 14:02:37 +02:00
|
|
|
* - Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
|
|
|
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
2014-07-30 19:10:28 +02:00
|
|
|
*/
|
|
|
|
|
2018-07-19 14:02:37 +02:00
|
|
|
#ifndef BUFFERED_OUTPUT_STREAM_HXX
|
|
|
|
#define BUFFERED_OUTPUT_STREAM_HXX
|
2014-07-30 19:10:28 +02:00
|
|
|
|
2018-08-20 16:19:17 +02:00
|
|
|
#include "util/Compiler.h"
|
2014-07-30 19:10:28 +02:00
|
|
|
#include "util/DynamicFifoBuffer.hxx"
|
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
|
2016-09-04 12:05:37 +02:00
|
|
|
#ifdef _UNICODE
|
|
|
|
#include <wchar.h>
|
|
|
|
#endif
|
|
|
|
|
2014-07-30 19:10:28 +02:00
|
|
|
class OutputStream;
|
|
|
|
|
2015-03-22 17:01:33 +01:00
|
|
|
/**
|
|
|
|
* An #OutputStream wrapper that buffers its output to reduce the
|
|
|
|
* number of OutputStream::Write() calls.
|
2016-09-04 12:05:37 +02:00
|
|
|
*
|
|
|
|
* All wchar_t based strings are converted to UTF-8.
|
2015-03-22 17:01:33 +01:00
|
|
|
*/
|
2014-07-30 19:10:28 +02:00
|
|
|
class BufferedOutputStream {
|
|
|
|
OutputStream &os;
|
|
|
|
|
|
|
|
DynamicFifoBuffer<char> buffer;
|
|
|
|
|
|
|
|
public:
|
2017-01-04 10:37:55 +01:00
|
|
|
explicit BufferedOutputStream(OutputStream &_os)
|
2014-07-30 19:10:28 +02:00
|
|
|
:os(_os), buffer(32768) {}
|
|
|
|
|
2015-12-16 10:24:43 +01:00
|
|
|
void Write(const void *data, size_t size);
|
2016-09-04 12:05:11 +02:00
|
|
|
|
|
|
|
void Write(const char &ch) {
|
|
|
|
Write(&ch, sizeof(ch));
|
|
|
|
}
|
|
|
|
|
2015-12-16 10:24:43 +01:00
|
|
|
void Write(const char *p);
|
2014-07-30 19:10:28 +02:00
|
|
|
|
|
|
|
gcc_printf(2,3)
|
2015-12-16 10:24:43 +01:00
|
|
|
void Format(const char *fmt, ...);
|
2014-07-30 19:10:28 +02:00
|
|
|
|
2016-09-04 12:05:37 +02:00
|
|
|
#ifdef _UNICODE
|
|
|
|
void Write(const wchar_t &ch) {
|
|
|
|
WriteWideToUTF8(&ch, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Write(const wchar_t *p);
|
|
|
|
#endif
|
|
|
|
|
2015-03-22 17:01:33 +01:00
|
|
|
/**
|
|
|
|
* Write buffer contents to the #OutputStream.
|
|
|
|
*/
|
2015-12-16 10:24:43 +01:00
|
|
|
void Flush();
|
2014-07-30 19:10:28 +02:00
|
|
|
|
|
|
|
private:
|
2015-12-16 10:24:43 +01:00
|
|
|
bool AppendToBuffer(const void *data, size_t size) noexcept;
|
2016-09-04 12:05:37 +02:00
|
|
|
|
|
|
|
#ifdef _UNICODE
|
|
|
|
void WriteWideToUTF8(const wchar_t *p, size_t length);
|
|
|
|
#endif
|
2014-07-30 19:10:28 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|