xoreos  0.0.5
writestream.cpp
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 /* Based on ScummVM (<http://scummvm.org>) code, which is released
26  * under the terms of version 2 or later of the GNU General Public
27  * License.
28  *
29  * The original copyright note in ScummVM reads as follows:
30  *
31  * ScummVM is the legal property of its developers, whose names
32  * are too numerous to list here. Please refer to the COPYRIGHT
33  * file distributed with this source distribution.
34  *
35  * This program is free software; you can redistribute it and/or
36  * modify it under the terms of the GNU General Public License
37  * as published by the Free Software Foundation; either version 2
38  * of the License, or (at your option) any later version.
39  *
40  * This program is distributed in the hope that it will be useful,
41  * but WITHOUT ANY WARRANTY; without even the implied warranty of
42  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
43  * GNU General Public License for more details.
44  *
45  * You should have received a copy of the GNU General Public License
46  * along with this program; if not, write to the Free Software
47  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
48  */
49 
50 #include <cstring>
51 
52 #include "src/common/writestream.h"
53 #include "src/common/readstream.h"
54 #include "src/common/util.h"
55 #include "src/common/ustring.h"
56 
57 namespace Common {
58 
60 }
61 
63  try {
64  flush();
65  } catch (...) {
66  }
67 }
68 
70 }
71 
72 size_t WriteStream::writeStream(ReadStream &stream, size_t n) {
73  size_t haveRead = 0, haveWritten = 0;
74 
75  byte buf[4096];
76  while (!stream.eos() && (n > 0)) {
77  const size_t toRead = MIN<size_t>(4096, n);
78  const size_t bufRead = stream.read(buf, toRead);
79 
80  const size_t bufWrite = write(buf, bufRead);
81 
82  n -= bufRead;
83  haveRead += bufRead;
84  haveWritten += bufWrite;
85  }
86 
87  return haveWritten;
88 }
89 
91  return writeStream(stream, 0xFFFFFFFF);
92 }
93 
95  if (write(str.c_str(), std::strlen(str.c_str())) != std::strlen(str.c_str()))
96  throw Exception(kWriteError);
97 }
98 
100 
101 }
102 
104 
105 }
106 
107 size_t SeekableWriteStream::evalSeek(ptrdiff_t offset, SeekableWriteStream::Origin whence, size_t pos, size_t begin,
108  size_t size) {
109  switch (whence) {
110  case kOriginEnd:
111  offset = size + offset;
113  case kOriginBegin:
114  return begin + offset;
115  case kOriginCurrent:
116  return pos + offset;
117 
118  default:
119  break;
120  }
121 
122  throw Exception("Invalid whence (%d)", (int) whence);
123 }
124 
125 } // End of namespace Common
Generic interface for a readable data stream.
Definition: readstream.h:64
Definition: 2dafile.h:39
virtual void flush()
Commit any buffered data to the underlying channel or storage medium; unbuffered streams can use the ...
Definition: writestream.cpp:69
A class holding an UTF-8 string.
Definition: ustring.h:48
void writeString(const UString &str)
Write the given string to the stream, encoded as UTF-8.
Definition: writestream.cpp:94
static size_t evalSeek(ptrdiff_t offset, Origin whence, size_t pos, size_t begin, size_t size)
Evaluate the seek offset relative to whence into a position from the beginning.
virtual bool eos() const =0
Returns true if a read failed because the stream has been reached.
Seek from the current position of the stream.
Definition: writestream.h:220
virtual size_t pos() const =0
Obtains the current value of the stream position indicator of the stream.
const char * c_str() const
Return the (utf8 encoded) string data.
Definition: ustring.cpp:249
Utility templates and functions.
Basic writing stream interfaces.
virtual size_t read(void *dataPtr, size_t dataSize)=0
Read data from the stream.
const Exception kWriteError("Write error")
Exception when writing to a stream failed.
Definition: error.h:64
Seek from the end of the stream.
Definition: writestream.h:221
StackException Exception
Definition: error.h:59
virtual size_t write(const void *dataPtr, size_t dataSize)=0
Write data into the stream.
Basic reading stream interfaces.
Unicode string handling.
#define XOREOS_FALLTHROUGH
Definition: fallthrough.h:60
virtual ~WriteStream()
Definition: writestream.cpp:62
size_t writeStream(ReadStream &stream, size_t n)
Copy n bytes of the given stream into the stream.
Definition: writestream.cpp:72
Origin
The position a seeking offset takes as a base.
Definition: writestream.h:218
virtual size_t size() const =0
Obtains the total size of the stream, measured in bytes.
uint8 byte
Definition: types.h:209
Seek from the begin of the stream.
Definition: writestream.h:219