aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-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
5 files changed, 46 insertions, 13 deletions
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() {