Sese Framework  x.y.z
A cross-platform framework
载入中...
搜索中...
未找到
FileStream.h
浏览该文件的文档.
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
21#pragma once
22
23#include "sese/io/Stream.h"
24#include "sese/io/Closeable.h"
26#include "sese/system/Path.h"
27#include "sese/util/Result.h"
28
29namespace sese::io {
30
32enum class Seek {
34 CUR = SEEK_CUR,
36 BEGIN = SEEK_SET,
38 END = SEEK_END
39};
40
44class FileStream final : public Stream, public Closeable, public PeekableStream {
45public:
46#ifdef SESE_PLATFORM_WINDOWS
47 static constexpr auto B_READ_W = "rb,ccs=utf-8"; // NOLINT
48 static constexpr auto B_WRITE_TRUNC_W = "wb,ccs=utf-8"; // NOLINT
49 static constexpr auto B_WRITE_APPEND_W = "ab,ccs=utf-8"; // NOLINT
50 static constexpr auto B_TRUNC_W = "wb+,ccs=utf-8"; // NOLINT
51 static constexpr auto B_APPEND_W = "ab+,ccs=utf-8"; // NOLINT
52 static constexpr auto T_READ_W = "rt,ccs=utf-8"; // NOLINT
53 static constexpr auto T_WRITE_TRUNC_W = "wt,ccs=utf-8"; // NOLINT
54 static constexpr auto T_WRITE_APPEND_W = "at,ccs=utf-8"; // NOLINT
55 static constexpr auto T_TRUNC_W = "wt+,ccs=utf-8"; // NOLINT
56 static constexpr auto T_APPEND_W = "at+,ccs=utf-8"; // NOLINT
57#endif
58 static constexpr auto B_READ = "rb"; // NOLINT
59 static constexpr auto B_WRITE_TRUNC = "wb"; // NOLINT
60 static constexpr auto B_WRITE_APPEND = "ab"; // NOLINT
61 static constexpr auto B_TRUNC = "wb+"; // NOLINT
62 static constexpr auto B_APPEND = "ab+"; // NOLINT
63 static constexpr auto T_READ = "rt"; // NOLINT
64 static constexpr auto T_WRITE_TRUNC = "wt"; // NOLINT
65 static constexpr auto T_WRITE_APPEND = "at"; // NOLINT
66 static constexpr auto T_TRUNC = "wt+"; // NOLINT
67 static constexpr auto T_APPEND = "at+"; // NOLINT
68
69 using Ptr = std::shared_ptr<FileStream>;
70
76 static FileStream::Ptr create(const std::string &file_path, const char *mode) noexcept;
77
82 static FileStream::Ptr createWithPath(const system::Path &path, const char *mode) noexcept;
83
84 static Result<FileStream::Ptr> createEx(const std::string &file_path, const char *mode) noexcept;
85
86 ~FileStream() override = default;
87
88 int64_t read(void *buffer, size_t length) override;
89 int64_t write(const void *buffer, size_t length) override;
90
91 void close() override;
92
93 int64_t peek(void *buffer, size_t length) override;
94 int64_t trunc(size_t length) override;
95
99 [[nodiscard]] bool eof() const;
100
101 [[nodiscard]] int64_t getSeek() const;
102 [[nodiscard]] int32_t setSeek(int64_t offset, int32_t whence) const;
103 [[nodiscard]] int32_t setSeek(int64_t offset, Seek type) const;
104
105 [[nodiscard]] int32_t flush() const;
106
107 [[nodiscard]] int32_t getFd() const;
108
109private:
110 FileStream() noexcept = default;
111 FILE *file = nullptr;
112};
113
115} // namespace sese::io