| 1 |
/* |
| 2 |
* Copyright (C) 2023 Linux Studio Plugins Project <https://lsp-plug.in/> |
| 3 |
* (C) 2023 Vladimir Sadovnikov <[email protected]> |
| 4 |
* |
| 5 |
* This file is part of lsp-runtime-lib |
| 6 |
* Created on: 23 февр. 2021 г. |
| 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 |
|
| 23 |
#include <lsp-plug.in/fmt/json/dom/String.h> |
| 24 |
#include <lsp-plug.in/io/InStringSequence.h> |
| 25 |
#include <lsp-plug.in/expr/Tokenizer.h> |
| 26 |
#include <lsp-plug.in/expr/types.h> |
| 27 |
#include <lsp-plug.in/stdlib/locale.h> |
| 28 |
|
| 29 |
namespace lsp |
| 30 |
{ |
| 31 |
namespace json |
| 32 |
{ |
| 33 |
bool String::valid() const |
| 34 |
{ |
| 35 |
return is_string(); |
| 36 |
} |
| 37 |
|
| 38 |
status_t String::create() |
| 39 |
{ |
| 40 |
node_t *node = new node_t(); |
| 41 |
if (node == NULL) |
| 42 |
return STATUS_NO_MEM; |
| 43 |
|
| 44 |
node->refs = 1; |
| 45 |
node->type = JN_STRING; |
| 46 |
node->sValue = new LSPString(); |
| 47 |
if (node->sValue == NULL) |
| 48 |
{ |
| 49 |
delete node; |
| 50 |
return STATUS_NO_MEM; |
| 51 |
} |
| 52 |
|
| 53 |
release_ref(pNode); |
| 54 |
pNode = node; |
| 55 |
|
| 56 |
return STATUS_OK; |
| 57 |
} |
| 58 |
|
| 59 |
status_t String::create(const LSPString *value) |
| 60 |
{ |
| 61 |
if (value == NULL) |
| 62 |
return STATUS_BAD_ARGUMENTS; |
| 63 |
|
| 64 |
node_t *node = new node_t(); |
| 65 |
if (node == NULL) |
| 66 |
return STATUS_NO_MEM; |
| 67 |
|
| 68 |
node->refs = 1; |
| 69 |
node->type = JN_STRING; |
| 70 |
node->sValue = value->clone(); |
| 71 |
if (node->sValue == NULL) |
| 72 |
{ |
| 73 |
delete node; |
| 74 |
return STATUS_NO_MEM; |
| 75 |
} |
| 76 |
|
| 77 |
release_ref(pNode); |
| 78 |
pNode = node; |
| 79 |
|
| 80 |
return STATUS_OK; |
| 81 |
} |
| 82 |
|
| 83 |
status_t String::create(const char *value, const char *charset) |
| 84 |
{ |
| 85 |
if (value == NULL) |
| 86 |
return STATUS_BAD_ARGUMENTS; |
| 87 |
|
| 88 |
node_t *node = new node_t(); |
| 89 |
if (node == NULL) |
| 90 |
return STATUS_NO_MEM; |
| 91 |
|
| 92 |
node->refs = 1; |
| 93 |
node->type = JN_STRING; |
| 94 |
node->sValue = new LSPString(); |
| 95 |
if (node->sValue == NULL) |
| 96 |
{ |
| 97 |
delete node; |
| 98 |
return STATUS_NO_MEM; |
| 99 |
} |
| 100 |
if (!node->sValue->set_native(value, charset)) |
| 101 |
{ |
| 102 |
delete node->sValue; |
| 103 |
delete node; |
| 104 |
return STATUS_NO_MEM; |
| 105 |
} |
| 106 |
|
| 107 |
release_ref(pNode); |
| 108 |
pNode = node; |
| 109 |
|
| 110 |
return STATUS_OK; |
| 111 |
} |
| 112 |
|
| 113 |
String *String::allocate() |
| 114 |
{ |
| 115 |
String *res = new String(); |
| 116 |
if (res == NULL) |
| 117 |
return NULL; |
| 118 |
else if (res->create() == STATUS_OK) |
| 119 |
return res; |
| 120 |
|
| 121 |
delete res; |
| 122 |
return NULL; |
| 123 |
} |
| 124 |
|
| 125 |
String *String::allocate(const LSPString *value) |
| 126 |
{ |
| 127 |
if (value == NULL) |
| 128 |
return NULL; |
| 129 |
|
| 130 |
String *res = new String(); |
| 131 |
if (res == NULL) |
| 132 |
return NULL; |
| 133 |
else if (res->create(value) == STATUS_OK) |
| 134 |
return res; |
| 135 |
|
| 136 |
delete res; |
| 137 |
return NULL; |
| 138 |
} |
| 139 |
|
| 140 |
String *String::allocate(const char *value, const char *charset) |
| 141 |
{ |
| 142 |
if (value == NULL) |
| 143 |
return NULL; |
| 144 |
|
| 145 |
String *res = new String(); |
| 146 |
if (res == NULL) |
| 147 |
return NULL; |
| 148 |
else if (res->create(value, charset) == STATUS_OK) |
| 149 |
return res; |
| 150 |
|
| 151 |
delete res; |
| 152 |
return NULL; |
| 153 |
} |
| 154 |
|
| 155 |
String String::build() |
| 156 |
{ |
| 157 |
String res; |
| 158 |
res.create(); |
| 159 |
return res; |
| 160 |
} |
| 161 |
|
| 162 |
String String::build(const LSPString *value) |
| 163 |
{ |
| 164 |
String res; |
| 165 |
res.create(value); |
| 166 |
return res; |
| 167 |
} |
| 168 |
|
| 169 |
String String::build(const char *value, const char *charset) |
| 170 |
{ |
| 171 |
String res; |
| 172 |
res.create(value, charset); |
| 173 |
return res; |
| 174 |
} |
| 175 |
|
| 176 |
status_t String::get(LSPString *dst) const |
| 177 |
{ |
| 178 |
if (dst == NULL) |
| 179 |
return STATUS_BAD_ARGUMENTS; |
| 180 |
if (pNode == NULL) |
| 181 |
return STATUS_OK; |
| 182 |
|
| 183 |
switch (pNode->type) |
| 184 |
{ |
| 185 |
case JN_INT: |
| 186 |
return (dst->fmt_ascii("%lld", (long long)pNode->nValue) > 0) ? STATUS_OK : STATUS_NO_MEM; |
| 187 |
case JN_DOUBLE: |
| 188 |
{ |
| 189 |
SET_LOCALE_SCOPED(LC_NUMERIC, "C"); |
| 190 |
return (dst->fmt_ascii("%f", (long long)pNode->fValue) > 0) ? STATUS_OK : STATUS_NO_MEM; |
| 191 |
} |
| 192 |
case JN_BOOL: |
| 193 |
return (dst->set_ascii((pNode->bValue) ? "true" : "false")) ? STATUS_OK : STATUS_NO_MEM; |
| 194 |
case JN_STRING: |
| 195 |
return (dst->set(pNode->sValue)) ? STATUS_OK : STATUS_NO_MEM; |
| 196 |
default: |
| 197 |
break; |
| 198 |
} |
| 199 |
|
| 200 |
return STATUS_BAD_TYPE; |
| 201 |
} |
| 202 |
|
| 203 |
status_t String::sget(LSPString *dst) const |
| 204 |
{ |
| 205 |
if (dst == NULL) |
| 206 |
return STATUS_BAD_ARGUMENTS; |
| 207 |
if (!is_string()) |
| 208 |
return STATUS_BAD_TYPE; |
| 209 |
|
| 210 |
return (dst->set(pNode->sValue)) ? STATUS_OK : STATUS_NO_MEM; |
| 211 |
} |
| 212 |
|
| 213 |
status_t String::cast() |
| 214 |
{ |
| 215 |
return cast_to_string(); |
| 216 |
} |
| 217 |
|
| 218 |
status_t String::set(const LSPString *value) |
| 219 |
{ |
| 220 |
if (value == NULL) |
| 221 |
return STATUS_BAD_ARGUMENTS; |
| 222 |
|
| 223 |
LSPString *tmp = value->clone(); |
| 224 |
if (tmp == NULL) |
| 225 |
return STATUS_NO_MEM; |
| 226 |
|
| 227 |
node_t *node = clear_node(pNode); |
| 228 |
if (node == NULL) |
| 229 |
{ |
| 230 |
delete tmp; |
| 231 |
return STATUS_NO_MEM; |
| 232 |
} |
| 233 |
|
| 234 |
pNode = node; |
| 235 |
pNode->type = JN_STRING; |
| 236 |
pNode->sValue = tmp; |
| 237 |
|
| 238 |
return STATUS_OK; |
| 239 |
} |
| 240 |
|
| 241 |
status_t String::set(const char *value, const char *charset) |
| 242 |
{ |
| 243 |
if (value == NULL) |
| 244 |
return STATUS_BAD_ARGUMENTS; |
| 245 |
|
| 246 |
LSPString *tmp = new LSPString(); |
| 247 |
if (tmp == NULL) |
| 248 |
return STATUS_NO_MEM; |
| 249 |
if (!tmp->set_native(value, charset)) |
| 250 |
{ |
| 251 |
delete tmp; |
| 252 |
return STATUS_NO_MEM; |
| 253 |
} |
| 254 |
|
| 255 |
node_t *node = clear_node(pNode); |
| 256 |
if (node == NULL) |
| 257 |
{ |
| 258 |
delete tmp; |
| 259 |
return STATUS_NO_MEM; |
| 260 |
} |
| 261 |
|
| 262 |
pNode = node; |
| 263 |
pNode->type = JN_STRING; |
| 264 |
pNode->sValue = tmp; |
| 265 |
|
| 266 |
return STATUS_OK; |
| 267 |
} |
| 268 |
|
| 269 |
status_t String::sset(const LSPString *value) |
| 270 |
{ |
| 271 |
if (value == NULL) |
| 272 |
return STATUS_BAD_ARGUMENTS; |
| 273 |
if (!is_string()) |
| 274 |
return STATUS_BAD_TYPE; |
| 275 |
|
| 276 |
return (pNode->sValue->set(value)) ? STATUS_OK : STATUS_NO_MEM; |
| 277 |
} |
| 278 |
|
| 279 |
status_t String::sset(const char *value, const char *charset) |
| 280 |
{ |
| 281 |
if (value == NULL) |
| 282 |
return STATUS_BAD_ARGUMENTS; |
| 283 |
if (!is_string()) |
| 284 |
return STATUS_BAD_TYPE; |
| 285 |
|
| 286 |
return (pNode->sValue->set_native(value, charset)) ? STATUS_OK : STATUS_NO_MEM; |
| 287 |
} |
| 288 |
} /* namespace json */ |
| 289 |
} /* namespace lsp */ |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|