Return a const char * from read_file_mapping::read

This commit is contained in:
Thomas Goyne 2014-03-21 11:08:26 -07:00
parent 23ff6dead1
commit 0268ffd345
7 changed files with 23 additions and 16 deletions

View file

@ -79,7 +79,11 @@ read_file_mapping::read_file_mapping(fs::path const& filename)
read_file_mapping::~read_file_mapping() { } read_file_mapping::~read_file_mapping() { }
char *read_file_mapping::read(int64_t s_offset, uint64_t length) { const char *read_file_mapping::read() {
return read(0, size());
}
const char *read_file_mapping::read(int64_t s_offset, uint64_t length) {
auto offset = static_cast<uint64_t>(s_offset); auto offset = static_cast<uint64_t>(s_offset);
if (offset + length > file_size) if (offset + length > file_size)
throw InternalError("Attempted to map beyond end of file", nullptr); throw InternalError("Attempted to map beyond end of file", nullptr);

View file

@ -43,6 +43,7 @@ namespace agi {
~read_file_mapping(); ~read_file_mapping();
uint64_t size() const { return file_size; } uint64_t size() const { return file_size; }
char *read(int64_t offset, uint64_t length); const char *read(int64_t offset, uint64_t length);
const char *read(); // Map the entire file
}; };
} }

View file

@ -50,7 +50,7 @@ AssAttachment::AssAttachment(agi::fs::path const& name, AssEntryGroup group)
filename = filename.get().substr(0, filename.get().size() - 4) + "_0" + filename.get().substr(filename.get().size() - 4); filename = filename.get().substr(0, filename.get().size() - 4) + "_0" + filename.get().substr(filename.get().size() - 4);
agi::read_file_mapping file(name); agi::read_file_mapping file(name);
auto buff = file.read(0, file.size()); auto buff = file.read();
entry_data = (group == AssEntryGroup::FONT ? "fontname: " : "filename: ") + filename.get() + "\r\n"; entry_data = (group == AssEntryGroup::FONT ? "fontname: " : "filename: ") + filename.get() + "\r\n";
entry_data = entry_data.get() + agi::ass::UUEncode(boost::make_iterator_range(buff, buff + file.size())); entry_data = entry_data.get() + agi::ass::UUEncode(boost::make_iterator_range(buff, buff + file.size()));
} }

View file

@ -58,7 +58,7 @@ protected:
float_samples = false; float_samples = false;
} }
char *EnsureRangeAccessible(int64_t start, int64_t length) const { const char *EnsureRangeAccessible(int64_t start, int64_t length) const {
try { try {
return file->read(start, static_cast<size_t>(length)); return file->read(start, static_cast<size_t>(length));
} }
@ -67,12 +67,16 @@ protected:
} }
} }
template<typename T>
T const& Read(int64_t start) const {
return *reinterpret_cast<const T *>(EnsureRangeAccessible(start, sizeof(T)));
}
struct IndexPoint { struct IndexPoint {
int64_t start_byte; int64_t start_byte;
int64_t num_samples; int64_t num_samples;
}; };
std::vector<IndexPoint> index_points; std::vector<IndexPoint> index_points;
}; };
void PCMAudioProvider::FillBuffer(void *buf, int64_t start, int64_t count) const { void PCMAudioProvider::FillBuffer(void *buf, int64_t start, int64_t count) const {
@ -159,8 +163,7 @@ public:
this->filename = filename; this->filename = filename;
// Read header // Read header
void *filestart = EnsureRangeAccessible(0, sizeof(RIFFChunk)); auto const& header = Read<RIFFChunk>(0);
RIFFChunk &header = *(RIFFChunk*)filestart;
// Check magic values // Check magic values
if (!CheckFourcc(header.ch.type, "RIFF")) if (!CheckFourcc(header.ch.type, "RIFF"))
@ -182,7 +185,7 @@ public:
// Continue reading chunks until out of data // Continue reading chunks until out of data
while (data_left) { while (data_left) {
ChunkHeader &ch = *(ChunkHeader*)EnsureRangeAccessible(filepos, sizeof(ChunkHeader)); auto const& ch = Read<ChunkHeader>(filepos);
// Update counters // Update counters
data_left -= sizeof(ch); data_left -= sizeof(ch);
@ -192,7 +195,7 @@ public:
if (got_fmt_header) throw agi::AudioProviderOpenError("Invalid file, multiple 'fmt ' chunks", nullptr); if (got_fmt_header) throw agi::AudioProviderOpenError("Invalid file, multiple 'fmt ' chunks", nullptr);
got_fmt_header = true; got_fmt_header = true;
fmtChunk &fmt = *(fmtChunk*)EnsureRangeAccessible(filepos, sizeof(fmtChunk)); auto const& fmt = Read<fmtChunk>(filepos);
if (fmt.compression != 1) if (fmt.compression != 1)
throw agi::AudioProviderOpenError("Can't use file, not PCM encoding", nullptr); throw agi::AudioProviderOpenError("Can't use file, not PCM encoding", nullptr);
@ -297,8 +300,7 @@ public:
throw agi::AudioDataNotFoundError("File is too small to be a Wave64 file", nullptr); throw agi::AudioDataNotFoundError("File is too small to be a Wave64 file", nullptr);
// Read header // Read header
void *filestart = EnsureRangeAccessible(0, sizeof(RiffChunk)); auto const& header = Read<RiffChunk>(0);
RiffChunk &header = *(RiffChunk*)filestart;
// Check magic values // Check magic values
if (!CheckGuid(header.riff_guid, w64GuidRIFF)) if (!CheckGuid(header.riff_guid, w64GuidRIFF))
@ -320,13 +322,13 @@ public:
// Continue reading chunks until out of data // Continue reading chunks until out of data
while (data_left) { while (data_left) {
uint8_t *chunk_guid = (uint8_t*)EnsureRangeAccessible(filepos, 16); uint8_t *chunk_guid = (uint8_t*)EnsureRangeAccessible(filepos, 16);
uint64_t chunk_size = *(uint64_t*)EnsureRangeAccessible(filepos+16, sizeof(uint64_t)); auto chunk_size = Read<uint64_t>(filepos + 16);
if (CheckGuid(chunk_guid, w64Guidfmt)) { if (CheckGuid(chunk_guid, w64Guidfmt)) {
if (got_fmt_header) if (got_fmt_header)
throw agi::AudioProviderOpenError("Bad file, found more than one 'fmt' chunk", nullptr); throw agi::AudioProviderOpenError("Bad file, found more than one 'fmt' chunk", nullptr);
FormatChunk &fmt = *(FormatChunk*)EnsureRangeAccessible(filepos, sizeof(FormatChunk)); auto const& fmt = Read<FormatChunk>(filepos);
got_fmt_header = true; got_fmt_header = true;
if (fmt.format.wFormatTag == 3) if (fmt.format.wFormatTag == 3)

View file

@ -38,7 +38,7 @@ namespace Automation4 {
} }
agi::read_file_mapping file(filename); agi::read_file_mapping file(filename);
char *buff = file.read(0, file.size()); auto buff = file.read();
size_t size = file.size(); size_t size = file.size();
// Discard the BOM if present // Discard the BOM if present

View file

@ -28,7 +28,7 @@
TextFileReader::TextFileReader(agi::fs::path const& filename, std::string encoding, bool trim) TextFileReader::TextFileReader(agi::fs::path const& filename, std::string encoding, bool trim)
: file(agi::util::make_unique<agi::read_file_mapping>(filename)) : file(agi::util::make_unique<agi::read_file_mapping>(filename))
, stream(agi::util::make_unique<boost::interprocess::bufferstream>(file->read(0, file->size()), file->size())) , stream(agi::util::make_unique<boost::interprocess::ibufferstream>(file->read(), file->size()))
, trim(trim) , trim(trim)
, iter(agi::line_iterator<std::string>(*stream, encoding)) , iter(agi::line_iterator<std::string>(*stream, encoding))
{ {

View file

@ -113,7 +113,7 @@ std::vector<std::string> YUV4MPEGVideoProvider::ReadHeader(uint64_t &pos) {
auto buff = file->read(pos, len); auto buff = file->read(pos, len);
// read header until terminating newline (0x0A) is found // read header until terminating newline (0x0A) is found
const char *curtag = buff; auto curtag = buff;
auto end = buff + len; auto end = buff + len;
for (; buff < end && *buff != 0x0A; ++buff, ++pos) { for (; buff < end && *buff != 0x0A; ++buff, ++pos) {
if (*buff == 0) if (*buff == 0)