blob: e611d134383fae75bed8c2a62084e5439f048e39 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
// * This file is part of the COLOBOT source code
// * Copyright (C) 2014 Polish Portal of Colobot (PPC)
// *
// * This program is free software: you can redistribute it and/or modify
// * it under the terms of the GNU General Public License as published by
// * the Free Software Foundation, either version 3 of the License, or
// * (at your option) any later version.
// *
// * This program is distributed in the hope that it will be useful,
// * but WITHOUT ANY WARRANTY; without even the implied warranty of
// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// * GNU General Public License for more details.
// *
// * You should have received a copy of the GNU General Public License
// * along with this program. If not, see http://www.gnu.org/licenses/.
#include "common/resources/outputstreambuffer.h"
#include <stdexcept>
#include <sstream>
COutputStreamBuffer::COutputStreamBuffer(size_t buffer_size) : m_buffer_size(buffer_size)
{
m_file = nullptr;
m_buffer = new char[buffer_size];
setp(m_buffer, m_buffer + buffer_size);
}
COutputStreamBuffer::~COutputStreamBuffer()
{
close();
delete m_buffer;
}
void COutputStreamBuffer::open(const std::string &filename)
{
if (PHYSFS_isInit())
m_file = PHYSFS_openWrite(filename.c_str());
}
void COutputStreamBuffer::close()
{
if (is_open())
PHYSFS_close(m_file);
}
bool COutputStreamBuffer::is_open()
{
return m_file;
}
std::streambuf::int_type COutputStreamBuffer::overflow(std::streambuf::int_type ch)
{
/* This function should be called when pptr() == epptr(). We use it also in sync()
so we also have to write data if buffer is not full. */
if (pbase() == pptr()) // no data to write, sync() called with empty buffer
return 0;
// save buffer
PHYSFS_sint64 bytes_written = PHYSFS_write(m_file, pbase(), 1, pptr() - pbase());
if (bytes_written <= 0)
return traits_type::eof();
pbump(-bytes_written);
// write final char
if (ch != traits_type::eof()) {
bytes_written = PHYSFS_write(m_file, &ch, 1, 1);
if (bytes_written <= 0)
return traits_type::eof();
}
return ch;
}
int COutputStreamBuffer::sync()
{
return overflow(traits_type::eof());
}
|