Debian Codegraph @

modules/lsp-runtime-lib/src/main/resource/ILoader.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: 23 окт. 2020 г.
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/common/debug.h>
23 #include <lsp-plug.in/io/InSequence.h>
24 #include <lsp-plug.in/io/InFileStream.h>
25 #include <lsp-plug.in/io/Dir.h>
26 #include <lsp-plug.in/io/Path.h>
27 #include <lsp-plug.in/lltl/darray.h>
28 #include <lsp-plug.in/resource/ILoader.h>
29 #include <lsp-plug.in/stdlib/string.h>
30
31 namespace lsp
32 {
33 namespace resource
34 {
35 ILoader::ILoader()
36 {
37 nError = STATUS_OK;
38 }
39
40 ILoader::~ILoader()
41 {
42 nError = STATUS_OK;
43 }
44
45 io::IInStream *ILoader::read_stream(const char *name)
46 {
47 io::Path tmp;
48 if ((nError = tmp.set(name)) != STATUS_OK)
49 return NULL;
50 return read_stream(&tmp);
51 }
52
53 io::IInStream *ILoader::read_stream(const LSPString *name)
54 {
55 io::Path tmp;
56 if ((nError = tmp.set(name)) != STATUS_OK)
57 return NULL;
58 return read_stream(&tmp);
59 }
60
61 io::IInStream *ILoader::read_stream(const io::Path *name)
62 {
63 io::InFileStream *is = new io::InFileStream();
64 if (is != NULL)
65 {
66 nError = is->open(name);
67 if (nError == STATUS_OK)
68 return is;
69 }
70 else
71 nError = STATUS_NO_MEM;
72
73 if (is != NULL)
74 {
75 is->close();
76 delete is;
77 }
78
79 return NULL;
80 }
81
82 io::IInSequence *ILoader::read_sequence(const char *name, const char *charset)
83 {
84 io::Path tmp;
85 if ((nError = tmp.set(name)) != STATUS_OK)
86 return NULL;
87 return read_sequence(&tmp, charset);
88 }
89
90 io::IInSequence *ILoader::read_sequence(const LSPString *name, const char *charset)
91 {
92 io::Path tmp;
93 if ((nError = tmp.set(name)) != STATUS_OK)
94 return NULL;
95 return read_sequence(&tmp, charset);
96 }
97
98 io::IInSequence *ILoader::read_sequence(const io::Path *name, const char *charset)
99 {
100 io::IInStream *is = read_stream(name);
101 if (is == NULL)
102 return NULL;
103
104 io::InSequence *isq = new io::InSequence();
105 if (isq != NULL)
106 {
107 // Wrap the file and return result
108 nError = isq->wrap(is, WRAP_CLOSE | WRAP_DELETE, charset);
109 if (nError == STATUS_OK)
110 return isq;
111
112 isq->close();
113 delete isq;
114 }
115 else
116 nError = STATUS_NO_MEM;
117
118 is->close();
119 delete is;
120
121 return NULL;
122 }
123
124 ssize_t ILoader::enumerate(const char *path, resource_t **list)
125 {
126 io::Path tmp;
127 if ((nError = tmp.set(path)) != STATUS_OK)
128 return -nError;
129 return enumerate(&tmp, list);
130 }
131
132 ssize_t ILoader::enumerate(const LSPString *path, resource_t **list)
133 {
134 io::Path tmp;
135 if ((nError = tmp.set(path)) != STATUS_OK)
136 return -nError;
137 return enumerate(&tmp, list);
138 }
139
140 ssize_t ILoader::enumerate(const io::Path *path, resource_t **list)
141 {
142 lsp_trace("path = %s", path->as_utf8());
143
144 lltl::darray<resource_t> xlist;
145 io::Dir dir;
146 LSPString item;
147 io::fattr_t attr;
148
149 status_t res = dir.open(path);
150 if (res != STATUS_OK)
151 return -set_error(res);
152
153 while ((res = dir.reads(&item, &attr)) == STATUS_OK)
154 {
155 // Skip dot and dotdot
156 if ((item.equals_ascii(".")) ||
157 (item.equals_ascii("..")))
158 continue;
159
160 // Add resource to list
161 resource_t *r = xlist.add();
162 if (r == NULL)
163 {
164 dir.close();
165 return -set_error(STATUS_NO_MEM);
166 }
167 const char *name = item.get_utf8();
168 if (name == NULL)
169 {
170 dir.close();
171 return -set_error(STATUS_NO_MEM);
172 }
173
174 r->type = (attr.type == io::fattr_t::FT_DIRECTORY) ? RES_DIR : RES_FILE;
175 strncpy(r->name, name, RESOURCE_NAME_MAX);
176 r->name[RESOURCE_NAME_MAX-1] = '\0';
177 }
178
179 if (res != STATUS_EOF)
180 {
181 dir.close();
182 return -set_error(res);
183 }
184 else if ((res = dir.close()) != STATUS_OK)
185 return -set_error(res);
186
187 // Detach data pointer from the collection and return as result
188 const ssize_t return_size = xlist.size();
189 *list = xlist.release();
190
191 lsp_trace("return %d resources %p", int(return_size), *list);
192
193 set_error(STATUS_OK);
194 return return_size;
195 }
196
197 } /* namespace resource */
198 } /* namespace lsp */