From 6cd1d68baf219b5d3c742ba5fb3a9ee04e830999 Mon Sep 17 00:00:00 2001 From: crupest Date: Thu, 5 Oct 2023 20:56:07 +0800 Subject: ... --- src/common/io/CFileStream.cpp | 25 ++++++++++++++++++++++--- src/common/io/FileNotExistException.cpp | 12 ++++++++++++ src/common/io/OpenFileFlag.cpp | 19 +++++++++++++++++++ 3 files changed, 53 insertions(+), 3 deletions(-) create mode 100644 src/common/io/FileNotExistException.cpp create mode 100644 src/common/io/OpenFileFlag.cpp (limited to 'src/common/io') diff --git a/src/common/io/CFileStream.cpp b/src/common/io/CFileStream.cpp index 68fb137d..133cd8f6 100644 --- a/src/common/io/CFileStream.cpp +++ b/src/common/io/CFileStream.cpp @@ -46,22 +46,41 @@ CFileStream::CFileStream(std::FILE* file, bool readable, bool writable, } namespace { -std::string ConvertOpenFileFlagToCFileFlag(OpenFileFlag flags) { +/** + * Generally the fopen will return a NULL ptr if the file does not exist. Then + * we must throw FileNotExistException. However, there are cases we have to + * check existence explicitly before. The case is OpenFileFlags::Write is + * specified but OpenFileFlags::Create is not, in which fopen usually create a + * new file automatically but we want a FileNotExistException to be thrown. + */ +std::string ConvertOpenFileFlagToCFileFlag(OpenFileFlag flags, + bool* explicit_check_exist) { + *explicit_check_exist = false; + std::string result; + bool need_read = flags & OpenFileFlags::Read; bool need_write = flags & OpenFileFlags::Write; - bool append = flags & OpenFileFlags::Append; if (!need_write) { // No need to write? The simplest + // Note even OpenFileFlags::Create is set, we still have to check exist. return "r"; } // Now we need writing. - if (!need_read) { + bool create = OpenFileFlags::Create; + + if (!create) { + *explicit_check_exist = true; } + bool append = flags & OpenFileFlags::Append; + + if (!need_read) { + return "w"; + } } } // namespace diff --git a/src/common/io/FileNotExistException.cpp b/src/common/io/FileNotExistException.cpp new file mode 100644 index 00000000..a2e1fdb1 --- /dev/null +++ b/src/common/io/FileNotExistException.cpp @@ -0,0 +1,12 @@ +#include "cru/common/io/FileNotExistException.h" + +#include "cru/common/Exception.h" +#include "cru/common/Format.h" + +#include + +namespace cru::io { + FileNotExistException::FileNotExistException(String path): Exception(), path_(std::move(path)) { + SetMessage(Format(u"File {} does not exist.", path_)); + } +} diff --git a/src/common/io/OpenFileFlag.cpp b/src/common/io/OpenFileFlag.cpp new file mode 100644 index 00000000..6b9957fe --- /dev/null +++ b/src/common/io/OpenFileFlag.cpp @@ -0,0 +1,19 @@ +#include "cru/common/io/OpenFileFlag.h" + +namespace cru::io { +bool CheckOpenFileFlag(OpenFileFlag flags) { + auto has = [flags](OpenFileFlag flag) { return flags & flag; }; + + if ((has(OpenFileFlags::Append) || has(OpenFileFlags::Truncate) || + has(OpenFileFlags::Create)) && + !has(OpenFileFlags::Write)) { + return false; + } + + if (has(OpenFileFlags::Truncate) && has(OpenFileFlags::Append)) { + return false; + } + + return true; +} +} // namespace cru::io -- cgit v1.2.3