| 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: 25 февр. 2023 г. |
| 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/format.h> |
| 23 |
#include <lsp-plug.in/expr/Tokenizer.h> |
| 24 |
#include <lsp-plug.in/io/InStringSequence.h> |
| 25 |
#include <lsp-plug.in/io/OutStringSequence.h> |
| 26 |
#include <lsp-plug.in/stdlib/locale.h> |
| 27 |
#include <lsp-plug.in/stdlib/math.h> |
| 28 |
#include <lsp-plug.in/stdlib/stdio.h> |
| 29 |
#include <lsp-plug.in/common/debug.h> |
| 30 |
|
| 31 |
#ifdef PLATFORM_WINDOWS |
| 32 |
#include <ctype.h> |
| 33 |
#endif /* PLATFORM_WINDOWS */ |
| 34 |
|
| 35 |
namespace lsp |
| 36 |
{ |
| 37 |
namespace expr |
| 38 |
{ |
| 39 |
status_t format(io::IOutSequence *out, const char *fmt, const Parameters *r) |
| 40 |
{ |
| 41 |
if ((out == NULL) || (fmt == NULL)) |
| 42 |
return STATUS_BAD_ARGUMENTS; |
| 43 |
|
| 44 |
io::InStringSequence xfmt; |
| 45 |
status_t res = xfmt.wrap(fmt); |
| 46 |
if (res != STATUS_OK) |
| 47 |
{ |
| 48 |
xfmt.close(); |
| 49 |
return res; |
| 50 |
} |
| 51 |
|
| 52 |
res = format(out, &xfmt, r); |
| 53 |
if (res != STATUS_OK) |
| 54 |
{ |
| 55 |
xfmt.close(); |
| 56 |
return res; |
| 57 |
} |
| 58 |
|
| 59 |
return xfmt.close(); |
| 60 |
} |
| 61 |
|
| 62 |
status_t format(io::IOutSequence *out, const LSPString *fmt, const Parameters *r) |
| 63 |
{ |
| 64 |
if ((out == NULL) || (fmt == NULL)) |
| 65 |
return STATUS_BAD_ARGUMENTS; |
| 66 |
|
| 67 |
io::InStringSequence xfmt; |
| 68 |
status_t res = xfmt.wrap(fmt); |
| 69 |
if (res != STATUS_OK) |
| 70 |
{ |
| 71 |
xfmt.close(); |
| 72 |
return res; |
| 73 |
} |
| 74 |
|
| 75 |
res = format(out, &xfmt, r); |
| 76 |
if (res != STATUS_OK) |
| 77 |
{ |
| 78 |
xfmt.close(); |
| 79 |
return res; |
| 80 |
} |
| 81 |
|
| 82 |
return xfmt.close(); |
| 83 |
} |
| 84 |
|
| 85 |
status_t format(LSPString *out, io::IInSequence *fmt, const Parameters *r) |
| 86 |
{ |
| 87 |
if ((out == NULL) || (fmt == NULL)) |
| 88 |
return STATUS_BAD_ARGUMENTS; |
| 89 |
|
| 90 |
io::OutStringSequence xout; |
| 91 |
out->set_length(0); |
| 92 |
status_t res = xout.wrap(out, false); |
| 93 |
if (res != STATUS_OK) |
| 94 |
{ |
| 95 |
xout.close(); |
| 96 |
return res; |
| 97 |
} |
| 98 |
|
| 99 |
res = format(&xout, fmt, r); |
| 100 |
if (res != STATUS_OK) |
| 101 |
{ |
| 102 |
xout.close(); |
| 103 |
return res; |
| 104 |
} |
| 105 |
|
| 106 |
return xout.close(); |
| 107 |
} |
| 108 |
|
| 109 |
status_t format(LSPString *out, const char *fmt, const Parameters *r) |
| 110 |
{ |
| 111 |
if ((out == NULL) || (fmt == NULL)) |
| 112 |
return STATUS_BAD_ARGUMENTS; |
| 113 |
|
| 114 |
io::OutStringSequence xout; |
| 115 |
out->set_length(0); |
| 116 |
status_t res = xout.wrap(out, false); |
| 117 |
if (res != STATUS_OK) |
| 118 |
{ |
| 119 |
xout.close(); |
| 120 |
return res; |
| 121 |
} |
| 122 |
|
| 123 |
res = format(&xout, fmt, r); |
| 124 |
if (res != STATUS_OK) |
| 125 |
{ |
| 126 |
xout.close(); |
| 127 |
return res; |
| 128 |
} |
| 129 |
|
| 130 |
return xout.close(); |
| 131 |
} |
| 132 |
|
| 133 |
status_t format(LSPString *out, const LSPString *fmt, const Parameters *r) |
| 134 |
{ |
| 135 |
if ((out == NULL) || (fmt == NULL)) |
| 136 |
return STATUS_BAD_ARGUMENTS; |
| 137 |
|
| 138 |
io::OutStringSequence xout; |
| 139 |
out->set_length(0); |
| 140 |
status_t res = xout.wrap(out, false); |
| 141 |
if (res != STATUS_OK) |
| 142 |
{ |
| 143 |
xout.close(); |
| 144 |
return res; |
| 145 |
} |
| 146 |
|
| 147 |
res = format(&xout, fmt, r); |
| 148 |
if (res != STATUS_OK) |
| 149 |
{ |
| 150 |
xout.close(); |
| 151 |
return res; |
| 152 |
} |
| 153 |
|
| 154 |
return xout.close(); |
| 155 |
} |
| 156 |
|
| 157 |
enum align_type_t |
| 158 |
{ |
| 159 |
AL_NONE, // No alignment |
| 160 |
AL_LEFT, // '<' - pad formatted value from right |
| 161 |
AL_RIGHT, // '>' - pad formatted value from left |
| 162 |
AL_MIDDLE, // '|' - pad formatted value from left and right |
| 163 |
AL_FROM_LEFT, // '>|' - pad formatted value from left and right, prefer right alignment |
| 164 |
AL_FROM_RIGHT, // '|<' - pad formatted value from left and right, prefer left alignment |
| 165 |
AL_TO_LEFT, // '<|' - take 1/4 from left, 3/4 from right |
| 166 |
AL_TO_RIGHT, // '|>' - take 1/4 from right, 3/4 from left |
| 167 |
}; |
| 168 |
|
| 169 |
enum fmt_flags_t |
| 170 |
{ |
| 171 |
F_NAME = 1 << 0, |
| 172 |
F_INDEX = 1 << 1, |
| 173 |
F_TYPE = 1 << 2, |
| 174 |
F_WIDTH = 1 << 3, |
| 175 |
F_FRAC = 1 << 4, |
| 176 |
F_SIGN = 1 << 5, |
| 177 |
F_LPAD = 1 << 6, |
| 178 |
F_RPAD = 1 << 7, |
| 179 |
F_XWIDTH = 1 << 8 |
| 180 |
}; |
| 181 |
|
| 182 |
typedef struct fmt_spec_t |
| 183 |
{ |
| 184 |
LSPString buf; // Buffer to store full specifier |
| 185 |
LSPString name; // Name of the parameter |
| 186 |
size_t index; // Index of parameter |
| 187 |
size_t flags; // Format flags |
| 188 |
lsp_wchar_t lpad; // Padding left |
| 189 |
lsp_wchar_t rpad; // Padding right |
| 190 |
align_type_t align; // Alignment |
| 191 |
lsp_wchar_t type; // Type |
| 192 |
size_t width; // Width |
| 193 |
size_t xwidth; // Extra width |
| 194 |
size_t frac; // Fraction |
| 195 |
} fmt_spec_t; |
| 196 |
|
| 197 |
void init_spec(fmt_spec_t *spec, size_t index) |
| 198 |
{ |
| 199 |
spec->buf.clear(); |
| 200 |
spec->name.clear(); |
| 201 |
spec->index = index; |
| 202 |
spec->flags = 0; |
| 203 |
spec->lpad = ' '; |
| 204 |
spec->rpad = ' '; |
| 205 |
spec->align = AL_NONE; |
| 206 |
spec->type = 0; |
| 207 |
spec->width = 0; |
| 208 |
spec->xwidth = 0; |
| 209 |
spec->frac = 0; |
| 210 |
} |
| 211 |
|
| 212 |
status_t read_specifier(io::IOutSequence *out, io::IInSequence *fmt, fmt_spec_t *spec) |
| 213 |
{ |
| 214 |
lsp_swchar_t c; |
| 215 |
status_t res = STATUS_OK; |
| 216 |
|
| 217 |
// Read format specifier as string |
| 218 |
while (true) |
| 219 |
{ |
| 220 |
// Read next character |
| 221 |
c = fmt->read(); |
| 222 |
if (c < 0) |
| 223 |
{ |
| 224 |
if (c != -STATUS_EOF) |
| 225 |
return -c; |
| 226 |
|
| 227 |
// Format specifier has been interrupted |
| 228 |
if ((res = out->write('{')) != STATUS_OK) |
| 229 |
return res; |
| 230 |
if ((res = out->write(&spec->buf)) != STATUS_OK) |
| 231 |
return res; |
| 232 |
return STATUS_BAD_FORMAT; |
| 233 |
} |
| 234 |
|
| 235 |
// End of format specifier? |
| 236 |
if (c == '}') |
| 237 |
break; |
| 238 |
|
| 239 |
// Append character and repeat |
| 240 |
if (!spec->buf.append(c)) |
| 241 |
return STATUS_NO_MEM; |
| 242 |
} |
| 243 |
|
| 244 |
// now parse format specifier |
| 245 |
size_t len = spec->buf.length(); |
| 246 |
for (size_t i=0; i<len; ) |
| 247 |
{ |
| 248 |
c = spec->buf.char_at(i++); |
| 249 |
|
| 250 |
switch (c) |
| 251 |
{ |
| 252 |
case '@': // Name |
| 253 |
// Prevent from duplicate definitions |
| 254 |
if (spec->flags & (F_NAME | F_INDEX)) |
| 255 |
{ |
| 256 |
res = STATUS_BAD_FORMAT; |
| 257 |
break; |
| 258 |
} |
| 259 |
|
| 260 |
// Check first character |
| 261 |
c = (i < len) ? spec->buf.char_at(i++) : 0; |
| 262 |
if (Tokenizer::is_identifier_first(c)) |
| 263 |
{ |
| 264 |
// Append character |
| 265 |
spec->flags |= F_NAME; |
| 266 |
if (!spec->name.append(c)) |
| 267 |
{ |
| 268 |
res = STATUS_NO_MEM; |
| 269 |
break; |
| 270 |
} |
| 271 |
|
| 272 |
// Check other characters |
| 273 |
for (; i < len; ++i) |
| 274 |
{ |
| 275 |
c = spec->buf.char_at(i); |
| 276 |
if (!Tokenizer::is_identifier_next(c)) |
| 277 |
break; |
| 278 |
else if (!spec->name.append(c)) |
| 279 |
{ |
| 280 |
res = STATUS_NO_MEM; |
| 281 |
break; |
| 282 |
} |
| 283 |
} |
| 284 |
} |
| 285 |
else |
| 286 |
res = STATUS_BAD_FORMAT; |
| 287 |
|
| 288 |
break; |
| 289 |
case '[': // Index |
| 290 |
// Prevent from duplicate definitions |
| 291 |
if (spec->flags & (F_NAME | F_INDEX)) |
| 292 |
{ |
| 293 |
res = STATUS_BAD_FORMAT; |
| 294 |
break; |
| 295 |
} |
| 296 |
|
| 297 |
// Read decimal index |
| 298 |
spec->index = 0; |
| 299 |
for ( ; i < len; ++i) |
| 300 |
{ |
| 301 |
c = spec->buf.char_at(i); |
| 302 |
if ((c < '0') || (c > '9')) |
| 303 |
break; |
| 304 |
|
| 305 |
spec->index = spec->index * 10 + (c - '0'); |
| 306 |
spec->flags |= F_INDEX; |
| 307 |
} |
| 308 |
|
| 309 |
// Require final ']' |
| 310 |
if ((spec->flags & F_INDEX) && (i < len)) |
| 311 |
{ |
| 312 |
c = spec->buf.char_at(i++); |
| 313 |
if (c != ']') |
| 314 |
res = STATUS_BAD_FORMAT; |
| 315 |
} |
| 316 |
else |
| 317 |
res = STATUS_BAD_FORMAT; |
| 318 |
|
| 319 |
break; |
| 320 |
|
| 321 |
case '^': // Left padding |
| 322 |
if ((!(spec->flags & F_LPAD)) && (i < len)) |
| 323 |
{ |
| 324 |
spec->flags |= F_LPAD; |
| 325 |
spec->lpad = spec->buf.char_at(i++); |
| 326 |
} |
| 327 |
else |
| 328 |
res = STATUS_BAD_FORMAT; |
| 329 |
break; |
| 330 |
|
| 331 |
case '$': // Right padding |
| 332 |
if ((!(spec->flags & F_RPAD)) && (i < len)) |
| 333 |
{ |
| 334 |
spec->flags |= F_RPAD; |
| 335 |
spec->rpad = spec->buf.char_at(i++); |
| 336 |
} |
| 337 |
else |
| 338 |
res = STATUS_BAD_FORMAT; |
| 339 |
break; |
| 340 |
|
| 341 |
case '>': // Right alignment |
| 342 |
if (spec->align == AL_NONE) |
| 343 |
{ |
| 344 |
spec->align = AL_RIGHT; |
| 345 |
if ((i < len) && (spec->buf.char_at(i) == '|')) // '>|' |
| 346 |
{ |
| 347 |
spec->align = AL_FROM_LEFT; |
| 348 |
++i; |
| 349 |
} |
| 350 |
} |
| 351 |
else |
| 352 |
res = STATUS_BAD_FORMAT; |
| 353 |
break; |
| 354 |
|
| 355 |
case '<': // Left alignment |
| 356 |
if (spec->align == AL_NONE) |
| 357 |
{ |
| 358 |
spec->align = AL_LEFT; |
| 359 |
if ((i < len) && (spec->buf.char_at(i) == '|')) // '<|' |
| 360 |
{ |
| 361 |
spec->align = AL_TO_LEFT; |
| 362 |
++i; |
| 363 |
} |
| 364 |
} |
| 365 |
else |
| 366 |
res = STATUS_BAD_FORMAT; |
| 367 |
break; |
| 368 |
|
| 369 |
case '|': // Middle alignment |
| 370 |
if (spec->align == AL_NONE) |
| 371 |
{ |
| 372 |
spec->align = AL_MIDDLE; |
| 373 |
if (i < len) |
| 374 |
{ |
| 375 |
// Lookup additional modes |
| 376 |
c = spec->buf.char_at(i); |
| 377 |
if (c == '<') // '|<' |
| 378 |
{ |
| 379 |
spec->align = AL_FROM_RIGHT; |
| 380 |
++i; |
| 381 |
} |
| 382 |
else if (c == '>') // '|>' |
| 383 |
{ |
| 384 |
spec->align = AL_TO_RIGHT; |
| 385 |
++i; |
| 386 |
} |
| 387 |
} |
| 388 |
} |
| 389 |
else |
| 390 |
res = STATUS_BAD_FORMAT; |
| 391 |
break; |
| 392 |
|
| 393 |
case ':': // Extra width |
| 394 |
// Read extra width |
| 395 |
for ( ; i < len; ++i) |
| 396 |
{ |
| 397 |
c = spec->buf.char_at(i); |
| 398 |
if ((c < '0') || (c > '9')) |
| 399 |
break; |
| 400 |
|
| 401 |
spec->flags |= F_XWIDTH; |
| 402 |
spec->xwidth = spec->xwidth * 10 + (c - '0'); |
| 403 |
} |
| 404 |
|
| 405 |
// Ensure that there is something read |
| 406 |
if (!(spec->flags & F_XWIDTH)) |
| 407 |
res = STATUS_BAD_FORMAT; |
| 408 |
break; |
| 409 |
|
| 410 |
case '%': // Type specifier |
| 411 |
// Prevent from duplicate definition |
| 412 |
if (spec->flags & F_TYPE) |
| 413 |
{ |
| 414 |
res = STATUS_BAD_FORMAT; |
| 415 |
break; |
| 416 |
} |
| 417 |
spec->flags |= F_TYPE; |
| 418 |
|
| 419 |
// Lookup for sign |
| 420 |
if ((i < len) && (spec->buf.char_at(i) == '+')) |
| 421 |
{ |
| 422 |
spec->flags |= F_SIGN; |
| 423 |
++i; |
| 424 |
} |
| 425 |
|
| 426 |
// Read width |
| 427 |
for ( ; i < len; ++i) |
| 428 |
{ |
| 429 |
c = spec->buf.char_at(i); |
| 430 |
if ((c < '0') || (c > '9')) |
| 431 |
break; |
| 432 |
|
| 433 |
spec->flags |= F_WIDTH; |
| 434 |
spec->width = spec->width * 10 + (c - '0'); |
| 435 |
} |
| 436 |
|
| 437 |
// Lookup for dot |
| 438 |
if ((i < len) && (spec->buf.char_at(i) == '.')) |
| 439 |
{ |
| 440 |
// Read fraction |
| 441 |
while ((++i) < len) |
| 442 |
{ |
| 443 |
c = spec->buf.char_at(i); |
| 444 |
if ((c < '0') || (c > '9')) |
| 445 |
break; |
| 446 |
|
| 447 |
spec->flags |= F_FRAC; |
| 448 |
spec->frac = spec->frac * 10 + (c - '0'); |
| 449 |
} |
| 450 |
|
| 451 |
// Check that fraction part is present |
| 452 |
if (!(spec->flags & F_FRAC)) |
| 453 |
{ |
| 454 |
res = STATUS_BAD_FORMAT; |
| 455 |
break; |
| 456 |
} |
| 457 |
} |
| 458 |
|
| 459 |
// Read type specifier |
| 460 |
if (i >= len) |
| 461 |
{ |
| 462 |
res = STATUS_BAD_FORMAT; |
| 463 |
break; |
| 464 |
} |
| 465 |
|
| 466 |
c = spec->buf.char_at(i++); |
| 467 |
switch (c) |
| 468 |
{ |
| 469 |
case 'i': case 'd': case 'u': // decimals |
| 470 |
case 'b': case 'o': case 'x': case 'X': // octals, binaries, hexadecimals |
| 471 |
case 'f': case 'F': case 'e': case 'E': // floating-points |
| 472 |
case 's': case 'S': // string |
| 473 |
spec->type = c; |
| 474 |
break; |
| 475 |
case 'l': // boolean, bOOLEAN |
| 476 |
c = (i < len) ? spec->buf.char_at(i) : 0; |
| 477 |
if (c == 'l') |
| 478 |
{ |
| 479 |
++i; |
| 480 |
spec->type = 'l'; |
| 481 |
} |
| 482 |
else if (c == 'L') |
| 483 |
{ |
| 484 |
++i; |
| 485 |
spec->type = 'z'; |
| 486 |
} |
| 487 |
else |
| 488 |
spec->type = 'l'; |
| 489 |
break; |
| 490 |
|
| 491 |
case 'L': // BOOLEAN, Boolean |
| 492 |
c = (i < len) ? spec->buf.char_at(i) : 0; |
| 493 |
if (c == 'l') |
| 494 |
{ |
| 495 |
++i; |
| 496 |
spec->type = 'Z'; |
| 497 |
} |
| 498 |
else if (c == 'L') |
| 499 |
{ |
| 500 |
++i; |
| 501 |
spec->type = 'L'; |
| 502 |
} |
| 503 |
else |
| 504 |
spec->type = 'L'; |
| 505 |
break; |
| 506 |
|
| 507 |
case 't': // text, tEXT |
| 508 |
c = (i < len) ? spec->buf.char_at(i) : 0; |
| 509 |
if (c == 't') |
| 510 |
{ |
| 511 |
++i; |
| 512 |
spec->type = 't'; |
| 513 |
} |
| 514 |
else if (c == 'T') |
| 515 |
{ |
| 516 |
++i; |
| 517 |
spec->type = 'y'; |
| 518 |
} |
| 519 |
else |
| 520 |
spec->type = 't'; |
| 521 |
break; |
| 522 |
case 'T': // TEXT, Text |
| 523 |
c = (i < len) ? spec->buf.char_at(i) : 0; |
| 524 |
if (c == 't') |
| 525 |
{ |
| 526 |
++i; |
| 527 |
spec->type = 'Y'; |
| 528 |
} |
| 529 |
else if (c == 'T') |
| 530 |
{ |
| 531 |
++i; |
| 532 |
spec->type = 'T'; |
| 533 |
} |
| 534 |
else |
| 535 |
spec->type = 'T'; |
| 536 |
break; |
| 537 |
default: |
| 538 |
// No type specifier, just width |
| 539 |
--i; |
| 540 |
break; |
| 541 |
} |
| 542 |
break; |
| 543 |
|
| 544 |
default: |
| 545 |
res = STATUS_BAD_FORMAT; |
| 546 |
break; |
| 547 |
} |
| 548 |
|
| 549 |
// Check result |
| 550 |
if (res == STATUS_BAD_FORMAT) |
| 551 |
{ |
| 552 |
if ((res = out->write('{')) != STATUS_OK) |
| 553 |
return res; |
| 554 |
if ((res = out->write(&spec->buf)) != STATUS_OK) |
| 555 |
return res; |
| 556 |
if ((res = out->write('}')) != STATUS_OK) |
| 557 |
return res; |
| 558 |
res = STATUS_BAD_FORMAT; |
| 559 |
break; |
| 560 |
} |
| 561 |
else if (res != STATUS_OK) |
| 562 |
break; |
| 563 |
} |
| 564 |
|
| 565 |
return res; |
| 566 |
} |
| 567 |
|
| 568 |
status_t check_specials(fmt_spec_t *spec, value_t *v) |
| 569 |
{ |
| 570 |
if (v->type == VT_NULL) |
| 571 |
return (spec->buf.set_ascii("<null>")) ? STATUS_SKIP : STATUS_NO_MEM; |
| 572 |
else if (v->type == VT_UNDEF) |
| 573 |
return (spec->buf.set_ascii("<undef>")) ? STATUS_SKIP : STATUS_NO_MEM; |
| 574 |
return STATUS_OK; |
| 575 |
} |
| 576 |
|
| 577 |
status_t int_append_extra(fmt_spec_t *spec, value_t *v) |
| 578 |
{ |
| 579 |
// Append to specified width |
| 580 |
if (spec->flags & F_WIDTH) |
| 581 |
{ |
| 582 |
while (spec->buf.length() < spec->width) |
| 583 |
{ |
| 584 |
if (!spec->buf.append('0')) |
| 585 |
return STATUS_NO_MEM; |
| 586 |
} |
| 587 |
} |
| 588 |
|
| 589 |
// Append sign |
| 590 |
if (v->v_int < 0) |
| 591 |
{ |
| 592 |
if (!spec->buf.append('-')) |
| 593 |
return STATUS_NO_MEM; |
| 594 |
} |
| 595 |
else if (spec->flags & F_SIGN) |
| 596 |
{ |
| 597 |
if (!spec->buf.append('+')) |
| 598 |
return STATUS_NO_MEM; |
| 599 |
} |
| 600 |
|
| 601 |
return STATUS_OK; |
| 602 |
} |
| 603 |
|
| 604 |
status_t int_to_dec(fmt_spec_t *spec, value_t *v) |
| 605 |
{ |
| 606 |
status_t res = check_specials(spec, v); |
| 607 |
if (res != STATUS_OK) |
| 608 |
return (res == STATUS_SKIP) ? STATUS_OK : res; |
| 609 |
|
| 610 |
ssize_t x = lsp_abs(v->v_int); |
| 611 |
do |
| 612 |
{ |
| 613 |
lsp_swchar_t rem = x % 10; |
| 614 |
if (!spec->buf.append(lsp_wchar_t((rem >= 0) ? '0' + rem : '0' - rem))) |
| 615 |
return STATUS_NO_MEM; |
| 616 |
x /= 10; |
| 617 |
} while (x); |
| 618 |
|
| 619 |
if ((res = int_append_extra(spec, v)) != STATUS_OK) |
| 620 |
return res; |
| 621 |
|
| 622 |
spec->buf.reverse(); |
| 623 |
return STATUS_OK; |
| 624 |
} |
| 625 |
|
| 626 |
status_t uint_to_dec(fmt_spec_t *spec, value_t *v) |
| 627 |
{ |
| 628 |
status_t res = check_specials(spec, v); |
| 629 |
if (res != STATUS_OK) |
| 630 |
return (res == STATUS_SKIP) ? STATUS_OK : res; |
| 631 |
|
| 632 |
size_t x = size_t(v->v_int); |
| 633 |
do |
| 634 |
{ |
| 635 |
if (!spec->buf.append(lsp_wchar_t((x % 10) + '0'))) |
| 636 |
return STATUS_NO_MEM; |
| 637 |
x /= 10; |
| 638 |
} while (x); |
| 639 |
|
| 640 |
if ((res = int_append_extra(spec, v)) != STATUS_OK) |
| 641 |
return res; |
| 642 |
|
| 643 |
spec->buf.reverse(); |
| 644 |
return STATUS_OK; |
| 645 |
} |
| 646 |
|
| 647 |
status_t int_to_bin(fmt_spec_t *spec, value_t *v) |
| 648 |
{ |
| 649 |
status_t res = check_specials(spec, v); |
| 650 |
if (res != STATUS_OK) |
| 651 |
return (res == STATUS_SKIP) ? STATUS_OK : res; |
| 652 |
|
| 653 |
size_t x = lsp_abs(v->v_int); |
| 654 |
do |
| 655 |
{ |
| 656 |
if (!spec->buf.append(lsp_wchar_t((x & 1) + '0'))) |
| 657 |
return STATUS_NO_MEM; |
| 658 |
x >>= 1; |
| 659 |
} while (x); |
| 660 |
|
| 661 |
if ((res = int_append_extra(spec, v)) != STATUS_OK) |
| 662 |
return res; |
| 663 |
|
| 664 |
spec->buf.reverse(); |
| 665 |
return STATUS_OK; |
| 666 |
} |
| 667 |
|
| 668 |
status_t int_to_oct(fmt_spec_t *spec, value_t *v) |
| 669 |
{ |
| 670 |
status_t res = check_specials(spec, v); |
| 671 |
if (res != STATUS_OK) |
| 672 |
return (res == STATUS_SKIP) ? STATUS_OK : res; |
| 673 |
|
| 674 |
size_t x = v->v_int; |
| 675 |
do |
| 676 |
{ |
| 677 |
if (!spec->buf.append(lsp_wchar_t((x & 0x7) + '0'))) |
| 678 |
return STATUS_NO_MEM; |
| 679 |
x >>= 3; |
| 680 |
} while (x); |
| 681 |
|
| 682 |
if ((res = int_append_extra(spec, v)) != STATUS_OK) |
| 683 |
return res; |
| 684 |
|
| 685 |
spec->buf.reverse(); |
| 686 |
return STATUS_OK; |
| 687 |
} |
| 688 |
|
| 689 |
static const char *hex_table = "0123456789abcdef0123456789ABCDEF"; |
| 690 |
|
| 691 |
status_t int_to_hex(fmt_spec_t *spec, value_t *v) |
| 692 |
{ |
| 693 |
status_t res = check_specials(spec, v); |
| 694 |
if (res != STATUS_OK) |
| 695 |
return (res == STATUS_SKIP) ? STATUS_OK : res; |
| 696 |
|
| 697 |
const char *table = (spec->type == 'X') ? &hex_table[16] : hex_table; |
| 698 |
size_t x = (v->v_int < 0) ? -v->v_int : v->v_int; |
| 699 |
|
| 700 |
do |
| 701 |
{ |
| 702 |
if (!spec->buf.append(table[x & 0xf])) |
| 703 |
return STATUS_NO_MEM; |
| 704 |
x >>= 4; |
| 705 |
} while (x); |
| 706 |
|
| 707 |
if ((res = int_append_extra(spec, v)) != STATUS_OK) |
| 708 |
return res; |
| 709 |
|
| 710 |
spec->buf.reverse(); |
| 711 |
return STATUS_OK; |
| 712 |
} |
| 713 |
|
| 714 |
inline bool is_float_upper(fmt_spec_t *spec) |
| 715 |
{ |
| 716 |
return ((spec->type == 'F') || (spec->type == 'E')); |
| 717 |
} |
| 718 |
|
| 719 |
status_t float_to_str(fmt_spec_t *spec, value_t *v) |
| 720 |
{ |
| 721 |
status_t res = check_specials(spec, v); |
| 722 |
if (res != STATUS_OK) |
| 723 |
return (res == STATUS_SKIP) ? STATUS_OK : res; |
| 724 |
|
| 725 |
if (isnan(v->v_float)) |
| 726 |
{ |
| 727 |
const char *text = (is_float_upper(spec)) ? "NAN" : "nan"; |
| 728 |
res = (spec->buf.set_ascii(text)) ? STATUS_OK : STATUS_NO_MEM; |
| 729 |
} |
| 730 |
else if (isinf(v->v_float)) |
| 731 |
{ |
| 732 |
const char *text; |
| 733 |
if (v->v_float < 0) |
| 734 |
{ |
| 735 |
v->v_float = INFINITY; |
| 736 |
text = (is_float_upper(spec)) ? "-INF" : "-inf"; |
| 737 |
} |
| 738 |
else if (spec->flags & F_SIGN) |
| 739 |
text = (is_float_upper(spec)) ? "+INF" : "+inf"; |
| 740 |
else |
| 741 |
text = (is_float_upper(spec)) ? "INF" : "inf"; |
| 742 |
|
| 743 |
res = (spec->buf.set_ascii(text)) ? STATUS_OK : STATUS_NO_MEM; |
| 744 |
} |
| 745 |
else |
| 746 |
{ |
| 747 |
char fmt[64]; |
| 748 |
|
| 749 |
SET_LOCALE_SCOPED(LC_NUMERIC, "C"); |
| 750 |
|
| 751 |
#ifdef PLATFORM_WINDOWS |
| 752 |
// Windows format string does not respect uppercase format letters. |
| 753 |
// That's why we need to do some workaround work |
| 754 |
// Initialize format specifier |
| 755 |
bool uppercase = isupper(spec->type); |
| 756 |
char spec_type = tolower(spec->type); |
| 757 |
if (spec->flags & F_FRAC) |
| 758 |
::snprintf(fmt, sizeof(fmt), "%%.%d%c", int(spec->frac), spec_type); |
| 759 |
else |
| 760 |
::snprintf(fmt, sizeof(fmt), "%%.6%c", spec_type); |
| 761 |
fmt[sizeof(fmt)-1] = '\0'; |
| 762 |
|
| 763 |
// Format the floating-point value and reverse buffer |
| 764 |
if (!spec->buf.fmt_ascii(fmt, lsp_abs(v->v_float))) |
| 765 |
return STATUS_NO_MEM; |
| 766 |
|
| 767 |
if (uppercase) |
| 768 |
spec->buf.toupper(); |
| 769 |
spec->buf.reverse(); |
| 770 |
#else |
| 771 |
// Initialize format specifier |
| 772 |
if (spec->flags & F_FRAC) |
| 773 |
::snprintf(fmt, sizeof(fmt), "%%.%d%c", int(spec->frac), char(spec->type)); |
| 774 |
else |
| 775 |
::snprintf(fmt, sizeof(fmt), "%%.6%c", char(spec->type)); |
| 776 |
fmt[sizeof(fmt)-1] = '\0'; |
| 777 |
|
| 778 |
// Format the floating-point value and reverse buffer |
| 779 |
if (!spec->buf.fmt_ascii(fmt, lsp_abs(v->v_float))) |
| 780 |
return STATUS_NO_MEM; |
| 781 |
spec->buf.reverse(); |
| 782 |
#endif /* PLATFORM_WINDOWS */ |
| 783 |
|
| 784 |
// Append to specified width |
| 785 |
if (spec->flags & F_WIDTH) |
| 786 |
{ |
| 787 |
while (spec->buf.length() < spec->width) |
| 788 |
{ |
| 789 |
if (!spec->buf.append('0')) |
| 790 |
return STATUS_NO_MEM; |
| 791 |
} |
| 792 |
} |
| 793 |
|
| 794 |
// Append sign |
| 795 |
if (v->v_float < 0) |
| 796 |
{ |
| 797 |
if (!spec->buf.append('-')) |
| 798 |
return STATUS_NO_MEM; |
| 799 |
} |
| 800 |
else if (spec->flags & F_SIGN) |
| 801 |
{ |
| 802 |
if (!spec->buf.append('+')) |
| 803 |
return STATUS_NO_MEM; |
| 804 |
} |
| 805 |
|
| 806 |
// Reverse again |
| 807 |
spec->buf.reverse(); |
| 808 |
} |
| 809 |
|
| 810 |
return res; |
| 811 |
} |
| 812 |
|
| 813 |
status_t text_to_str(fmt_spec_t *spec, value_t *v) |
| 814 |
{ |
| 815 |
status_t res = check_specials(spec, v); |
| 816 |
if (res != STATUS_OK) |
| 817 |
return (res == STATUS_SKIP) ? STATUS_OK : res; |
| 818 |
|
| 819 |
if (!spec->buf.set(v->v_str)) |
| 820 |
return STATUS_NO_MEM; |
| 821 |
|
| 822 |
// Additional modifications? |
| 823 |
switch (spec->type) |
| 824 |
{ |
| 825 |
case 't': spec->buf.tolower(); break; |
| 826 |
case 'T': spec->buf.toupper(); break; |
| 827 |
case 'y': |
| 828 |
if (spec->buf.length() > 0) |
| 829 |
{ |
| 830 |
spec->buf.tolower(0, 1); |
| 831 |
if (spec->buf.length() > 1) |
| 832 |
spec->buf.toupper(1); |
| 833 |
} |
| 834 |
break; |
| 835 |
case 'Y': |
| 836 |
if (spec->buf.length() > 0) |
| 837 |
{ |
| 838 |
spec->buf.toupper(0, 1); |
| 839 |
if (spec->buf.length() > 1) |
| 840 |
spec->buf.tolower(1); |
| 841 |
} |
| 842 |
break; |
| 843 |
} |
| 844 |
|
| 845 |
return STATUS_OK; |
| 846 |
} |
| 847 |
|
| 848 |
status_t bool_to_str(fmt_spec_t *spec, value_t *v) |
| 849 |
{ |
| 850 |
status_t res = check_specials(spec, v); |
| 851 |
if (res != STATUS_OK) |
| 852 |
return (res == STATUS_SKIP) ? STATUS_OK : res; |
| 853 |
|
| 854 |
bool success = true; |
| 855 |
switch (spec->type) |
| 856 |
{ |
| 857 |
case 'l': success = spec->buf.set_ascii((v->v_bool) ? "true" : "false"); break; |
| 858 |
case 'L': success = spec->buf.set_ascii((v->v_bool) ? "TRUE" : "FALSE"); break; |
| 859 |
case 'z': success = spec->buf.set_ascii((v->v_bool) ? "tRUE" : "fALSE"); break; |
| 860 |
case 'Z': success = spec->buf.set_ascii((v->v_bool) ? "True" : "False"); break; |
| 861 |
} |
| 862 |
return (success) ? STATUS_OK : STATUS_NO_MEM; |
| 863 |
} |
| 864 |
|
| 865 |
status_t emit_parameter(io::IOutSequence *out, fmt_spec_t *spec, const Parameters *r) |
| 866 |
{ |
| 867 |
value_t v; |
| 868 |
init_value(&v); |
| 869 |
status_t res = STATUS_OK; |
| 870 |
|
| 871 |
// Fetch the value if resolver is present |
| 872 |
if (r != NULL) |
| 873 |
{ |
| 874 |
if (spec->flags & F_NAME) |
| 875 |
res = r->get(&spec->name, &v); |
| 876 |
else |
| 877 |
res = r->get(spec->index, &v); |
| 878 |
|
| 879 |
if (res != STATUS_OK) |
| 880 |
{ |
| 881 |
destroy_value(&v); |
| 882 |
return res; |
| 883 |
} |
| 884 |
} |
| 885 |
|
| 886 |
// Cast the value's type |
| 887 |
spec->buf.clear(); |
| 888 |
|
| 889 |
switch (spec->type) |
| 890 |
{ |
| 891 |
case 'i': case 'd': |
| 892 |
res = cast_value(&v, VT_INT); |
| 893 |
if (res == STATUS_OK) |
| 894 |
res = int_to_dec(spec, &v); |
| 895 |
break; |
| 896 |
case 'u': |
| 897 |
res = cast_value(&v, VT_INT); |
| 898 |
if (res == STATUS_OK) |
| 899 |
res = uint_to_dec(spec, &v); |
| 900 |
break; |
| 901 |
case 'b': |
| 902 |
res = cast_value(&v, VT_INT); |
| 903 |
if (res == STATUS_OK) |
| 904 |
res = int_to_bin(spec, &v); |
| 905 |
break; |
| 906 |
case 'o': |
| 907 |
res = cast_value(&v, VT_INT); |
| 908 |
if (res == STATUS_OK) |
| 909 |
res = int_to_oct(spec, &v); |
| 910 |
break; |
| 911 |
case 'x': case 'X': |
| 912 |
res = cast_value(&v, VT_INT); |
| 913 |
if (res == STATUS_OK) |
| 914 |
res = int_to_hex(spec, &v); |
| 915 |
break; |
| 916 |
case 'e': case 'E': |
| 917 |
case 'f': case 'F': |
| 918 |
res = cast_value(&v, VT_FLOAT); |
| 919 |
if (res == STATUS_OK) |
| 920 |
res = float_to_str(spec, &v); |
| 921 |
break; |
| 922 |
case 's': case 'S': case 't': case 'T': case 'y': case 'Y': |
| 923 |
res = cast_value(&v, VT_STRING); |
| 924 |
if (res == STATUS_OK) |
| 925 |
res = text_to_str(spec, &v); |
| 926 |
break; |
| 927 |
case 'l': case 'L': case 'z': case 'Z': |
| 928 |
res = cast_value(&v, VT_BOOL); |
| 929 |
if (res == STATUS_OK) |
| 930 |
res = bool_to_str(spec, &v); |
| 931 |
break; |
| 932 |
default: |
| 933 |
res = cast_value(&v, VT_STRING); |
| 934 |
if (res == STATUS_OK) |
| 935 |
res = text_to_str(spec, &v); |
| 936 |
break; |
| 937 |
} |
| 938 |
|
| 939 |
if (res != STATUS_OK) |
| 940 |
{ |
| 941 |
destroy_value(&v); |
| 942 |
return res; |
| 943 |
} |
| 944 |
|
| 945 |
// Compute padding |
| 946 |
ssize_t lpad = 0, rpad = 0, pad = ssize_t(lsp_max(spec->width, spec->xwidth)) - ssize_t(spec->buf.length()); |
| 947 |
if ((spec->flags & (F_WIDTH | F_XWIDTH)) && (pad > 0)) |
| 948 |
{ |
| 949 |
switch (spec->align) |
| 950 |
{ |
| 951 |
case AL_LEFT: |
| 952 |
rpad = pad; |
| 953 |
break; |
| 954 |
case AL_RIGHT: |
| 955 |
lpad = pad; |
| 956 |
break; |
| 957 |
case AL_MIDDLE: |
| 958 |
case AL_FROM_LEFT: |
| 959 |
lpad = pad >> 1; |
| 960 |
rpad = pad - lpad; |
| 961 |
break; |
| 962 |
case AL_FROM_RIGHT: |
| 963 |
rpad = pad >> 1; |
| 964 |
lpad = pad - rpad; |
| 965 |
break; |
| 966 |
case AL_TO_LEFT: |
| 967 |
lpad = pad >> 2; |
| 968 |
rpad = pad - lpad; |
| 969 |
break; |
| 970 |
case AL_TO_RIGHT: |
| 971 |
rpad = pad >> 2; |
| 972 |
lpad = pad - rpad; |
| 973 |
break; |
| 974 |
default: |
| 975 |
break; |
| 976 |
} |
| 977 |
} |
| 978 |
|
| 979 |
// Emit value |
| 980 |
while (lpad--) |
| 981 |
{ |
| 982 |
if ((res = out->write(spec->lpad)) != STATUS_OK) |
| 983 |
{ |
| 984 |
destroy_value(&v); |
| 985 |
return res; |
| 986 |
} |
| 987 |
} |
| 988 |
if ((res = out->write(&spec->buf)) != STATUS_OK) |
| 989 |
{ |
| 990 |
destroy_value(&v); |
| 991 |
return res; |
| 992 |
} |
| 993 |
while (rpad--) |
| 994 |
{ |
| 995 |
if ((res = out->write(spec->rpad)) != STATUS_OK) |
| 996 |
{ |
| 997 |
destroy_value(&v); |
| 998 |
return res; |
| 999 |
} |
| 1000 |
} |
| 1001 |
|
| 1002 |
destroy_value(&v); |
| 1003 |
return res; |
| 1004 |
} |
| 1005 |
|
| 1006 |
status_t format(io::IOutSequence *out, io::IInSequence *fmt, const Parameters *r) |
| 1007 |
{ |
| 1008 |
if ((out == NULL) || (fmt == NULL)) |
| 1009 |
return STATUS_BAD_ARGUMENTS; |
| 1010 |
|
| 1011 |
status_t res; |
| 1012 |
size_t index = 0; |
| 1013 |
bool protector = false; |
| 1014 |
fmt_spec_t spec; |
| 1015 |
init_spec(&spec, index); |
| 1016 |
|
| 1017 |
while (true) |
| 1018 |
{ |
| 1019 |
// Read character |
| 1020 |
lsp_swchar_t c = fmt->read(); |
| 1021 |
if (c < 0) |
| 1022 |
return (c != -STATUS_EOF) ? -c : STATUS_OK; |
| 1023 |
|
| 1024 |
switch (c) |
| 1025 |
{ |
| 1026 |
case '\\': |
| 1027 |
if (protector) |
| 1028 |
{ |
| 1029 |
if ((res = out->write('\\')) != STATUS_OK) |
| 1030 |
return res; |
| 1031 |
} |
| 1032 |
protector = !protector; |
| 1033 |
break; |
| 1034 |
|
| 1035 |
case '{': |
| 1036 |
if (protector) |
| 1037 |
{ |
| 1038 |
if ((res = out->write('{')) != STATUS_OK) |
| 1039 |
return res; |
| 1040 |
protector = false; |
| 1041 |
} |
| 1042 |
else |
| 1043 |
{ |
| 1044 |
// Read specifier and format the value |
| 1045 |
res = read_specifier(out, fmt, &spec); |
| 1046 |
if (res == STATUS_OK) |
| 1047 |
{ |
| 1048 |
if ((res = emit_parameter(out, &spec, r)) != STATUS_OK) |
| 1049 |
return res; |
| 1050 |
|
| 1051 |
// Reset specifier |
| 1052 |
if (!(spec.flags & (F_NAME | F_INDEX))) |
| 1053 |
++index; |
| 1054 |
} |
| 1055 |
else if (res != STATUS_BAD_FORMAT) |
| 1056 |
return res; |
| 1057 |
init_spec(&spec, index); |
| 1058 |
} |
| 1059 |
break; |
| 1060 |
case '}': |
| 1061 |
protector = false; |
| 1062 |
if ((res = out->write('}')) != STATUS_OK) |
| 1063 |
return res; |
| 1064 |
break; |
| 1065 |
|
| 1066 |
default: |
| 1067 |
if (protector) |
| 1068 |
{ |
| 1069 |
if ((res = out->write('\\')) != STATUS_OK) |
| 1070 |
return res; |
| 1071 |
protector = false; |
| 1072 |
} |
| 1073 |
if ((res = out->write(c)) != STATUS_OK) |
| 1074 |
return res; |
| 1075 |
break; |
| 1076 |
} // c |
| 1077 |
} |
| 1078 |
} |
| 1079 |
} /* namespace expr */ |
| 1080 |
} /* namespace lsp */ |
| 1081 |
|