mirror of
https://github.com/luanti-org/luanti.git
synced 2025-08-11 17:51:04 +00:00
Introduce proper error handling for file streams
This commit is contained in:
parent
c38e0d05bf
commit
39fd9b93c3
23 changed files with 235 additions and 149 deletions
|
@ -23,6 +23,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
|
|||
#include <string>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
#include <fstream>
|
||||
|
||||
#ifdef _WIN32
|
||||
#define DIR_DELIM "\\"
|
||||
|
@ -148,14 +149,74 @@ std::string AbsolutePath(const std::string &path);
|
|||
// delimiter is found.
|
||||
const char *GetFilenameFromPath(const char *path);
|
||||
|
||||
// Replace the content of a file on disk in a way that is safe from
|
||||
// corruption/truncation on a crash.
|
||||
// logs and returns false on error
|
||||
bool safeWriteToFile(const std::string &path, std::string_view content);
|
||||
|
||||
#ifndef SERVER
|
||||
bool extractZipFile(irr::io::IFileSystem *fs, const char *filename, const std::string &destination);
|
||||
#endif
|
||||
|
||||
bool ReadFile(const std::string &path, std::string &out);
|
||||
bool ReadFile(const std::string &path, std::string &out, bool log_error = false);
|
||||
|
||||
bool Rename(const std::string &from, const std::string &to);
|
||||
|
||||
/**
|
||||
* Open a file buffer with error handling, commonly used with `std::fstream.rdbuf()`.
|
||||
*
|
||||
* @param stream stream references, must not already be open
|
||||
* @param filename filename to open
|
||||
* @param mode mode bits (used as-is)
|
||||
* @param log_error log failure to errorstream?
|
||||
* @param log_warn log failure to warningstream?
|
||||
* @return true if success
|
||||
*/
|
||||
bool OpenStream(std::filebuf &stream, const char *filename,
|
||||
std::ios::openmode mode, bool log_error, bool log_warn);
|
||||
|
||||
} // namespace fs
|
||||
|
||||
// outside of namespace for convenience:
|
||||
|
||||
/**
|
||||
* Helper function to open an output file stream (= writing).
|
||||
*
|
||||
* For compatibility with fopen() binary mode and truncate are always set.
|
||||
* Use `fs::OpenStream` for more control.
|
||||
* @param name file name
|
||||
* @param log if true, failure to open the file will be logged to errorstream
|
||||
* @param mode additional mode bits (e.g. std::ios::app)
|
||||
* @return file stream, will be !good in case of error
|
||||
*/
|
||||
inline std::ofstream open_ofstream(const char *name, bool log,
|
||||
std::ios::openmode mode = std::ios::openmode())
|
||||
{
|
||||
mode |= std::ios::out | std::ios::binary;
|
||||
if (!(mode & std::ios::app))
|
||||
mode |= std::ios::trunc;
|
||||
std::ofstream ofs;
|
||||
if (!fs::OpenStream(*ofs.rdbuf(), name, mode, log, false))
|
||||
ofs.setstate(std::ios::failbit);
|
||||
return ofs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to open an input file stream (= reading).
|
||||
*
|
||||
* For compatibility with fopen() binary mode is always set.
|
||||
* Use `fs::OpenStream` for more control.
|
||||
* @param name file name
|
||||
* @param log if true, failure to open the file will be logged to errorstream
|
||||
* @param mode additional mode bits (e.g. std::ios::ate)
|
||||
* @return file stream, will be !good in case of error
|
||||
*/
|
||||
inline std::ifstream open_ifstream(const char *name, bool log,
|
||||
std::ios::openmode mode = std::ios::openmode())
|
||||
{
|
||||
mode |= std::ios::in | std::ios::binary;
|
||||
std::ifstream ifs;
|
||||
if (!fs::OpenStream(*ifs.rdbuf(), name, mode, log, false))
|
||||
ifs.setstate(std::ios::failbit);
|
||||
return ifs;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue