Skip to content

Commit

Permalink
Common: Prevent non_path IOFile usage.
Browse files Browse the repository at this point in the history
  • Loading branch information
unknownbrackets committed May 15, 2021
1 parent f2b2f26 commit e4dc8e6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 19 deletions.
17 changes: 5 additions & 12 deletions Common/File/FileUtil.cpp
Expand Up @@ -791,28 +791,21 @@ const Path &GetExeDirectory() {
return ExePath;
}

IOFile::IOFile()
: m_file(NULL), m_good(true)
{}
IOFile::IOFile() {}

IOFile::IOFile(std::FILE* file)
: m_file(file), m_good(true)
: m_file(file)
{}

IOFile::IOFile(const std::string& filename, const char openmode[])
: m_file(NULL), m_good(true)
{
IOFile::IOFile(const std::string &filename, const char openmode[]) {
Open(filename, openmode);
}

IOFile::IOFile(const Path &filename, const char openmode[])
: m_file(NULL), m_good(true)
{
IOFile::IOFile(const Path &filename, const char openmode[]) {
Open(filename.ToString(), openmode);
}

IOFile::~IOFile()
{
IOFile::~IOFile() {
Close();
}

Expand Down
15 changes: 8 additions & 7 deletions Common/File/FileUtil.h
Expand Up @@ -109,15 +109,13 @@ class IOFile {
public:
IOFile();
IOFile(FILE* file);
IOFile(const std::string& filename, const char openmode[]);
IOFile(const Path &filename, const char openmode[]);
~IOFile();

// Prevent copies.
IOFile(const IOFile &) = delete;
void operator=(const IOFile &) = delete;

bool Open(const std::string& filename, const char openmode[]);
bool Open(const Path &filename, const char openmode[]);
bool Close();

Expand Down Expand Up @@ -149,11 +147,11 @@ class IOFile {
return WriteArray(reinterpret_cast<const char*>(data), length);
}

bool IsOpen() { return NULL != m_file; }
bool IsOpen() const { return nullptr != m_file; }

// m_good is set to false when a read, write or other function fails
bool IsGood() { return m_good; }
operator void*() { return m_good ? m_file : NULL; }
bool IsGood() const { return m_good; }
operator bool() const { return IsGood() && IsOpen(); }

std::FILE* ReleaseHandle();

Expand All @@ -175,8 +173,11 @@ class IOFile {
}

private:
std::FILE *m_file;
bool m_good;
IOFile(const std::string &filename, const char openmode[]);
bool Open(const std::string &filename, const char openmode[]);

std::FILE *m_file = nullptr;
bool m_good = true;
};

// TODO: Refactor, this was moved from the old file_util.cpp.
Expand Down

0 comments on commit e4dc8e6

Please sign in to comment.