mirror of
https://github.com/OpenTTD/OpenTTD
synced 2026-01-19 18:32:35 +01:00
Codechange: Store start and end position in RandomAccessFile.
This allows callers to do more bounds checking.
This commit is contained in:
committed by
Peter Nelson
parent
b5264a72ae
commit
719763dfcb
@@ -24,13 +24,18 @@
|
||||
*/
|
||||
RandomAccessFile::RandomAccessFile(const std::string &filename, Subdirectory subdir) : filename(filename)
|
||||
{
|
||||
this->file_handle = FioFOpenFile(filename, "rb", subdir);
|
||||
size_t file_size;
|
||||
this->file_handle = FioFOpenFile(filename, "rb", subdir, &file_size);
|
||||
if (this->file_handle == nullptr) UserError("Cannot open file '{}'", filename);
|
||||
|
||||
/* When files are in a tar-file, the begin of the file might not be at 0. */
|
||||
long pos = ftell(this->file_handle);
|
||||
if (pos < 0) UserError("Cannot read file '{}'", filename);
|
||||
|
||||
/* Make a note of start and end position for readers who check bounds. */
|
||||
this->start_pos = pos;
|
||||
this->end_pos = this->start_pos + file_size;
|
||||
|
||||
/* Store the filename without path and extension */
|
||||
auto t = filename.rfind(PATHSEPCHAR);
|
||||
std::string name_without_path = filename.substr(t != std::string::npos ? t + 1 : 0);
|
||||
@@ -76,6 +81,15 @@ size_t RandomAccessFile::GetPos() const
|
||||
return this->pos + (this->buffer - this->buffer_end);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if we have reached the end of the file.
|
||||
* @return True iff the current position as at or after the end of the file.
|
||||
*/
|
||||
bool RandomAccessFile::AtEndOfFile() const
|
||||
{
|
||||
return this->GetPos() >= this->GetEndPos();
|
||||
}
|
||||
|
||||
/**
|
||||
* Seek in the current file.
|
||||
* @param pos New position.
|
||||
|
||||
Reference in New Issue
Block a user