Debian Codegraph @

modules/lsp-runtime-lib/src/main/fmt/RoomEQWizard.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: 6 сент. 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/common/alloc.h>
23 #include <lsp-plug.in/common/endian.h>
24 #include <lsp-plug.in/common/debug.h>
25 #include <lsp-plug.in/stdlib/math.h>
26 #include <lsp-plug.in/io/InFileStream.h>
27 #include <lsp-plug.in/io/OutMemoryStream.h>
28 #include <lsp-plug.in/io/InMemoryStream.h>
29 #include <lsp-plug.in/io/InSequence.h>
30
31 #include <lsp-plug.in/fmt/RoomEQWizard.h>
32 #include <lsp-plug.in/fmt/java/ObjectStream.h>
33
34
35 #include <lsp-plug.in/lltl/darray.h>
36
37 namespace lsp
38 {
39 namespace room_ew
40 {
41 const char *charsets[] =
42 {
43 "UTF-8",
44 "UTF-16LE",
45 "UTF-16BE",
46 NULL
47 };
48
49 config_t *build_config(const LSPString *eq, const LSPString *notes,
50 int32_t major, int32_t minor, size_t filters)
51 {
52 const char *peq = eq->get_utf8();
53 if (peq == NULL)
54 return NULL;
55 const char *pnotes = notes->get_utf8();
56 if (pnotes == NULL)
57 return NULL;
58
59 size_t n_hdr = ::strlen(peq) + 1;
60 size_t n_notes = ::strlen(pnotes) + 1;
61 size_t n_strings = align_size(n_hdr + n_notes, DEFAULT_ALIGN);
62 size_t n_cfg = align_size(sizeof(config_t), DEFAULT_ALIGN);
63 size_t n_filters = align_size(sizeof(filter_t) * filters, DEFAULT_ALIGN);
64 size_t to_alloc = n_strings + n_cfg + n_filters;
65
66 uint8_t *ptr = reinterpret_cast<uint8_t *>(::malloc(to_alloc));
67 if (ptr == NULL)
68 return NULL;
69 ::bzero(ptr, to_alloc);
70
71 config_t *cfg = reinterpret_cast<config_t *>(ptr);
72 ptr += n_cfg;
73
74 char *xeq = reinterpret_cast<char *>(ptr);
75 char *xnotes = &xeq[n_hdr];
76 ptr += n_strings;
77 ::memcpy(xeq, peq, n_hdr);
78 ::memcpy(xnotes, pnotes, n_notes);
79
80 cfg->vFilters = reinterpret_cast<filter_t *>(ptr);
81 ptr += n_filters;
82
83 // Fill the basic configuration
84 cfg->nVerMaj = major;
85 cfg->nVerMin = minor;
86 cfg->sEqType = xeq;
87 cfg->sNotes = xnotes;
88 cfg->fPreamp = 0.0;
89 cfg->nFilters = filters;
90
91 return cfg;
92 }
93
94 filter_type_t decode_filter_type(const char *type)
95 {
96 #define DECODE(text, ftype) \
97 if (!::strcasecmp(type, text)) return ftype;
98
99 DECODE("PK", PK);
100 DECODE("MODAL", MODAL);
101 DECODE("LP", LP);
102 DECODE("HP", HP);
103 DECODE("LPQ", LPQ);
104 DECODE("HPQ", HPQ);
105 DECODE("LS", LS);
106 DECODE("LSC", LSC);
107 DECODE("HS", HS);
108 DECODE("HSC", HSC);
109 DECODE("LS6", LS6);
110 DECODE("HS6", HS6);
111 DECODE("LS12", LS12);
112 DECODE("HS12", HS12);
113 DECODE("NO", NO);
114 DECODE("AP", AP);
115 DECODE("BP", BP);
116
117 #undef DECODE
118
119 return NONE;
120 }
121
122 status_t load_object_stream(java::ObjectStream *os, config_t **dst)
123 {
124 status_t res;
125 LSPString eq, notes, xeq;
126 java::RawArray *filters;
127 java::int_t major=0, minor=0, stub=0;
128
129 // Read the REW data in Java serialization format
130 if ((res = os->read_string(&eq)) != STATUS_OK)
131 return res;
132 if (!xeq.set_ascii("Equaliser:"))
133 return STATUS_NO_MEM;
134 ssize_t idx = eq.index_of(&xeq);
135 if (idx >= 0)
136 eq.remove(0, idx + xeq.length());
137 lsp_trace("equalizer: %s", eq.get_utf8());
138 if ((res = os->read_int(&major)) != STATUS_OK)
139 return res;
140 if ((res = os->read_int(&minor)) != STATUS_OK)
141 return res;
142 lsp_trace("version: %d.%d", int(major), int(minor));
143 if ((res = os->read_string(&notes)) != STATUS_OK)
144 return res;
145 if (notes.starts_with_ascii("Notes:"))
146 notes.remove(0, 6);
147 lsp_trace("notes: %s", notes.get_utf8());
148
149 if ((res = os->read_int(&stub)) != STATUS_OK) // Don't know what this number actually means currently
150 return res;
151 lsp_trace("stub: %d", int(stub));
152 if ((res = os->read_array(&filters)) != STATUS_OK)
153 return res;
154
155 // Now we are ready to allocate the data
156 config_t *cfg = build_config(&eq, &notes, major, minor, filters->length());
157 if (cfg == NULL)
158 return STATUS_NO_MEM;
159
160 // Read the entire data
161 const java::Object **vsf = filters->get_objects();
162 filter_t *vdf = cfg->vFilters;
163
164 for (size_t i=0, n=filters->length(); i<n; ++i)
165 {
166 const java::Object *sf = vsf[i];
167 filter_t *df = &vdf[i];
168
169 java::double_t Q, fc, gain;
170 java::bool_t enabled;
171 const char *ftype;
172
173 // Read the filter parameters
174 if ((res = sf->get_double("Q", &Q)) != STATUS_OK)
175 break;
176 if ((res = sf->get_double("fc", &fc)) != STATUS_OK)
177 break;
178 if ((res = sf->get_double("gain", &gain)) != STATUS_OK)
179 break;
180 if ((res = sf->get_bool("enabled", &enabled)) != STATUS_OK)
181 break;
182 if ((res = sf->get_enum("filterType", &ftype)) != STATUS_OK)
183 break;
184
185 lsp_trace(
186 "Filter %d: %s %s Fc %.0f Hz Gain %.1f dB Q %.7f",
187 int(i+1), (enabled) ? "ON" : "OFF", ftype,
188 double(fc), double(gain), double(Q)
189 );
190
191 // lsp_trace(
192 // "check_filter(&vf[idx++], %s, room_ew::%s, %.2f, %.2f, %.7f);",
193 // (enabled) ? "true" : "false", ftype,
194 // double(fc), double(gain), double(Q)
195 // );
196
197 // Fill the state of filter
198 df->Q = Q;
199 df->fc = fc;
200 df->gain = gain;
201 df->enabled = enabled;
202 df->filterType = decode_filter_type(ftype);
203 }
204
205 if ((res != STATUS_OK) || (dst == NULL))
206 ::free(cfg);
207 else if (res == STATUS_OK)
208 *dst = cfg;
209
210 return STATUS_OK;
211 }
212
213 status_t load_java(io::IInStream *is, config_t **dst)
214 {
215 // Open file
216 java::Handles handles;
217 java::ObjectStream os(&handles);
218
219 status_t res = os.wrap(is, WRAP_NONE);
220 if (res == STATUS_OK)
221 res = load_object_stream(&os, dst);
222
223 if (res == STATUS_OK)
224 return os.close();
225
226 os.close();
227 return res;
228 }
229
230 status_t skip_whitespace(const LSPString *s, size_t *offset)
231 {
232 size_t len = s->length();
233 while (*offset < len)
234 {
235 switch (s->char_at(*offset))
236 {
237 case ' ':
238 case '\n':
239 case '\t':
240 case '\r':
241 ++(*offset);
242 break;
243 default:
244 return STATUS_OK;
245 }
246 }
247
248 return STATUS_OK;
249 }
250
251 status_t skip_data(const LSPString *s, size_t *offset)
252 {
253 size_t len = s->length();
254 while (*offset < len)
255 {
256 switch (s->char_at(*offset))
257 {
258 case ' ':
259 case '\n':
260 case '\t':
261 case '\r':
262 return STATUS_OK;
263 default:
264 ++(*offset);
265 break;
266 }
267 }
268
269 return STATUS_OK;
270 }
271
272 status_t parse_decimal(ssize_t *dst, const LSPString *s, size_t *offset)
273 {
274 status_t res = skip_whitespace(s, offset);
275 if (res != STATUS_OK)
276 return res;
277
278 ssize_t num = 0, n=0;
279 size_t len = s->length();
280 for ( ; *offset < len; ++(*offset), ++n)
281 {
282 lsp_wchar_t wc = s->char_at(*offset);
283 if ((wc >= '0') && (wc <= '9'))
284 num = num * 10 + (wc - '0');
285 else
286 break;
287 }
288 if (n <= 0)
289 return STATUS_BAD_FORMAT;
290
291 *dst = num;
292 return STATUS_OK;
293 }
294
295 status_t parse_double(double *dst, const LSPString *s, size_t *offset)
296 {
297 status_t res = skip_whitespace(s, offset);
298 lsp_wchar_t wc;
299 if (res != STATUS_OK)
300 return res;
301
302 lsp_trace("s = %s", s->get_utf8(*offset));
303
304 size_t len = s->length();
305
306 // Check sign
307 bool negative = false;
308 ssize_t is = 0;
309 if (*offset < len)
310 {
311 wc = s->char_at(*offset);
312 if (wc == '+')
313 {
314 ++is;
315 ++(*offset);
316 }
317 else if (wc == '-')
318 {
319 ++is;
320 ++(*offset);
321 negative = true;
322 }
323 }
324
325 // Parse integer part
326 double num = 0;
327 ssize_t in = 0;
328 for ( ; *offset < len; ++(*offset), ++in)
329 {
330 wc = s->char_at(*offset);
331 if ((wc >= '0') && (wc <= '9'))
332 num = num * 10 + (wc - '0');
333 else
334 break;
335 }
336
337 // No fraction part?
338 if ((*offset) >= len)
339 {
340 if (in <= 0)
341 return STATUS_BAD_FORMAT;
342 *dst = num;
343 return STATUS_OK;
344 }
345 else
346 {
347 wc = s->char_at(*offset);
348 if ((wc != '.') && (wc != ','))
349 {
350 if (in <= 0)
351 return STATUS_BAD_FORMAT;
352 *dst = num;
353 return STATUS_OK;
354 }
355
356 ++(*offset);
357 }
358
359 // Parse fraction part
360 double fnum = 0.1;
361 ssize_t fn=0;
362 for ( ; *offset < len; ++(*offset), ++fn)
363 {
364 wc = s->char_at(*offset);
365 if ((wc >= '0') && (wc <= '9'))
366 {
367 num += (wc - '0') * fnum;
368 fnum *= 0.1;
369 }
370 else
371 break;
372 }
373
374 // No integer and no fraction parts?
375 if ((in <= 0) && (fn <= 0))
376 {
377 --(*offset);
378 if (is > 0)
379 --(*offset);
380 return STATUS_BAD_FORMAT;
381 }
382
383 // Return the result
384 *dst = (negative) ? -num : num;
385 return STATUS_OK;
386 }
387
388 status_t parse_filter_settings(filter_t *f, const LSPString *s, size_t *offset)
389 {
390 status_t res;
391 if ((res = skip_whitespace(s, offset)) != STATUS_OK)
392 return res;
393 LSPString tmp;
394
395 /**
396 * Filter 6: ON None
397 * Filter 7: ON PK Fc 100 Hz Gain 0.0 dB Q 10.000
398 * Filter 8: ON Modal Fc 100 Hz Gain 0.0 dB Q 13.643 T60 target 300 ms
399 */
400 // On/Off
401 if (s->starts_with_ascii_nocase("on ", *offset))
402 {
403 *offset += 3;
404 f->enabled = true;
405 }
406 else if (s->starts_with_ascii_nocase("off ", *offset))
407 {
408 *offset += 4;
409 f->enabled = false;
410 }
411 else
412 return STATUS_BAD_FORMAT;
413
414 if ((res = skip_whitespace(s, offset)) != STATUS_OK)
415 return res;
416
417 // Filter type
418 if (s->starts_with_ascii_nocase("none ", *offset))
419 {
420 f->filterType = NONE;
421 *offset += 5;
422 }
423 else if (s->starts_with_ascii_nocase("modal ", *offset))
424 {
425 f->filterType = MODAL;
426 *offset += 6;
427 }
428 else if (s->starts_with_ascii_nocase("pk ", *offset))
429 {
430 f->filterType = PK;
431 *offset += 3;
432 }
433 else if (s->starts_with_ascii_nocase("lp ", *offset))
434 {
435 f->filterType = LP;
436 *offset += 3;
437 }
438 else if (s->starts_with_ascii_nocase("hp ", *offset))
439 {
440 f->filterType = HP;
441 *offset += 3;
442 }
443 else if (s->starts_with_ascii_nocase("lpq ", *offset))
444 {
445 f->filterType = LPQ;
446 *offset += 4;
447 }
448 else if (s->starts_with_ascii_nocase("hpq ", *offset))
449 {
450 f->filterType = HPQ;
451 *offset += 4;
452 }
453 else if (s->starts_with_ascii_nocase("ls 6dB ", *offset))
454 {
455 f->filterType = LS6;
456 *offset += 7;
457 }
458 else if (s->starts_with_ascii_nocase("ls 12dB ", *offset))
459 {
460 f->filterType = LS12;
461 *offset += 8;
462 }
463 else if (s->starts_with_ascii_nocase("ls ", *offset))
464 {
465 f->filterType = LS;
466 *offset += 3;
467 }
468 else if (s->starts_with_ascii_nocase("lsc ", *offset))
469 {
470 f->filterType = LSC;
471 *offset += 4;
472 }
473 else if (s->starts_with_ascii_nocase("hs 6dB ", *offset))
474 {
475 f->filterType = HS6;
476 *offset += 7;
477 }
478 else if (s->starts_with_ascii_nocase("hs 12dB ", *offset))
479 {
480 f->filterType = HS12;
481 *offset += 8;
482 }
483 else if (s->starts_with_ascii_nocase("hs ", *offset))
484 {
485 f->filterType = HS;
486 *offset += 3;
487 }
488 else if (s->starts_with_ascii_nocase("hsc ", *offset))
489 {
490 f->filterType = HSC;
491 *offset += 4;
492 }
493 else if (s->starts_with_ascii_nocase("no ", *offset))
494 {
495 f->filterType = NO;
496 *offset += 3;
497 }
498 else if (s->starts_with_ascii_nocase("ap ", *offset))
499 {
500 f->filterType = AP;
501 *offset += 3;
502 }
503 else if (s->starts_with_ascii_nocase("bp ", *offset))
504 {
505 f->filterType = BP;
506 *offset += 3;
507 }
508 else
509 return STATUS_BAD_FORMAT;
510
511 // Other parameters
512 f->Q = 1.0;
513 f->fc = 100.0;
514 f->gain = 0.0;
515
516 if ((f->filterType == LP) || (f->filterType == HP) || (f->filterType == BP))
517 f->Q = M_SQRT1_2;
518
519 if ((res = skip_whitespace(s, offset)) != STATUS_OK)
520 return res;
521
522 // Scan the rest of line for optional parameters
523 size_t len = s->length();
524 lsp_trace("offset = %d, len = %d", int(*offset), int(len));
525
526 while (*offset < len)
527 {
528 lsp_trace("analyzing: %s", s->get_utf8(*offset));
529 if (s->starts_with_ascii_nocase("fc ", *offset))
530 {
531 *offset += 3;
532 if ((res = parse_double(&f->fc, s, offset)) != STATUS_OK)
533 return res;
534 if (f->fc < 0)
535 return STATUS_BAD_FORMAT;
536
537 if ((res = skip_whitespace(s, offset)) != STATUS_OK)
538 return res;
539
540 if (s->starts_with_ascii_nocase("hz ", *offset))
541 *offset += 3;
542 else if (s->starts_with_ascii_nocase("h z ", *offset))
543 *offset += 4;
544 else
545 return STATUS_BAD_FORMAT;
546 }
547 else if (s->starts_with_ascii_nocase("gain ", *offset))
548 {
549 *offset += 5;
550 if ((res = parse_double(&f->gain, s, offset)) != STATUS_OK)
551 return res;
552
553 if ((res = skip_whitespace(s, offset)) != STATUS_OK)
554 return res;
555 if (!s->starts_with_ascii_nocase("db ", *offset))
556 return STATUS_BAD_FORMAT;
557 *offset += 3;
558 }
559 else if (s->starts_with_ascii_nocase("q ", *offset))
560 {
561 *offset += 2;
562 if ((res = parse_double(&f->Q, s, offset)) != STATUS_OK)
563 return res;
564 }
565 else
566 {
567 if ((res = skip_data(s, offset)) != STATUS_OK)
568 return res;
569 }
570
571 if ((res = skip_whitespace(s, offset)) != STATUS_OK)
572 return res;
573 }
574
575 return STATUS_OK;
576 }
577
578 status_t parse_text_config(io::IInSequence *is, config_t **dst)
579 {
580 LSPString s;
581 status_t res;
582
583 // Read lines
584 LSPString notes, eq;
585 ssize_t major=0, minor=0;
586 size_t offset = 0;
587 lltl::darray<filter_t> vfilters;
588 double preamp = 0.0;
589
590 while ((res = is->read_line(&s, true)) == STATUS_OK)
591 {
592 lsp_trace("Parsing line: %s", s.get_utf8());
593 if (s.starts_with_ascii("Room EQ V"))
594 {
595 offset = 9;
596 if ((res = parse_decimal(&major, &s, &offset)) != STATUS_OK)
597 return res;
598 if (offset < s.length())
599 {
600 lsp_wchar_t wc = s.char_at(offset);
601 if ((wc != '.') && (wc != ','))
602 return STATUS_BAD_FORMAT;
603 ++offset;
604 }
605 else
606 return STATUS_BAD_FORMAT;
607
608
609 if ((res = parse_decimal(&minor, &s, &offset)) != STATUS_OK)
610 return res;
611 }
612 else if (s.starts_with_ascii("Notes:"))
613 {
614 if (!notes.set(&s, 6))
615 return STATUS_NO_MEM;
616 }
617 else if ((s.starts_with_ascii("Equaliser:")) || (s.starts_with_ascii("Equalizer:")))
618 {
619 offset = 10;
620 if ((res = skip_whitespace(&s, &offset)) != STATUS_OK)
621 return res;
622 if (!eq.set(&s, offset))
623 return STATUS_NO_MEM;
624 }
625 else if (s.starts_with_ascii("Preamp:"))
626 {
627 offset = 7;
628 if ((res = skip_whitespace(&s, &offset)) != STATUS_OK)
629 return res;
630 if ((res = parse_double(&preamp, &s, &offset)) != STATUS_OK)
631 return res;
632 if ((res = skip_whitespace(&s, &offset)) != STATUS_OK)
633 return res;
634 if (s.starts_with_ascii("dB", offset))
635 offset += 2;
636 else
637 preamp = exp((M_LN10 * 0.05) * preamp);
638 if ((res = skip_whitespace(&s, &offset)) != STATUS_OK)
639 return res;
640 }
641 else if (s.starts_with_ascii("Filter Settings file"))
642 {
643 // Nothing
644 }
645 else if (s.starts_with_ascii("Filter"))
646 {
647 offset = 6;
648 if (!s.append(' ')) // For easier parsing, we add a whitespace at the end
649 return STATUS_NO_MEM;
650
651 // Check syntax
652 lsp_wchar_t ch = s.char_at(offset++);
653 if (ch == ' ')
654 {
655 // Find filter definition
656 size_t len = s.length();
657 while (offset < len)
658 if (s.char_at(offset++) == ':')
659 break;
660 }
661 else if (ch != ':')
662 continue;
663
664 // Allocate filter
665 filter_t *f = vfilters.add();
666 if (f == NULL)
667 return STATUS_NO_MEM;
668
669 // Parse filter settings
670 if ((res = parse_filter_settings(f, &s, &offset)) != STATUS_OK)
671 return res;
672
673 lsp_trace(
674 "Filter %d: %s [%d] Fc %.0f Hz Gain %.1f dB Q %.7f",
675 int(vfilters.size()), (f->enabled) ? "ON" : "OFF", f->filterType,
676 double(f->fc), double(f->gain), double(f->Q)
677 );
678 }
679 }
680
681 // Analyze current status
682 if (res == STATUS_EOF)
683 res = STATUS_OK;
684 else if (res != STATUS_OK)
685 return res;
686
687 // Now we are ready to allocate the data
688 config_t *cfg = build_config(&eq, &notes, int32_t(major), int32_t(minor), vfilters.size());
689 if (cfg == NULL)
690 return STATUS_NO_MEM;
691
692 // Copy filter data
693 cfg->fPreamp = preamp;
694 ::memcpy(cfg->vFilters, vfilters.array(), sizeof(filter_t) * vfilters.size());
695
696 // Store the result
697 if (dst != NULL)
698 *dst = cfg;
699 else
700 ::free(cfg);
701
702 return STATUS_OK;
703 }
704
705 status_t load_text_file(io::IInStream *is, config_t **dst, const char *charset)
706 {
707 io::InSequence cs;
708 status_t res = cs.wrap(is, WRAP_NONE, charset);
709 if (res != STATUS_OK)
710 {
711 cs.close();
712 return res;
713 }
714
715 res = parse_text_config(&cs, dst);
716 if (res == STATUS_OK)
717 return cs.close();
718
719 cs.close();
720 return res;
721 }
722
723 status_t load_text(io::IInStream *is, config_t **dst)
724 {
725 // Read UTF-16 signature (if present)
726 uint16_t signature;
727 status_t res = is->read_block(&signature, sizeof(signature));
728 if (res != STATUS_OK)
729 return (res == STATUS_EOF) ? STATUS_BAD_FORMAT : res;
730
731 signature = BE_TO_CPU(signature);
732 if (signature == 0xfeff) // UTF-16BE
733 {
734 if ((res = load_text_file(is, dst, "UTF-16BE")) == STATUS_OK)
735 return res;
736 }
737 else if (signature == 0xfffe) // UTF-16LE
738 {
739 if ((res = load_text_file(is, dst, "UTF-16LE")) == STATUS_OK)
740 return res;
741 }
742
743 // Try to load unicode character sets
744 for (const char **cset=charsets; *cset != NULL; ++cset)
745 {
746 const wssize_t position = is->seek(0);
747 if (position < 0)
748 return status_t(-res);
749 else if (position != 0)
750 return STATUS_IO_ERROR;
751 if ((res = load_text_file(is, dst, *cset)) == STATUS_OK)
752 return res;
753 }
754
755 // Try default system charset
756 const wssize_t position = is->seek(0);
757 if (position < 0)
758 return status_t(-res);
759 else if (position != 0)
760 return STATUS_IO_ERROR;
761 return load_text_file(is, dst, NULL);
762 }
763
764
765 status_t load(const char *path, config_t **dst)
766 {
767 if (path == NULL)
768 return STATUS_BAD_ARGUMENTS;
769
770 LSPString tmp;
771 if (!tmp.set_utf8(path))
772 return STATUS_NO_MEM;
773
774 return load(&tmp, dst);
775 }
776
777 status_t load(const LSPString *path, config_t **dst)
778 {
779 if (path == NULL)
780 return STATUS_BAD_ARGUMENTS;
781
782 io::InFileStream ifs;
783 status_t res = ifs.open(path);
784 if (res != STATUS_OK)
785 {
786 ifs.close();
787 return res;
788 }
789
790 res = load(&ifs, dst);
791 if (res != STATUS_OK)
792 {
793 ifs.close();
794 return res;
795 }
796
797 return ifs.close();
798 }
799
800 status_t load(const io::Path *path, config_t **dst)
801 {
802 if (path == NULL)
803 return STATUS_BAD_ARGUMENTS;
804 return load(path->as_string(), dst);
805 }
806
807 status_t load(FILE *fd, config_t **dst)
808 {
809 if (fd == NULL)
810 return STATUS_BAD_ARGUMENTS;
811
812 io::InFileStream ifs;
813 status_t res = ifs.wrap(fd, false);
814 if (res != STATUS_OK)
815 {
816 ifs.close();
817 return res;
818 }
819 res = load(&ifs, dst);
820 if (res != STATUS_OK)
821 {
822 ifs.close();
823 return res;
824 }
825
826 return ifs.close();
827 }
828
829 status_t load(io::File *fd, config_t **dst)
830 {
831 if (fd == NULL)
832 return STATUS_BAD_ARGUMENTS;
833
834 io::InFileStream ifs;
835 status_t res = ifs.wrap(fd, false);
836 if (res != STATUS_OK)
837 {
838 ifs.close();
839 return res;
840 }
841 res = load(&ifs, dst);
842 if (res != STATUS_OK)
843 {
844 ifs.close();
845 return res;
846 }
847
848 return ifs.close();
849 }
850
851 status_t load(io::IInStream *is, config_t **dst)
852 {
853 if (is == NULL)
854 return STATUS_BAD_ARGUMENTS;
855
856 // We need to sink file to memory to detect the file format (java or text)
857 io::OutMemoryStream os;
858 wssize_t bytes = is->sink(&os);
859 if (bytes < 0)
860 {
861 os.close();
862 return status_t(-bytes);
863 }
864
865 // Load the file
866 status_t res = load(os.data(), os.size(), dst);
867 if (res != STATUS_OK)
868 {
869 os.close();
870 return res;
871 }
872
873 return os.close();
874 }
875
876
877 status_t load(const void *data, size_t size, config_t **dst)
878 {
879 if ((data == NULL) || (dst == NULL))
880 return STATUS_BAD_ARGUMENTS;
881
882 io::InMemoryStream is;
883 is.wrap(data, size);
884
885 // Try to load java format
886 status_t res = load_java(&is, dst);
887 if (res == STATUS_OK)
888 return is.close();
889 else if (res != STATUS_BAD_FORMAT)
890 {
891 is.close();
892 return res;
893 }
894
895 // Try to load plain text format
896 is.seek(0);
897 res = load_text(&is, dst);
898 if (res == STATUS_OK)
899 return is.close();
900
901 is.close();
902 return res;
903 }
904 } /* namespace room_ew */
905 } /* namespace lsp */
906
907