Debian Codegraph @

modules/lsp-runtime-lib/include/lsp-plug.in/io/InBufStream.h

raw
1 /*
2 * Copyright (C) 2026 Linux Studio Plugins Project <https://lsp-plug.in/>
3 * (C) 2026 Vladimir Sadovnikov <[email protected]>
4 *
5 * This file is part of lsp-runtime-lib
6 * Created on: 17 янв. 2026 г.
7 *
8 * lsp-runtime-lib is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * any later version.
12 *
13 * lsp-runtime-lib is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with lsp-runtime-lib. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22 #ifndef LSP_PLUG_IN_IO_INBUFSTREAM_H_
23 #define LSP_PLUG_IN_IO_INBUFSTREAM_H_
24
25 #include <lsp-plug.in/runtime/version.h>
26
27 #include <lsp-plug.in/common/types.h>
28 #include <lsp-plug.in/common/status.h>
29 #include <lsp-plug.in/io/File.h>
30 #include <lsp-plug.in/io/IInStream.h>
31 #include <lsp-plug.in/stdlib/stdio.h>
32
33 namespace lsp
34 {
35 namespace io
36 {
37 /**
38 * This class works as a proxy around the input stream but keeps last
39 * read bytes in the internal buffer that allows to roll-back some
40 * read operations. This may be useful, for example, for detecting
41 * file format when reading files.
42 */
43 class InBufStream: public IInStream
44 {
45 private:
46 enum ext_flags_t
47 {
48 EXT_BUFFERING = 1 << (NUM_WRAP_FLAGS + 0),
49 EXT_SEEK_BUFFERING = 1 << (NUM_WRAP_FLAGS + 1)
50 };
51
52 private:
53 IInStream *pIS; // Input stream
54 uint8_t *vBuffer; // Pointer to the beginning of the ring buffer
55 uint32_t nBufCap; // Overall ring buffer capacity
56 uint32_t nBufHead; // Head position inside of the ring buffer
57 uint32_t nBufSize; // Number of elements stored in the ring buffer
58 uint32_t nBufOff; // Read offset in the ring buffer
59 wsize_t nPosition; // Position of the buffer in the stream
60 size_t nWrapFlags; // Wrap flags
61
62 private:
63 bool init_buffer();
64 inline void clear_buffer();
65 status_t do_close();
66 status_t do_wrap(IInStream *is, size_t flags);
67
68 public:
69 explicit InBufStream(size_t buf_size = 0x1000);
70 InBufStream(const InBufStream &) = delete;
71 InBufStream(InBufStream &&) = delete;
72 virtual ~InBufStream() override;
73
74 InBufStream & operator = (const InBufStream &) = delete;
75 InBufStream & operator = (InBufStream &&) = delete;
76
77 public: // io::IInStream
78 virtual wssize_t avail() override;
79 virtual wssize_t position() override;
80 virtual ssize_t read_byte() override;
81 virtual ssize_t read(void *dst, size_t count) override;
82 virtual wssize_t seek(wsize_t position) override;
83 virtual wssize_t skip(wsize_t amount) override;
84 virtual status_t close() override;
85
86 public: // Open operations
87 /** Open input stream associated with file. The Reader should be in closed state.
88 *
89 * @param path file location path
90 * @return status of operation
91 */
92 status_t open(const char *path);
93
94 /** Open input stream associated with file. The Reader should be in closed state.
95 *
96 * @param path file location path
97 * @return status of operation
98 */
99 status_t open(const LSPString *path);
100
101 /** Open input stream associated with file. Before open currently open stream is closed and it's
102 * state is reset.
103 *
104 * @param path file location path
105 * @return status of operation
106 */
107 status_t open(const Path *path);
108
109 public: // Wrap operations
110 /** Wrap stdio file descriptor. The Reader should be in closed state.
111 *
112 * @param fd file descriptor
113 * @param close close file descriptor on close()
114 * @return status of operation
115 */
116 status_t wrap(FILE *fd, bool close);
117
118 /** Wrap native file descriptor. The Reader should be in closed state.
119 *
120 * @param fd file descriptor
121 * @param close close file descriptor on close()
122 * @return status of operation
123 */
124 status_t wrap_native(fhandle_t fd, bool close);
125
126 /** Wrap file descriptor. The Reader should be in closed state.
127 *
128 * @param fd file descriptor
129 * @param flags wrapping flags
130 * @return status of operation
131 */
132 status_t wrap(File *fd, size_t flags);
133
134 /** Wrap output stream
135 *
136 * @param is input stream
137 * @param flags wrapping flags
138 * @return status of operation
139 */
140 status_t wrap(IInStream *is, size_t flags = 0);
141
142 public: // Buffering operations
143 /**
144 * Enable/disable buffering
145 * @param enable buffering enable/disable flag
146 */
147 void set_buffering(bool enable);
148
149 /**
150 * Enable/disable buffering for seek and skip methods.
151 * Non-buffered seek and skip methods flush internal buffer and use the
152 * seek and skip methods of underlying stream if the new position
153 * of the stream is out of the area currently covered by the buffer.
154 * Buffered seek and skip methods aim to fill as much as possibe the
155 * buffer before the seek position if the position is outside of the
156 * area currently covered by the buffer.
157 *
158 * @param enable quick seek mode enable/disable flag
159 */
160 void set_seek_buffering(bool enable);
161
162 /**
163 * Get start position of the area covered by buffer
164 * @return start position
165 */
166 inline wssize_t buffer_position() const { return nPosition; }
167
168 /**
169 * Get the size of the area covered by buffer
170 * @return size of the area
171 */
172 inline ssize_t buffer_size() const { return nBufSize; }
173
174 /**
175 * Get the maximum possible size of the area covered by buffer
176 * @return maximum possible size of the area
177 */
178 inline ssize_t buffer_capacity() const { return nBufCap; }
179
180 /**
181 * Get current read position inside of the area covered by buffer
182 * @return current read position
183 */
184 inline ssize_t buffer_offset() const { return nBufOff; }
185
186 };
187
188 } /* namespace io */
189 } /* namespace lsp */
190
191
192
193 #endif /* LSP_PLUG_IN_IO_INBUFSTREAM_H_ */