1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227
|
/*
str_ops.c:
Copyright (C) 2005, 2006 Istvan Varga
(C) 2005 Matt J. Ingalls, John ffitch
(C) 2013 V Lazzarini (new string code)
This file is part of Csound.
The Csound Library is free software; you can redistribute it
and/or modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
Csound is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with Csound; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "csoundCore.h"
#define CSOUND_STR_OPS_C 1
#include "str_ops.h"
#include <ctype.h>
#ifdef HAVE_CURL
#include <curl/curl.h>
#include "corfile.h"
#endif
#define STRSMAX 8
#ifndef HAVE_SNPRINTF
/* add any compiler/system that has snprintf() */
#if defined(HAVE_C99)
#define HAVE_SNPRINTF 1
#endif
#endif
int32_t s_opcode(CSOUND *csound, STRGET_OP *p){
if (p->r->data == NULL){
p->r->data = (char *) csound->Malloc(csound, 15);
p->r->size = 15;
} else if (p->r->size < 15){
p->r->data = (char *) csound->ReAlloc(csound, p->r->data, 15);
p->r->size = 15;
}
snprintf(p->r->data, p->r->size, "%f", *p->indx);
return OK;
}
int32_t s_opcode_k(CSOUND *csound, STRGET_OP *p){
IGN(csound);
snprintf(p->r->data, p->r->size, "%f", *p->indx);
return OK;
}
/* strset by John ffitch */
static void str_set(CSOUND *csound, int32_t ndx, const char *s)
{
if (UNLIKELY(csound->strsets == NULL)) {
csound->strsmax = STRSMAX;
csound->strsets = (char **) csound->Calloc(csound, (csound->strsmax + 1)
* sizeof(char*));
}
if (UNLIKELY(ndx > (int32_t) csound->strsmax)) {
int32_t i, newmax;
/* assumes power of two STRSMAX */
newmax = (ndx | (STRSMAX - 1)) + 1;
csound->strsets = (char**) csound->ReAlloc(csound, csound->strsets,
(newmax + 1) * sizeof(char*));
for (i = (csound->strsmax + 1); i <= newmax; i++)
csound->strsets[i] = NULL;
csound->strsmax = newmax;
}
if (UNLIKELY(ndx < 0)) { /* -ve index */
csound->InitError(csound, Str("illegal strset index"));
return;
}
if (csound->strsets[ndx] != NULL) {
if (strcmp(s, csound->strsets[ndx]) == 0)
return;
if (UNLIKELY(csound->oparms->msglevel & CS_WARNMSG)) {
csound->Warning(csound, Str("strset index conflict at %d"), ndx);
csound->Warning(csound, Str("previous value: '%s', replaced with '%s'"),
csound->strsets[ndx], s);
}
csound->Free(csound, csound->strsets[ndx]);
}
csound->strsets[ndx] = (char*) csound->Malloc(csound, strlen(s) + 1);
strcpy(csound->strsets[ndx], s);
if ((csound->oparms->msglevel & 7) == 7)
csound->Message(csound, "Strsets[%d]: '%s'\n", ndx, s);
}
int32_t strset_init(CSOUND *csound, STRSET_OP *p)
{
str_set(csound, (int32_t) MYFLT2LRND(*p->indx), p->str->data);
return OK;
}
/* for argdecode.c */
void strset_option(CSOUND *csound, char *s)
{
int32_t indx = 0;
if (UNLIKELY(!isdigit(*s))) {
csound->Warning(csound, Str("--strset: invalid format"));
return;
}
do {
indx = (indx * 10) + (int32_t) (*s++ - '0');
} while (isdigit(*s));
if (UNLIKELY(*s++ != '=')){
csound->Warning(csound, Str("--strset: invalid format"));
return;
}
str_set(csound, indx, s);
}
int32_t strget_init(CSOUND *csound, STRGET_OP *p)
{
int32_t indx;
if (csound->ISSTRCOD(*(p->indx))) {
char *ss = csound->init_event->strarg;
if (ss == NULL)
return OK;
ss = get_arg_string(csound, *p->indx);
if (p->r->data == NULL) {
p->r->data = cs_strdup(csound, ss);
p->r->size = strlen(ss)+1;
}
else if ((int32_t) strlen(ss) >= p->r->size) {
csound->Free(csound, p->r->data);
p->r->data = cs_strdup(csound, ss);
p->r->size = strlen(ss) + 1;
}
else {
int32_t n = strlen(ss)+1;
p->r->size = n;
strNcpy(p->r->data, ss, n);
//p->r->data[p->r->size - 1] = '\0';
}
return OK;
}
indx = (int32_t)((double)*(p->indx) + (*(p->indx) >= FL(0.0) ? 0.5 : -0.5));
if (indx < 0 || indx > (int32_t) csound->strsmax ||
csound->strsets == NULL || csound->strsets[indx] == NULL)
return OK;
if (UNLIKELY((int32_t) strlen(csound->strsets[indx]) >= p->r->size)){
int32_t size = strlen(csound->strsets[indx]);
p->r->data = csound->ReAlloc(csound, p->r->data, size + 1);
p->r->size = size + 1;
}
strcpy((char*) p->r->data, csound->strsets[indx]);
return OK;
}
static CS_NOINLINE int32_t StrOp_ErrMsg(void *p, const char *msg)
{
CSOUND *csound = ((OPDS*) p)->insdshead->csound;
const char *opname = csound->GetOpcodeName(p);
if (UNLIKELY(csound->ids != NULL && csound->ids->insdshead == csound->curip))
return csound->InitError(csound, "%s: %s", opname, Str(msg));
else if (UNLIKELY(((OPDS*) p)->insdshead->pds != NULL))
return csound->PerfError(csound, (OPDS*)p,
"%s: %s", opname, Str(msg));
else
csound->Warning(csound, "%s: %s", opname, Str(msg));
return NOTOK;
}
/* strcpy */
int32_t strcpy_opcode_S(CSOUND *csound, STRCPY_OP *p)
{
char *newVal = p->str->data;
if (p->r->data == NULL) {
p->r->data = cs_strdup(csound, newVal);
p->r->size = strlen(p->str->data) + 1;
//printf("NULL str:%p %p \n", p->r, p->r->data);
return OK;
}
if (p->r->data == p->str->data){
//printf("sameptr str:%p %p \n", p->r->data);
return OK;
}
if (UNLIKELY((int32_t) strlen(newVal) >= p->r->size)){
csound->Free(csound, p->r->data);
p->r->data = cs_strdup(csound, newVal);
p->r->size = strlen(newVal) + 1;
//printf("dup str:%p %p \n", p->r, p->r->data);
}
else {
strcpy((char*) p->r->data, newVal);
p->r->size = strlen(newVal) + 1;
//printf("str:%p %p \n", p->r, p->r->data);
}
return OK;
}
int32_t strassign_opcode_S(CSOUND *csound, STRCPY_OP *p)
{
IGN(csound);
p->r->data = p->str->data;
p->r->size = p->str->size;
return OK;
}
int32_t strassign_opcode_Sk(CSOUND *csound, STRCPY_OP *p)
{
IGN(csound);
if (strcmp(p->r->data, p->str->data)!=0){
p->r->data = p->str->data;
p->r->size = p->str->size;
}
//csound->Message(csound, p->r->data);
return OK;
}
int32_t str_changed(CSOUND *csound, STRCHGD *p)
{
if (p->mem != NULL)
csound->Free(csound, p->mem);
p->mem = cs_strdup(csound, p->str->data);
*p->r = 0;
return OK;
}
int32_t str_changed_k(CSOUND *csound, STRCHGD *p)
{
if (p->str->data && ( p->mem == NULL || strcmp(p->str->data, p->mem)!=0)) {
csound->Free(csound, p->mem);
p->mem = cs_strdup(csound, p->str->data);
*p->r = 1;
}
else *p->r = 0;
return OK;
}
extern char* get_strarg(CSOUND *csound, MYFLT p, char *strarg);
int32_t strcpy_opcode_p(CSOUND *csound, STRGET_OP *p)
{
if (csound->ISSTRCOD(*p->indx)) {
char *ss;
ss = get_arg_string(csound, *p->indx);
if (ss == NULL){
if (UNLIKELY(((OPDS*) p)->insdshead->pds != NULL))
return csoundPerfError(csound, (OPDS*)p,
Str("NULL string\n"));
else
return csoundInitError(csound, Str("NULL string\n"));
}
if (p->r->data == NULL) {
p->r->data = cs_strdup(csound, ss);
p->r->size = strlen(ss)+1;
}
else if ((int32_t) strlen(ss) >= p->r->size) {
csound->Free(csound, p->r->data);
p->r->data = cs_strdup(csound, ss);
p->r->size = strlen(ss) + 1;
}
else {
strcpy(p->r->data,ss);
p->r->size = strlen(ss) + 1;
}
}
else {
p->r->data = csound->strarg2name(csound, NULL, p->indx, "soundin.", 0);
p->r->size = strlen(p->r->data) + 1;
}
return OK;
}
/* strcat */
int32_t strcat_opcode(CSOUND *csound, STRCAT_OP *p)
{
int32_t size;
char *str1 = cs_strdup(csound, p->str1->data),
*str2 = cs_strdup(csound, p->str2->data);
if (str1 == NULL || str2 == NULL){
csound->Free(csound,str1);
csound->Free(csound,str2);
if (UNLIKELY(((OPDS*) p)->insdshead->pds != NULL))
return csoundPerfError(csound, (OPDS*)p, Str("NULL string\n"));
else return csoundInitError(csound, Str("NULL string\n"));
}
size = strlen(str1) + strlen(str2);
if (p->r->data == NULL) {
p->r->data = csound->Calloc(csound, size+1);
p->r->size = size+1;
}
else if (UNLIKELY((int32_t) size >= p->r->size)) {
char *nstr = csound->ReAlloc(csound, p->r->data, size + 1);
if (p->r->data == p->str1->data){
p->str1->data = nstr;
p->str1->size = size + 1;
}
if (p->r->data == p->str2->data){
p->str2->data = nstr;
p->str2->size = size + 1;
}
p->r->data = nstr;
p->r->size = size + 1;
}
strNcpy((char*) p->r->data, str1, p->r->size);
strcat((char*) p->r->data, str2);
csound->Free(csound, str2); /* not needed anymore */
csound->Free(csound, str1);
return OK;
}
/* strcmp */
int32_t strcmp_opcode(CSOUND *csound, STRCMP_OP *p)
{
int32_t i;
if (p->str1->data == NULL || p->str2->data == NULL){
if (UNLIKELY(((OPDS*) p)->insdshead->pds != NULL))
return csoundPerfError(csound, (OPDS*)p, Str("NULL string\n"));
else return csoundInitError(csound, Str("NULL string\n"));
}
*(p->r) = FL(0.0);
if (p->str1 == p->str2)
return OK;
i = strcmp((char*) p->str1->data, (char*) p->str2->data);
if (i < 0)
*(p->r) = FL(-1.0);
else if (i > 0)
*(p->r) = FL(1.0);
return OK;
}
/* perform a sprintf-style format -- based on code by Matt J. Ingalls */
static CS_NOINLINE int32_t
sprintf_opcode_(CSOUND *csound,
void *p, /* opcode data structure pointer */
STRINGDAT *str, /* pointer to space for output string */
const char *fmt, /* format string */
MYFLT **kvals, /* array of argument pointers */
int32_t numVals, /* number of arguments */
int32_t strCode) /* bit mask for string arguments */
{
int32_t len = 0;
char *strseg, *outstring = str->data;
MYFLT *parm = NULL;
int32_t i = 0, j = 0, n;
const char *segwaiting = NULL;
int32_t maxChars, siz = strlen(fmt) + numVals*7 + 1;
for (i = 0; i < numVals; i++) {
if (UNLIKELY(IS_ASIG_ARG(kvals[i]))) {
return StrOp_ErrMsg(p, Str("a-rate argument not allowed"));
}
}
if (UNLIKELY((int32_t) ((OPDS*) p)->optext->t.inArgCount > 31)){
StrOp_ErrMsg(p, Str("too many arguments"));
return NOTOK;
}
if (numVals==0) {
strcpy(str->data, fmt);
return OK;
}
strseg = csound->Malloc(csound, siz);
i = 0;
while (1) {
if (UNLIKELY(i >= siz)) {
// return StrOp_ErrMsg(p, "format string too long");
siz *= 2;
strseg = csound->ReAlloc(csound, strseg, siz);
}
if (*fmt != '%' && *fmt != '\0') {
strseg[i++] = *fmt++;
continue;
}
if (fmt[0] == '%' && fmt[1] == '%') {
strseg[i++] = *fmt++; /* Odd code: %% is usually % and as we
know the value of *fmt the loads are
unnecessary */
strseg[i++] = *fmt++;
continue;
}
/* if already a segment waiting, then lets print it */
if (segwaiting != NULL) {
maxChars = str->size - len;
strseg[i] = '\0';
if (UNLIKELY(numVals <= 0)) {
csound->Free(csound, strseg);
return StrOp_ErrMsg(p, Str("insufficient arguments for format"));
}
numVals--;
/* if (UNLIKELY((*segwaiting == 's' && !(strCode & 1)) || */
/* (*segwaiting != 's' && (strCode & 1)))) { */
/* return StrOp_ErrMsg(p, "argument type inconsistent with format"); */
/* } */
strCode >>= 1;
parm = kvals[j++];
switch (*segwaiting) {
case 'd':
case 'i':
case 'o':
case 'x':
case 'X':
case 'u':
case 'c':
#ifdef HAVE_SNPRINTF
if ((int32_t)strlen(strseg) + 24 > (int32_t)maxChars) {
int32_t offs = outstring - str->data;
str->data = csound->ReAlloc(csound, str->data,
str->size + 24);
if(str->data == NULL) {
return StrOp_ErrMsg(p, Str("memory allocation failure"));
}
str->size += 24;
maxChars += 24;
outstring = str->data + offs;
//printf("maxchars = %d %s\n", maxChars, strseg);
//printf("size: %d \n",str->size);
}
n = snprintf(outstring, maxChars, strseg, (int32_t) MYFLT2LRND(*parm));
#else
n = sprintf(outstring, strseg, (int32_t) MYFLT2LRND(*parm));
#endif
break;
case 'e':
case 'E':
case 'f':
case 'F':
case 'g':
case 'G':
#ifdef HAVE_SNPRINTF
//printf("%d %d \n", str->size, strlen(str->data));
if (strlen(strseg) + 24 > (uint32_t)maxChars) {
int32_t offs = outstring - str->data;
str->data = csound->ReAlloc(csound, str->data, str->size + 13);
if(str->data == NULL) {
return StrOp_ErrMsg(p, Str("memory allocation failure"));
}
str->size += 24;
maxChars += 24;
outstring = str->data + offs;
//printf("maxchars = %d %s\n", maxChars, strseg);
}
//printf("%d %d \n", str->size, strlen(str->data));
n = snprintf(outstring, maxChars, strseg, (double)*parm);
#else
n = sprintf(outstring, strseg, (double)*parm);
#endif
break;
case 's':
if (LIKELY(IS_STR_ARG(parm))) {
if (UNLIKELY(((STRINGDAT*)parm)->data == str->data)) {
csound->Free(csound, strseg);
return StrOp_ErrMsg(p, Str("output argument may not be "
"the same as any of the input args"));
}
if (UNLIKELY(((STRINGDAT*)parm)->size+strlen(strseg) >= (uint32_t)maxChars)) {
int32_t offs = outstring - str->data;
str->data = csound->ReAlloc(csound, str->data,
str->size + ((STRINGDAT*)parm)->size +
strlen(strseg));
if(str->data == NULL){
return StrOp_ErrMsg(p, Str("memory allocation failure"));
}
str->size += ((STRINGDAT*)parm)->size + strlen(strseg);
maxChars += ((STRINGDAT*)parm)->size + strlen(strseg);
outstring = str->data + offs;
}
n = snprintf(outstring, maxChars, strseg, ((STRINGDAT*)parm)->data);
}
else return StrOp_ErrMsg(p, Str("Not string type for %%s"));
break;
default:
csound->Free(csound, strseg);
return StrOp_ErrMsg(p, Str("invalid format string"));
}
if (n < 0 || n >= maxChars) {
/* safely detected excess string length */
int32_t offs = outstring - str->data;
str->data = csound->ReAlloc(csound, str->data, maxChars*2);
if (str->data == NULL) {
return StrOp_ErrMsg(p, Str("memory allocation failure"));
}
outstring = str->data + offs;
str->size = maxChars*2;
// VL: Coverity says this is unused. (which is true)
// maxChars += str->size;
}
outstring += n;
len += n;
i = 0;
}
if (*fmt == '\0')
break;
/* copy the '%' */
strseg[i++] = *fmt++;
/* find the format code */
segwaiting = fmt;
while (!isalpha(*segwaiting) && *segwaiting != '\0')
segwaiting++;
}
if (UNLIKELY(numVals > 0)) {
csound->Free(csound, strseg);
return StrOp_ErrMsg(p, Str("too many arguments for format"));
}
csound->Free(csound, strseg);
return OK;
}
int32_t sprintf_opcode(CSOUND *csound, SPRINTF_OP *p)
{
int32_t size = p->sfmt->size+ 18*((int32_t) p->INOCOUNT);
//printf("%d %d \n", p->r->size, strlen(p->r->data));
if (p->r->data == NULL || p->r->size < size) {
/* this 10 is 1n incorrect guess which is OK with numbers*/
p->r->data = csound->Calloc(csound, size);
p->r->size = size;
}
if (UNLIKELY(sprintf_opcode_(csound, p, p->r,
(char*) p->sfmt->data, &(p->args[0]),
(int32_t) p->INOCOUNT - 1,0) == NOTOK)) {
((char*) p->r->data)[0] = '\0';
return NOTOK;
}
return OK;
}
static CS_NOINLINE int32_t printf_opcode_(CSOUND *csound, PRINTF_OP *p)
{
STRINGDAT buf;
int32_t err;
buf.size = /*strlen(p->sfmt->data) +*/ 3072;
buf.data = csound->Calloc(csound, buf.size);
err = sprintf_opcode_(csound, p, &buf, (char*) p->sfmt->data, &(p->args[0]),
(int32_t) p->INOCOUNT - 2,0);
if (LIKELY(err == OK))
csound->MessageS(csound, CSOUNDMSG_ORCH, "%s", buf.data);
csound->Free(csound, buf.data);
return err;
}
int32_t printf_opcode_init(CSOUND *csound, PRINTF_OP *p)
{
if (*p->ktrig > FL(0.0))
return (printf_opcode_(csound, p));
return OK;
}
int32_t printf_opcode_set(CSOUND *csound, PRINTF_OP *p)
{
(void) csound;
p->prv_ktrig = FL(0.0);
return OK;
}
int32_t printf_opcode_perf(CSOUND *csound, PRINTF_OP *p)
{
MYFLT ktrig = *p->ktrig;
if (ktrig == p->prv_ktrig)
return OK;
p->prv_ktrig = ktrig;
if (ktrig > FL(0.0))
return (printf_opcode_(csound, p));
return OK;
}
int32_t puts_opcode_init(CSOUND *csound, PUTS_OP *p)
{
p->noNewLine = (*p->no_newline == FL(0.0) ? 0 : 1);
if (*p->ktrig > FL(0.0)) {
if (!p->noNewLine)
csound->MessageS(csound, CSOUNDMSG_ORCH, "%s\n", (char*) p->str->data);
else
csound->MessageS(csound, CSOUNDMSG_ORCH, "%s", (char*) p->str->data);
}
p->prv_ktrig = *p->ktrig;
return OK;
}
int32_t puts_opcode_perf(CSOUND *csound, PUTS_OP *p)
{
if (*p->ktrig != p->prv_ktrig && *p->ktrig > FL(0.0)) {
p->prv_ktrig = *p->ktrig;
if (!p->noNewLine)
csound->MessageS(csound, CSOUNDMSG_ORCH, "%s\n", (char*) p->str->data);
else
csound->MessageS(csound, CSOUNDMSG_ORCH, "%s", (char*) p->str->data);
}
return OK;
}
int32_t strtod_opcode_p(CSOUND *csound, STRTOD_OP *p)
{
char *s = NULL, *tmp;
double x;
if (csound->ISSTRCOD(*p->str))
s = get_arg_string(csound, *p->str);
else {
int32_t ndx = (int32_t) MYFLT2LRND(*p->str);
if (ndx >= 0 && ndx <= (int32_t) csound->strsmax && csound->strsets != NULL)
s = csound->strsets[ndx];
}
if (UNLIKELY(s == NULL))
return StrOp_ErrMsg(p, Str("empty string"));
while (isblank(*s)) s++;
if (UNLIKELY(*s == '\0'))
return StrOp_ErrMsg(p, Str("empty string"));
x = cs_strtod(s, &tmp);
if (UNLIKELY(*tmp != '\0'))
return StrOp_ErrMsg(p, Str("invalid format"));
*p->indx = (MYFLT) x;
return OK;
}
int32_t strtod_opcode_S(CSOUND *csound, STRSET_OP *p)
{
IGN(csound);
char *s = NULL, *tmp;
double x;
s = (char*) p->str->data;
while (isblank(*s)) s++;
if (UNLIKELY(*s == '\0'))
return StrOp_ErrMsg(p, Str("empty string"));
x = cs_strtod(s, &tmp);
if (UNLIKELY(*tmp != '\0'))
return StrOp_ErrMsg(p, Str("invalid format"));
*p->indx = (MYFLT) x;
return OK;
}
int32_t strtol_opcode_S(CSOUND *csound, STRSET_OP *p)
{
IGN(csound);
char *s = NULL;
int32_t sgn = 0, radix = 10;
int32_t x = 0;
s = (char*) p->str->data;
while (isblank(*s)) s++;
if (UNLIKELY(*s == '\0'))
return StrOp_ErrMsg(p, Str("empty string"));
if (*s == '+') s++;
else if (*s == '-') sgn++, s++;
if (*s == '0') {
if (s[1] == 'x' || s[1] == 'X')
radix = 16, s += 2;
else if (s[1] != '\0')
radix = 8, s++;
else {
*p->indx = FL(0.0);
return OK;
}
}
if (UNLIKELY(*s == '\0'))
return StrOp_ErrMsg(p, Str("invalid format"));
switch (radix) {
case 8:
while (*s >= '0' && *s <= '7') x = (x * 8L) + (int32_t) (*s++ - '0');
break;
case 10:
while (isdigit(*s)) x = (x * 10L) + (int32_t) (*s++ - '0');
break;
default:
while (1) {
if (isdigit(*s))
x = (x * 16L) + (int32_t) (*s++ - '0');
else if (*s >= 'A' && *s <= 'F')
x = (x * 16L) + (int32_t) (*s++ - 'A') + 10L;
else if (*s >= 'a' && *s <= 'f')
x = (x * 16L) + (int32_t) (*s++ - 'a') + 10L;
else
break;
}
}
if (UNLIKELY(*s != '\0'))
return StrOp_ErrMsg(p, Str("invalid format"));
if (sgn) x = -x;
*p->indx = (MYFLT) x;
return OK;
}
int32_t strtol_opcode_p(CSOUND *csound, STRTOD_OP *p)
{
char *s = NULL;
int32_t sgn = 0, radix = 10;
int32_t x = 0L;
if (csound->ISSTRCOD(*p->str))
s = get_arg_string(csound, *p->str);
else {
int32_t ndx = (int32_t) MYFLT2LRND(*p->str);
if (ndx >= 0 && ndx <= (int32_t) csound->strsmax && csound->strsets != NULL)
s = csound->strsets[ndx];
}
if (UNLIKELY(s == NULL))
return StrOp_ErrMsg(p, Str("empty string"));
while (isblank(*s)) s++;
if (UNLIKELY(*s == '\0'))
return StrOp_ErrMsg(p, Str("empty string"));
if (*s == '+') s++;
else if (*s == '-') sgn++, s++;
if (*s == '0') {
if (s[1] == 'x' || s[1] == 'X')
radix = 16, s += 2;
else if (s[1] != '\0')
radix = 8, s++;
else {
*p->indx = FL(0.0);
return OK;
}
}
if (UNLIKELY(*s == '\0'))
return StrOp_ErrMsg(p, Str("invalid format"));
switch (radix) {
case 8:
while (*s >= '0' && *s <= '7') x = (x * 8L) + (int32_t) (*s++ - '0');
break;
case 10:
while (isdigit(*s)) x = (x * 10L) + (int32_t) (*s++ - '0');
break;
default:
while (1) {
if (isdigit(*s))
x = (x * 16L) + (int32_t) (*s++ - '0');
else if (*s >= 'A' && *s <= 'F')
x = (x * 16L) + (int32_t) (*s++ - 'A') + 10L;
else if (*s >= 'a' && *s <= 'f')
x = (x * 16L) + (int32_t) (*s++ - 'a') + 10L;
else
break;
}
}
if (UNLIKELY(*s != '\0'))
return StrOp_ErrMsg(p, Str("invalid format"));
if (sgn) x = -x;
*p->indx = (MYFLT) x;
return OK;
}
/**
* Sdst strsub Ssrc[, istart[, iend]]
* Sdst strsubk Ssrc, kstart, kend
*
* Extract a part of Ssrc, from istart to iend; if istart or iend is
* less than 0, or greater than the length of the source string, it is
* interpreted as the end of the source string. istart > iend will
* reverse the string. The default parameters are istart = 0, iend = -1.
*/
int32_t strsub_opcode(CSOUND *csound, STRSUB_OP *p)
{
const char *src;
char *dst;
int32_t i, len, strt, end, rev = 0;
if (p->Ssrc->data == NULL) return NOTOK;
if (p->Sdst->data == NULL || p->Sdst->size < p->Ssrc->size) {
int32_t size = p->Ssrc->size;
if (p->Sdst->data != NULL) csound->Free(csound, p->Sdst->data);
p->Sdst->data = csound->Calloc(csound, size);
p->Sdst->size = size;
}
src = (char*) p->Ssrc->data;
dst = (char*) p->Sdst->data;
len = (int32_t) strlen(src);
#if defined(MSVC) || (defined(__GNUC__) && defined(__i386__))
strt = (int32_t) MYFLT2LRND(*(p->istart));
end = (int32_t) MYFLT2LRND(*(p->iend));
#else
strt = (int32_t) (*(p->istart) + FL(1.5)) - 1;
end = (int32_t) (*(p->iend) + FL(1.5)) - 1;
#endif
if (strt < 0 || strt > len)
strt = len;
if (end < 0 || end > len)
end = len;
if (strt == end) {
/* trivial case: empty output */
dst[0] = '\0';
return OK;
}
if (strt > end) {
int32_t tmp = strt;
/* reverse output */
strt = end;
end = tmp;
rev = 1;
}
src += strt;
len = end - strt;
if (UNLIKELY(len >= p->Sdst->size)) {
p->Sdst->data = csound->ReAlloc(csound, p->Sdst->data, len+1);
p->Sdst->size = len+1;
dst = (char*) p->Sdst->data;
}
i = 0;
if (!rev || p->Sdst->data == p->Ssrc->data) {
/* copying in forward direction is safe */
/* even if Ssrc and Sdst are the same */
do {
dst[i] = src[i];
} while (++i < len);
dst[i] = '\0';
if (rev) {
int32_t j;
/* if the destination string variable is the same as the source, */
/* reversing needs to be handled in a special way */
i = 0;
j = len - 1;
while (i < j) {
char tmp = dst[i];
dst[i++] = dst[j];
dst[j--] = tmp;
}
}
}
else {
/* reverse string out of place (Ssrc and Sdst are not the same) */
int32_t j = len;
do {
dst[i] = src[--j];
} while (++i < len);
dst[i] = '\0';
}
return OK;
}
/**
* ichr strchar Sstr[, ipos]
* kchr strchark Sstr[, kpos]
*
* Return the ASCII code of the character in Sstr at ipos (defaults to 0).
* If ipos is out of range, 0 is returned.
*/
int32_t strchar_opcode(CSOUND *csound, STRCHAR_OP *p)
{
int32_t len = (int32_t) strlen((char*) p->Ssrc->data);
#if defined(MSVC) || (defined(__GNUC__) && defined(__i386__))
int32_t pos = (int32_t) MYFLT2LRND(*(p->ipos));
#else
int32_t pos = (int32_t) (*(p->ipos) + FL(1.5)) - 1;
#endif
(void) csound;
if (pos < 0 || pos >= len)
*(p->ichr) = FL(0.0);
else
*(p->ichr) = (MYFLT) ((int32_t)((unsigned char)((char*) p->Ssrc->data)[pos]));
return OK;
}
/**
* ilen strlen Sstr
* klen strlenk Sstr
*
* Return the length of a string.
*/
int32_t strlen_opcode(CSOUND *csound, STRLEN_OP *p)
{
(void) csound;
if (p->Ssrc->size)
*(p->ilen) = (MYFLT) strlen(p->Ssrc->data);
else *(p->ilen) = FL(0.0);
return OK;
}
/**
* Sdst strupper Ssrc
* Sdst strupperk Ssrc
* Sdst strlower Ssrc
* Sdst strlowerk Ssrc
*
* Convert a string to upper or lower case.
*/
int32_t strupper_opcode(CSOUND *csound, STRUPPER_OP *p)
{
const char *src;
char *dst;
int32_t i;
if (p->Ssrc->data == NULL) return NOTOK;
if (p->Sdst->data == NULL || p->Sdst->size < p->Ssrc->size) {
int32_t size = p->Ssrc->size;
if (p->Sdst->data != NULL) csound->Free(csound, p->Sdst->data);
p->Sdst->data = csound->Calloc(csound, size);
p->Sdst->size = size;
}
(void) csound;
src = (char*) p->Ssrc->data;
dst = (char*) p->Sdst->data;
for (i = 0; src[i] != '\0'; i++) {
unsigned char tmp;
tmp = (unsigned char) src[i];
dst[i] = (char) (islower(tmp) ? (unsigned char) toupper(tmp) : tmp);
}
return OK;
}
int32_t strlower_opcode(CSOUND *csound, STRUPPER_OP *p)
{
const char *src;
char *dst;
int32_t i;
if (p->Ssrc->data == NULL) return NOTOK;
if (p->Sdst->data == NULL || p->Sdst->size < p->Ssrc->size) {
int32_t size = p->Ssrc->size;
if (p->Sdst->data != NULL) csound->Free(csound,p->Sdst->data);
p->Sdst->data = csound->Calloc(csound, size);
p->Sdst->size = size;
}
(void) csound;
src = (char*) p->Ssrc->data;
dst = (char*) p->Sdst->data;
for (i = 0; src[i] != '\0'; i++) {
unsigned char tmp;
tmp = (unsigned char) src[i];
dst[i] = (char) (isupper(tmp) ? (unsigned char) tolower(tmp) : tmp);
}
return OK;
}
/**
* Sval getcfg iopt
*
* Returns the value of a global setting (e.g. input file name) as a
* string.
*/
int32_t getcfg_opcode(CSOUND *csound, GETCFG_OP *p)
{
const char *s;
#if defined(MSVC) || (defined(__GNUC__) && defined(__i386__))
int32_t opt = (int32_t) MYFLT2LRND(*(p->iopt));
#else
int32_t opt = (int32_t) (*(p->iopt) + FL(0.5));
#endif
char buf[32];
if (p->Sdst->size < 32){
csound->Free(csound, p->Sdst->data);
p->Sdst->data = csound->Calloc(csound,32);
p->Sdst->size = 32;
}
//((char*) p->Sdst->data)[0] = '\0';
buf[0] = '\0';
s = &(buf[0]);
switch (opt) {
case 1: /* maximum length of variable */
snprintf(&(buf[0]), 32, "%d", (int32_t) p->Sdst->size - 1);
break;
case 2: /* input sound file name */
s = (csound->oparms->sfread && !csound->initonly ?
csound->oparms->infilename : (char*) NULL);
break;
case 3: /* output sound file name */
s = (csound->oparms->sfwrite && !csound->initonly ?
csound->oparms->outfilename : (char*) NULL);
break;
case 4: /* is real-time audio being used ? (0: no, 1: yes) */
buf[0] = '0';
buf[1] = '\0';
if ((csound->oparms->sfread && !csound->initonly &&
check_rtaudio_name(csound->oparms->infilename, NULL, 0) >= 0) ||
(csound->oparms->sfwrite && !csound->initonly &&
check_rtaudio_name(csound->oparms->outfilename, NULL, 1) >= 0))
buf[0] = '1';
break;
case 5: /* is beat mode being used ? (0: no, 1: yes) */
buf[0] = (csound->oparms->Beatmode ? '1' : '0');
buf[1] = '\0';
break;
case 6: /* host OS name */
#ifdef LINUX
s = "Linux";
#elif defined(WIN32)
s = "Win32";
#elif defined(MACOSX)
s = "MacOSX";
#else
s = "unknown";
#endif
break;
case 7: /* is the channel I/O callback set ? (0: no, 1: yes) */
buf[0] = 0;
buf[1] = '\0';
break;
default:
return csound->InitError(csound, Str("invalid option code: %g"),
*(p->iopt));
}
if (s != NULL) {
if (p->Sdst->data == NULL) {
int32_t size = strlen(s) + 1;
p->Sdst->data = csound->Calloc(csound, size);
p->Sdst->size = size;
}
else if (UNLIKELY((int32_t) strlen(s) >= p->Sdst->size)) {
p->Sdst->data = csound->ReAlloc(csound, p->Sdst->data, strlen(s) + 1);
p->Sdst->size = strlen(s) + 1;
}
strcpy((char*) p->Sdst->data, s);
}
return OK;
}
/**
* ipos strindex Sstr1, Sstr2
* kpos strindexk Sstr1, Sstr2
*
* Return the position of the first occurence of Sstr2 in Sstr1,
* or -1 if not found. If Sstr2 is empty, 0 is returned.
*/
int32_t strindex_opcode(CSOUND *csound, STRINDEX_OP *p)
{
const char *s1 = (char*) p->Ssrc1->data;
const char *s2 = (char*) p->Ssrc2->data;
int32_t i, j;
(void) csound;
/* search substring from left to right, */
/* and return position of first match */
i = j = 0;
while (s2[j] != '\0') {
if (s1[i] == '\0') {
*(p->ipos) = -FL(1.0);
return OK;
}
j = (s1[i] != s2[j] ? 0 : j + 1);
i++;
}
*(p->ipos) = (MYFLT) (i - j);
return OK;
}
/**
* ipos strrindex Sstr1, Sstr2
* kpos strrindexk Sstr1, Sstr2
*
* Return the position of the last occurence of Sstr2 in Sstr1,
* or -1 if not found. If Sstr2 is empty, the length of Sstr1 is
* returned.
*/
int32_t strrindex_opcode(CSOUND *csound, STRINDEX_OP *p)
{
const char *s1 = (char*) p->Ssrc1->data;
const char *s2 = (char*) p->Ssrc2->data;
int32_t i, j, k;
(void) csound;
/* search substring from left to right, */
/* and return position of last match */
i = j = 0;
k = -1;
while (1) {
if (s2[j] == '\0') {
k = i - j;
j = 0;
}
if (s1[i] == '\0')
break;
j = (s1[i] != s2[j] ? 0 : j + 1);
i++;
}
*(p->ipos) = (MYFLT) k;
return OK;
}
#ifdef HAVE_CURL
int32_t str_from_url(CSOUND *csound, STRCPY_OP *p)
{
char *newVal = p->str->data;
if (strstr(newVal, ":/")==NULL) return strcpy_opcode_S(csound, p);
{
CORFIL *mm = copy_url_corefile(csound, newVal,0);
int32_t len = corfile_length(mm);
if (p->r->data == NULL) {
p->r->data = cs_strdup(csound, corfile_body(mm));
p->r->size = len + 1;
goto cleanup;
}
if (UNLIKELY(len >= p->r->size)) {
csound->Free(csound, p->r->data);
p->r->data = cs_strdup(csound, corfile_body(mm));
p->r->size = len + 1;
}
else strcpy((char*) p->r->data, corfile_body(mm));
cleanup:
corfile_rm(csound, &mm);
return OK;
}
}
#endif
#if !defined(HAVE_STRLCAT) && !defined(strlcat) && !defined(EMSCRIPTEN)
/* Direct from BSD sources */
/*
* Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*/
size_t
strlcat(char *dst, const char *src, size_t siz)
{
char *d = dst;
const char *s = src;
size_t n = siz;
size_t dlen;
/* Find the end of dst and adjust bytes left but don't go past end */
while (n-- != 0 && *d != '\0')
d++;
dlen = d - dst;
n = siz - dlen;
if (n == 0)
return (dlen + strlen(s));
while (*s != '\0') {
if (n != 1) {
*d++ = *s;
n--;
}
s++;
}
*d = '\0';
return (dlen + (s - src)); /* count does not include NUL */
}
#endif
/* Modified from BSD sources for strlcpy */
/*
* Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*/
/* modifed for speed -- JPff */
char *
strNcpy(char *dst, const char *src, size_t siz)
{
char *d = dst;
const char *s = src;
size_t n = siz;
/* Copy as many bytes as will fit or until NULL */
if (n != 0) {
while (--n != 0) {
if ((*d++ = *s++) == '\0')
break;
}
}
/* Not enough room in dst, add NUL */
if (n == 0) {
if (siz != 0)
*d = '\0'; /* NUL-terminate dst */
//while (*s++) ;
}
return dst; /* count does not include NUL */
}
/* Debugging opcode for testing runtime type identification */
int32_t print_type_opcode(CSOUND* csound, PRINT_TYPE_OP* p) {
char* ptr = (char*)p->inVar;
CS_TYPE* varType = *(CS_TYPE**)(ptr - CS_VAR_TYPE_OFFSET);
csound->Message(csound, "Variable Type: %s\n", varType->varTypeName);
return OK;
}
|