31 m_fileHandle(fileHandle),
33 m_writeable(writeable),
40 m_fileHandle(nullptr),
45 m_data.
resize(buffer.length());
46 memcpy(&m_data[0], &buffer[0], buffer.length());
67 m_pos = PHYSFS_tell(m_fileHandle);
68 PHYSFS_seek(m_fileHandle, 0);
69 int size = PHYSFS_fileLength(m_fileHandle);
71 if(PHYSFS_readBytes(m_fileHandle, m_data.
data(),
size) == -1)
72 throwError(
"unable to read file data",
true);
73 PHYSFS_close(m_fileHandle);
74 m_fileHandle =
nullptr;
80 if(m_fileHandle && PHYSFS_isInit()) {
81 if(!PHYSFS_close(m_fileHandle))
82 throwError(
"close failed",
true);
83 m_fileHandle =
nullptr;
93 throwError(
"filestream is not writeable");
97 if(!PHYSFS_seek(m_fileHandle, 0))
98 throwError(
"flush seek failed",
true);
100 if(PHYSFS_writeBytes(m_fileHandle, m_data.
data(), len) != len)
101 throwError(
"flush write failed",
true);
104 if(PHYSFS_flush(m_fileHandle) == 0)
105 throwError(
"flush failed",
true);
112 int res = PHYSFS_readBytes(m_fileHandle, buffer,
size * nmemb);
114 throwError(
"read failed",
true);
119 for(
uint i=0;i<nmemb;++i) {
124 outBuffer[writePos++] = m_data[m_pos++];
133 if(PHYSFS_writeBytes(m_fileHandle, buffer, count) != count)
134 throwError(
"write failed",
true);
136 m_data.
grow(m_pos + count);
137 memcpy(&m_data[m_pos], buffer, count);
145 if(!PHYSFS_seek(m_fileHandle, pos))
146 throwError(
"seek failed",
true);
148 if(pos > m_data.
size())
149 throwError(
"seek failed");
162 return PHYSFS_fileLength(m_fileHandle);
164 return m_data.
size();
170 return PHYSFS_tell(m_fileHandle);
178 return PHYSFS_eof(m_fileHandle);
180 return m_pos >= m_data.
size();
187 if(PHYSFS_readBytes(m_fileHandle, &v, 1) != 1)
188 throwError(
"read failed",
true);
190 if(m_pos+1 > m_data.
size())
191 throwError(
"read failed");
203 if(PHYSFS_readULE16(m_fileHandle, &v) == 0)
204 throwError(
"read failed",
true);
206 if(m_pos+2 > m_data.
size())
207 throwError(
"read failed");
219 if(PHYSFS_readULE32(m_fileHandle, &v) == 0)
220 throwError(
"read failed",
true);
222 if(m_pos+4 > m_data.
size())
223 throwError(
"read failed");
235 if(PHYSFS_readULE64(m_fileHandle, (PHYSFS_uint64*)&v) == 0)
236 throwError(
"read failed",
true);
238 if(m_pos+8 > m_data.
size())
239 throwError(
"read failed");
250 if(PHYSFS_readBytes(m_fileHandle, &v, 1) != 1)
251 throwError(
"read failed",
true);
253 if(m_pos+1 > m_data.
size())
254 throwError(
"read failed");
266 if(PHYSFS_readSLE16(m_fileHandle, &v) == 0)
267 throwError(
"read failed",
true);
269 if(m_pos+2 > m_data.
size())
270 throwError(
"read failed");
282 if(PHYSFS_readSLE32(m_fileHandle, &v) == 0)
283 throwError(
"read failed",
true);
285 if(m_pos+4 > m_data.
size())
286 throwError(
"read failed");
298 if(PHYSFS_readSLE64(m_fileHandle, (PHYSFS_sint64*)&v) == 0)
299 throwError(
"read failed",
true);
301 if(m_pos+8 > m_data.
size())
302 throwError(
"read failed");
313 if(len > 0 && len < 8192) {
316 if(PHYSFS_readBytes(m_fileHandle, buffer, len) == 0)
317 throwError(
"read failed",
true);
319 str = std::string(buffer, len);
321 if(m_pos+len > m_data.
size()) {
322 throwError(
"read failed");
326 str = std::string((
char*)&m_data[m_pos], len);
330 throwError(
"read failed because string is too big");
357 if(PHYSFS_writeBytes(m_fileHandle, &v, 1) != 1)
358 throwError(
"write failed",
true);
368 if(PHYSFS_writeULE16(m_fileHandle, v) == 0)
369 throwError(
"write failed",
true);
371 m_data.
grow(m_pos + 2);
380 if(PHYSFS_writeULE32(m_fileHandle, v) == 0)
381 throwError(
"write failed",
true);
383 m_data.
grow(m_pos + 4);
392 if(PHYSFS_writeULE64(m_fileHandle, v) == 0)
393 throwError(
"write failed",
true);
395 m_data.
grow(m_pos + 8);
404 if(PHYSFS_writeBytes(m_fileHandle, &v, 1) != 1)
405 throwError(
"write failed",
true);
415 if(PHYSFS_writeSLE16(m_fileHandle, v) == 0)
416 throwError(
"write failed",
true);
418 m_data.
grow(m_pos + 2);
427 if(PHYSFS_writeSLE32(m_fileHandle, v) == 0)
428 throwError(
"write failed",
true);
430 m_data.
grow(m_pos + 4);
439 if(PHYSFS_writeSLE64(m_fileHandle, v) == 0)
440 throwError(
"write failed",
true);
442 m_data.
grow(m_pos + 8);
451 write(v.c_str(), v.length());
454 void FileStream::throwError(
const std::string& message,
bool physfsError)
456 std::string completeMessage =
stdext::format(
"in file '%s': %s", m_name, message);
458 completeMessage += std::string(
": ") + PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode());