Debian Codegraph @

modules/lsp-runtime-lib/src/main/io/InFileStream.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: 13 мар. 2019 г.
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/NativeFile.h>
23 #include <lsp-plug.in/io/StdioFile.h>
24 #include <lsp-plug.in/io/InFileStream.h>
25
26 namespace lsp
27 {
28 namespace io
29 {
30
31 InFileStream::InFileStream()
32 {
33 pFD = NULL;
34 nWrapFlags = 0;
35 }
36
37 InFileStream::~InFileStream()
38 {
39 // Close file descriptor
40 if (pFD != NULL)
41 {
42 if (nWrapFlags & WRAP_CLOSE)
43 pFD->close();
44 if (nWrapFlags & WRAP_DELETE)
45 delete pFD;
46 pFD = NULL;
47 }
48 nWrapFlags = 0;
49 }
50
51 status_t InFileStream::close()
52 {
53 status_t res = STATUS_OK;
54
55 // Close file descriptor
56 if (pFD != NULL)
57 {
58 if (nWrapFlags & WRAP_CLOSE)
59 res = pFD->close();
60 if (nWrapFlags & WRAP_DELETE)
61 delete pFD;
62 pFD = NULL;
63 }
64 nWrapFlags = 0;
65
66 return set_error(res);
67 }
68
69 status_t InFileStream::wrap(FILE *fd, bool close)
70 {
71 if (pFD != NULL)
72 return set_error(STATUS_BAD_STATE);
73 else if (fd == NULL)
74 return set_error(STATUS_BAD_ARGUMENTS);
75
76 StdioFile *f = new StdioFile();
77 if (f == NULL)
78 return set_error(STATUS_NO_MEM);
79 status_t res = f->wrap(fd, File::FM_READ, close);
80 if (res != STATUS_OK)
81 {
82 f->close();
83 delete f;
84 return set_error(res);
85 }
86
87 res = wrap(f, WRAP_DELETE);
88 if (res != STATUS_OK)
89 {
90 f->close();
91 delete f;
92 }
93 return set_error(res);
94 }
95
96 status_t InFileStream::wrap_native(fhandle_t fd, bool close)
97 {
98 if (pFD != NULL)
99 return set_error(STATUS_BAD_STATE);
100
101 NativeFile *f = new NativeFile();
102 if (f == NULL)
103 return set_error(STATUS_NO_MEM);
104 status_t res = f->wrap(fd, File::FM_READ, close);
105 if (res != STATUS_OK)
106 {
107 f->close();
108 delete f;
109 return set_error(res);
110 }
111
112 res = wrap(f, WRAP_DELETE);
113 if (res != STATUS_OK)
114 {
115 f->close();
116 delete f;
117 }
118 return set_error(res);
119 }
120
121 status_t InFileStream::wrap(File *fd, size_t flags)
122 {
123 if (pFD != NULL)
124 return set_error(STATUS_BAD_STATE);
125 else if (fd == NULL)
126 return set_error(STATUS_BAD_ARGUMENTS);
127
128 // Store pointers
129 pFD = fd;
130 nWrapFlags = flags;
131
132 return set_error(STATUS_OK);
133 }
134
135 status_t InFileStream::open(const char *path)
136 {
137 if (pFD != NULL)
138 return set_error(STATUS_BAD_STATE);
139 else if (path == NULL)
140 return set_error(STATUS_BAD_ARGUMENTS);
141
142 LSPString tmp;
143 if (!tmp.set_utf8(path))
144 return set_error(STATUS_NO_MEM);
145 return open(&tmp);
146 }
147
148 status_t InFileStream::open(const LSPString *path)
149 {
150 if (pFD != NULL)
151 return set_error(STATUS_BAD_STATE);
152 else if (path == NULL)
153 return set_error(STATUS_BAD_ARGUMENTS);
154
155 NativeFile *f = new NativeFile();
156 if (f == NULL)
157 return set_error(STATUS_NO_MEM);
158
159 status_t res = f->open(path, File::FM_READ);
160 if (res != STATUS_OK)
161 {
162 f->close();
163 delete f;
164 return set_error(res);
165 }
166
167 res = wrap(f, WRAP_CLOSE | WRAP_DELETE);
168 if (res != STATUS_OK)
169 {
170 f->close();
171 delete f;
172 }
173
174 return set_error(res);
175 }
176
177 status_t InFileStream::open(const Path *path)
178 {
179 return open(path->as_string());
180 }
181
182 wssize_t InFileStream::avail()
183 {
184 wssize_t pos = pFD->position();
185 if (pos < 0)
186 {
187 set_error(status_t(-pos));
188 return pos;
189 }
190 wssize_t size = pFD->size();
191 if (size < 0)
192 {
193 set_error(status_t(-size));
194 return size;
195 }
196 return size - pos;
197 }
198
199 wssize_t InFileStream::position()
200 {
201 if (pFD == NULL)
202 return set_error(STATUS_CLOSED);
203 wssize_t pos = pFD->position();
204 set_error((pos >= 0) ? STATUS_OK : status_t(-pos));
205 return pos;
206 }
207
208 ssize_t InFileStream::read(void *dst, size_t count)
209 {
210 if (pFD == NULL)
211 return set_error(STATUS_CLOSED);
212 ssize_t res = pFD->read(dst, count);
213 set_error((res >= 0) ? STATUS_OK : status_t(-res));
214 return res;
215 }
216
217 wssize_t InFileStream::seek(wsize_t position)
218 {
219 if (pFD == NULL)
220 return set_error(STATUS_CLOSED);
221 status_t res = pFD->seek(position, File::FSK_SET);
222 if (res != STATUS_OK)
223 return -set_error(res);
224 wssize_t pos = pFD->position();
225 set_error((pos >= 0) ? STATUS_OK : status_t(-pos));
226 return pos;
227 }
228
229 wssize_t InFileStream::skip(wsize_t amount)
230 {
231 if (pFD == NULL)
232 return set_error(STATUS_CLOSED);
233 wssize_t before = pFD->position();
234 if (before < 0)
235 return IInStream::skip(amount);
236 status_t res = pFD->seek(amount, File::FSK_CUR);
237 if (res != STATUS_OK)
238 return (res == STATUS_NOT_SUPPORTED) ?
239 IInStream::skip(amount) : set_error(res);
240
241 wssize_t after = pFD->position();
242 if (after < 0)
243 {
244 set_error(status_t(-after));
245 return after;
246 }
247
248 return after - before;
249 }
250
251
252
253 } /* namespace io */
254 } /* namespace lsp */