| 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: 19 сент. 2019 г. |
| 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/expr/types.h> |
| 23 |
#include <lsp-plug.in/expr/Tokenizer.h> |
| 24 |
#include <lsp-plug.in/io/InStringSequence.h> |
| 25 |
#include <lsp-plug.in/stdlib/locale.h> |
| 26 |
#include <lsp-plug.in/stdlib/math.h> |
| 27 |
|
| 28 |
namespace lsp |
| 29 |
{ |
| 30 |
namespace expr |
| 31 |
{ |
| 32 |
template <class T> |
| 33 |
static inline const char *special_value(const T f) |
| 34 |
{ |
| 35 |
if (isinf(f)) |
| 36 |
return (f < 0.0f) ? "-inf" : "inf"; |
| 37 |
if (isnan(f)) |
| 38 |
return "nan"; |
| 39 |
return NULL; |
| 40 |
} |
| 41 |
|
| 42 |
void init_value(value_t *dst) |
| 43 |
{ |
| 44 |
dst->type = VT_UNDEF; |
| 45 |
dst->v_str = NULL; |
| 46 |
} |
| 47 |
|
| 48 |
void init_value_null(value_t *dst) |
| 49 |
{ |
| 50 |
dst->type = VT_NULL; |
| 51 |
dst->v_str = NULL; |
| 52 |
} |
| 53 |
|
| 54 |
void init_value_undef(value_t *dst) |
| 55 |
{ |
| 56 |
dst->type = VT_UNDEF; |
| 57 |
dst->v_str = NULL; |
| 58 |
} |
| 59 |
|
| 60 |
void init_value_int(value_t *dst, ssize_t value) |
| 61 |
{ |
| 62 |
dst->type = VT_INT; |
| 63 |
dst->v_int = value; |
| 64 |
} |
| 65 |
|
| 66 |
void init_value_float(value_t *dst, double value) |
| 67 |
{ |
| 68 |
dst->type = VT_FLOAT; |
| 69 |
dst->v_float = value; |
| 70 |
} |
| 71 |
|
| 72 |
void init_value_bool(value_t *dst, bool value) |
| 73 |
{ |
| 74 |
dst->type = VT_BOOL; |
| 75 |
dst->v_str = NULL; |
| 76 |
dst->v_bool = value; |
| 77 |
} |
| 78 |
|
| 79 |
status_t init_value_string(value_t *dst, const LSPString *value) |
| 80 |
{ |
| 81 |
LSPString *str = value->clone(); |
| 82 |
if (str != NULL) |
| 83 |
{ |
| 84 |
dst->type = VT_STRING; |
| 85 |
dst->v_str = str; |
| 86 |
return STATUS_OK; |
| 87 |
} |
| 88 |
|
| 89 |
dst->type = VT_UNDEF; |
| 90 |
dst->v_str = NULL; |
| 91 |
|
| 92 |
return STATUS_NO_MEM; |
| 93 |
} |
| 94 |
|
| 95 |
status_t init_value_string(value_t *dst, const char *value) |
| 96 |
{ |
| 97 |
LSPString *str = new LSPString(); |
| 98 |
if (str != NULL) |
| 99 |
{ |
| 100 |
if (str->set_utf8(value)) |
| 101 |
{ |
| 102 |
dst->type = VT_STRING; |
| 103 |
dst->v_str = str; |
| 104 |
return STATUS_OK; |
| 105 |
} |
| 106 |
delete str; |
| 107 |
} |
| 108 |
|
| 109 |
dst->type = VT_UNDEF; |
| 110 |
dst->v_str = NULL; |
| 111 |
|
| 112 |
return STATUS_NO_MEM; |
| 113 |
} |
| 114 |
|
| 115 |
status_t init_value_string(value_t *dst, const char *value, const char *charset) |
| 116 |
{ |
| 117 |
LSPString *str = new LSPString(); |
| 118 |
if (str != NULL) |
| 119 |
{ |
| 120 |
if (str->set_native(value, charset)) |
| 121 |
{ |
| 122 |
dst->type = VT_STRING; |
| 123 |
dst->v_str = str; |
| 124 |
return STATUS_OK; |
| 125 |
} |
| 126 |
delete str; |
| 127 |
} |
| 128 |
|
| 129 |
dst->type = VT_UNDEF; |
| 130 |
dst->v_str = NULL; |
| 131 |
|
| 132 |
return STATUS_NO_MEM; |
| 133 |
} |
| 134 |
|
| 135 |
inline void destroy_value_internal(value_t *value) |
| 136 |
{ |
| 137 |
if (value->type == VT_STRING) |
| 138 |
{ |
| 139 |
delete value->v_str; |
| 140 |
value->v_str = NULL; |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
void set_value_null(value_t *dst) |
| 145 |
{ |
| 146 |
destroy_value_internal(dst); |
| 147 |
dst->type = VT_NULL; |
| 148 |
dst->v_str = NULL; |
| 149 |
} |
| 150 |
|
| 151 |
void set_value_undef(value_t *dst) |
| 152 |
{ |
| 153 |
destroy_value_internal(dst); |
| 154 |
dst->type = VT_UNDEF; |
| 155 |
dst->v_str = NULL; |
| 156 |
} |
| 157 |
|
| 158 |
void set_value_int(value_t *dst, ssize_t value) |
| 159 |
{ |
| 160 |
destroy_value_internal(dst); |
| 161 |
dst->type = VT_INT; |
| 162 |
dst->v_str = NULL; |
| 163 |
dst->v_int = value; |
| 164 |
} |
| 165 |
|
| 166 |
void set_value_float(value_t *dst, double value) |
| 167 |
{ |
| 168 |
destroy_value_internal(dst); |
| 169 |
dst->type = VT_FLOAT; |
| 170 |
dst->v_str = NULL; |
| 171 |
dst->v_float = value; |
| 172 |
} |
| 173 |
|
| 174 |
void set_value_bool(value_t *dst, bool value) |
| 175 |
{ |
| 176 |
destroy_value_internal(dst); |
| 177 |
dst->type = VT_BOOL; |
| 178 |
dst->v_str = NULL; |
| 179 |
dst->v_bool = value; |
| 180 |
} |
| 181 |
|
| 182 |
status_t init_value(value_t *dst, const value_t *src) |
| 183 |
{ |
| 184 |
if (src == NULL) |
| 185 |
{ |
| 186 |
dst->type = VT_NULL; |
| 187 |
dst->v_str = NULL; |
| 188 |
return STATUS_OK; |
| 189 |
} |
| 190 |
else if ((src->type == VT_STRING) && (src->v_str != NULL)) |
| 191 |
{ |
| 192 |
dst->v_str = src->v_str->clone(); |
| 193 |
if (dst->v_str == NULL) |
| 194 |
{ |
| 195 |
dst->type = VT_UNDEF; |
| 196 |
return STATUS_NO_MEM; |
| 197 |
} |
| 198 |
|
| 199 |
dst->type = VT_STRING; |
| 200 |
} |
| 201 |
else |
| 202 |
*dst = *src; |
| 203 |
return STATUS_OK; |
| 204 |
} |
| 205 |
|
| 206 |
void init_value_move(value_t *dst, value_t *src) |
| 207 |
{ |
| 208 |
*dst = *src; |
| 209 |
src->type = VT_UNDEF; |
| 210 |
src->v_str = NULL; |
| 211 |
} |
| 212 |
|
| 213 |
status_t set_value_string(value_t *dst, const LSPString *value) |
| 214 |
{ |
| 215 |
if (value == NULL) |
| 216 |
{ |
| 217 |
set_value_null(dst); |
| 218 |
return STATUS_OK; |
| 219 |
} |
| 220 |
else if (dst->type == VT_STRING) |
| 221 |
return (dst->v_str->set(value)) ? STATUS_OK : STATUS_NO_MEM; |
| 222 |
|
| 223 |
LSPString *copy = value->clone(); |
| 224 |
if (copy == NULL) |
| 225 |
return STATUS_NO_MEM; |
| 226 |
|
| 227 |
dst->type = VT_STRING; |
| 228 |
dst->v_str = copy; |
| 229 |
return STATUS_OK; |
| 230 |
} |
| 231 |
|
| 232 |
status_t set_value_string(value_t *dst, const char *value) |
| 233 |
{ |
| 234 |
if (value == NULL) |
| 235 |
{ |
| 236 |
set_value_null(dst); |
| 237 |
return STATUS_OK; |
| 238 |
} |
| 239 |
else if (dst->type == VT_STRING) |
| 240 |
return (dst->v_str->set_utf8(value)) ? STATUS_OK : STATUS_NO_MEM; |
| 241 |
|
| 242 |
LSPString *tmp = new LSPString(); |
| 243 |
if (tmp == NULL) |
| 244 |
return STATUS_NO_MEM; |
| 245 |
if (!tmp->set_utf8(value)) |
| 246 |
{ |
| 247 |
delete tmp; |
| 248 |
return STATUS_NO_MEM; |
| 249 |
} |
| 250 |
|
| 251 |
dst->type = VT_STRING; |
| 252 |
dst->v_str = tmp; |
| 253 |
return STATUS_OK; |
| 254 |
} |
| 255 |
|
| 256 |
status_t set_value_string(value_t *dst, const char *value, const char *charset) |
| 257 |
{ |
| 258 |
if (value == NULL) |
| 259 |
{ |
| 260 |
set_value_null(dst); |
| 261 |
return STATUS_OK; |
| 262 |
} |
| 263 |
else if (dst->type == VT_STRING) |
| 264 |
return (dst->v_str->set_native(value, charset)) ? STATUS_OK : STATUS_NO_MEM; |
| 265 |
|
| 266 |
LSPString *tmp = new LSPString(); |
| 267 |
if (tmp == NULL) |
| 268 |
return STATUS_NO_MEM; |
| 269 |
if (!tmp->set_native(value, charset)) |
| 270 |
{ |
| 271 |
delete tmp; |
| 272 |
return STATUS_NO_MEM; |
| 273 |
} |
| 274 |
|
| 275 |
dst->type = VT_STRING; |
| 276 |
dst->v_str = tmp; |
| 277 |
return STATUS_OK; |
| 278 |
} |
| 279 |
|
| 280 |
status_t copy_value(value_t *dst, const value_t *src) |
| 281 |
{ |
| 282 |
if (src == NULL) |
| 283 |
{ |
| 284 |
set_value_null(dst); |
| 285 |
return STATUS_OK; |
| 286 |
} |
| 287 |
else if (src->type == VT_STRING) |
| 288 |
{ |
| 289 |
if (dst->type == VT_STRING) |
| 290 |
{ |
| 291 |
if (!dst->v_str->set(src->v_str)) |
| 292 |
return STATUS_NO_MEM; |
| 293 |
} |
| 294 |
else |
| 295 |
{ |
| 296 |
LSPString *copy = src->v_str->clone(); |
| 297 |
if (copy == NULL) |
| 298 |
return STATUS_NO_MEM; |
| 299 |
|
| 300 |
dst->type = VT_STRING; |
| 301 |
dst->v_str = copy; |
| 302 |
} |
| 303 |
} |
| 304 |
else |
| 305 |
{ |
| 306 |
if (dst->type == VT_STRING) |
| 307 |
delete dst->v_str; |
| 308 |
|
| 309 |
*dst = *src; |
| 310 |
} |
| 311 |
return STATUS_OK; |
| 312 |
} |
| 313 |
|
| 314 |
void move_value(value_t *dst, value_t *src) |
| 315 |
{ |
| 316 |
if (src->type == VT_STRING) |
| 317 |
{ |
| 318 |
if (dst->type == VT_STRING) |
| 319 |
delete dst->v_str; |
| 320 |
|
| 321 |
dst->type = VT_STRING; |
| 322 |
dst->v_str = src->v_str; |
| 323 |
} |
| 324 |
else |
| 325 |
{ |
| 326 |
if (dst->type == VT_STRING) |
| 327 |
delete dst->v_str; |
| 328 |
|
| 329 |
*dst = *src; |
| 330 |
} |
| 331 |
|
| 332 |
src->type = VT_UNDEF; |
| 333 |
src->v_str = NULL; |
| 334 |
} |
| 335 |
|
| 336 |
void swap_value(value_t *dst, value_t *src) |
| 337 |
{ |
| 338 |
lsp::swap(*dst, *src); |
| 339 |
} |
| 340 |
|
| 341 |
void destroy_value(value_t *value) |
| 342 |
{ |
| 343 |
destroy_value_internal(value); |
| 344 |
value->type = VT_UNDEF; |
| 345 |
} |
| 346 |
|
| 347 |
status_t fetch_int(ssize_t *dst, const value_t *v) |
| 348 |
{ |
| 349 |
if (v->type != VT_INT) |
| 350 |
return STATUS_BAD_TYPE; |
| 351 |
*dst = v->v_int; |
| 352 |
return STATUS_OK; |
| 353 |
} |
| 354 |
|
| 355 |
status_t fetch_float(float *dst, const value_t *v) |
| 356 |
{ |
| 357 |
if (v->type != VT_FLOAT) |
| 358 |
return STATUS_BAD_TYPE; |
| 359 |
*dst = v->v_float; |
| 360 |
return STATUS_OK; |
| 361 |
} |
| 362 |
|
| 363 |
status_t fetch_float(double *dst, const value_t *v) |
| 364 |
{ |
| 365 |
if (v->type != VT_FLOAT) |
| 366 |
return STATUS_BAD_TYPE; |
| 367 |
*dst = v->v_float; |
| 368 |
return STATUS_OK; |
| 369 |
} |
| 370 |
|
| 371 |
status_t fetch_bool(bool *dst, const value_t *v) |
| 372 |
{ |
| 373 |
if (v->type != VT_BOOL) |
| 374 |
return STATUS_BAD_TYPE; |
| 375 |
*dst = v->v_bool; |
| 376 |
return STATUS_OK; |
| 377 |
} |
| 378 |
|
| 379 |
status_t fetch_string(LSPString *dst, const value_t *v) |
| 380 |
{ |
| 381 |
if (v->type != VT_STRING) |
| 382 |
return STATUS_BAD_TYPE; |
| 383 |
return (dst->set(v->v_str)) ? STATUS_OK : STATUS_NO_MEM; |
| 384 |
} |
| 385 |
|
| 386 |
status_t fetch_as_int(ssize_t *dst, const value_t *v) |
| 387 |
{ |
| 388 |
if (v->type == VT_INT) |
| 389 |
{ |
| 390 |
*dst = v->v_int; |
| 391 |
return STATUS_OK; |
| 392 |
} |
| 393 |
|
| 394 |
// Perform cast |
| 395 |
value_t tmp; |
| 396 |
init_value(&tmp); |
| 397 |
lsp_finally { destroy_value(&tmp); }; |
| 398 |
|
| 399 |
status_t res = cast_int(&tmp, v); |
| 400 |
if (res == STATUS_OK) |
| 401 |
*dst = tmp.v_int; |
| 402 |
|
| 403 |
return res; |
| 404 |
} |
| 405 |
|
| 406 |
status_t fetch_as_float(float *dst, const value_t *v) |
| 407 |
{ |
| 408 |
if (v->type == VT_FLOAT) |
| 409 |
{ |
| 410 |
*dst = v->v_float; |
| 411 |
return STATUS_OK; |
| 412 |
} |
| 413 |
|
| 414 |
// Perform cast |
| 415 |
value_t tmp; |
| 416 |
init_value(&tmp); |
| 417 |
lsp_finally { destroy_value(&tmp); }; |
| 418 |
|
| 419 |
status_t res = cast_float(&tmp, v); |
| 420 |
if (res == STATUS_OK) |
| 421 |
*dst = tmp.v_float; |
| 422 |
|
| 423 |
return res; |
| 424 |
} |
| 425 |
|
| 426 |
status_t fetch_as_float(double *dst, const value_t *v) |
| 427 |
{ |
| 428 |
if (v->type == VT_FLOAT) |
| 429 |
{ |
| 430 |
*dst = v->v_float; |
| 431 |
return STATUS_OK; |
| 432 |
} |
| 433 |
|
| 434 |
// Perform cast |
| 435 |
value_t tmp; |
| 436 |
init_value(&tmp); |
| 437 |
lsp_finally { destroy_value(&tmp); }; |
| 438 |
|
| 439 |
status_t res = cast_float(&tmp, v); |
| 440 |
if (res == STATUS_OK) |
| 441 |
*dst = tmp.v_float; |
| 442 |
|
| 443 |
return res; |
| 444 |
} |
| 445 |
|
| 446 |
status_t fetch_as_bool(bool *dst, const value_t *v) |
| 447 |
{ |
| 448 |
if (v->type == VT_BOOL) |
| 449 |
{ |
| 450 |
*dst = v->v_bool; |
| 451 |
return STATUS_OK; |
| 452 |
} |
| 453 |
|
| 454 |
// Perform cast |
| 455 |
value_t tmp; |
| 456 |
init_value(&tmp); |
| 457 |
lsp_finally { destroy_value(&tmp); }; |
| 458 |
|
| 459 |
status_t res = cast_bool(&tmp, v); |
| 460 |
if (res == STATUS_OK) |
| 461 |
*dst = tmp.v_bool; |
| 462 |
|
| 463 |
return res; |
| 464 |
} |
| 465 |
|
| 466 |
status_t fetch_as_string(LSPString *dst, const value_t *v) |
| 467 |
{ |
| 468 |
if (v->type == VT_STRING) |
| 469 |
return (dst->set(v->v_str)) ? STATUS_OK : STATUS_NO_MEM; |
| 470 |
|
| 471 |
// Perform cast |
| 472 |
value_t tmp; |
| 473 |
init_value(&tmp); |
| 474 |
lsp_finally { destroy_value(&tmp); }; |
| 475 |
|
| 476 |
status_t res = cast_string(&tmp, v); |
| 477 |
if (res == STATUS_OK) |
| 478 |
dst->swap(tmp.v_str); |
| 479 |
|
| 480 |
return res; |
| 481 |
} |
| 482 |
|
| 483 |
status_t cast_value(value_t *v, value_type_t type) |
| 484 |
{ |
| 485 |
switch (type) |
| 486 |
{ |
| 487 |
case VT_INT: return cast_int(v); |
| 488 |
case VT_FLOAT: return cast_float(v); |
| 489 |
case VT_BOOL: return cast_bool(v); |
| 490 |
case VT_STRING: return cast_string(v); |
| 491 |
case VT_NULL: |
| 492 |
set_value_null(v); |
| 493 |
return STATUS_OK; |
| 494 |
case VT_UNDEF: |
| 495 |
set_value_undef(v); |
| 496 |
return STATUS_OK; |
| 497 |
} |
| 498 |
|
| 499 |
return STATUS_BAD_TYPE; |
| 500 |
} |
| 501 |
|
| 502 |
status_t cast_value(value_t *dst, const value_t *v, value_type_t type) |
| 503 |
{ |
| 504 |
status_t res = copy_value(dst, v); |
| 505 |
if (res != STATUS_OK) |
| 506 |
return res; |
| 507 |
|
| 508 |
switch (type) |
| 509 |
{ |
| 510 |
case VT_INT: return cast_int(dst); |
| 511 |
case VT_FLOAT: return cast_float(dst); |
| 512 |
case VT_BOOL: return cast_bool(dst); |
| 513 |
case VT_STRING: return cast_string(dst); |
| 514 |
case VT_NULL: |
| 515 |
set_value_null(dst); |
| 516 |
return STATUS_OK; |
| 517 |
case VT_UNDEF: |
| 518 |
set_value_undef(dst); |
| 519 |
return STATUS_OK; |
| 520 |
} |
| 521 |
|
| 522 |
return STATUS_BAD_TYPE; |
| 523 |
} |
| 524 |
|
| 525 |
status_t cast_int(value_t *v) |
| 526 |
{ |
| 527 |
switch (v->type) |
| 528 |
{ |
| 529 |
case VT_INT: return STATUS_OK; |
| 530 |
case VT_FLOAT: v->v_int = ssize_t(v->v_float); break; |
| 531 |
case VT_BOOL: v->v_int = (v->v_bool) ? 1 : 0; break; |
| 532 |
case VT_STRING: |
| 533 |
{ |
| 534 |
// Parse integer/float number as string and cast to integer |
| 535 |
io::InStringSequence s(v->v_str); |
| 536 |
Tokenizer t(&s); |
| 537 |
ssize_t ivalue; |
| 538 |
|
| 539 |
switch (t.get_token(TF_GET)) |
| 540 |
{ |
| 541 |
case TT_IVALUE: ivalue = t.int_value(); break; |
| 542 |
case TT_FVALUE: ivalue = t.int_value(); break; |
| 543 |
case TT_TRUE: ivalue = 1; break; |
| 544 |
case TT_FALSE: ivalue = 0; break; |
| 545 |
default: |
| 546 |
delete v->v_str; |
| 547 |
v->type = VT_UNDEF; |
| 548 |
return STATUS_OK; |
| 549 |
} |
| 550 |
|
| 551 |
if (t.get_token(TF_GET) != TT_EOF) |
| 552 |
return STATUS_BAD_FORMAT; |
| 553 |
delete v->v_str; |
| 554 |
v->v_int = ivalue; |
| 555 |
break; |
| 556 |
} |
| 557 |
case VT_NULL: |
| 558 |
case VT_UNDEF: |
| 559 |
return STATUS_OK; |
| 560 |
default: |
| 561 |
return STATUS_BAD_TYPE; |
| 562 |
} |
| 563 |
|
| 564 |
v->type = VT_INT; |
| 565 |
return STATUS_OK; |
| 566 |
} |
| 567 |
|
| 568 |
status_t cast_float(value_t *v) |
| 569 |
{ |
| 570 |
switch (v->type) |
| 571 |
{ |
| 572 |
case VT_INT: v->v_float = v->v_int; break; |
| 573 |
case VT_FLOAT: return STATUS_OK; |
| 574 |
case VT_BOOL: v->v_float = (v->v_bool) ? 1.0 : 0.0; break; |
| 575 |
case VT_STRING: |
| 576 |
{ |
| 577 |
// Parse integer/float number as string and cast to integer |
| 578 |
io::InStringSequence s(v->v_str); |
| 579 |
Tokenizer t(&s); |
| 580 |
double fvalue; |
| 581 |
|
| 582 |
switch (t.get_token(TF_GET)) |
| 583 |
{ |
| 584 |
case TT_IVALUE: fvalue = t.int_value(); break; |
| 585 |
case TT_FVALUE: fvalue = t.float_value(); break; |
| 586 |
case TT_TRUE: fvalue = 1.0; break; |
| 587 |
case TT_FALSE: fvalue = 0.0; break; |
| 588 |
default: |
| 589 |
delete v->v_str; |
| 590 |
v->type = VT_UNDEF; |
| 591 |
return STATUS_OK; |
| 592 |
} |
| 593 |
|
| 594 |
if (t.get_token(TF_GET) != TT_EOF) |
| 595 |
return STATUS_BAD_FORMAT; |
| 596 |
delete v->v_str; |
| 597 |
v->v_float = fvalue; |
| 598 |
break; |
| 599 |
} |
| 600 |
case VT_NULL: |
| 601 |
case VT_UNDEF: |
| 602 |
return STATUS_OK; |
| 603 |
default: |
| 604 |
return STATUS_BAD_TYPE; |
| 605 |
} |
| 606 |
|
| 607 |
v->type = VT_FLOAT; |
| 608 |
return STATUS_OK; |
| 609 |
} |
| 610 |
|
| 611 |
status_t cast_bool(value_t *v) |
| 612 |
{ |
| 613 |
switch (v->type) |
| 614 |
{ |
| 615 |
case VT_INT: v->v_bool = v->v_int != 0; break; |
| 616 |
case VT_FLOAT: v->v_bool = (v->v_float >= 0.5f) || (v->v_float <= -0.5f); break; |
| 617 |
case VT_BOOL: return STATUS_OK; |
| 618 |
case VT_STRING: |
| 619 |
{ |
| 620 |
// Parse integer/float number as string and cast to integer |
| 621 |
io::InStringSequence s(v->v_str); |
| 622 |
Tokenizer t(&s); |
| 623 |
bool bvalue; |
| 624 |
|
| 625 |
switch (t.get_token(TF_GET)) |
| 626 |
{ |
| 627 |
case TT_IVALUE: bvalue = t.int_value() != 0; break; |
| 628 |
case TT_FVALUE: bvalue = (t.float_value() > 0.5f) || (t.float_value() <= -0.5f); break; |
| 629 |
case TT_TRUE: bvalue = true; break; |
| 630 |
case TT_FALSE: bvalue = false; break; |
| 631 |
default: |
| 632 |
delete v->v_str; |
| 633 |
v->type = VT_UNDEF; |
| 634 |
return STATUS_OK; |
| 635 |
} |
| 636 |
|
| 637 |
if (t.get_token(TF_GET) != TT_EOF) |
| 638 |
return STATUS_BAD_FORMAT; |
| 639 |
delete v->v_str; |
| 640 |
v->v_bool = bvalue; |
| 641 |
break; |
| 642 |
} |
| 643 |
case VT_NULL: |
| 644 |
case VT_UNDEF: |
| 645 |
return STATUS_OK; |
| 646 |
default: |
| 647 |
return STATUS_BAD_TYPE; |
| 648 |
} |
| 649 |
|
| 650 |
v->type = VT_BOOL; |
| 651 |
return STATUS_OK; |
| 652 |
} |
| 653 |
|
| 654 |
status_t cast_string(value_t *v) |
| 655 |
{ |
| 656 |
LSPString tmp; |
| 657 |
|
| 658 |
switch (v->type) |
| 659 |
{ |
| 660 |
case VT_INT: |
| 661 |
if (!tmp.fmt_ascii("%ld", long(v->v_int))) |
| 662 |
return STATUS_NO_MEM; |
| 663 |
break; |
| 664 |
case VT_FLOAT: |
| 665 |
{ |
| 666 |
const char *special = special_value(v->v_float); |
| 667 |
if (special) |
| 668 |
{ |
| 669 |
if (!tmp.set_ascii(special)) |
| 670 |
return STATUS_NO_MEM; |
| 671 |
} |
| 672 |
else |
| 673 |
{ |
| 674 |
// Update locale |
| 675 |
SET_LOCALE_SCOPED(LC_NUMERIC, "C"); |
| 676 |
if (!tmp.fmt_ascii("%f", double(v->v_float))) |
| 677 |
return STATUS_NO_MEM; |
| 678 |
} |
| 679 |
break; |
| 680 |
} |
| 681 |
case VT_BOOL: |
| 682 |
if (!tmp.set_ascii((v->v_bool) ? "true" : "false")) |
| 683 |
return STATUS_NO_MEM; |
| 684 |
break; |
| 685 |
case VT_STRING: |
| 686 |
return STATUS_OK; |
| 687 |
case VT_NULL: |
| 688 |
case VT_UNDEF: |
| 689 |
return STATUS_OK; |
| 690 |
default: |
| 691 |
return STATUS_BAD_TYPE; |
| 692 |
} |
| 693 |
|
| 694 |
LSPString *ns = tmp.release(); |
| 695 |
if (ns == NULL) |
| 696 |
return STATUS_NO_MEM; |
| 697 |
|
| 698 |
v->type = VT_STRING; |
| 699 |
v->v_str = ns; |
| 700 |
|
| 701 |
return STATUS_OK; |
| 702 |
} |
| 703 |
|
| 704 |
status_t cast_string_ext(value_t *v) |
| 705 |
{ |
| 706 |
LSPString tmp; |
| 707 |
|
| 708 |
switch (v->type) |
| 709 |
{ |
| 710 |
case VT_INT: |
| 711 |
if (!tmp.fmt_ascii("%ld", long(v->v_int))) |
| 712 |
return STATUS_NO_MEM; |
| 713 |
break; |
| 714 |
case VT_FLOAT: |
| 715 |
{ |
| 716 |
const char *special = special_value(v->v_float); |
| 717 |
if (special) |
| 718 |
{ |
| 719 |
if (!tmp.set_ascii(special)) |
| 720 |
return STATUS_NO_MEM; |
| 721 |
} |
| 722 |
else |
| 723 |
{ |
| 724 |
// Update locale |
| 725 |
SET_LOCALE_SCOPED(LC_NUMERIC, "C"); |
| 726 |
if (!tmp.fmt_ascii("%f", double(v->v_float))) |
| 727 |
return STATUS_NO_MEM; |
| 728 |
} |
| 729 |
break; |
| 730 |
} |
| 731 |
case VT_BOOL: |
| 732 |
if (!tmp.set_ascii((v->v_bool) ? "true" : "false")) |
| 733 |
return STATUS_NO_MEM; |
| 734 |
break; |
| 735 |
case VT_STRING: |
| 736 |
return STATUS_OK; |
| 737 |
case VT_NULL: |
| 738 |
if (!tmp.set_ascii("null")) |
| 739 |
return STATUS_NO_MEM; |
| 740 |
break; |
| 741 |
case VT_UNDEF: |
| 742 |
if (!tmp.set_ascii("undef")) |
| 743 |
return STATUS_NO_MEM; |
| 744 |
break; |
| 745 |
default: |
| 746 |
return STATUS_BAD_TYPE; |
| 747 |
} |
| 748 |
|
| 749 |
LSPString *ns = tmp.release(); |
| 750 |
if (ns == NULL) |
| 751 |
return STATUS_NO_MEM; |
| 752 |
|
| 753 |
v->type = VT_STRING; |
| 754 |
v->v_str = ns; |
| 755 |
|
| 756 |
return STATUS_OK; |
| 757 |
} |
| 758 |
|
| 759 |
status_t cast_numeric(value_t *v) |
| 760 |
{ |
| 761 |
switch (v->type) |
| 762 |
{ |
| 763 |
case VT_INT: return STATUS_OK; |
| 764 |
case VT_FLOAT: return STATUS_OK; |
| 765 |
case VT_BOOL: |
| 766 |
v->type = VT_INT; |
| 767 |
v->v_int = (v->v_bool) ? 1 : 0; |
| 768 |
break; |
| 769 |
case VT_STRING: |
| 770 |
{ |
| 771 |
// Parse integer/float number as string and cast to integer |
| 772 |
value_t xv; |
| 773 |
io::InStringSequence s(v->v_str); |
| 774 |
Tokenizer t(&s); |
| 775 |
|
| 776 |
switch (t.get_token(TF_GET)) |
| 777 |
{ |
| 778 |
case TT_IVALUE: |
| 779 |
xv.type = VT_INT; |
| 780 |
xv.v_int = t.int_value(); |
| 781 |
break; |
| 782 |
case TT_FVALUE: |
| 783 |
xv.type = VT_FLOAT; |
| 784 |
xv.v_float = t.float_value(); |
| 785 |
break; |
| 786 |
case TT_TRUE: |
| 787 |
xv.type = VT_INT; |
| 788 |
xv.v_int = 1; |
| 789 |
break; |
| 790 |
case TT_FALSE: |
| 791 |
xv.type = VT_INT; |
| 792 |
xv.v_int = 0; |
| 793 |
break; |
| 794 |
default: |
| 795 |
delete v->v_str; |
| 796 |
v->type = VT_UNDEF; |
| 797 |
return STATUS_OK; |
| 798 |
} |
| 799 |
|
| 800 |
if (t.get_token(TF_GET) != TT_EOF) |
| 801 |
return STATUS_BAD_FORMAT; |
| 802 |
|
| 803 |
delete v->v_str; |
| 804 |
*v = xv; |
| 805 |
break; |
| 806 |
} |
| 807 |
case VT_NULL: |
| 808 |
case VT_UNDEF: |
| 809 |
return STATUS_OK; |
| 810 |
default: |
| 811 |
return STATUS_BAD_TYPE; |
| 812 |
} |
| 813 |
return STATUS_OK; |
| 814 |
} |
| 815 |
|
| 816 |
status_t cast_non_string(value_t *v) |
| 817 |
{ |
| 818 |
if (v->type != VT_STRING) |
| 819 |
return STATUS_OK; |
| 820 |
|
| 821 |
// Parse integer/float number as string and cast to integer |
| 822 |
value_t xv; |
| 823 |
io::InStringSequence s(v->v_str); |
| 824 |
Tokenizer t(&s); |
| 825 |
|
| 826 |
switch (t.get_token(TF_GET)) |
| 827 |
{ |
| 828 |
case TT_IVALUE: |
| 829 |
xv.type = VT_INT; |
| 830 |
xv.v_int = t.int_value(); |
| 831 |
break; |
| 832 |
case TT_FVALUE: |
| 833 |
xv.type = VT_FLOAT; |
| 834 |
xv.v_float = t.float_value(); |
| 835 |
break; |
| 836 |
case TT_TRUE: |
| 837 |
xv.type = VT_BOOL; |
| 838 |
xv.v_bool = true; |
| 839 |
break; |
| 840 |
case TT_FALSE: |
| 841 |
xv.type = VT_BOOL; |
| 842 |
xv.v_bool = 0; |
| 843 |
break; |
| 844 |
default: |
| 845 |
delete v->v_str; |
| 846 |
v->type = VT_UNDEF; |
| 847 |
return STATUS_OK; |
| 848 |
} |
| 849 |
|
| 850 |
if (t.get_token(TF_GET) != TT_EOF) |
| 851 |
return STATUS_BAD_FORMAT; |
| 852 |
|
| 853 |
delete v->v_str; |
| 854 |
*v = xv; |
| 855 |
|
| 856 |
return STATUS_OK; |
| 857 |
} |
| 858 |
|
| 859 |
status_t cast_int(value_t *dst, const value_t *v) |
| 860 |
{ |
| 861 |
status_t res = copy_value(dst, v); |
| 862 |
if (res == STATUS_OK) |
| 863 |
res = cast_int(dst); |
| 864 |
return res; |
| 865 |
} |
| 866 |
|
| 867 |
status_t cast_float(value_t *dst, const value_t *v) |
| 868 |
{ |
| 869 |
status_t res = copy_value(dst, v); |
| 870 |
if (res == STATUS_OK) |
| 871 |
res = cast_float(dst); |
| 872 |
return res; |
| 873 |
} |
| 874 |
|
| 875 |
status_t cast_bool(value_t *dst, const value_t *v) |
| 876 |
{ |
| 877 |
status_t res = copy_value(dst, v); |
| 878 |
if (res == STATUS_OK) |
| 879 |
res = cast_bool(dst); |
| 880 |
return res; |
| 881 |
} |
| 882 |
|
| 883 |
status_t cast_string(value_t *dst, const value_t *v) |
| 884 |
{ |
| 885 |
status_t res = copy_value(dst, v); |
| 886 |
if (res == STATUS_OK) |
| 887 |
res = cast_string(dst); |
| 888 |
return res; |
| 889 |
} |
| 890 |
|
| 891 |
status_t cast_string_ext(value_t *dst, const value_t *v) |
| 892 |
{ |
| 893 |
status_t res = copy_value(dst, v); |
| 894 |
if (res == STATUS_OK) |
| 895 |
res = cast_string_ext(dst); |
| 896 |
return res; |
| 897 |
} |
| 898 |
|
| 899 |
status_t cast_numeric(value_t *dst, const value_t *v) |
| 900 |
{ |
| 901 |
status_t res = copy_value(dst, v); |
| 902 |
if (res == STATUS_OK) |
| 903 |
res = cast_numeric(dst); |
| 904 |
return res; |
| 905 |
} |
| 906 |
|
| 907 |
status_t cast_non_string(value_t *dst, const value_t *v) |
| 908 |
{ |
| 909 |
if (v->type != VT_STRING) |
| 910 |
return STATUS_OK; |
| 911 |
|
| 912 |
// Parse integer/float number as string and cast to integer |
| 913 |
value_t xv; |
| 914 |
io::InStringSequence s(v->v_str); |
| 915 |
Tokenizer t(&s); |
| 916 |
|
| 917 |
switch (t.get_token(TF_GET)) |
| 918 |
{ |
| 919 |
case TT_IVALUE: |
| 920 |
xv.type = VT_INT; |
| 921 |
xv.v_int = t.int_value(); |
| 922 |
break; |
| 923 |
case TT_FVALUE: |
| 924 |
xv.type = VT_FLOAT; |
| 925 |
xv.v_float = t.float_value(); |
| 926 |
break; |
| 927 |
case TT_TRUE: |
| 928 |
xv.type = VT_BOOL; |
| 929 |
xv.v_bool = true; |
| 930 |
break; |
| 931 |
case TT_FALSE: |
| 932 |
xv.type = VT_BOOL; |
| 933 |
xv.v_bool = false; |
| 934 |
break; |
| 935 |
default: |
| 936 |
xv.type = VT_UNDEF; |
| 937 |
break; |
| 938 |
} |
| 939 |
|
| 940 |
if (t.get_token(TF_GET) != TT_EOF) |
| 941 |
return STATUS_BAD_FORMAT; |
| 942 |
|
| 943 |
return copy_value(dst, &xv); |
| 944 |
} |
| 945 |
|
| 946 |
} /* namespace expr */ |
| 947 |
} /* namespace lsp */ |
| 948 |
|
| 949 |
|