Debian Codegraph @

modules/lsp-runtime-lib/src/main/io/InMarkSequence.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: 12 февр. 2021 г.
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/InMarkSequence.h>
23
24 #define MARKSEQ_SIZE 0x400
25
26 namespace lsp
27 {
28 namespace io
29 {
30 InMarkSequence::InMarkSequence()
31 {
32 pSequence = NULL;
33 nMarkPos = 0;
34 nMarkLen = -1;
35 nMarkMax = 0;
36 pBuf = NULL;
37 nBufCap = 0;
38 bClose = false;
39 }
40
41 InMarkSequence::~InMarkSequence()
42 {
43 do_close();
44 }
45
46 status_t InMarkSequence::wrap(IInSequence *in, bool close)
47 {
48 if (in == NULL)
49 return set_error(STATUS_BAD_ARGUMENTS);
50 if (pSequence != NULL)
51 return set_error(STATUS_BAD_STATE);
52
53 pSequence = in;
54 bClose = close;
55
56 clear_mark();
57
58 return set_error(STATUS_OK);
59 }
60
61 status_t InMarkSequence::do_close()
62 {
63 status_t res = STATUS_OK;
64
65 if ((pSequence != NULL) && (bClose))
66 res = pSequence->close();
67
68 pSequence = NULL;
69 bClose = false;
70
71 clear_mark();
72
73 return res;
74 }
75
76 status_t InMarkSequence::close()
77 {
78 status_t res = do_close();
79 return set_error(res);
80 }
81
82 ssize_t InMarkSequence::grow_buffer(size_t amount)
83 {
84 size_t last = lsp_min(nMarkMax, ssize_t(nMarkPos + amount));
85 if (last <= nBufCap)
86 return nBufCap - nMarkPos;
87 last = lsp_min(last, size_t(MARKSEQ_SIZE));
88 size_t cap = lsp_max(last, (nBufCap + (nBufCap >> 1)));
89
90 lsp_wchar_t *buf = reinterpret_cast<lsp_wchar_t *>(realloc(pBuf, sizeof(lsp_wchar_t) * cap));
91 if (buf == NULL)
92 return -STATUS_NO_MEM;
93
94 pBuf = buf;
95 nBufCap = cap;
96
97 return cap - nMarkPos;
98 }
99
100 void InMarkSequence::clear_mark()
101 {
102 nMarkPos = 0;
103 nMarkLen = 0;
104 nMarkMax = -1;
105
106 if (pBuf != NULL)
107 free(pBuf);
108 pBuf = NULL;
109 nBufCap = 0;
110 }
111
112 ssize_t InMarkSequence::read(lsp_wchar_t *dst, size_t count)
113 {
114 ssize_t nread;
115 if (pSequence == NULL)
116 return -set_error(STATUS_CLOSED);
117
118 // Simple read if there is no mark set or mark buffer is full
119 if (nMarkMax < 0)
120 {
121 nread = pSequence->read(dst, count);
122 if (nread < 0)
123 set_error(status_t(-nread));
124 return nread;
125 }
126
127 // Fetch all pending characters from the buffer
128 size_t total = 0;
129 nread = lsp_min(count, size_t(nMarkLen - nMarkPos));
130 if (nread > 0)
131 {
132 memcpy(dst, &pBuf[nMarkPos], sizeof(lsp_wchar_t) * nread);
133 nMarkPos += nread;
134 total += nread;
135 dst += nread;
136
137 if ((count -= nread) <= 0)
138 {
139 set_error(STATUS_OK);
140 return total;
141 }
142 }
143
144 // Read data to buffer and emit to output
145 while (nMarkPos < nMarkMax)
146 {
147 // Grow buffer
148 ssize_t avail = grow_buffer(MARKSEQ_SIZE);
149 if (avail < 0)
150 return -set_error(STATUS_NO_MEM);
151 if (avail > ssize_t(count))
152 avail = count;
153
154 // Read data
155 nread = pSequence->read(&pBuf[nMarkPos], avail);
156 if (nread < 0)
157 {
158 if (total > 0)
159 {
160 set_error(STATUS_OK);
161 return total;
162 }
163
164 set_error(status_t(-nread));
165 return nread;
166 }
167
168 // Copy data and pdate positions
169 memcpy(dst, &pBuf[nMarkPos], sizeof(lsp_wchar_t) * nread);
170
171 nMarkPos += nread;
172 nMarkLen += nread;
173 total += nread;
174 dst += nread;
175
176 if ((count -= nread) <= 0)
177 {
178 set_error(STATUS_OK);
179 return total;
180 }
181 }
182
183 // Reset mark if we are out of buffer and return
184 nread = pSequence->read(dst, count);
185 if (nread > 0)
186 {
187 set_error(STATUS_OK);
188 clear_mark();
189 return total + nread;
190 }
191 else if (total > 0)
192 {
193 set_error(STATUS_OK);
194 return total;
195 }
196 set_error(status_t(-nread));
197 return nread;
198 }
199
200 lsp_swchar_t InMarkSequence::read()
201 {
202 lsp_swchar_t ch;
203 if (pSequence == NULL)
204 return -set_error(STATUS_CLOSED);
205
206 // Simple read if there is no mark set or mark buffer is full
207 if (nMarkMax < 0)
208 {
209 ch = pSequence->read();
210 set_error((ch < 0) ? -ch : STATUS_OK);
211 return ch;
212 }
213
214 // Check that we should read from buffer
215 if (nMarkPos < nMarkLen)
216 {
217 set_error(STATUS_OK);
218 return pBuf[nMarkPos++];
219 }
220
221 // Read the character
222 ch = pSequence->read();
223 if (ch < 0)
224 {
225 set_error(-ch);
226 return ch;
227 }
228
229 // Reset mark if we are out of buffer and return
230 if (nMarkPos >= nMarkMax)
231 {
232 set_error(STATUS_OK);
233 clear_mark();
234 return ch;
235 }
236
237 // Push character to buffer, update counters and return
238 if (grow_buffer(1) < 0)
239 return -set_error(STATUS_NO_MEM);
240
241 set_error(STATUS_OK);
242 nMarkLen++;
243 pBuf[nMarkPos++] = ch;
244
245 return ch;
246 }
247
248 ssize_t InMarkSequence::skip(size_t count)
249 {
250 if (pSequence == NULL)
251 return -set_error(STATUS_CLOSED);
252
253 // No mark set?
254 if (nMarkMax < 0)
255 {
256 ssize_t res = pSequence->skip(count);
257 set_error((res < 0) ? status_t(-res) : STATUS_OK);
258 return res;
259 }
260
261 // Skip all pending characters in the buffer
262 size_t skipped = 0;
263 ssize_t step = lsp_min(count, size_t(nMarkLen - nMarkPos));
264 nMarkPos += step;
265 skipped += step;
266
267 if ((count -= skipped) <= 0)
268 {
269 set_error(STATUS_OK);
270 return skipped;
271 }
272
273 // Read data to buffer
274 while (nMarkPos < nMarkMax)
275 {
276 // Grow buffer
277 ssize_t avail = grow_buffer(MARKSEQ_SIZE);
278 if (avail < 0)
279 return -set_error(STATUS_NO_MEM);
280 if (avail > ssize_t(count))
281 avail = count;
282
283 // Read data
284 ssize_t nread = pSequence->read(&pBuf[nMarkPos], avail);
285 if (nread < 0)
286 {
287 if (skipped > 0)
288 {
289 set_error(STATUS_OK);
290 return skipped;
291 }
292
293 set_error(status_t(-nread));
294 return nread;
295 }
296
297 // Update positions
298 nMarkPos += nread;
299 nMarkLen += nread;
300 skipped += nread;
301
302 if ((count -= skipped) <= 0)
303 {
304 set_error(STATUS_OK);
305 return skipped;
306 }
307 }
308
309 // Reset mark if we are out of buffer and return
310 step = pSequence->skip(count);
311 if (step > 0)
312 {
313 set_error(STATUS_OK);
314 clear_mark();
315 return skipped + step;
316 }
317 else if (skipped > 0)
318 {
319 set_error(STATUS_OK);
320 return skipped;
321 }
322 set_error(status_t(-step));
323 return step;
324 }
325
326 status_t InMarkSequence::mark(ssize_t limit)
327 {
328 if (pSequence == NULL)
329 return set_error(STATUS_CLOSED);
330 else if (limit < 0)
331 return set_error(STATUS_OK);
332
333 // Clear previous mark
334 nMarkPos = 0;
335 nMarkLen = 0;
336 nMarkMax = limit;
337
338 return set_error(STATUS_OK);
339 }
340
341 status_t InMarkSequence::reset()
342 {
343 if (pSequence == NULL)
344 return set_error(STATUS_CLOSED);
345 else if (nMarkMax < 0)
346 return set_error(STATUS_NOT_FOUND);
347
348 nMarkPos = 0;
349 return set_error(STATUS_OK);
350 }
351
352
353 } /* namespace io */
354 } /* namespace lsp */