forked from mia/Aegisub
Return a const char * from read_file_mapping::read
This commit is contained in:
parent
23ff6dead1
commit
0268ffd345
7 changed files with 23 additions and 16 deletions
|
@ -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<uint64_t>(s_offset);
|
||||
if (offset + length > file_size)
|
||||
throw InternalError("Attempted to map beyond end of file", nullptr);
|
||||
|
|
|
@ -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
|
||||
};
|
||||
}
|
||||
|
|
|
@ -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()));
|
||||
}
|
||||
|
|
|
@ -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<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 {
|
||||
int64_t start_byte;
|
||||
int64_t num_samples;
|
||||
};
|
||||
std::vector<IndexPoint> 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<RIFFChunk>(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<ChunkHeader>(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<fmtChunk>(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<RiffChunk>(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<uint64_t>(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<FormatChunk>(filepos);
|
||||
got_fmt_header = true;
|
||||
|
||||
if (fmt.format.wFormatTag == 3)
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -28,7 +28,7 @@
|
|||
|
||||
TextFileReader::TextFileReader(agi::fs::path const& filename, std::string encoding, bool trim)
|
||||
: 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)
|
||||
, iter(agi::line_iterator<std::string>(*stream, encoding))
|
||||
{
|
||||
|
|
|
@ -113,7 +113,7 @@ std::vector<std::string> 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)
|
||||
|
|
Loading…
Reference in a new issue