| 1 |
/* |
| 2 |
* Copyright (C) 2024 Linux Studio Plugins Project <https://lsp-plug.in/> |
| 3 |
* (C) 2024 Vladimir Sadovnikov <[email protected]> |
| 4 |
* |
| 5 |
* This file is part of lsp-runtime-lib |
| 6 |
* Created on: 14 июн. 2018 г. |
| 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/IInSequence.h> |
| 23 |
#include <lsp-plug.in/io/IOutSequence.h> |
| 24 |
#include <lsp-plug.in/common/debug.h> |
| 25 |
|
| 26 |
namespace lsp |
| 27 |
{ |
| 28 |
namespace io |
| 29 |
{ |
| 30 |
static lsp_wchar_t skip_buf[0x1000]; |
| 31 |
|
| 32 |
IInSequence::IInSequence() |
| 33 |
{ |
| 34 |
nErrorCode = STATUS_OK; |
| 35 |
} |
| 36 |
|
| 37 |
IInSequence::~IInSequence() |
| 38 |
{ |
| 39 |
} |
| 40 |
|
| 41 |
ssize_t IInSequence::read(lsp_wchar_t *dst, size_t count) |
| 42 |
{ |
| 43 |
return -set_error(STATUS_EOF); |
| 44 |
} |
| 45 |
|
| 46 |
lsp_swchar_t IInSequence::read() |
| 47 |
{ |
| 48 |
return -set_error(STATUS_EOF); |
| 49 |
} |
| 50 |
|
| 51 |
status_t IInSequence::read_line(LSPString *s, bool force) |
| 52 |
{ |
| 53 |
return set_error(STATUS_EOF); |
| 54 |
} |
| 55 |
|
| 56 |
ssize_t IInSequence::skip(size_t count) |
| 57 |
{ |
| 58 |
ssize_t skipped = 0; |
| 59 |
|
| 60 |
while (count > 0) |
| 61 |
{ |
| 62 |
size_t to_read = (count > ((sizeof(skip_buf))/sizeof(lsp_wchar_t))) ? |
| 63 |
((sizeof(skip_buf))/sizeof(lsp_wchar_t)) : count; |
| 64 |
ssize_t nread = read(skip_buf, to_read); |
| 65 |
if (nread <= 0) |
| 66 |
break; |
| 67 |
|
| 68 |
count -= nread; |
| 69 |
skipped += nread; |
| 70 |
} |
| 71 |
return skipped; |
| 72 |
} |
| 73 |
|
| 74 |
status_t IInSequence::close() |
| 75 |
{ |
| 76 |
return set_error(STATUS_OK); |
| 77 |
} |
| 78 |
|
| 79 |
status_t IInSequence::mark(ssize_t limit) |
| 80 |
{ |
| 81 |
return set_error(STATUS_NOT_SUPPORTED); |
| 82 |
} |
| 83 |
|
| 84 |
status_t IInSequence::reset() |
| 85 |
{ |
| 86 |
return set_error(STATUS_NOT_SUPPORTED); |
| 87 |
} |
| 88 |
|
| 89 |
wssize_t IInSequence::sink(IOutSequence *os, size_t buf_size) |
| 90 |
{ |
| 91 |
if ((os == NULL) || (buf_size < 1)) |
| 92 |
return -set_error(STATUS_BAD_ARGUMENTS); |
| 93 |
|
| 94 |
lsp_wchar_t *buf = reinterpret_cast<lsp_wchar_t *>(::malloc(buf_size * sizeof(lsp_wchar_t))); |
| 95 |
if (buf == NULL) |
| 96 |
return STATUS_NO_MEM; |
| 97 |
lsp_finally { |
| 98 |
::free(buf); |
| 99 |
}; |
| 100 |
|
| 101 |
wssize_t count = 0; |
| 102 |
while (true) |
| 103 |
{ |
| 104 |
// Read data |
| 105 |
ssize_t nread = read(buf, buf_size); |
| 106 |
if (nread < 0) |
| 107 |
{ |
| 108 |
if (nread == -STATUS_EOF) |
| 109 |
{ |
| 110 |
set_error(STATUS_OK); |
| 111 |
return count; |
| 112 |
} |
| 113 |
|
| 114 |
set_error(status_t(-nread)); |
| 115 |
return nread; |
| 116 |
} |
| 117 |
count += nread; |
| 118 |
|
| 119 |
// Write data |
| 120 |
status_t res = os->write(buf, nread); |
| 121 |
if (res != STATUS_OK) |
| 122 |
{ |
| 123 |
set_error(-res); |
| 124 |
return -res; |
| 125 |
} |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
} /* namespace io */ |
| 130 |
} /* namespace lsp */ |