33 Open(iFileName, iMode);
61 const char *
Mode =
nullptr;
71 ASSERT(!
"Unhandled file mode");
76 m_File = _fsopen((iFileName).c_str(),
Mode, _SH_DENYWR);
89 m_File = _fsopen((iFileName).c_str(),
"wb+", _SH_DENYWR);
91 m_File = fopen((iFileName).c_str(),
"wb+");
95 return (
m_File !=
nullptr);
120 return (
m_File !=
nullptr);
137 return (feof(
m_File) != 0);
153 return static_cast<int>(fread(a_Buffer, 1, a_NumBytes,
m_File));
170 res.resize(a_NumBytes);
171 auto newSize = fread(res.data(),
sizeof(std::byte), a_NumBytes,
m_File);
189 int res =
static_cast<int>(fwrite(a_Buffer, 1, a_NumBytes,
m_File));
206 if (fseek(
m_File, iPosition, SEEK_SET) != 0)
242 long CurPos =
Tell();
247 if (fseek(
m_File, 0, SEEK_END) != 0)
252 if (fseek(
m_File,
static_cast<long>(CurPos), SEEK_SET) != 0)
278 long Position =
Tell();
284 auto DataSize =
static_cast<size_t>(TotalSize - Position);
286 a_Contents.resize(DataSize);
287 return Read(a_Contents.data(), DataSize);
323 return (RemoveDirectoryA(a_FolderName.c_str()) != 0);
325 return (rmdir(a_FolderName.c_str()) == 0);
336 for (
const auto & item: Contents)
370 return (remove(a_FileName.c_str()) == 0);
379 return (rename(a_OrigFileName.c_str(), a_NewFileName.c_str()) == 0);
389 return (CopyFileA(a_SrcFileName.c_str(), a_DstFileName.c_str(), FALSE) != 0);
392 std::ifstream src(a_SrcFileName.c_str(), std::ios::binary);
393 std::ofstream dst(a_DstFileName.c_str(), std::ios::binary);
413 DWORD FileAttrib = GetFileAttributesA(a_Path.c_str());
414 return ((FileAttrib != INVALID_FILE_ATTRIBUTES) && ((FileAttrib & FILE_ATTRIBUTE_DIRECTORY) != 0));
417 return ((stat(a_Path.c_str(), &st) == 0) && S_ISDIR(st.st_mode));
428 DWORD FileAttrib = GetFileAttributesA(a_Path.c_str());
429 return ((FileAttrib != INVALID_FILE_ATTRIBUTES) && ((FileAttrib & (FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_DEVICE)) == 0));
432 return ((stat(a_Path.c_str(), &st) == 0) && S_ISREG(st.st_mode));
443 if (stat(a_FileName.c_str(), &st) == 0)
445 return static_cast<int>(st.st_size);
457 return (CreateDirectoryA(a_FolderPath.c_str(),
nullptr) != 0);
459 return (mkdir(a_FolderPath.c_str(), S_IRWXU | S_IRWXG | S_IRWXO) == 0);
470 if (a_FolderPath.empty())
476 auto len = a_FolderPath.length();
477 for (decltype(len) i = 0; i < len; i++)
480 if ((a_FolderPath[i] ==
'\\') || (a_FolderPath[i] ==
'/'))
482 if (a_FolderPath[i] ==
'/')
507 !FileFilter.empty() &&
508 (FileFilter[FileFilter.length() - 1] !=
'\\') &&
509 (FileFilter[FileFilter.length() - 1] !=
'/')
512 FileFilter.push_back(
'\\');
516 FileFilter.append(
"*.*");
518 WIN32_FIND_DATAA FindFileData;
519 if ((hFind = FindFirstFileA(FileFilter.c_str(), &FindFileData)) != INVALID_HANDLE_VALUE)
524 (strcmp(FindFileData.cFileName,
".") == 0) ||
525 (strcmp(FindFileData.cFileName,
"..") == 0)
530 AllFiles.push_back(FindFileData.cFileName);
531 }
while (FindNextFileA(hFind, &FindFileData));
543 if ((dp = opendir(Folder.c_str())) ==
nullptr)
545 LOGERROR(
"Error (%i) opening directory \"%s\"\n", errno, Folder.c_str());
550 while ((dirp = readdir(dp)) !=
nullptr)
553 (strcmp(dirp->d_name,
".") == 0) ||
554 (strcmp(dirp->d_name,
"..") == 0)
559 AllFiles.push_back(dirp->d_name);
591 auto res = a_FileName;
594 #if defined(_MSC_VER)
596 auto LastPathSep = res.find_last_of(
"/\\");
597 #elif defined(_WIN32)
599 auto LastPathSep = res.rfind(
'\\');
602 auto LastPathSep = res.rfind(
'/');
604 if ((LastPathSep != AString::npos) && (LastPathSep + 1 == res.size()))
610 auto DotPos = res.rfind(
'.');
612 (DotPos == AString::npos) ||
613 ((LastPathSep != AString::npos) && (LastPathSep > DotPos))
617 if (!a_NewExt.empty() && (a_NewExt[0] !=
'.'))
622 res.append(a_NewExt);
627 if (!a_NewExt.empty() && (a_NewExt[0] !=
'.'))
630 res.erase(DotPos + 1, AString::npos);
634 res.erase(DotPos, AString::npos);
636 res.append(a_NewExt);
648 if (stat(a_FileName.c_str(), &st) < 0)
654 return static_cast<unsigned>(st.st_mtime);
655 #elif defined(ANDROID)
657 auto Time =
static_cast<time_t
>(st.st_mtime);
658 return static_cast<unsigned>(mktime(localtime(&Time)));
661 return static_cast<unsigned>(mktime(localtime(&st.st_mtime)));
704 template <
class StreamType>
708 FileStream::exceptions(FileStream::failbit | FileStream::badbit);
711 FileStream::open(Path);
714 FileStream::exceptions(FileStream::badbit);
721 template <
class StreamType>
725 FileStream::exceptions(FileStream::failbit | FileStream::badbit);
728 FileStream::open(Path,
Mode);
731 FileStream::exceptions(FileStream::badbit);
739 #pragma clang diagnostic push
740 #pragma clang diagnostic ignored "-Wweak-template-vtables"
748 #pragma clang diagnostic pop
std::basic_string< std::byte > ContiguousByteBuffer
void LOGERROR(std::string_view a_Format, const Args &... args)
std::vector< AString > AStringVector
static bool CreateFolderRecursive(const AString &a_FolderPath)
Creates a new folder with the specified name, creating its parents if needed.
static AString GetExecutableExt()
Returns the customary executable extension used by the current platform.
static bool IsFolder(const AString &a_Path)
Returns true if the specified path is a folder.
int Read(void *a_Buffer, size_t a_NumBytes)
Reads up to a_NumBytes bytes into a_Buffer, returns the number of bytes actually read,...
static unsigned GetLastModificationTime(const AString &a_FileName)
Returns the last modification time (in current timezone) of the specified file.
long Seek(int iPosition)
Seeks to iPosition bytes from file start, returns old position or -1 for failure; asserts if not open...
static bool DeleteFile(const AString &a_FileName)
Deletes a file, returns true if successful.
static AString ChangeFileExt(const AString &a_FileName, const AString &a_NewExt)
Returns a_FileName with its extension changed to a_NewExt.
static bool Delete(const AString &a_Path)
Deletes a file or a folder, returns true if successful.
void Flush()
Flushes all the bufferef output into the file (only when writing)
static bool CreateFolder(const AString &a_FolderPath)
Creates a new folder with the specified name.
static bool Copy(const AString &a_SrcFileName, const AString &a_DstFileName)
Copies a file, returns true if successful.
static AString GetPathSeparator()
Returns the path separator used by the current platform.
static bool Rename(const AString &a_OrigPath, const AString &a_NewPath)
Renames a file or folder, returns true if successful.
eMode
The mode in which to open the file.
cFile(void)
Simple constructor - creates an unopened file object, use Open() to open / create a real file.
static bool Exists(const AString &a_FileName)
Returns true if the file specified exists.
bool Open(const AString &iFileName, eMode iMode)
static AString ReadWholeFile(const AString &a_FileName)
Returns the entire contents of the specified file as a string.
static AStringVector GetFolderContents(const AString &a_Folder)
Returns the list of all items in the specified folder (files, folders, nix pipes, whatever's there).
~cFile()
Auto-closes the file, if open.
long GetSize(void) const
Returns the size of file, in bytes, or -1 for failure; asserts if not open.
static bool DeleteFolderContents(const AString &a_FolderName)
Deletes all content from the specified folder.
int ReadRestOfFile(AString &a_Contents)
Reads the file from current position till EOF into an AString; returns the number of bytes read or -1...
long Tell(void) const
Returns the current position (bytes from file start) or -1 for failure; asserts if not open.
int Write(const void *a_Buffer, size_t a_NumBytes)
Writes up to a_NumBytes bytes from a_Buffer, returns the number of bytes actually written,...
static bool IsFile(const AString &a_Path)
Returns true if the specified path is a regular file.
static bool DeleteFolder(const AString &a_FolderName)
Deletes a folder, returns true if successful.
A wrapper for file streams that enables exceptions.
FileStream(const std::string &Path)