| 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: 14 нояб. 2017 г. |
| 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/IInStream.h> |
| 23 |
#include <lsp-plug.in/io/IOutStream.h> |
| 24 |
#include <lsp-plug.in/common/status.h> |
| 25 |
|
| 26 |
#include <stdlib.h> |
| 27 |
|
| 28 |
namespace lsp |
| 29 |
{ |
| 30 |
namespace io |
| 31 |
{ |
| 32 |
|
| 33 |
IInStream::IInStream() |
| 34 |
{ |
| 35 |
nErrorCode = STATUS_OK; |
| 36 |
} |
| 37 |
|
| 38 |
IInStream::~IInStream() |
| 39 |
{ |
| 40 |
nErrorCode = STATUS_OK; |
| 41 |
} |
| 42 |
|
| 43 |
wssize_t IInStream::avail() |
| 44 |
{ |
| 45 |
return - set_error(STATUS_NOT_IMPLEMENTED); |
| 46 |
} |
| 47 |
|
| 48 |
wssize_t IInStream::position() |
| 49 |
{ |
| 50 |
return - set_error(STATUS_NOT_IMPLEMENTED); |
| 51 |
} |
| 52 |
|
| 53 |
ssize_t IInStream::read(void *dst, size_t count) |
| 54 |
{ |
| 55 |
return - set_error(STATUS_NOT_IMPLEMENTED); |
| 56 |
} |
| 57 |
|
| 58 |
ssize_t IInStream::read_byte() |
| 59 |
{ |
| 60 |
uint8_t byte; |
| 61 |
ssize_t nread = read(&byte, sizeof(byte)); |
| 62 |
if (nread > 0) |
| 63 |
return byte; |
| 64 |
return (nread != 0) ? nread : -STATUS_EOF; |
| 65 |
} |
| 66 |
|
| 67 |
ssize_t IInStream::read_fully(void *dst, size_t count) |
| 68 |
{ |
| 69 |
uint8_t *ptr = reinterpret_cast<uint8_t *>(dst); |
| 70 |
size_t left = count; |
| 71 |
while (left > 0) |
| 72 |
{ |
| 73 |
ssize_t act_read = read(ptr, left); |
| 74 |
if (act_read < 0) |
| 75 |
{ |
| 76 |
if (left > count) |
| 77 |
break; |
| 78 |
else |
| 79 |
return act_read; |
| 80 |
} |
| 81 |
|
| 82 |
left -= act_read; |
| 83 |
ptr += act_read; |
| 84 |
} |
| 85 |
|
| 86 |
return count - left; |
| 87 |
} |
| 88 |
|
| 89 |
status_t IInStream::read_block(void *dst, size_t count) |
| 90 |
{ |
| 91 |
if (dst == NULL) |
| 92 |
return set_error(STATUS_BAD_ARGUMENTS); |
| 93 |
else if (count == 0) |
| 94 |
return set_error(STATUS_OK); |
| 95 |
|
| 96 |
ssize_t read = read_fully(dst, count); |
| 97 |
if (read < 0) |
| 98 |
return set_error(status_t(-read)); |
| 99 |
|
| 100 |
return set_error((size_t(read) == count) ? STATUS_OK : STATUS_EOF); |
| 101 |
} |
| 102 |
|
| 103 |
wssize_t IInStream::seek(wsize_t position) |
| 104 |
{ |
| 105 |
return - set_error(STATUS_NOT_IMPLEMENTED); |
| 106 |
} |
| 107 |
|
| 108 |
wssize_t IInStream::skip(wsize_t amount) |
| 109 |
{ |
| 110 |
uint8_t skip_buf[0x1000]; |
| 111 |
|
| 112 |
wsize_t initial = amount; |
| 113 |
while (amount > 0) |
| 114 |
{ |
| 115 |
ssize_t n = read(skip_buf, (amount > sizeof(skip_buf)) ? sizeof(skip_buf) : amount); |
| 116 |
if (n < 0) |
| 117 |
{ |
| 118 |
if (initial > amount) |
| 119 |
break; |
| 120 |
return n; |
| 121 |
} |
| 122 |
amount -= n; |
| 123 |
} |
| 124 |
|
| 125 |
return initial - amount; |
| 126 |
} |
| 127 |
|
| 128 |
wssize_t IInStream::sink(IOutStream *os, size_t buf_size) |
| 129 |
{ |
| 130 |
if ((os == NULL) || (buf_size < 1)) |
| 131 |
return -set_error(STATUS_BAD_ARGUMENTS); |
| 132 |
|
| 133 |
uint8_t *buf = reinterpret_cast<uint8_t *>(::malloc(buf_size)); |
| 134 |
if (buf == NULL) |
| 135 |
return STATUS_NO_MEM; |
| 136 |
lsp_finally { |
| 137 |
::free(buf); |
| 138 |
}; |
| 139 |
|
| 140 |
wssize_t count = 0; |
| 141 |
while (true) |
| 142 |
{ |
| 143 |
// Read data |
| 144 |
ssize_t nread = read(buf, buf_size); |
| 145 |
if (nread < 0) |
| 146 |
{ |
| 147 |
if (nread == -STATUS_EOF) |
| 148 |
{ |
| 149 |
set_error(STATUS_OK); |
| 150 |
return count; |
| 151 |
} |
| 152 |
|
| 153 |
set_error(status_t(-nread)); |
| 154 |
return nread; |
| 155 |
} |
| 156 |
count += nread; |
| 157 |
|
| 158 |
// Write data |
| 159 |
ssize_t off = 0; |
| 160 |
while (off < nread) |
| 161 |
{ |
| 162 |
ssize_t nwritten = os->write(&buf[off], nread-off); |
| 163 |
if (nwritten < 0) |
| 164 |
{ |
| 165 |
set_error(status_t(-nwritten)); |
| 166 |
return nwritten; |
| 167 |
} |
| 168 |
off += nwritten; |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
status_t IInStream::close() |
| 174 |
{ |
| 175 |
return set_error(nErrorCode); |
| 176 |
} |
| 177 |
|
| 178 |
status_t finalize(io::IInStream *is, size_t wrap_flags) |
| 179 |
{ |
| 180 |
if (is == NULL) |
| 181 |
return STATUS_OK; |
| 182 |
status_t res = (wrap_flags & WRAP_CLOSE) ? is->close() : STATUS_OK; |
| 183 |
if (wrap_flags & WRAP_DELETE) |
| 184 |
delete is; |
| 185 |
return res; |
| 186 |
} |
| 187 |
|
| 188 |
} /* namespace io */ |
| 189 |
} /* namespace lsp */ |