| 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: 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/InStringSequence.h> |
| 23 |
|
| 24 |
namespace lsp |
| 25 |
{ |
| 26 |
namespace io |
| 27 |
{ |
| 28 |
InStringSequence::InStringSequence() |
| 29 |
{ |
| 30 |
pString = NULL; |
| 31 |
bDelete = false; |
| 32 |
nOffset = 0; |
| 33 |
nMark = -1; |
| 34 |
nMarkLen = 0; |
| 35 |
} |
| 36 |
|
| 37 |
InStringSequence::InStringSequence(const LSPString *s) |
| 38 |
{ |
| 39 |
pString = const_cast<LSPString *>(s); |
| 40 |
bDelete = false; |
| 41 |
nOffset = 0; |
| 42 |
nMark = -1; |
| 43 |
nMarkLen = 0; |
| 44 |
} |
| 45 |
|
| 46 |
InStringSequence::InStringSequence(LSPString *s, bool destroy) |
| 47 |
{ |
| 48 |
pString = s; |
| 49 |
bDelete = destroy; |
| 50 |
nOffset = 0; |
| 51 |
nMark = -1; |
| 52 |
nMarkLen = 0; |
| 53 |
} |
| 54 |
|
| 55 |
InStringSequence::~InStringSequence() |
| 56 |
{ |
| 57 |
do_close(); |
| 58 |
} |
| 59 |
|
| 60 |
status_t InStringSequence::wrap(const LSPString *in) |
| 61 |
{ |
| 62 |
return wrap(const_cast<LSPString *>(in), false); |
| 63 |
} |
| 64 |
|
| 65 |
status_t InStringSequence::wrap(LSPString *in, bool del) |
| 66 |
{ |
| 67 |
if (pString != NULL) |
| 68 |
return set_error(STATUS_BAD_STATE); |
| 69 |
else if (in == NULL) |
| 70 |
return set_error(STATUS_BAD_ARGUMENTS); |
| 71 |
pString = in; |
| 72 |
bDelete = del; |
| 73 |
nMark = -1; |
| 74 |
nMarkLen = 0; |
| 75 |
|
| 76 |
return set_error(STATUS_OK); |
| 77 |
} |
| 78 |
|
| 79 |
status_t InStringSequence::wrap(const char *s, const char *charset) |
| 80 |
{ |
| 81 |
if (pString != NULL) |
| 82 |
return set_error(STATUS_BAD_STATE); |
| 83 |
else if (s == NULL) |
| 84 |
return set_error(STATUS_BAD_ARGUMENTS); |
| 85 |
|
| 86 |
LSPString *str = new LSPString(); |
| 87 |
if (str == NULL) |
| 88 |
return set_error(STATUS_NO_MEM); |
| 89 |
if (!str->set_native(s, charset)) |
| 90 |
{ |
| 91 |
delete str; |
| 92 |
return set_error(STATUS_NO_MEM); |
| 93 |
} |
| 94 |
|
| 95 |
status_t res = wrap(str, true); |
| 96 |
if (res != STATUS_OK) |
| 97 |
delete s; |
| 98 |
|
| 99 |
return res; |
| 100 |
} |
| 101 |
|
| 102 |
status_t InStringSequence::wrap(const char *s) |
| 103 |
{ |
| 104 |
if (pString != NULL) |
| 105 |
return set_error(STATUS_BAD_STATE); |
| 106 |
if (s == NULL) |
| 107 |
return set_error(STATUS_BAD_ARGUMENTS); |
| 108 |
|
| 109 |
LSPString *str = new LSPString(); |
| 110 |
if (str == NULL) |
| 111 |
return set_error(STATUS_NO_MEM); |
| 112 |
if (!str->set_utf8(s)) |
| 113 |
{ |
| 114 |
delete str; |
| 115 |
return set_error(STATUS_NO_MEM); |
| 116 |
} |
| 117 |
|
| 118 |
status_t res = wrap(str, true); |
| 119 |
if (res != STATUS_OK) |
| 120 |
delete s; |
| 121 |
|
| 122 |
return res; |
| 123 |
} |
| 124 |
|
| 125 |
void InStringSequence::do_close() |
| 126 |
{ |
| 127 |
nMark = -1; |
| 128 |
nMarkLen = 0; |
| 129 |
|
| 130 |
if (pString == NULL) |
| 131 |
return; |
| 132 |
if (bDelete) |
| 133 |
delete const_cast<LSPString *>(pString); |
| 134 |
pString = NULL; |
| 135 |
bDelete = false; |
| 136 |
} |
| 137 |
|
| 138 |
ssize_t InStringSequence::read(lsp_wchar_t *dst, size_t count) |
| 139 |
{ |
| 140 |
if (pString == NULL) |
| 141 |
return set_error(STATUS_CLOSED); |
| 142 |
|
| 143 |
size_t avail = pString->length() - nOffset; |
| 144 |
if (avail <= 0) |
| 145 |
return -set_error(STATUS_EOF); |
| 146 |
if (count > avail) |
| 147 |
count = avail; |
| 148 |
|
| 149 |
// Perform a copy |
| 150 |
const lsp_wchar_t *v = pString->characters(); |
| 151 |
v += nOffset; |
| 152 |
nOffset += count; |
| 153 |
::memcpy(dst, v, count * sizeof(lsp_wchar_t)); |
| 154 |
|
| 155 |
// Reset mark if it was set |
| 156 |
if ((nMark > 0) && (nOffset > size_t(nMark + nMarkLen))) |
| 157 |
nMark = -1; |
| 158 |
|
| 159 |
set_error(STATUS_OK); |
| 160 |
return count; |
| 161 |
} |
| 162 |
|
| 163 |
lsp_swchar_t InStringSequence::read() |
| 164 |
{ |
| 165 |
if (pString == NULL) |
| 166 |
return -set_error(STATUS_CLOSED); |
| 167 |
|
| 168 |
if (nOffset < pString->length()) |
| 169 |
{ |
| 170 |
set_error(STATUS_OK); |
| 171 |
lsp_swchar_t ch = pString->char_at(nOffset++); |
| 172 |
|
| 173 |
// Reset mark if it was set |
| 174 |
if ((nMark > 0) && (nOffset > size_t(nMark + nMarkLen))) |
| 175 |
nMark = -1; |
| 176 |
|
| 177 |
return ch; |
| 178 |
} |
| 179 |
return -set_error(STATUS_EOF); |
| 180 |
} |
| 181 |
|
| 182 |
status_t InStringSequence::read_line(LSPString *s, bool force) |
| 183 |
{ |
| 184 |
if (pString == NULL) |
| 185 |
return set_error(STATUS_CLOSED); |
| 186 |
|
| 187 |
ssize_t new_pos; |
| 188 |
ssize_t idx = pString->index_of(nOffset, '\n'); |
| 189 |
|
| 190 |
if (idx < 0) |
| 191 |
{ |
| 192 |
if ((!force) || (nOffset >= pString->length())) |
| 193 |
return set_error(STATUS_EOF); |
| 194 |
idx = pString->length(); |
| 195 |
new_pos = idx; |
| 196 |
} |
| 197 |
else |
| 198 |
new_pos = idx + 1; |
| 199 |
|
| 200 |
if (!s->set(pString, nOffset, idx)) |
| 201 |
return set_error(STATUS_NO_MEM); |
| 202 |
|
| 203 |
// Trim '\r' symbol at the end |
| 204 |
if (s->last() == '\r') |
| 205 |
s->remove_last(); |
| 206 |
|
| 207 |
nOffset = new_pos; |
| 208 |
|
| 209 |
// Reset mark if it was set |
| 210 |
if ((nMark > 0) && (nOffset > size_t(nMark + nMarkLen))) |
| 211 |
nMark = -1; |
| 212 |
|
| 213 |
return set_error(STATUS_OK); |
| 214 |
} |
| 215 |
|
| 216 |
ssize_t InStringSequence::skip(size_t count) |
| 217 |
{ |
| 218 |
if (pString == NULL) |
| 219 |
return -set_error(STATUS_CLOSED); |
| 220 |
|
| 221 |
size_t avail = pString->length() - nOffset; |
| 222 |
if (count > avail) |
| 223 |
count = avail; |
| 224 |
|
| 225 |
nOffset += count; |
| 226 |
|
| 227 |
// Reset mark if it was set |
| 228 |
if ((nMark > 0) && (nOffset > size_t(nMark + nMarkLen))) |
| 229 |
nMark = -1; |
| 230 |
|
| 231 |
set_error(STATUS_OK); |
| 232 |
return count; |
| 233 |
} |
| 234 |
|
| 235 |
status_t InStringSequence::close() |
| 236 |
{ |
| 237 |
do_close(); |
| 238 |
return set_error(STATUS_OK); |
| 239 |
} |
| 240 |
|
| 241 |
status_t InStringSequence::mark(ssize_t limit) |
| 242 |
{ |
| 243 |
if (limit < 0) |
| 244 |
return set_error(STATUS_OK); |
| 245 |
if (pString == NULL) |
| 246 |
return set_error(STATUS_CLOSED); |
| 247 |
|
| 248 |
nMark = nOffset; |
| 249 |
nMarkLen = limit; |
| 250 |
|
| 251 |
return set_error(STATUS_OK); |
| 252 |
} |
| 253 |
|
| 254 |
status_t InStringSequence::reset() |
| 255 |
{ |
| 256 |
if (nMark < 0) |
| 257 |
return set_error(STATUS_NOT_FOUND); |
| 258 |
nOffset = nMark; |
| 259 |
return set_error(STATUS_OK); |
| 260 |
} |
| 261 |
} |
| 262 |
} /* namespace lsp */ |