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/common/io/CFileStream.cpp | |
parent | b09a5c7cea5b4eb32b3318c97b046f018b297d8c (diff) | |
download | cru-6cd1d68baf219b5d3c742ba5fb3a9ee04e830999.tar.gz cru-6cd1d68baf219b5d3c742ba5fb3a9ee04e830999.tar.bz2 cru-6cd1d68baf219b5d3c742ba5fb3a9ee04e830999.zip |
...
Diffstat (limited to 'src/common/io/CFileStream.cpp')
-rw-r--r-- | src/common/io/CFileStream.cpp | 25 |
1 files changed, 22 insertions, 3 deletions
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 |