| 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: 21 окт. 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/ChunkReaderStream.h> |
| 23 |
|
| 24 |
namespace lsp |
| 25 |
{ |
| 26 |
namespace lspc |
| 27 |
{ |
| 28 |
ChunkReaderStream::ChunkReaderStream(ChunkReader *reader, bool free) |
| 29 |
{ |
| 30 |
pReader = reader; |
| 31 |
bDelete = free; |
| 32 |
} |
| 33 |
|
| 34 |
ChunkReaderStream::~ChunkReaderStream() |
| 35 |
{ |
| 36 |
do_close(); |
| 37 |
} |
| 38 |
|
| 39 |
void ChunkReaderStream::do_close() |
| 40 |
{ |
| 41 |
if (pReader == NULL) |
| 42 |
return; |
| 43 |
if (bDelete) |
| 44 |
delete pReader; |
| 45 |
pReader = NULL; |
| 46 |
} |
| 47 |
|
| 48 |
wssize_t ChunkReaderStream::position() |
| 49 |
{ |
| 50 |
if (pReader == NULL) |
| 51 |
return -set_error(STATUS_CLOSED); |
| 52 |
wssize_t res = pReader->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 ChunkReaderStream::read(void *dst, size_t count) |
| 62 |
{ |
| 63 |
if (pReader == NULL) |
| 64 |
return -set_error(STATUS_CLOSED); |
| 65 |
ssize_t res = pReader->read(dst, count); |
| 66 |
if (res > 0) |
| 67 |
{ |
| 68 |
set_error(STATUS_OK); |
| 69 |
return res; |
| 70 |
} |
| 71 |
else if (res == 0) |
| 72 |
return -set_error(STATUS_EOF); |
| 73 |
return -set_error(status_t(-res)); |
| 74 |
} |
| 75 |
|
| 76 |
ssize_t ChunkReaderStream::read_byte() |
| 77 |
{ |
| 78 |
if (pReader == NULL) |
| 79 |
return -set_error(STATUS_CLOSED); |
| 80 |
uint8_t buf; |
| 81 |
ssize_t res = pReader->read(&buf, sizeof(buf)); |
| 82 |
if (res > 0) |
| 83 |
{ |
| 84 |
set_error(STATUS_OK); |
| 85 |
return buf; |
| 86 |
} |
| 87 |
else if (res == 0) |
| 88 |
return -set_error(STATUS_EOF); |
| 89 |
return -set_error(status_t(-res)); |
| 90 |
} |
| 91 |
|
| 92 |
wssize_t ChunkReaderStream::skip(wsize_t amount) |
| 93 |
{ |
| 94 |
if (pReader == NULL) |
| 95 |
return -set_error(STATUS_CLOSED); |
| 96 |
ssize_t res = pReader->skip(amount); |
| 97 |
if (res >= 0) |
| 98 |
{ |
| 99 |
set_error(STATUS_OK); |
| 100 |
return res; |
| 101 |
} |
| 102 |
return -set_error(status_t(-res)); |
| 103 |
} |
| 104 |
|
| 105 |
status_t ChunkReaderStream::close() |
| 106 |
{ |
| 107 |
do_close(); |
| 108 |
return set_error(STATUS_OK); |
| 109 |
} |
| 110 |
} /* namespace lspc */ |
| 111 |
} /* namespace lsp */ |
| 112 |
|
| 113 |
|
| 114 |
|