aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2023-10-03 23:52:51 +0800
committercrupest <crupest@outlook.com>2023-10-04 00:13:26 +0800
commitb09a5c7cea5b4eb32b3318c97b046f018b297d8c (patch)
tree46e21a6536fd0dcbca1bb0e3ec3a55cc17c110e6
parent21985bda6c2631c1b78d3a0e9987f67dffca5bad (diff)
downloadcru-b09a5c7cea5b4eb32b3318c97b046f018b297d8c.tar.gz
cru-b09a5c7cea5b4eb32b3318c97b046f018b297d8c.tar.bz2
cru-b09a5c7cea5b4eb32b3318c97b046f018b297d8c.zip
...
-rw-r--r--include/cru/common/io/CFileStream.h3
-rw-r--r--include/cru/common/io/FileStream.h5
-rw-r--r--include/cru/common/io/OpenFileFlag.h27
-rw-r--r--src/common/io/CFileStream.cpp23
4 files changed, 58 insertions, 0 deletions
diff --git a/include/cru/common/io/CFileStream.h b/include/cru/common/io/CFileStream.h
index 65de2ac7..5d464c9b 100644
--- a/include/cru/common/io/CFileStream.h
+++ b/include/cru/common/io/CFileStream.h
@@ -1,6 +1,7 @@
#pragma once
#include "Stream.h"
+#include "OpenFileFlag.h"
#include <cstdio>
@@ -11,6 +12,8 @@ class CRU_BASE_API CFileStream : public Stream {
explicit CFileStream(std::FILE* file, bool readable = true,
bool writable = true, bool auto_close = true);
+ CFileStream(String path, OpenFileFlag flags); // To make it a FileStream
+
CRU_DELETE_COPY(CFileStream)
CRU_DELETE_MOVE(CFileStream)
diff --git a/include/cru/common/io/FileStream.h b/include/cru/common/io/FileStream.h
index 9176739a..4eab612f 100644
--- a/include/cru/common/io/FileStream.h
+++ b/include/cru/common/io/FileStream.h
@@ -17,6 +17,11 @@
* (TODO: Currently the problem is that when I implemented for Windows and UNIX
* I didn't take this into consideration so I have to fix this inconsistency
* later.)
+ *
+ * The requirement of FileStream:
+ * 1. It must be derived from Stream, of course.
+ * 2. It must have a constructor FileStream(String path, io::OpenFlag flags), so
+ * user can construct one with the same interface.
*/
#pragma once
diff --git a/include/cru/common/io/OpenFileFlag.h b/include/cru/common/io/OpenFileFlag.h
index bca1d737..fd8b6e2c 100644
--- a/include/cru/common/io/OpenFileFlag.h
+++ b/include/cru/common/io/OpenFileFlag.h
@@ -9,11 +9,38 @@ struct OpenFileFlagTag {};
using OpenFileFlag = Bitmask<details::OpenFileFlagTag>;
struct OpenFileFlags {
+ /**
+ * \brief for reading
+ */
static constexpr OpenFileFlag Read{0x1};
+
+ /**
+ * \brief for writing
+ */
static constexpr OpenFileFlag Write{0x2};
+
+ /**
+ * \brief when writing, seek to end first
+ * Only effective for writing.
+ */
static constexpr OpenFileFlag Append{0x4};
+
+ /**
+ * \brief when writing, truncate the file to empty
+ * Only effective for writing.
+ * So the content is erased! Be careful!
+ */
static constexpr OpenFileFlag Truncate{0x8};
+
+ /**
+ * \brief when writing, if the file does not exist, create one
+ * Only effective for writing.
+ */
static constexpr OpenFileFlag Create{0x10};
+
+ /**
+ * TODO: ???
+ */
static constexpr OpenFileFlag Exclusive{0x20};
};
} // namespace cru::io
diff --git a/src/common/io/CFileStream.cpp b/src/common/io/CFileStream.cpp
index de195538..68fb137d 100644
--- a/src/common/io/CFileStream.cpp
+++ b/src/common/io/CFileStream.cpp
@@ -1,5 +1,6 @@
#include "cru/common/io/CFileStream.h"
#include "cru/common/Exception.h"
+#include "cru/common/io/OpenFileFlag.h"
#include "cru/common/io/Stream.h"
#include <cstdio>
@@ -44,6 +45,28 @@ CFileStream::CFileStream(std::FILE* file, bool readable, bool writable,
}
}
+namespace {
+std::string ConvertOpenFileFlagToCFileFlag(OpenFileFlag flags) {
+ 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
+ return "r";
+ }
+
+ // Now we need writing.
+ if (!need_read) {
+
+ }
+
+}
+} // namespace
+
+CFileStream::CFileStream(String path, OpenFileFlag flags) {}
+
CFileStream::~CFileStream() {
if (auto_close_ && file_ != nullptr) {
std::fclose(file_);