MCPcopy Create free account
hub / github.com/OpenTTD/OpenTTD / RandomAccessFile

Method RandomAccessFile

src/random_access_file.cpp:25–46  ·  view source on GitHub ↗

* Create the RandomAccesFile. * @param filename Name of the file at the disk. * @param subdir The sub directory to search this file in. */

Source from the content-addressed store, hash-verified

23 * @param subdir The sub directory to search this file in.
24 */
25RandomAccessFile::RandomAccessFile(std::string_view filename, Subdirectory subdir) : filename(filename)
26{
27 size_t file_size;
28 this->file_handle = FioFOpenFile(filename, "rb", subdir, &file_size);
29 if (!this->file_handle.has_value()) UserError("Cannot open file '{}'", filename);
30
31 /* When files are in a tar-file, the begin of the file might not be at 0. */
32 long pos = ftell(*this->file_handle);
33 if (pos < 0) UserError("Cannot read file '{}'", filename);
34
35 /* Make a note of start and end position for readers who check bounds. */
36 this->start_pos = pos;
37 this->end_pos = this->start_pos + file_size;
38
39 /* Store the filename without path and extension */
40 auto t = filename.rfind(PATHSEPCHAR);
41 std::string name_without_path{filename.substr(t != std::string::npos ? t + 1 : 0)};
42 this->simplified_filename = name_without_path.substr(0, name_without_path.rfind('.'));
43 strtolower(this->simplified_filename);
44
45 this->SeekTo(static_cast<size_t>(pos), SEEK_SET);
46}
47
48/**
49 * Get the filename of the opened file with the path from the SubDirectory and the extension.

Callers

nothing calls this directly

Calls 4

SeekToMethod · 0.95
FioFOpenFileFunction · 0.85
strtolowerFunction · 0.85
substrMethod · 0.80

Tested by

no test coverage detected