Debian Codegraph @

modules/lsp-runtime-lib/src/main/io/InSequence.cpp

raw
1 /*
2 * Copyright (C) 2025 Linux Studio Plugins Project <https://lsp-plug.in/>
3 * (C) 2025 Vladimir Sadovnikov <[email protected]>
4 *
5 * This file is part of lsp-runtime-lib
6 * Created on: 16 июн. 2018 г.
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 #include <lsp-plug.in/io/charset.h>
23 #include <lsp-plug.in/io/StdioFile.h>
24 #include <lsp-plug.in/io/NativeFile.h>
25 #include <lsp-plug.in/io/InFileStream.h>
26 #include <lsp-plug.in/io/InSequence.h>
27
28 #include <errno.h>
29
30 #define CBUF_SIZE 0x1000
31 #define BBUF_SIZE 0x4000
32
33 namespace lsp
34 {
35 namespace io
36 {
37 InSequence::InSequence()
38 {
39 pIS = NULL;
40 nWrapFlags = 0;
41 }
42
43 InSequence::~InSequence()
44 {
45 // Close file descriptor
46 if (pIS != NULL)
47 {
48 if (nWrapFlags & WRAP_CLOSE)
49 pIS->close();
50 if (nWrapFlags & WRAP_DELETE)
51 delete pIS;
52 pIS = NULL;
53 }
54 nWrapFlags = 0;
55
56 // Close decoder
57 sDecoder.close();
58 }
59
60 status_t InSequence::close()
61 {
62 status_t res = STATUS_OK;
63
64 // Close file descriptor
65 if (pIS != NULL)
66 {
67 if (nWrapFlags & WRAP_CLOSE)
68 res = pIS->close();
69 if (nWrapFlags & WRAP_DELETE)
70 delete pIS;
71 pIS = NULL;
72 }
73 nWrapFlags = 0;
74
75 // Close decoder
76 sDecoder.close();
77
78 // Return result
79 return set_error(res);
80 }
81
82 status_t InSequence::wrap(FILE *fd, bool close, const char *charset)
83 {
84 if (pIS != NULL)
85 return set_error(STATUS_BAD_STATE);
86 else if (fd == NULL)
87 return set_error(STATUS_BAD_ARGUMENTS);
88
89 // Create input file stream
90 InFileStream *is = new InFileStream();
91 status_t res = is->wrap(fd, close);
92 if (res != STATUS_OK)
93 {
94 is->close();
95 delete is;
96 return set_error(res);
97 }
98
99 // Wrap input file stream
100 res = wrap(is, WRAP_CLOSE | WRAP_DELETE, charset);
101 if (res != STATUS_OK)
102 {
103 is->close();
104 delete is;
105 return set_error(res);
106 }
107
108 return set_error(res);
109 }
110
111 status_t InSequence::wrap_native(fhandle_t fd, bool close, const char *charset)
112 {
113 if (pIS != NULL)
114 return set_error(STATUS_BAD_STATE);
115
116 // Create input file stream
117 InFileStream *is = new InFileStream();
118 status_t res = is->wrap_native(fd, close);
119 if (res != STATUS_OK)
120 {
121 is->close();
122 delete is;
123 return set_error(res);
124 }
125
126 // Wrap input file stream
127 res = wrap(is, WRAP_CLOSE | WRAP_DELETE, charset);
128 if (res != STATUS_OK)
129 {
130 is->close();
131 delete is;
132 return set_error(res);
133 }
134
135 return set_error(res);
136 }
137
138 status_t InSequence::wrap(File *fd, size_t flags, const char *charset)
139 {
140 if (pIS != NULL)
141 return set_error(STATUS_BAD_STATE);
142 else if (fd == NULL)
143 return set_error(STATUS_BAD_ARGUMENTS);
144
145 // Create input file stream
146 InFileStream *is = new InFileStream();
147 status_t res = is->wrap(fd, flags);
148 if (res != STATUS_OK)
149 {
150 is->close();
151 delete is;
152 return set_error(res);
153 }
154
155 // Wrap input file stream
156 res = wrap(is, WRAP_CLOSE | WRAP_DELETE, charset);
157 if (res != STATUS_OK)
158 {
159 is->close();
160 delete is;
161 return set_error(res);
162 }
163
164 return set_error(res);
165 }
166
167 status_t InSequence::wrap(IInStream *is, size_t flags, const char *charset)
168 {
169 if (pIS != NULL)
170 return set_error(STATUS_BAD_STATE);
171 else if (is == NULL)
172 return set_error(STATUS_BAD_ARGUMENTS);
173
174 // Initialize decoder
175 status_t res = sDecoder.init(charset);
176 if (res != STATUS_OK)
177 {
178 sDecoder.close();
179 return set_error(res);
180 }
181
182 // Store pointers
183 pIS = is;
184 nWrapFlags = flags;
185
186 return set_error(STATUS_OK);
187 }
188
189 status_t InSequence::open(const char *path, const char *charset)
190 {
191 if (pIS != NULL)
192 return set_error(STATUS_BAD_STATE);
193 else if (path == NULL)
194 return set_error(STATUS_BAD_ARGUMENTS);
195
196 LSPString tmp;
197 if (!tmp.set_utf8(path))
198 return set_error(STATUS_NO_MEM);
199 return open(&tmp, charset);
200 }
201
202 status_t InSequence::open(const LSPString *path, const char *charset)
203 {
204 if (pIS != NULL)
205 return set_error(STATUS_BAD_STATE);
206 else if (path == NULL)
207 return set_error(STATUS_BAD_ARGUMENTS);
208
209 // Create input file stream
210 InFileStream *is = new InFileStream();
211 status_t res = is->open(path);
212 if (res != STATUS_OK)
213 {
214 is->close();
215 delete is;
216 return set_error(res);
217 }
218
219 // Wrap input file stream
220 res = wrap(is, WRAP_CLOSE | WRAP_DELETE, charset);
221 if (res != STATUS_OK)
222 {
223 is->close();
224 delete is;
225 return set_error(res);
226 }
227
228 return set_error(res);
229 }
230
231 status_t InSequence::open(const Path *path, const char *charset)
232 {
233 return open(path->as_string(), charset);
234 }
235
236 ssize_t InSequence::read(lsp_wchar_t *dst, size_t count)
237 {
238 if (pIS == NULL)
239 return -set_error(STATUS_CLOSED);
240
241 // Clear line buffer
242 sLine.clear();
243
244 size_t n_read = 0;
245 while (n_read < count)
246 {
247 // Try to fetch data
248 ssize_t fetched = sDecoder.fetch(dst, count - n_read);
249 if (fetched > 0)
250 {
251 n_read += fetched;
252 dst += fetched;
253 continue;
254 }
255
256 // No data to fetch? Try to fill buffer
257 ssize_t filled = sDecoder.fill(pIS);
258 if (filled > 0)
259 continue;
260
261 // Nothing to do more? Skip any errors if there was data processed
262 if (n_read > 0)
263 break;
264
265 // Analyze errors
266 if (fetched < 0)
267 return -set_error(status_t(-fetched));
268 else if (filled < 0)
269 return -set_error(status_t(-filled));
270
271 set_error(STATUS_OK);
272 break;
273 }
274
275 return n_read;
276 }
277
278 lsp_swchar_t InSequence::read_internal()
279 {
280 // Try to fetch character
281 lsp_swchar_t ch = sDecoder.fetch();
282 if (ch < 0)
283 {
284 // Analyze error
285 if (ch != -STATUS_EOF)
286 return -set_error(-ch);
287
288 // No data to fetch? Try to fill buffer
289 ssize_t filled = sDecoder.fill(pIS);
290 if (filled < 0)
291 return -set_error(status_t(-filled));
292 else if (filled == 0)
293 return -set_error(STATUS_EOF);
294
295 // Try to fetch character again
296 ch = sDecoder.fetch();
297 if (ch < 0)
298 return -set_error(-ch);
299 }
300 return ch;
301 }
302
303 lsp_swchar_t InSequence::read()
304 {
305 if (pIS == NULL)
306 return -set_error(STATUS_CLOSED);
307
308 // Clear line buffer
309 sLine.clear();
310 return read_internal();
311 }
312
313 status_t InSequence::read_line(LSPString *s, bool force)
314 {
315 if (pIS == NULL)
316 return set_error(STATUS_CLOSED);
317
318 while (true)
319 {
320 // Try to fetch character
321 lsp_swchar_t ch = read_internal();
322 if (ch < 0)
323 {
324 if (ch == -STATUS_EOF)
325 break;
326 return set_error(-ch);
327 }
328
329 // End of line?
330 if (ch == '\n')
331 {
332 if (sLine.last() == '\r')
333 sLine.remove_last();
334 s->take(&sLine);
335 return set_error(STATUS_OK);
336 }
337
338 // Append character
339 if (!sLine.append(lsp_wchar_t(ch)))
340 return set_error(STATUS_NO_MEM);
341 }
342
343 // Check force flag
344 if ((force) && (sLine.length() > 0))
345 {
346 s->take(&sLine);
347 return set_error(STATUS_OK);
348 }
349
350 return set_error(STATUS_EOF);
351 }
352
353 ssize_t InSequence::skip(size_t count)
354 {
355 sLine.clear();
356 return IInSequence::skip(count);
357 }
358
359 } /* namespace io */
360 } /* namespace lsp */