aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2022-01-15 22:41:11 +0800
committercrupest <crupest@outlook.com>2022-01-15 22:41:11 +0800
commit570e95f4551385a81eb2cf6f2e386e3b0cb291a8 (patch)
tree8ed08ab29f49b260ac66e88a75aa436c4daefb57
parentabb9d48a72e2d580d6bfbfcb600795c5ec66e204 (diff)
downloadcru-570e95f4551385a81eb2cf6f2e386e3b0cb291a8.tar.gz
cru-570e95f4551385a81eb2cf6f2e386e3b0cb291a8.tar.bz2
cru-570e95f4551385a81eb2cf6f2e386e3b0cb291a8.zip
...
-rw-r--r--include/cru/common/Exception.hpp11
-rw-r--r--include/cru/common/io/Stream.hpp2
-rw-r--r--include/cru/common/io/UnixFileStream.hpp4
-rw-r--r--src/common/CMakeLists.txt1
-rw-r--r--src/common/ErrnoException.cpp3
-rw-r--r--src/common/Exception.cpp17
-rw-r--r--src/common/io/Stream.cpp8
-rw-r--r--src/common/io/UnixFileStream.cpp30
-rw-r--r--test/common/CMakeLists.txt6
-rw-r--r--test/common/io/UnixFileStreamTest.cpp32
10 files changed, 96 insertions, 18 deletions
diff --git a/include/cru/common/Exception.hpp b/include/cru/common/Exception.hpp
index 4e5d3a16..e8395178 100644
--- a/include/cru/common/Exception.hpp
+++ b/include/cru/common/Exception.hpp
@@ -2,21 +2,24 @@
#include "String.hpp"
namespace cru {
-class CRU_BASE_API Exception {
+class CRU_BASE_API Exception : public std::exception {
public:
- Exception() = default;
- explicit Exception(String message) : message_(std::move(message)) {}
+ Exception();
+ explicit Exception(String message);
CRU_DEFAULT_COPY(Exception)
CRU_DEFAULT_MOVE(Exception)
- virtual ~Exception() = default;
+ ~Exception() override;
public:
String GetMessage() const { return message_; }
+ const char* what() const noexcept override;
+
private:
String message_;
+ mutable std::string utf8_message_;
};
class CRU_BASE_API TextEncodeException : public Exception {
diff --git a/include/cru/common/io/Stream.hpp b/include/cru/common/io/Stream.hpp
index d75f3ee3..fedd4a0f 100644
--- a/include/cru/common/io/Stream.hpp
+++ b/include/cru/common/io/Stream.hpp
@@ -30,6 +30,8 @@ class CRU_BASE_API Stream : public Object {
virtual bool CanWrite() = 0;
virtual Index Write(const std::byte* buffer, Index offset, Index size) = 0;
virtual Index Write(const std::byte* buffer, Index size);
+ Index Write(const char* buffer, Index offset, Index size);
+ Index Write(const char* buffer, Index size);
virtual void Flush();
diff --git a/include/cru/common/io/UnixFileStream.hpp b/include/cru/common/io/UnixFileStream.hpp
index 8bd3829a..633b9bd1 100644
--- a/include/cru/common/io/UnixFileStream.hpp
+++ b/include/cru/common/io/UnixFileStream.hpp
@@ -25,9 +25,11 @@ class UnixFileStream : public Stream {
bool CanRead() override;
Index Read(std::byte* buffer, Index offset, Index size) override;
+ using Stream::Read;
bool CanWrite() override;
Index Write(const std::byte* buffer, Index offset, Index size) override;
+ using Stream::Write;
void Close() override;
@@ -36,7 +38,7 @@ class UnixFileStream : public Stream {
private:
String path_;
- OpenFileFlag flags;
+ OpenFileFlag flags_;
int file_descriptor_;
bool closed_ = false;
};
diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt
index 07d6cd9a..b5b20d46 100644
--- a/src/common/CMakeLists.txt
+++ b/src/common/CMakeLists.txt
@@ -1,5 +1,6 @@
set(CRU_BASE_INCLUDE_DIR ${CRU_INCLUDE_DIR}/cru/common)
add_library(cru_base SHARED
+ Exception.cpp
Logger.cpp
PropertyTree.cpp
String.cpp
diff --git a/src/common/ErrnoException.cpp b/src/common/ErrnoException.cpp
index df2349ac..15d298b3 100644
--- a/src/common/ErrnoException.cpp
+++ b/src/common/ErrnoException.cpp
@@ -7,5 +7,6 @@ ErrnoException::ErrnoException(const String& message)
: ErrnoException(message, errno) {}
ErrnoException::ErrnoException(const String& message, int errno_code)
- : Exception(message), errno_code_(errno_code) {}
+ : Exception(Format(u"{}. Errno is {}.", message, errno_code)),
+ errno_code_(errno_code) {}
} // namespace cru
diff --git a/src/common/Exception.cpp b/src/common/Exception.cpp
new file mode 100644
index 00000000..779c65d6
--- /dev/null
+++ b/src/common/Exception.cpp
@@ -0,0 +1,17 @@
+#include "cru/common/Exception.hpp"
+
+namespace cru {
+Exception::Exception() {}
+
+Exception::Exception(String message) : message_(std::move(message)) {}
+
+Exception::~Exception() {}
+
+const char* Exception::what() const noexcept {
+ if (!message_.empty() && utf8_message_.empty()) {
+ utf8_message_ = message_.ToUtf8();
+ }
+
+ return utf8_message_.c_str();
+}
+} // namespace cru
diff --git a/src/common/io/Stream.cpp b/src/common/io/Stream.cpp
index 7c30406f..5062aa72 100644
--- a/src/common/io/Stream.cpp
+++ b/src/common/io/Stream.cpp
@@ -19,6 +19,14 @@ Index Stream::Write(const std::byte* buffer, Index size) {
return Write(buffer, 0, size);
}
+Index Stream::Write(const char* buffer, Index offset, Index size) {
+ return Write(reinterpret_cast<const std::byte*>(buffer), offset, size);
+}
+
+Index Stream::Write(const char* buffer, Index size) {
+ return Write(reinterpret_cast<const std::byte*>(buffer), size);
+}
+
void Stream::Flush() {}
void Stream::Close() {}
diff --git a/src/common/io/UnixFileStream.cpp b/src/common/io/UnixFileStream.cpp
index 4fa61b7b..9c9abba5 100644
--- a/src/common/io/UnixFileStream.cpp
+++ b/src/common/io/UnixFileStream.cpp
@@ -3,36 +3,37 @@
#include "cru/common/io/OpenFileFlag.hpp"
#include <fcntl.h>
+#include <sys/_types/_s_ifmt.h>
#include <unistd.h>
namespace cru::io {
namespace {
-int MapOpenFileFlag(OpenFileFlag flag) {
+int MapOpenFileFlag(OpenFileFlag flags) {
int result = 0;
- if (flag & OpenFileFlags::Read) {
- if (flag & OpenFileFlags::Write) {
+ if (flags & OpenFileFlags::Read) {
+ if (flags & OpenFileFlags::Write) {
result |= O_RDWR;
} else {
result |= O_RDONLY;
}
} else {
- if (flag & OpenFileFlags::Write) {
+ if (flags & OpenFileFlags::Write) {
result |= O_WRONLY;
} else {
throw Exception(u"Invalid open file flag.");
}
}
- if (flag | OpenFileFlags::Append) {
+ if (flags & OpenFileFlags::Append) {
result |= O_APPEND;
}
- if (flag | OpenFileFlags::Create) {
+ if (flags & OpenFileFlags::Create) {
result |= O_CREAT;
}
- if (flag | OpenFileFlags::ThrowOnExist) {
+ if (flags & OpenFileFlags::ThrowOnExist) {
result |= O_EXCL;
}
@@ -55,10 +56,14 @@ int MapSeekOrigin(Stream::SeekOrigin origin) {
UnixFileStream::~UnixFileStream() { Close(); }
-UnixFileStream::UnixFileStream(String path, OpenFileFlag flags) {
- file_descriptor_ = ::open(path.ToUtf8().c_str(), MapOpenFileFlag(flags));
+UnixFileStream::UnixFileStream(String path, OpenFileFlag flags)
+ : path_(std::move(path)), flags_(flags) {
+ auto p = path_.ToUtf8();
+ file_descriptor_ =
+ ::open(p.c_str(), MapOpenFileFlag(flags_), S_IRUSR | S_IWUSR);
if (file_descriptor_ == -1) {
- throw ErrnoException(u"Failed to open file.");
+ throw ErrnoException(
+ Format(u"Failed to open file {} with flags {}.", path_, flags_.value));
}
}
@@ -86,7 +91,7 @@ void UnixFileStream::Seek(Index offset, SeekOrigin origin) {
bool UnixFileStream::CanRead() {
CheckClosed();
- return flags & OpenFileFlags::Read;
+ return flags_ & OpenFileFlags::Read;
}
Index UnixFileStream::Read(std::byte *buffer, Index offset, Index size) {
@@ -100,7 +105,7 @@ Index UnixFileStream::Read(std::byte *buffer, Index offset, Index size) {
bool UnixFileStream::CanWrite() {
CheckClosed();
- return flags & OpenFileFlags::Write;
+ return flags_ & OpenFileFlags::Write;
}
Index UnixFileStream::Write(const std::byte *buffer, Index offset, Index size) {
@@ -117,6 +122,7 @@ void UnixFileStream::Close() {
if (::close(file_descriptor_) == -1) {
throw ErrnoException(u"Failed to close file.");
}
+ closed_ = true;
}
void UnixFileStream::CheckClosed() {
diff --git a/test/common/CMakeLists.txt b/test/common/CMakeLists.txt
index 810c68d5..1c669dcc 100644
--- a/test/common/CMakeLists.txt
+++ b/test/common/CMakeLists.txt
@@ -6,4 +6,10 @@ add_executable(cru_base_test
)
target_link_libraries(cru_base_test PRIVATE cru_base cru_test_base)
+if (UNIX)
+ target_sources(cru_base_test PRIVATE
+ io/UnixFileStreamTest.cpp
+ )
+endif()
+
gtest_discover_tests(cru_base_test)
diff --git a/test/common/io/UnixFileStreamTest.cpp b/test/common/io/UnixFileStreamTest.cpp
new file mode 100644
index 00000000..1b73dd3b
--- /dev/null
+++ b/test/common/io/UnixFileStreamTest.cpp
@@ -0,0 +1,32 @@
+#include "cru/common/io/OpenFileFlag.hpp"
+#include "cru/common/io/UnixFileStream.hpp"
+
+#include <gtest/gtest.h>
+
+#include <cstdio>
+#include <filesystem>
+
+TEST(UnixFileStream, Work) {
+ using namespace cru;
+ using namespace cru::io;
+
+ auto temp_file_path =
+ (std::filesystem::temp_directory_path() / "cru_test_temp.XXXXXX")
+ .generic_string();
+ mktemp(temp_file_path.data());
+
+ String path = String::FromUtf8(temp_file_path);
+
+ UnixFileStream file(path, OpenFileFlags::Write | OpenFileFlags::Create);
+ file.Write("abc", 3);
+ file.Close();
+
+ UnixFileStream file2(path, OpenFileFlags::Read);
+ auto buffer = std::make_unique<std::byte[]>(3);
+ file2.Read(buffer.get(), 3);
+ ASSERT_EQ(std::string_view(reinterpret_cast<const char*>(buffer.get()), 3),
+ "abc");
+ file2.Close();
+
+ std::filesystem::remove(temp_file_path);
+}