diff options
author | crupest <crupest@outlook.com> | 2023-10-05 20:56:07 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2023-10-05 20:56:07 +0800 |
commit | 6cd1d68baf219b5d3c742ba5fb3a9ee04e830999 (patch) | |
tree | 523c97530f235139ef89f9137997ca4b70b32237 /src | |
parent | b09a5c7cea5b4eb32b3318c97b046f018b297d8c (diff) | |
download | cru-6cd1d68baf219b5d3c742ba5fb3a9ee04e830999.tar.gz cru-6cd1d68baf219b5d3c742ba5fb3a9ee04e830999.tar.bz2 cru-6cd1d68baf219b5d3c742ba5fb3a9ee04e830999.zip |
...
Diffstat (limited to 'src')
-rw-r--r-- | src/common/Base.cpp | 2 | ||||
-rw-r--r-- | src/common/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/common/io/CFileStream.cpp | 25 | ||||
-rw-r--r-- | src/common/io/FileNotExistException.cpp | 12 | ||||
-rw-r--r-- | src/common/io/OpenFileFlag.cpp | 19 |
5 files changed, 55 insertions, 4 deletions
diff --git a/src/common/Base.cpp b/src/common/Base.cpp index b6d39fef..ba4077b4 100644 --- a/src/common/Base.cpp +++ b/src/common/Base.cpp @@ -1,6 +1,6 @@ #include "cru/common/Base.h" -#include <stdexcept> +#include <exception> namespace cru { void UnreachableCode() { std::terminate(); } diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 2c7f28e0..6541f156 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -7,6 +7,7 @@ add_library(CruBase StringToNumberConverter.cpp StringUtil.cpp io/CFileStream.cpp + io/FileNotExistException.cpp io/Stream.cpp io/Resource.cpp io/MemoryStream.cpp 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 <utility> + +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 |