Debian Codegraph @

modules/lsp-runtime-lib/src/main/fmt/xpm/xpm.cpp

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: 21 янв. 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 #include <lsp-plug.in/fmt/xpm/xpm.h>
23 #include <lsp-plug.in/fmt/xpm/Parser.h>
24 #include <lsp-plug.in/io/InFileStream.h>
25 #include <lsp-plug.in/io/InMemoryStream.h>
26
27 #include <private/fmt/xpm/Tokenizer.h>
28 #include <private/fmt/xpm/XPM1BuiltinParser.h>
29 #include <private/fmt/xpm/XPM1StreamParser.h>
30 #include <private/fmt/xpm/XPM2StreamParser.h>
31 #include <private/fmt/xpm/XPM3BuiltinParser.h>
32 #include <private/fmt/xpm/XPM3StreamParser.h>
33
34 namespace lsp
35 {
36 namespace xpm
37 {
38
39 status_t open(Parser **dst, const char *path)
40 {
41 io::InFileStream *is = new io::InFileStream();
42 if (is == NULL)
43 return STATUS_NO_MEM;
44
45 status_t res = is->open(path);
46 if (res != STATUS_OK)
47 {
48 is->close();
49 delete is;
50 return res;
51 }
52
53 return wrap(dst, is, WRAP_CLOSE | WRAP_DELETE);
54 }
55
56 status_t open(Parser **dst, const LSPString *path)
57 {
58 io::InFileStream *is = new io::InFileStream();
59 if (is == NULL)
60 return STATUS_NO_MEM;
61
62 status_t res = is->open(path);
63 if (res != STATUS_OK)
64 {
65 is->close();
66 delete is;
67 return res;
68 }
69
70 return wrap(dst, is, WRAP_CLOSE | WRAP_DELETE);
71 }
72
73 status_t open(Parser **dst, const io::Path *path)
74 {
75 io::InFileStream *is = new io::InFileStream();
76 if (is == NULL)
77 return STATUS_NO_MEM;
78
79 status_t res = is->open(path);
80 if (res != STATUS_OK)
81 {
82 is->close();
83 delete is;
84 return res;
85 }
86
87 return wrap(dst, is, WRAP_CLOSE | WRAP_DELETE);
88 }
89
90 status_t wrap(Parser **dst, const char *str)
91 {
92 io::InMemoryStream *is = new io::InMemoryStream();
93 if (is == NULL)
94 return STATUS_NO_MEM;
95
96 is->wrap(str, strlen(str));
97 return wrap(dst, is, WRAP_CLOSE | WRAP_DELETE);
98 }
99
100 status_t wrap(Parser **dst, const void *buf, size_t len)
101 {
102 io::InMemoryStream *is = new io::InMemoryStream();
103 if (is == NULL)
104 return STATUS_NO_MEM;
105
106 is->wrap(buf, len);
107 return wrap(dst, is, WRAP_CLOSE | WRAP_DELETE);
108 }
109
110 status_t wrap(Parser **dst, void *buf, size_t len, lsp_memdrop_t drop)
111 {
112 io::InMemoryStream *is = new io::InMemoryStream();
113 if (is == NULL)
114 return STATUS_NO_MEM;
115
116 is->wrap(buf, len, drop);
117 return wrap(dst, is, WRAP_CLOSE | WRAP_DELETE);
118 }
119
120 status_t wrap(Parser **dst, io::IInStream *is, size_t flags)
121 {
122 if ((dst == NULL) || (is == NULL))
123 return STATUS_BAD_ARGUMENTS;
124
125 Tokenizer *tok = new Tokenizer(is, flags);
126 if (tok == NULL)
127 {
128 finalize(is, flags);
129 return STATUS_NO_MEM;
130 }
131
132 lsp_finally {
133 if (tok != NULL)
134 {
135 tok->close();
136 delete tok;
137 }
138 };
139
140 status_t res = tok->init();
141 if (res != STATUS_OK)
142 return res;
143
144 token_type_t ttype;
145 const char *tvalue;
146 res = tok->read_token(ttype, tvalue);
147 if (res != STATUS_OK)
148 return res;
149
150 if (ttype == TOK_DEFINE) // XPM 1
151 {
152 tok->unread_token();
153
154 XPM1StreamParser * const parser = new XPM1StreamParser(tok);
155 if (parser == NULL)
156 return STATUS_NO_MEM;
157
158 tok = NULL;
159 *dst = parser;
160
161 return STATUS_OK;
162 }
163 else if (ttype == TOK_XPM2_SIG) // XPM 2
164 {
165 XPM2StreamParser * const parser = new XPM2StreamParser(tok);
166 if (parser == NULL)
167 return STATUS_NO_MEM;
168
169 tok = NULL;
170 *dst = parser;
171
172 return STATUS_OK;
173 }
174 else if (ttype == TOK_XPM3_SIG) // XPM 3
175 {
176 XPM3StreamParser * const parser = new XPM3StreamParser(tok);
177 if (parser == NULL)
178 return STATUS_NO_MEM;
179
180 tok = NULL;
181 *dst = parser;
182
183 return STATUS_OK;
184 }
185
186 return STATUS_UNSUPPORTED_FORMAT;
187 }
188
189 status_t make_xpm1(
190 Parser **dst,
191 size_t width, size_t height,
192 size_t num_colors, size_t chars_per_pixel,
193 const char * const * colors, const char * const * pixels)
194 {
195 if (dst == NULL)
196 return STATUS_BAD_ARGUMENTS;
197
198 header_t hdr;
199
200 hdr.version = VERSION_XPM1;
201 hdr.width = width;
202 hdr.height = height;
203 hdr.num_colors = num_colors;
204 hdr.chars_per_pixel = chars_per_pixel;
205 hdr.x_hotspot = 0;
206 hdr.y_hotspot = 0;
207 hdr.has_extensions = false;
208
209 XPM1BuiltinParser * parser = new XPM1BuiltinParser(hdr, colors, pixels);
210 if (parser == NULL)
211 return STATUS_NO_MEM;
212
213 *dst = parser;
214
215 return STATUS_OK;
216 }
217
218 status_t make_xpm3(Parser **dst, const char * const * lines, size_t count)
219 {
220 if (dst == NULL)
221 return STATUS_BAD_ARGUMENTS;
222
223 XPM3BuiltinParser * parser = new XPM3BuiltinParser(lines, count);
224 if (parser == NULL)
225 return STATUS_NO_MEM;
226
227 *dst = parser;
228
229 return STATUS_OK;
230 }
231
232
233 } /* namespace xpm */
234 } /* namespace lsp */
235
236