| 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: 24 окт. 2022 г. |
| 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/alloc.h> |
| 23 |
#include <lsp-plug.in/common/types.h> |
| 24 |
#include <lsp-plug.in/fmt/lspc/util/config.h> |
| 25 |
#include <lsp-plug.in/io/InFileStream.h> |
| 26 |
#include <lsp-plug.in/io/OutFileStream.h> |
| 27 |
#include <lsp-plug.in/io/InMemoryStream.h> |
| 28 |
#include <lsp-plug.in/io/OutMemoryStream.h> |
| 29 |
|
| 30 |
namespace lsp |
| 31 |
{ |
| 32 |
namespace lspc |
| 33 |
{ |
| 34 |
LSP_RUNTIME_LIB_PUBLIC |
| 35 |
status_t write_config(chunk_id_t *chunk_id, File *file, const char *path, size_t buf_size) |
| 36 |
{ |
| 37 |
io::InFileStream is; |
| 38 |
status_t res = is.open(path); |
| 39 |
if (res != STATUS_OK) |
| 40 |
return res; |
| 41 |
res = write_config(chunk_id, file, &is, buf_size); |
| 42 |
status_t res2 = is.close(); |
| 43 |
return (res != STATUS_OK) ? res : res2; |
| 44 |
} |
| 45 |
|
| 46 |
LSP_RUNTIME_LIB_PUBLIC |
| 47 |
status_t write_config(chunk_id_t *chunk_id, File *file, const io::Path *path, size_t buf_size) |
| 48 |
{ |
| 49 |
io::InFileStream is; |
| 50 |
status_t res = is.open(path); |
| 51 |
if (res != STATUS_OK) |
| 52 |
return res; |
| 53 |
res = write_config(chunk_id, file, &is, buf_size); |
| 54 |
status_t res2 = is.close(); |
| 55 |
return (res != STATUS_OK) ? res : res2; |
| 56 |
} |
| 57 |
|
| 58 |
LSP_RUNTIME_LIB_PUBLIC |
| 59 |
status_t write_config(chunk_id_t *chunk_id, File *file, const LSPString *path, size_t buf_size) |
| 60 |
{ |
| 61 |
io::InFileStream is; |
| 62 |
status_t res = is.open(path); |
| 63 |
if (res != STATUS_OK) |
| 64 |
return res; |
| 65 |
res = write_config(chunk_id, file, &is, buf_size); |
| 66 |
status_t res2 = is.close(); |
| 67 |
return (res != STATUS_OK) ? res : res2; |
| 68 |
} |
| 69 |
|
| 70 |
LSP_RUNTIME_LIB_PUBLIC |
| 71 |
status_t write_config(chunk_id_t *chunk_id, File *file, io::IInStream *is, size_t buf_size) |
| 72 |
{ |
| 73 |
lspc::ChunkWriter *wr = NULL; |
| 74 |
chunk_id_t res_chunk_id; |
| 75 |
|
| 76 |
// Create configuration entry and write header |
| 77 |
status_t res = write_config(&res_chunk_id, file, &wr); |
| 78 |
if (res != STATUS_OK) |
| 79 |
return res; |
| 80 |
else if (wr == NULL) |
| 81 |
return STATUS_NO_MEM; |
| 82 |
lsp_finally { delete wr; }; |
| 83 |
|
| 84 |
// Write data to configuration entry |
| 85 |
io::IOutStream *os = wr->stream(); |
| 86 |
wssize_t written = is->sink(os, buf_size); |
| 87 |
res = (written < 0) ? status_t(-written) : STATUS_OK; |
| 88 |
|
| 89 |
// Close stream, writer and exit |
| 90 |
res = update_status(res, os->close()); |
| 91 |
res = update_status(res, wr->close()); |
| 92 |
|
| 93 |
// Return result |
| 94 |
if (res != STATUS_OK) |
| 95 |
return res; |
| 96 |
|
| 97 |
if (chunk_id != NULL) |
| 98 |
*chunk_id = res_chunk_id; |
| 99 |
|
| 100 |
return STATUS_OK; |
| 101 |
} |
| 102 |
|
| 103 |
LSP_RUNTIME_LIB_PUBLIC |
| 104 |
status_t write_config(chunk_id_t *chunk_id, File *file, io::IOutStream **os) |
| 105 |
{ |
| 106 |
lspc::ChunkWriter *wr = NULL; |
| 107 |
chunk_id_t res_chunk_id; |
| 108 |
|
| 109 |
// We should have valid pointer to store the result |
| 110 |
if (os == NULL) |
| 111 |
return STATUS_BAD_ARGUMENTS; |
| 112 |
|
| 113 |
// Create configuration entry and write header |
| 114 |
status_t res = write_config(&res_chunk_id, file, &wr); |
| 115 |
if (res != STATUS_OK) |
| 116 |
return res; |
| 117 |
else if (wr == NULL) |
| 118 |
return STATUS_NO_MEM; |
| 119 |
|
| 120 |
// Allocate stream pointer and return it |
| 121 |
ChunkWriterStream *cws = new ChunkWriterStream(wr, true); |
| 122 |
if (cws == NULL) |
| 123 |
{ |
| 124 |
wr->close(); |
| 125 |
delete wr; |
| 126 |
return STATUS_NO_MEM; |
| 127 |
} |
| 128 |
|
| 129 |
// Return result |
| 130 |
if (chunk_id != NULL) |
| 131 |
*chunk_id = res_chunk_id; |
| 132 |
*os = cws; |
| 133 |
return STATUS_OK; |
| 134 |
} |
| 135 |
|
| 136 |
LSP_RUNTIME_LIB_PUBLIC |
| 137 |
status_t write_config_data(chunk_id_t *chunk_id, File *file, const char *data, size_t buf_size) |
| 138 |
{ |
| 139 |
if (data == NULL) |
| 140 |
return STATUS_BAD_ARGUMENTS; |
| 141 |
return write_config_data(chunk_id, file, data, strlen(data)); |
| 142 |
} |
| 143 |
|
| 144 |
LSP_RUNTIME_LIB_PUBLIC |
| 145 |
status_t write_config_data(chunk_id_t *chunk_id, File *file, const LSPString *data, size_t buf_size) |
| 146 |
{ |
| 147 |
if (data == NULL) |
| 148 |
return STATUS_BAD_ARGUMENTS; |
| 149 |
return write_config_data(chunk_id, file, data->get_utf8()); |
| 150 |
} |
| 151 |
|
| 152 |
LSP_RUNTIME_LIB_PUBLIC |
| 153 |
status_t write_config_data(chunk_id_t *chunk_id, File *file, const void *data, size_t bytes, size_t buf_size) |
| 154 |
{ |
| 155 |
if (data == NULL) |
| 156 |
return STATUS_BAD_ARGUMENTS; |
| 157 |
|
| 158 |
io::InMemoryStream is; |
| 159 |
is.wrap(data, bytes); |
| 160 |
status_t res = write_config(chunk_id, file, &is, buf_size); |
| 161 |
status_t res2 = is.close(); |
| 162 |
return (res != STATUS_OK) ? res : res2; |
| 163 |
} |
| 164 |
|
| 165 |
LSP_RUNTIME_LIB_PUBLIC |
| 166 |
status_t write_config(chunk_id_t *chunk_id, File *file, lspc::ChunkWriter **writer) |
| 167 |
{ |
| 168 |
if (writer == NULL) |
| 169 |
return STATUS_BAD_ARGUMENTS; |
| 170 |
|
| 171 |
// Create chunk and remember it's size |
| 172 |
status_t res; |
| 173 |
lspc::ChunkWriter *cw = file->write_chunk(LSPC_CHUNK_TEXT_CONFIG); |
| 174 |
if (cw == NULL) |
| 175 |
return STATUS_BAD_STATE; |
| 176 |
lsp_finally { |
| 177 |
if (cw != NULL) |
| 178 |
delete cw; |
| 179 |
}; |
| 180 |
|
| 181 |
// Form the chunk header |
| 182 |
chunk_text_config_t hdr; |
| 183 |
bzero(&hdr, sizeof(hdr)); |
| 184 |
hdr.common.size = sizeof(hdr); |
| 185 |
hdr.common.version = 0; |
| 186 |
|
| 187 |
// Write header of the chunk |
| 188 |
if ((res = cw->write_header(&hdr)) != STATUS_OK) |
| 189 |
return res; |
| 190 |
|
| 191 |
// Return result |
| 192 |
if (chunk_id != NULL) |
| 193 |
*chunk_id = cw->unique_id(); |
| 194 |
*writer = cw; |
| 195 |
cw = NULL; |
| 196 |
|
| 197 |
return STATUS_OK; |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
LSP_RUNTIME_LIB_PUBLIC |
| 202 |
status_t read_config(chunk_id_t chunk_id, File *file, const char *path, size_t buf_size) |
| 203 |
{ |
| 204 |
io::OutFileStream os; |
| 205 |
status_t res = os.open(path, io::File::FM_WRITE_NEW); |
| 206 |
if (res != STATUS_OK) |
| 207 |
return res; |
| 208 |
res = read_config(chunk_id, file, &os, buf_size); |
| 209 |
status_t res2 = os.close(); |
| 210 |
return (res != STATUS_OK) ? res : res2; |
| 211 |
} |
| 212 |
|
| 213 |
LSP_RUNTIME_LIB_PUBLIC |
| 214 |
status_t read_config(chunk_id_t chunk_id, File *file, const io::Path *path, size_t buf_size) |
| 215 |
{ |
| 216 |
io::OutFileStream os; |
| 217 |
status_t res = os.open(path, io::File::FM_WRITE_NEW); |
| 218 |
if (res != STATUS_OK) |
| 219 |
return res; |
| 220 |
res = read_config(chunk_id, file, &os, buf_size); |
| 221 |
status_t res2 = os.close(); |
| 222 |
return (res != STATUS_OK) ? res : res2; |
| 223 |
} |
| 224 |
|
| 225 |
LSP_RUNTIME_LIB_PUBLIC |
| 226 |
status_t read_config(chunk_id_t chunk_id, File *file, const LSPString *path, size_t buf_size) |
| 227 |
{ |
| 228 |
io::OutFileStream os; |
| 229 |
status_t res = os.open(path, io::File::FM_WRITE_NEW); |
| 230 |
if (res != STATUS_OK) |
| 231 |
return res; |
| 232 |
res = read_config(chunk_id, file, &os, buf_size); |
| 233 |
status_t res2 = os.close(); |
| 234 |
return (res != STATUS_OK) ? res : res2; |
| 235 |
} |
| 236 |
|
| 237 |
LSP_RUNTIME_LIB_PUBLIC |
| 238 |
status_t read_config_data(chunk_id_t chunk_id, File *file, char **data, size_t buf_size) |
| 239 |
{ |
| 240 |
// Validate arguments |
| 241 |
if (data == NULL) |
| 242 |
return STATUS_BAD_ARGUMENTS; |
| 243 |
|
| 244 |
// Read data |
| 245 |
io::OutMemoryStream os; |
| 246 |
status_t res = read_config(chunk_id, file, &os, buf_size); |
| 247 |
if (res == STATUS_OK) |
| 248 |
{ |
| 249 |
const ssize_t written = os.writeb('\0'); |
| 250 |
res = (written < 0) ? status_t(-written) : STATUS_OK; |
| 251 |
} |
| 252 |
|
| 253 |
res = update_status(res, os.close()); |
| 254 |
|
| 255 |
// Return result |
| 256 |
*data = reinterpret_cast<char *>(os.release()); |
| 257 |
return STATUS_OK; |
| 258 |
} |
| 259 |
|
| 260 |
LSP_RUNTIME_LIB_PUBLIC |
| 261 |
status_t read_config_data(chunk_id_t chunk_id, File *file, void **data, size_t *size, size_t buf_size) |
| 262 |
{ |
| 263 |
// Validate arguments |
| 264 |
if ((data == NULL) || (size == NULL)) |
| 265 |
return STATUS_BAD_ARGUMENTS; |
| 266 |
|
| 267 |
// Read data |
| 268 |
io::OutMemoryStream os; |
| 269 |
status_t res = read_config(chunk_id, file, &os, buf_size); |
| 270 |
status_t res2 = os.close(); |
| 271 |
if (res == STATUS_OK) |
| 272 |
res = res2; |
| 273 |
|
| 274 |
// Return result |
| 275 |
*size = os.size(); |
| 276 |
*data = os.release(); |
| 277 |
return STATUS_OK; |
| 278 |
} |
| 279 |
|
| 280 |
LSP_RUNTIME_LIB_PUBLIC |
| 281 |
status_t read_config_data(chunk_id_t chunk_id, File *file, LSPString *data, size_t buf_size) |
| 282 |
{ |
| 283 |
// Validate arguments |
| 284 |
if (data == NULL) |
| 285 |
return STATUS_BAD_ARGUMENTS; |
| 286 |
|
| 287 |
// Read data |
| 288 |
io::OutMemoryStream os; |
| 289 |
status_t res = read_config(chunk_id, file, &os, buf_size); |
| 290 |
status_t res2 = os.close(); |
| 291 |
if (res == STATUS_OK) |
| 292 |
res = res2; |
| 293 |
|
| 294 |
// Return result |
| 295 |
return (data->set_utf8(reinterpret_cast<const char *>(os.data()), os.size())) ? |
| 296 |
STATUS_OK : STATUS_NO_MEM; |
| 297 |
} |
| 298 |
|
| 299 |
LSP_RUNTIME_LIB_PUBLIC |
| 300 |
status_t read_config(chunk_id_t chunk_id, File *file, io::IOutStream *os, size_t buf_size) |
| 301 |
{ |
| 302 |
if ((file == NULL) || (os == NULL)) |
| 303 |
return STATUS_BAD_ARGUMENTS; |
| 304 |
|
| 305 |
// Open the reader |
| 306 |
ChunkReader *rd = file->read_chunk(chunk_id, LSPC_CHUNK_TEXT_CONFIG); |
| 307 |
if (rd == NULL) |
| 308 |
return STATUS_NOT_FOUND; |
| 309 |
lsp_finally { delete rd; }; |
| 310 |
|
| 311 |
// Read the header |
| 312 |
lspc::chunk_text_config_t hdr; |
| 313 |
ssize_t nread = rd->read_header(&hdr, sizeof(hdr)); |
| 314 |
if (nread < 0) |
| 315 |
return status_t(-nread); |
| 316 |
else if (nread != sizeof(hdr)) |
| 317 |
return STATUS_CORRUPTED; |
| 318 |
else if (hdr.common.version != 0) |
| 319 |
return STATUS_NOT_SUPPORTED; |
| 320 |
|
| 321 |
// Do the stuff |
| 322 |
wssize_t written = rd->stream()->sink(os, buf_size); |
| 323 |
status_t res = rd->close(); |
| 324 |
|
| 325 |
return (written < 0) ? status_t(-written) : res; |
| 326 |
} |
| 327 |
|
| 328 |
LSP_RUNTIME_LIB_PUBLIC |
| 329 |
status_t read_config(chunk_id_t chunk_id, File *file, io::IInStream **is) |
| 330 |
{ |
| 331 |
if ((file == NULL) || (is == NULL)) |
| 332 |
return STATUS_BAD_ARGUMENTS; |
| 333 |
|
| 334 |
// Open the reader |
| 335 |
ChunkReader *rd = file->read_chunk(chunk_id, LSPC_CHUNK_TEXT_CONFIG); |
| 336 |
if (rd == NULL) |
| 337 |
return STATUS_NOT_FOUND; |
| 338 |
lsp_finally { |
| 339 |
if (rd != NULL) |
| 340 |
delete rd; |
| 341 |
}; |
| 342 |
|
| 343 |
// Read the header |
| 344 |
lspc::chunk_text_config_t hdr; |
| 345 |
ssize_t nread = rd->read_header(&hdr, sizeof(hdr)); |
| 346 |
if (nread < 0) |
| 347 |
return status_t(-nread); |
| 348 |
else if (nread != sizeof(hdr)) |
| 349 |
return STATUS_CORRUPTED; |
| 350 |
else if (hdr.common.version != 0) |
| 351 |
return STATUS_NOT_SUPPORTED; |
| 352 |
|
| 353 |
// Wrap with the input stream |
| 354 |
ChunkReaderStream *cs = new ChunkReaderStream(rd, true); |
| 355 |
if (cs == NULL) |
| 356 |
return STATUS_NO_MEM; |
| 357 |
|
| 358 |
// Return he result |
| 359 |
*is = cs; |
| 360 |
rd = NULL; |
| 361 |
|
| 362 |
return STATUS_OK; |
| 363 |
} |
| 364 |
} /* namespace lspc */ |
| 365 |
} /* namespace lsp */ |
| 366 |
|
| 367 |
|
| 368 |
|