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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
|
/*
* 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 2 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, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifndef DATA_H
#define DATA_H
#include "U7obj.h"
#include "endianio.h"
#include "utils.h"
#include <cassert>
#include <cstddef>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <memory>
#include <string>
class ODataSource;
/**
* Abstract input base class.
*/
class IDataSource {
public:
IDataSource() = default;
IDataSource(const IDataSource&) = delete;
IDataSource& operator=(const IDataSource&) = delete;
IDataSource(IDataSource&&) noexcept = default;
IDataSource& operator=(IDataSource&&) noexcept = default;
virtual ~IDataSource() noexcept = default;
virtual uint32 peek() = 0;
virtual uint32 read1() = 0;
virtual uint16 read2() = 0;
virtual uint16 read2high() = 0;
virtual uint32 read4() = 0;
virtual uint32 read4high() = 0;
virtual void read(void*, size_t) = 0;
virtual void read(std::string&, size_t) = 0;
std::unique_ptr<unsigned char[]> readN(size_t N) {
auto ptr = std::make_unique<unsigned char[]>(N);
read(ptr.get(), N);
return ptr;
}
virtual std::unique_ptr<IDataSource> makeSource(size_t) = 0;
virtual void seek(size_t) = 0;
virtual void skip(std::streamoff) = 0;
virtual size_t getSize() const = 0;
virtual size_t getPos() const = 0;
size_t getAvail() const {
const size_t msize = getSize();
const size_t mpos = getPos();
return msize >= mpos ? msize - mpos : 0;
}
virtual bool eof() const = 0;
virtual bool good() const {
return true;
}
virtual void clear_error() {}
virtual void copy_to(ODataSource& dest);
void readline(std::string& str) {
str.erase();
while (!eof()) {
const char character = static_cast<char>(read1());
if (character == '\r') {
continue; // Skip cr
}
if (character == '\n') {
break; // break on line feed
}
str += character;
}
}
};
/**
* Stream-based input data source which does not own the stream.
*/
class IStreamDataSource : public IDataSource {
protected:
std::istream* in;
public:
explicit IStreamDataSource(std::istream* data_stream) : in(data_stream) {}
uint32 peek() final {
return in->peek();
}
uint32 read1() final {
return Read1(in);
}
uint16 read2() final {
return little_endian::Read2(in);
}
uint16 read2high() final {
return big_endian::Read2(in);
}
uint32 read4() final {
return little_endian::Read4(in);
}
uint32 read4high() final {
return big_endian::Read4(in);
}
void read(void* b, size_t len) final {
in->read(static_cast<char*>(b), len);
}
void read(std::string& s, size_t len) final {
s.resize(len);
in->read(s.data(), len);
}
std::unique_ptr<IDataSource> makeSource(size_t len) final;
void seek(size_t pos) final {
in->seekg(pos);
}
void skip(std::streamoff pos) final {
in->seekg(pos, std::ios::cur);
}
size_t getSize() const final {
return get_file_size(*in);
}
size_t getPos() const final {
return in->tellg();
}
bool eof() const final {
in->get();
const bool ret = in->eof();
if (!ret) {
in->unget();
}
return ret;
}
bool good() const final {
return in && in->good();
}
void clear_error() final {
in->clear();
}
};
/**
* File-based input data source does owns the stream.
*/
class IFileDataSource : public IStreamDataSource {
std::unique_ptr<std::istream> pFin;
public:
explicit IFileDataSource(const File_spec& spec, bool is_text = false)
: IStreamDataSource(nullptr) {
if (U7exists(spec.name)) {
pFin = U7open_in(spec.name.c_str(), is_text);
} else {
// Set fail bit
pFin = std::make_unique<std::ifstream>();
auto& fin = *pFin;
fin.seekg(0);
}
in = pFin.get();
}
};
/**
* Buffer-based input data source which does not own the buffer.
*/
class IBufferDataView : public IDataSource {
protected:
const unsigned char* buf;
const unsigned char* buf_ptr;
std::size_t size;
public:
IBufferDataView(const void* data, size_t len)
: buf(static_cast<const unsigned char*>(data)), buf_ptr(buf),
size(len) {
// data can be nullptr if len is also 0
assert(data != nullptr || len == 0);
}
IBufferDataView(const std::unique_ptr<unsigned char[]>& data_, size_t len)
: IBufferDataView(data_.get(), len) {}
// Prevent use after free.
IBufferDataView(std::unique_ptr<unsigned char[]>&& data_, size_t len)
= delete;
uint32 peek() final {
return *buf_ptr;
}
uint32 read1() final {
return Read1(buf_ptr);
}
uint16 read2() final {
return little_endian::Read2(buf_ptr);
}
uint16 read2high() final {
return big_endian::Read2(buf_ptr);
}
uint32 read4() final {
return little_endian::Read4(buf_ptr);
}
uint32 read4high() final {
return big_endian::Read4(buf_ptr);
}
void read(void* b, size_t len) final {
std::memcpy(b, buf_ptr, len);
buf_ptr += len;
}
void read(std::string& s, size_t len) final {
s = std::string(reinterpret_cast<const char*>(buf_ptr), len);
buf_ptr += len;
}
std::unique_ptr<IDataSource> makeSource(size_t len) final;
void seek(size_t pos) final {
buf_ptr = buf + pos;
}
void skip(std::streamoff pos) final {
buf_ptr += pos;
}
size_t getSize() const final {
return size;
}
size_t getPos() const final {
return buf_ptr - buf;
}
const unsigned char* getPtr() {
return buf_ptr;
}
bool eof() const final {
return buf_ptr >= buf + size;
}
bool good() const final {
return (buf != nullptr) && (size != 0U);
}
void copy_to(ODataSource& dest) final;
};
/**
* Buffer-based input data source which owns the stream.
*/
class IBufferDataSource : public IBufferDataView {
protected:
std::unique_ptr<unsigned char[]> data;
public:
IBufferDataSource(void* data_, size_t len)
: IBufferDataView(data_, len),
data(static_cast<unsigned char*>(data_)) {}
IBufferDataSource(std::unique_ptr<unsigned char[]> data_, size_t len)
: IBufferDataView(data_, len), data(std::move(data_)) {}
auto steal_data(size_t& len) {
len = size;
return std::move(data);
}
};
/**
* Buffer-based input data source which opens an U7 object or
* multiobject, and reads into an internal buffer.
*/
class IExultDataSource : public IBufferDataSource {
public:
IExultDataSource(const File_spec& fname, int index)
: IBufferDataSource(nullptr, 0) {
const U7object obj(fname, index);
data = obj.retrieve(size);
buf = buf_ptr = data.get();
}
IExultDataSource(
const File_spec& fname0, const File_spec& fname1, int index)
: IBufferDataSource(nullptr, 0) {
const U7multiobject obj(fname0, fname1, index);
data = obj.retrieve(size);
buf = buf_ptr = data.get();
}
IExultDataSource(
const File_spec& fname0, const File_spec& fname1,
const File_spec& fname2, int index)
: IBufferDataSource(nullptr, 0) {
const U7multiobject obj(fname0, fname1, fname2, index);
data = obj.retrieve(size);
buf = buf_ptr = data.get();
}
};
/**
* Abstract output base class.
*/
class ODataSource {
public:
ODataSource() = default;
ODataSource(const ODataSource&) = delete;
ODataSource& operator=(const ODataSource&) = delete;
ODataSource(ODataSource&&) noexcept = default;
ODataSource& operator=(ODataSource&&) noexcept = default;
virtual ~ODataSource() noexcept = default;
virtual void write1(uint32) = 0;
virtual void write2(uint16) = 0;
virtual void write2high(uint16) = 0;
virtual void write4(uint32) = 0;
virtual void write4high(uint32) = 0;
virtual void write(const void*, size_t) = 0;
virtual void write(const std::string&) = 0;
virtual void seek(size_t) = 0;
virtual void skip(std::streamoff) = 0;
virtual size_t getSize() const = 0;
virtual size_t getPos() const = 0;
virtual void flush() {}
virtual bool good() const {
return true;
}
virtual void clear_error() {}
};
/**
* Stream-based output data source which does not own the stream.
*/
class OStreamDataSource : public ODataSource {
protected:
std::ostream* out;
public:
explicit OStreamDataSource(std::ostream* data_stream) : out(data_stream) {}
void write1(uint32 val) final {
Write1(out, static_cast<uint16>(val));
}
void write2(uint16 val) final {
little_endian::Write2(out, val);
}
void write2high(uint16 val) final {
big_endian::Write2(out, val);
}
void write4(uint32 val) final {
little_endian::Write4(out, val);
}
void write4high(uint32 val) final {
big_endian::Write4(out, val);
}
void write(const void* b, size_t len) final {
out->write(static_cast<const char*>(b), len);
}
void write(const std::string& s) final {
out->write(s.data(), s.size());
}
void seek(size_t pos) final {
out->seekp(pos);
}
void skip(std::streamoff pos) final {
out->seekp(pos, std::ios::cur);
}
size_t getSize() const final {
return out->tellp();
}
size_t getPos() const final {
return out->tellp();
}
void flush() final {
out->flush();
}
bool good() const final {
return out->good();
}
void clear_error() final {
out->clear();
}
};
/**
* File-based output data source which owns the stream.
*/
class OFileDataSource : public OStreamDataSource {
std::unique_ptr<std::ostream> fout;
public:
explicit OFileDataSource(const File_spec& spec, bool is_text = false)
: OStreamDataSource(nullptr) {
fout = U7open_out(spec.name.c_str(), is_text);
out = fout.get();
}
};
/**
* Buffer-based output data source which does not own the buffer.
*/
class OBufferDataSpan : public ODataSource {
protected:
unsigned char* buf;
unsigned char* buf_ptr;
std::size_t size;
public:
OBufferDataSpan(void* data, size_t len)
: buf(static_cast<unsigned char*>(data)), buf_ptr(buf), size(len) {
// data can be nullptr if len is also 0
assert(data != nullptr || len == 0);
}
OBufferDataSpan(const std::unique_ptr<unsigned char[]>& data_, size_t len)
: OBufferDataSpan(data_.get(), len) {}
// Prevent use after free.
OBufferDataSpan(std::unique_ptr<unsigned char[]>&& data_, size_t len)
= delete;
void write1(uint32 val) final {
Write1(buf_ptr, val);
}
void write2(uint16 val) final {
little_endian::Write2(buf_ptr, val);
}
void write2high(uint16 val) final {
big_endian::Write2(buf_ptr, val);
}
void write4(uint32 val) final {
little_endian::Write4(buf_ptr, val);
}
void write4high(uint32 val) final {
big_endian::Write4(buf_ptr, val);
}
void write(const void* b, size_t len) final {
std::memcpy(buf_ptr, b, len);
buf_ptr += len;
}
void write(const std::string& s) final {
write(s.data(), s.size());
}
void seek(size_t pos) final {
buf_ptr = buf + pos;
}
void skip(std::streamoff pos) final {
buf_ptr += pos;
}
size_t getSize() const final {
return size;
}
size_t getPos() const final {
return buf_ptr - buf;
}
unsigned char* getPtr() {
return buf_ptr;
}
};
/**
* Buffer-based output data source which owns the buffer.
*/
class OBufferDataSource : public OBufferDataSpan {
std::unique_ptr<unsigned char[]> data;
public:
explicit OBufferDataSource(size_t len)
: OBufferDataSpan(nullptr, 0),
data(std::make_unique<unsigned char[]>(len)) {
assert(len != 0);
buf_ptr = buf = data.get();
size = len;
}
OBufferDataSource(std::unique_ptr<unsigned char[]> data_, size_t len)
: OBufferDataSpan(data_, len), data(std::move(data_)) {}
OBufferDataSource(void* data_, size_t len)
: OBufferDataSpan(data_, len),
data(static_cast<unsigned char*>(data_)) {}
};
inline void IDataSource::copy_to(ODataSource& dest) {
const size_t len = getSize();
auto data = readN(len);
dest.write(data.get(), len);
}
inline std::unique_ptr<IDataSource> IStreamDataSource::makeSource(size_t len) {
return std::make_unique<IBufferDataSource>(readN(len), len);
}
inline std::unique_ptr<IDataSource> IBufferDataView::makeSource(size_t len) {
const size_t avail = getAvail();
if (avail < len) {
len = avail;
}
const unsigned char* ptr = getPtr();
skip(len);
return std::make_unique<IBufferDataView>(ptr, len);
}
inline void IBufferDataView::copy_to(ODataSource& dest) {
const size_t len = getAvail();
dest.write(getPtr(), len);
skip(len);
}
#endif
|