aboutsummaryrefslogtreecommitdiff
path: root/include/cru
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2023-07-05 01:54:08 +0800
committercrupest <crupest@outlook.com>2023-07-05 01:54:08 +0800
commitbcfd51a7fabf7adf5f134247ca1020fac98b24b5 (patch)
tree41471cee6af6a7e49140e50ba818366b316c8ea7 /include/cru
parentaa9bc6bb7c11e810745c80548dd5ed5b2cb08740 (diff)
downloadcru-bcfd51a7fabf7adf5f134247ca1020fac98b24b5.tar.gz
cru-bcfd51a7fabf7adf5f134247ca1020fac98b24b5.tar.bz2
cru-bcfd51a7fabf7adf5f134247ca1020fac98b24b5.zip
Add CFileStream. Move ErrnoException.
Diffstat (limited to 'include/cru')
-rw-r--r--include/cru/common/Exception.h17
-rw-r--r--include/cru/common/io/CFileStream.h46
-rw-r--r--include/cru/common/io/Stream.h11
-rw-r--r--include/cru/common/platform/unix/ErrnoException.h18
4 files changed, 76 insertions, 16 deletions
diff --git a/include/cru/common/Exception.h b/include/cru/common/Exception.h
index 365d2064..6f458d35 100644
--- a/include/cru/common/Exception.h
+++ b/include/cru/common/Exception.h
@@ -29,4 +29,21 @@ class CRU_BASE_API TextEncodeException : public Exception {
public:
using Exception::Exception;
};
+
+class ErrnoException : public Exception {
+ public:
+ ErrnoException() : ErrnoException(String{}) {}
+ explicit ErrnoException(const String& message);
+ ErrnoException(const String& message, int errno_code);
+
+ CRU_DELETE_COPY(ErrnoException)
+ CRU_DELETE_MOVE(ErrnoException)
+
+ ~ErrnoException() override = default;
+
+ int GetErrnoCode() const { return errno_code_; }
+
+ private:
+ int errno_code_;
+};
} // namespace cru
diff --git a/include/cru/common/io/CFileStream.h b/include/cru/common/io/CFileStream.h
new file mode 100644
index 00000000..65de2ac7
--- /dev/null
+++ b/include/cru/common/io/CFileStream.h
@@ -0,0 +1,46 @@
+#pragma once
+
+#include "Stream.h"
+
+#include <cstdio>
+
+namespace cru::io {
+class CRU_BASE_API CFileStream : public Stream {
+ public:
+ CFileStream(const char* path, const char* mode);
+ explicit CFileStream(std::FILE* file, bool readable = true,
+ bool writable = true, bool auto_close = true);
+
+ CRU_DELETE_COPY(CFileStream)
+ CRU_DELETE_MOVE(CFileStream)
+
+ ~CFileStream() override;
+
+ public:
+ bool CanSeek() override;
+ Index Seek(Index offset, SeekOrigin origin = SeekOrigin::Current) override;
+ Index Tell() override;
+ void Rewind() override;
+
+ bool CanRead() override;
+ Index Read(std::byte* buffer, Index offset, Index size) override;
+
+ bool CanWrite() override;
+ Index Write(const std::byte* buffer, Index offset, Index size) override;
+
+ void Flush() override;
+
+ void Close() override;
+
+ std::FILE* GetHandle() const;
+
+ private:
+ void CheckClosed();
+
+ private:
+ std::FILE* file_;
+ bool readable_;
+ bool writable_;
+ bool auto_close_;
+};
+} // namespace cru::io
diff --git a/include/cru/common/io/Stream.h b/include/cru/common/io/Stream.h
index 66be4468..f833b0b9 100644
--- a/include/cru/common/io/Stream.h
+++ b/include/cru/common/io/Stream.h
@@ -2,12 +2,23 @@
#include "../Base.h"
+#include "../Exception.h"
#include "../String.h"
#include <cstddef>
#include <vector>
namespace cru::io {
+class CRU_BASE_API StreamAlreadyClosedException : public Exception {
+ public:
+ using Exception::Exception;
+
+ CRU_DEFAULT_COPY(StreamAlreadyClosedException)
+ CRU_DEFAULT_MOVE(StreamAlreadyClosedException)
+
+ CRU_DEFAULT_DESTRUCTOR(StreamAlreadyClosedException)
+};
+
class CRU_BASE_API Stream : public Object {
public:
enum class SeekOrigin { Current, Begin, End };
diff --git a/include/cru/common/platform/unix/ErrnoException.h b/include/cru/common/platform/unix/ErrnoException.h
index 00e1864b..97edcc78 100644
--- a/include/cru/common/platform/unix/ErrnoException.h
+++ b/include/cru/common/platform/unix/ErrnoException.h
@@ -7,22 +7,8 @@
#include "../Exception.h"
namespace cru::platform::unix {
-class ErrnoException : public PlatformException {
- public:
- ErrnoException() : ErrnoException(String{}) {}
- explicit ErrnoException(const String& message);
- ErrnoException(const String& message, int errno_code);
-
- CRU_DELETE_COPY(ErrnoException)
- CRU_DELETE_MOVE(ErrnoException)
-
- ~ErrnoException() override = default;
-
- int GetErrnoCode() const { return errno_code_; }
-
- private:
- int errno_code_;
-};
+// Moved to Exception.h
+using ErrnoException = cru::ErrnoException;
} // namespace cru::platform::unix
#endif