Sese Framework  2.3.0
A cross-platform framework
Loading...
Searching...
No Matches
FileStream.h
Go to the documentation of this file.
1// Copyright 2024 libsese
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
22#pragma once
23
24#include "sese/io/Stream.h"
25#include "sese/io/Closeable.h"
27#include "sese/system/Path.h"
28#include "sese/util/ErrorCode.h"
29#include "sese/util/Result.h"
30
31namespace sese::io {
32
34enum class Seek {
36 CUR = SEEK_CUR,
38 BEGIN = SEEK_SET,
40 END = SEEK_END
41};
42
46class FileStream final : public Stream, public Closeable, public PeekableStream {
47public:
48#ifdef SESE_PLATFORM_WINDOWS
49 static constexpr auto B_READ_W = "rb,ccs=utf-8"; // NOLINT
50 static constexpr auto B_WRITE_TRUNC_W = "wb,ccs=utf-8"; // NOLINT
51 static constexpr auto B_WRITE_APPEND_W = "ab,ccs=utf-8"; // NOLINT
52 static constexpr auto B_TRUNC_W = "wb+,ccs=utf-8"; // NOLINT
53 static constexpr auto B_APPEND_W = "ab+,ccs=utf-8"; // NOLINT
54 static constexpr auto T_READ_W = "rt,ccs=utf-8"; // NOLINT
55 static constexpr auto T_WRITE_TRUNC_W = "wt,ccs=utf-8"; // NOLINT
56 static constexpr auto T_WRITE_APPEND_W = "at,ccs=utf-8"; // NOLINT
57 static constexpr auto T_TRUNC_W = "wt+,ccs=utf-8"; // NOLINT
58 static constexpr auto T_APPEND_W = "at+,ccs=utf-8"; // NOLINT
59#endif
60 static constexpr auto B_READ = "rb"; // NOLINT
61 static constexpr auto B_WRITE_TRUNC = "wb"; // NOLINT
62 static constexpr auto B_WRITE_APPEND = "ab"; // NOLINT
63 static constexpr auto B_TRUNC = "wb+"; // NOLINT
64 static constexpr auto B_APPEND = "ab+"; // NOLINT
65 static constexpr auto T_READ = "rt"; // NOLINT
66 static constexpr auto T_WRITE_TRUNC = "wt"; // NOLINT
67 static constexpr auto T_WRITE_APPEND = "at"; // NOLINT
68 static constexpr auto T_TRUNC = "wt+"; // NOLINT
69 static constexpr auto T_APPEND = "at+"; // NOLINT
70
71 using Ptr = std::shared_ptr<FileStream>;
72
78 static FileStream::Ptr create(const std::string &file_path, const char *mode) noexcept;
79
84 static FileStream::Ptr createWithPath(const system::Path &path, const char *mode) noexcept;
85
86 static Result<Ptr, ErrorCode> createEx(const std::string &file_path, const char *mode) noexcept;
87
88 ~FileStream() override = default;
89
90 int64_t read(void *buffer, size_t length) override;
91 int64_t write(const void *buffer, size_t length) override;
92
93 void close() override;
94
95 int64_t peek(void *buffer, size_t length) override;
96 int64_t trunc(size_t length) override;
97
101 [[nodiscard]] bool eof() const;
102
103 [[nodiscard]] int64_t getSeek() const;
104 [[nodiscard]] int32_t setSeek(int64_t offset, int32_t whence) const;
105 [[nodiscard]] int32_t setSeek(int64_t offset, Seek type) const;
106
107 [[nodiscard]] int32_t flush() const;
108
109 [[nodiscard]] int32_t getFd() const;
110
111private:
112 FileStream() noexcept = default;
113 FILE *file = nullptr;
114};
115
117} // namespace sese::io