Debian Codegraph @

modules/lsp-runtime-lib/src/main/io/InMemoryStream.cpp

raw
1 /*
2 * Copyright (C) 2020 Linux Studio Plugins Project <https://lsp-plug.in/>
3 * (C) 2020 Vladimir Sadovnikov <[email protected]>
4 *
5 * This file is part of lsp-runtime-lib
6 * Created on: 21 авг. 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/io/InMemoryStream.h>
23 #include <lsp-plug.in/stdlib/string.h>
24 #include <stdlib.h>
25
26 namespace lsp
27 {
28 namespace io
29 {
30
31 InMemoryStream::InMemoryStream()
32 {
33 pData = NULL;
34 nOffset = 0;
35 nSize = 0;
36 enDrop = MEMDROP_NONE;
37 }
38
39 InMemoryStream::InMemoryStream(void *data, size_t size, lsp_memdrop_t drop)
40 {
41 pData = reinterpret_cast<uint8_t *>(data);
42 nOffset = 0;
43 nSize = size;
44 enDrop = drop;
45 }
46
47 InMemoryStream::InMemoryStream(const void *data, size_t size)
48 {
49 pData = reinterpret_cast<uint8_t *>(const_cast<void *>(data));
50 nOffset = 0;
51 nSize = size;
52 enDrop = MEMDROP_NONE;
53 }
54
55 InMemoryStream::~InMemoryStream()
56 {
57 drop();
58 }
59
60 void InMemoryStream::wrap(void *data, size_t size, lsp_memdrop_t drop)
61 {
62 InMemoryStream::drop();
63 pData = reinterpret_cast<uint8_t *>(data);
64 nOffset = 0;
65 nSize = size;
66 enDrop = drop;
67 }
68
69 void InMemoryStream::wrap(const void *data, size_t size)
70 {
71 InMemoryStream::drop();
72 pData = reinterpret_cast<uint8_t *>(const_cast<void *>(data));
73 nOffset = 0;
74 nSize = size;
75 enDrop = MEMDROP_NONE;
76 }
77
78 bool InMemoryStream::drop(lsp_memdrop_t drop)
79 {
80 if (pData == NULL)
81 return false;
82 switch (drop)
83 {
84 case MEMDROP_FREE: ::free(pData); break;
85 case MEMDROP_DELETE: delete pData; break;
86 case MEMDROP_ARR_DELETE: delete [] pData; break;
87 default: break;
88 }
89
90 pData = NULL;
91 nOffset = 0;
92 nSize = 0;
93 enDrop = MEMDROP_NONE;
94
95 return true;
96 }
97
98 wssize_t InMemoryStream::avail()
99 {
100 if (pData == NULL)
101 return -set_error(STATUS_NO_DATA);
102 return nSize - nOffset;
103 }
104
105 wssize_t InMemoryStream::position()
106 {
107 if (pData == NULL)
108 return -set_error(STATUS_NO_DATA);
109 return nOffset;
110 }
111
112 ssize_t InMemoryStream::read(void *dst, size_t count)
113 {
114 if (pData == NULL)
115 return -set_error(STATUS_NO_DATA);
116
117 size_t avail = nSize - nOffset;
118 if (count > avail)
119 count = avail;
120 if (count <= 0)
121 return -set_error(STATUS_EOF);
122
123 ::memcpy(dst, &pData[nOffset], count);
124 nOffset += count;
125 return count;
126 }
127
128 ssize_t InMemoryStream::read_byte()
129 {
130 if (pData == NULL)
131 return -set_error(STATUS_NO_DATA);
132 return (nOffset < nSize) ? pData[nOffset++] : -STATUS_EOF;
133 }
134
135 wssize_t InMemoryStream::seek(wsize_t position)
136 {
137 if (pData == NULL)
138 return -set_error(STATUS_NO_DATA);
139 if (position > nSize)
140 position = nSize;
141
142 return nOffset = position;
143 }
144
145 wssize_t InMemoryStream::skip(wsize_t amount)
146 {
147 if (pData == NULL)
148 return -set_error(STATUS_NO_DATA);
149 size_t avail = nSize - nOffset;
150 if (avail > amount)
151 avail = amount;
152 nOffset += avail;
153 return avail;
154 }
155
156 status_t InMemoryStream::close()
157 {
158 drop(enDrop);
159 return STATUS_OK;
160 }
161
162 void InMemoryStream::take(OutMemoryStream &src)
163 {
164 drop(enDrop);
165
166 pData = const_cast<uint8_t *>(src.data());
167 nOffset = 0;
168 nSize = src.size();
169 enDrop = (pData != NULL) ? MEMDROP_FREE : MEMDROP_NONE;
170
171 src.release();
172 }
173
174 void InMemoryStream::take(OutMemoryStream *src)
175 {
176 drop(enDrop);
177
178 pData = const_cast<uint8_t *>(src->data());
179 nOffset = 0;
180 nSize = src->size();
181 enDrop = (pData != NULL) ? MEMDROP_FREE : MEMDROP_NONE;
182
183 src->release();
184 }
185
186 } /* namespace io */
187 } /* namespace lsp */