Debian Codegraph @

modules/lsp-runtime-lib/src/main/resource/OutProxyStream.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 мар. 2021 г.
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/resource/OutProxyStream.h>
23
24 namespace lsp
25 {
26 namespace resource
27 {
28 OutProxyStream::OutProxyStream()
29 {
30 pOS = NULL;
31 nPosition = 0;
32 nWrapFlags = 0;
33 }
34
35 OutProxyStream::~OutProxyStream()
36 {
37 // Close file descriptor
38 if (pOS != NULL)
39 {
40 pOS->flush();
41
42 if (nWrapFlags & WRAP_CLOSE)
43 pOS->close();
44 if (nWrapFlags & WRAP_DELETE)
45 delete pOS;
46 pOS = NULL;
47 }
48
49 nPosition = 0;
50 nWrapFlags = 0;
51 }
52
53
54 status_t OutProxyStream::close()
55 {
56 status_t res = STATUS_OK, tres;
57
58 // Close file descriptor
59 if (pOS != NULL)
60 {
61 // Flush buffers
62 res = flush();
63
64 // Perform close
65 if (nWrapFlags & WRAP_CLOSE)
66 {
67 tres = pOS->close();
68 if (res == STATUS_OK)
69 res = tres;
70 }
71 if (nWrapFlags & WRAP_DELETE)
72 delete pOS;
73 pOS = NULL;
74 }
75 nWrapFlags = 0;
76
77 // Return result
78 return set_error(res);
79 }
80
81 status_t OutProxyStream::wrap(io::IOutStream *os, size_t flags)
82 {
83 if (pOS != NULL)
84 return set_error(STATUS_BAD_STATE);
85
86 pOS = os;
87 nWrapFlags = flags;
88 nPosition = 0;
89
90 return set_error(STATUS_OK);
91 }
92
93 wssize_t OutProxyStream::position()
94 {
95 return (pOS != NULL) ? nPosition : -set_error(STATUS_CLOSED);
96 }
97
98 ssize_t OutProxyStream::write(const void *buf, size_t count)
99 {
100 if (pOS == NULL)
101 return -set_error(STATUS_CLOSED);
102
103 ssize_t res = pOS->write(buf, count);
104 if (res >= 0)
105 {
106 nPosition += res;
107 set_error(STATUS_OK);
108 return res;
109 }
110
111 set_error(status_t(-res));
112 return res;
113 }
114
115 wssize_t OutProxyStream::seek(wsize_t position)
116 {
117 if (pOS == NULL)
118 return -set_error(STATUS_CLOSED);
119
120 wssize_t pos = pOS->seek(position);
121 if (pos >= 0)
122 {
123 nPosition = pos;
124 set_error(STATUS_OK);
125 }
126 else
127 set_error(status_t(-pos));
128
129 return pos;
130 }
131
132 status_t OutProxyStream::flush()
133 {
134 return (pOS != NULL) ? pOS->flush() : set_error(STATUS_CLOSED);
135 }
136
137 } /* namespace resource */
138 } /* namespace lsp */
139
140