From 0268ffd3452c6ef48d04fae3220d5d0f0f7c8e13 Mon Sep 17 00:00:00 2001 From: Thomas Goyne Date: Fri, 21 Mar 2014 11:08:26 -0700 Subject: [PATCH] Return a const char * from read_file_mapping::read --- libaegisub/common/file_mapping.cpp | 6 +++++- libaegisub/include/libaegisub/file_mapping.h | 3 ++- src/ass_attachment.cpp | 2 +- src/audio_provider_pcm.cpp | 22 +++++++++++--------- src/auto4_lua_scriptreader.cpp | 2 +- src/text_file_reader.cpp | 2 +- src/video_provider_yuv4mpeg.cpp | 2 +- 7 files changed, 23 insertions(+), 16 deletions(-) diff --git a/libaegisub/common/file_mapping.cpp b/libaegisub/common/file_mapping.cpp index aeec3a081..99803347d 100644 --- a/libaegisub/common/file_mapping.cpp +++ b/libaegisub/common/file_mapping.cpp @@ -79,7 +79,11 @@ read_file_mapping::read_file_mapping(fs::path const& filename) 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(s_offset); if (offset + length > file_size) throw InternalError("Attempted to map beyond end of file", nullptr); diff --git a/libaegisub/include/libaegisub/file_mapping.h b/libaegisub/include/libaegisub/file_mapping.h index 2bff376dc..ce531404e 100644 --- a/libaegisub/include/libaegisub/file_mapping.h +++ b/libaegisub/include/libaegisub/file_mapping.h @@ -43,6 +43,7 @@ namespace agi { ~read_file_mapping(); 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 }; } diff --git a/src/ass_attachment.cpp b/src/ass_attachment.cpp index 10e0a5caa..f5fba865b 100644 --- a/src/ass_attachment.cpp +++ b/src/ass_attachment.cpp @@ -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); 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 = entry_data.get() + agi::ass::UUEncode(boost::make_iterator_range(buff, buff + file.size())); } diff --git a/src/audio_provider_pcm.cpp b/src/audio_provider_pcm.cpp index 841dc8e84..15cc664ef 100644 --- a/src/audio_provider_pcm.cpp +++ b/src/audio_provider_pcm.cpp @@ -58,7 +58,7 @@ protected: float_samples = false; } - char *EnsureRangeAccessible(int64_t start, int64_t length) const { + const char *EnsureRangeAccessible(int64_t start, int64_t length) const { try { return file->read(start, static_cast(length)); } @@ -67,12 +67,16 @@ protected: } } + template + T const& Read(int64_t start) const { + return *reinterpret_cast(EnsureRangeAccessible(start, sizeof(T))); + } + struct IndexPoint { int64_t start_byte; int64_t num_samples; }; std::vector index_points; - }; void PCMAudioProvider::FillBuffer(void *buf, int64_t start, int64_t count) const { @@ -159,8 +163,7 @@ public: this->filename = filename; // Read header - void *filestart = EnsureRangeAccessible(0, sizeof(RIFFChunk)); - RIFFChunk &header = *(RIFFChunk*)filestart; + auto const& header = Read(0); // Check magic values if (!CheckFourcc(header.ch.type, "RIFF")) @@ -182,7 +185,7 @@ public: // Continue reading chunks until out of data while (data_left) { - ChunkHeader &ch = *(ChunkHeader*)EnsureRangeAccessible(filepos, sizeof(ChunkHeader)); + auto const& ch = Read(filepos); // Update counters data_left -= sizeof(ch); @@ -192,7 +195,7 @@ public: if (got_fmt_header) throw agi::AudioProviderOpenError("Invalid file, multiple 'fmt ' chunks", nullptr); got_fmt_header = true; - fmtChunk &fmt = *(fmtChunk*)EnsureRangeAccessible(filepos, sizeof(fmtChunk)); + auto const& fmt = Read(filepos); if (fmt.compression != 1) 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); // Read header - void *filestart = EnsureRangeAccessible(0, sizeof(RiffChunk)); - RiffChunk &header = *(RiffChunk*)filestart; + auto const& header = Read(0); // Check magic values if (!CheckGuid(header.riff_guid, w64GuidRIFF)) @@ -320,13 +322,13 @@ public: // Continue reading chunks until out of data while (data_left) { 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(filepos + 16); if (CheckGuid(chunk_guid, w64Guidfmt)) { if (got_fmt_header) throw agi::AudioProviderOpenError("Bad file, found more than one 'fmt' chunk", nullptr); - FormatChunk &fmt = *(FormatChunk*)EnsureRangeAccessible(filepos, sizeof(FormatChunk)); + auto const& fmt = Read(filepos); got_fmt_header = true; if (fmt.format.wFormatTag == 3) diff --git a/src/auto4_lua_scriptreader.cpp b/src/auto4_lua_scriptreader.cpp index 4ef2ea6ca..f979053aa 100644 --- a/src/auto4_lua_scriptreader.cpp +++ b/src/auto4_lua_scriptreader.cpp @@ -38,7 +38,7 @@ namespace Automation4 { } agi::read_file_mapping file(filename); - char *buff = file.read(0, file.size()); + auto buff = file.read(); size_t size = file.size(); // Discard the BOM if present diff --git a/src/text_file_reader.cpp b/src/text_file_reader.cpp index 72db609ac..a56a695b3 100644 --- a/src/text_file_reader.cpp +++ b/src/text_file_reader.cpp @@ -28,7 +28,7 @@ TextFileReader::TextFileReader(agi::fs::path const& filename, std::string encoding, bool trim) : file(agi::util::make_unique(filename)) -, stream(agi::util::make_unique(file->read(0, file->size()), file->size())) +, stream(agi::util::make_unique(file->read(), file->size())) , trim(trim) , iter(agi::line_iterator(*stream, encoding)) { diff --git a/src/video_provider_yuv4mpeg.cpp b/src/video_provider_yuv4mpeg.cpp index 32c97e339..527bf958f 100644 --- a/src/video_provider_yuv4mpeg.cpp +++ b/src/video_provider_yuv4mpeg.cpp @@ -113,7 +113,7 @@ std::vector YUV4MPEGVideoProvider::ReadHeader(uint64_t &pos) { auto buff = file->read(pos, len); // read header until terminating newline (0x0A) is found - const char *curtag = buff; + auto curtag = buff; auto end = buff + len; for (; buff < end && *buff != 0x0A; ++buff, ++pos) { if (*buff == 0)