Debian Codegraph @

modules/lsp-runtime-lib/src/main/fmt/lspc/ChunkWriterStream.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: 20 окт. 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/fmt/lspc/ChunkWriterStream.h>
23
24 namespace lsp
25 {
26 namespace lspc
27 {
28 ChunkWriterStream::ChunkWriterStream(ChunkWriter *writer, bool free)
29 {
30 pWriter = writer;
31 bDelete = free;
32 }
33
34 ChunkWriterStream::~ChunkWriterStream()
35 {
36 do_close();
37 }
38
39 void ChunkWriterStream::do_close()
40 {
41 if (pWriter == NULL)
42 return;
43 if (bDelete)
44 delete pWriter;
45 pWriter = NULL;
46 }
47
48 wssize_t ChunkWriterStream::position()
49 {
50 if (pWriter == NULL)
51 return -set_error(STATUS_CLOSED);
52 wssize_t res = pWriter->position();
53 if (res >= 0)
54 {
55 set_error(STATUS_OK);
56 return res;
57 }
58 return -set_error(status_t(-res));
59 }
60
61 ssize_t ChunkWriterStream::write(const void *buf, size_t count)
62 {
63 if (pWriter == NULL)
64 return -set_error(STATUS_CLOSED);
65 status_t res = pWriter->write(buf, count);
66 if (res != STATUS_OK)
67 return -set_error(res);
68 set_error(STATUS_OK);
69 return count;
70 }
71
72 ssize_t ChunkWriterStream::writeb(int v)
73 {
74 if (pWriter == NULL)
75 return -set_error(STATUS_CLOSED);
76 uint8_t buf = v;
77 status_t res = pWriter->write(&buf, sizeof(buf));
78 if (res != STATUS_OK)
79 return -set_error(res);
80 set_error(STATUS_OK);
81 return sizeof(buf);
82 }
83
84 status_t ChunkWriterStream::flush()
85 {
86 if (pWriter == NULL)
87 return -set_error(STATUS_CLOSED);
88 return set_error(pWriter->flush());
89 }
90
91 status_t ChunkWriterStream::close()
92 {
93 do_close();
94 return set_error(STATUS_OK);
95 }
96
97 } /* namespace lspc */
98 } /* namespace lsp */