27 : filename(
std::move(filename_))
30 if (
auto pos = filename.find_last_of(
'/'); pos != std::string::npos) {
59 "File \"", filename,
"\" not found");
62 "Error opening file \"", filename,
"\": ",
70 : filename(
std::move(filename_))
72 assert(strchr(mode,
'b'));
93 if (fread(buffer.data(), 1, buffer.size(), file.get()) != buffer.size()) {
94 if (ferror(file.get())) {
97 if (feof(file.get())) {
105 if (fwrite(buffer.data(), 1, buffer.size(), file.get()) != buffer.size()) {
106 if (ferror(file.get())) {
116 if (size == 0)
return {
static_cast<uint8_t*
>(
nullptr), size};
119 int fd = _fileno(file.get());
121 throw FileException(
"_fileno failed");
123 auto hFile =
reinterpret_cast<HANDLE
>(_get_osfhandle(fd));
124 if (hFile == INVALID_HANDLE_VALUE) {
125 throw FileException(
"_get_osfhandle failed");
128 hMmap = CreateFileMapping(hFile,
nullptr, PAGE_WRITECOPY, 0, 0,
nullptr);
131 "CreateFileMapping failed: ", GetLastError());
133 mmem =
static_cast<uint8_t*
>(MapViewOfFile(hMmap, FILE_MAP_COPY, 0, 0, 0));
135 DWORD gle = GetLastError();
138 throw FileException(
"MapViewOfFile failed: ", gle);
154 if (!UnmapViewOfFile(mmem)) {
155 std::cerr <<
"UnmapViewOfFile failed: "
171 if (size == 0)
return {
static_cast<uint8_t*
>(
nullptr), size};
174 mmem =
static_cast<uint8_t*
>(
175 ::mmap(
nullptr, size, PROT_READ | PROT_WRITE,
176 MAP_PRIVATE, fileno(file.get()), 0));
179 auto* MY_MAP_FAILED = std::bit_cast<void*>(intptr_t(-1));
180 if (mmem == MY_MAP_FAILED) {
181 throw FileException(
"Error mmapping file");
192 }
catch (FileException&) {
207 int ret = _fstat64(fileno(file.get()), &st);
210 int ret = fstat(fileno(file.get()), &st);
211 if (ret && (errno == EOVERFLOW)) {
215 "32-bit platforms: ",
getURL());
227 int ret = _fseeki64(file.get(), pos, SEEK_SET);
229 int ret = fseek(file.get(), narrow_cast<long>(pos), SEEK_SET);
238 return ftell(file.get());
244 int fd = fileno(file.get());
245 if (ftruncate(fd, narrow_cast<off_t>(size))) {
277 int ret = _fstat64(fileno(file.get()), &st);
280 int ret = fstat(fileno(file.get()), &st);
virtual std::span< const uint8_t > mmap()
virtual void truncate(size_t size)
time_t getModificationDate() override
void write(std::span< const uint8_t > buffer) override
void read(std::span< uint8_t > buffer) override
void seek(size_t pos) override
bool isReadOnly() const override
LocalFile(std::string filename, File::OpenMode mode)
std::string getLocalReference() override
const std::string & getURL() const override
size_t getSize() override
const std::string & getNativePath(const std::string &path)
Returns the path in native path-delimiter.
FILE_t openFile(zstring_view filename, zstring_view mode)
Call fopen() in a platform-independent manner.
void mkdirp(string path)
Acts like the unix command "mkdir -p".
This file implemented 3 utility functions:
size_t size(std::string_view utf8)