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
|
/*
* SCSTR.C - string manipulation functions
*
* Source Version: 2.0
* Software Release #92-0043
*
*/
#include "cpyright.h"
#include "score.h"
#ifndef EDOM
#define EDOM 16
#endif
#define MBASE 32
#define ishexdigit(x) \
(isdigit(x) ? (x) - '0' : \
islower(x) ? (x) + 10 - 'a' : (x) + 10 - 'A')
#ifndef ATOF_FUNCTION
#define ATOF_FUNCTION atof
#endif
#ifndef ATOL_FUNCTION
#define ATOL_FUNCTION atol
#endif
#ifndef STRTOD_FUNCTION
#define STRTOD_FUNCTION strtod
#endif
#ifndef STRTOL_FUNCTION
#define STRTOL_FUNCTION strtol
#endif
SC_lexical_stream
*SC_current_lexical_stream = NULL;
PFDouble
SC_atof_hook = ATOF_FUNCTION,
SC_strtod_hook = STRTOD_FUNCTION;
PFLong
SC_otol_hook = _SC_otol,
SC_htol_hook = _SC_htol,
SC_atol_hook = ATOL_FUNCTION,
SC_strtol_hook = STRTOL_FUNCTION;
static int
SC_DECLARE(_SC_lex_putc, (int c)),
SC_DECLARE(_SC_lex_getc, (byte)),
SC_DECLARE(_SC_lex_push, (int c)),
SC_DECLARE(_SC_lex_wrap, (byte));
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
/* SC_BLANKP - return TRUE if the string s is blank or a comment
* - and FALSE otherwise
*/
int SC_blankp(s, chr)
char *s, *chr;
{int c;
if (strchr(chr, (int) s[0]) != NULL)
{if (strchr(" \t\r\n", s[1]) != NULL)
return(TRUE);}
else
switch (*s)
{case '\0' :
case '\n' :
return(TRUE);
default :
while (TRUE)
{c = *s++;
if (c == '\0')
return(TRUE);
if (strchr(" \t\r\n", c) == NULL)
return(FALSE);};};
return(FALSE);}
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
/* SC_STOF - string to double
* - returns 0.0 if string is NULL
*/
double SC_stof(s)
char *s;
{if (s == NULL)
return(0.0);
else
return(ATOF(s));}
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
/* SC_STOI - string to integer
* - returns 0 if string is NULL
*/
int SC_stoi(s)
char *s;
{if (s == NULL)
return(0);
else
return(atoi(s));}
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
/* SC_STOL - string to long integer
* - returns 0 if string is NULL
*/
long SC_stol(s)
char *s;
{if (s == NULL)
return(0);
else
return(ATOL(s));}
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
/* _SC_ATOF - a fast, working version of ATOF */
double _SC_atof(ps)
char *ps;
{int sign;
Register int val, i, pwr, exponent;
double accum;
char *s;
char *p;
sign = 1;
s = ps;
accum = 0;
/* get past white space */
for (p = s; (*p == ' ') || (*p == '\n') || (*p == '\t'); p++);
if ((*p == '+') || (*p == '-'))
sign = (*(p++) == '+') ? 1 : -1;
for (val = 0, i = 0; (*p >= '0') && (*p <= '9'); p++, i++)
{if (i < 4)
val = 10*val + *p - '0';
else
{accum = accum*1.0e4 + val;
val = *p - '0';
i = 0;};};
if (*p == '.')
p++;
for (pwr = 0; (*p >= '0') && (*p <= '9'); p++, i++)
{if (i < 4)
{val = 10*val + *p - '0';
pwr--;}
else
{accum = accum*1.0e4 + val;
val = *p - '0';
pwr--;
i = 0;};};
if (i != 0)
{for (; i < 4; i++, pwr--)
val *= 10;
accum = accum*1.0e4 + val;};
accum*=sign;
if ((*p == 'D') || (*p == 'E') ||
(*p == 'd') || (*p == 'e'))
p++;
sign = 1;
if ((*p == '+') || (*p == '-'))
sign = (*(p++) == '+') ? 1 : -1;
for (exponent = 0; (*p >= '0') && (*p <= '9'); p++)
exponent = 10*exponent + *p - '0';
exponent *= sign;
ps = p;
pwr += exponent;
return(accum*pow(10.0, (double) pwr));}
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
/* _SC_STRTOD - correct version of C library function strtod */
double _SC_strtod(nptr, endptr)
char *nptr;
char **endptr;
{char next, /* next character in ascii string */
*ptr; /* pointer to ascii string */
int dpseen;
dpseen = FALSE;
/* skip the white space */
ptr = nptr;
next = *ptr;
while (isspace(next))
next = *++ptr;
/* collect the fraction digits, record decimal point position if present */
if ((next == '+') || (next == '-') || (next == '.'))
{next = *++ptr;
if (next == '.')
{next = *++ptr;
dpseen = TRUE;};};
/* illegal digit string in fraction */
if (((next < '0') || (next > '9')) && (next != '.'))
{errno = EDOM;
if (endptr != NULL)
*endptr = ptr;
return(0);};
while (((next >= '0') && (next <= '9')) || (next == '.'))
{switch(next)
{case '.' : if (dpseen) /* illegal string - two decimal points */
{errno = EDOM;
if (endptr != NULL)
*endptr = ptr;
return(0);};
default : break;};
next = *++ptr;};
/* collect exponent digits, if present */
if ((next == 'e') || (next == 'E') ||
(next == 'd') || (next == 'D') ||
(next == '+') || (next == '-'))
{next = *++ptr;
if ((next == '+') || (next == '-'))
next = *++ptr;
/* illegal digit string in exponent */
if (!isdigit(next))
{errno = EDOM;
if (endptr != NULL)
*endptr = ptr;
return(0.0);};
while (isdigit(next))
next = *++ptr;};
/* return, setting endptr appropriately */
if (endptr != NULL)
*endptr = ptr;
return(_SC_atof(nptr));}
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
/* _SC_STRTOL - strtol done right (since some libraries are bad) */
long int _SC_strtol(str, ptr, base)
char *str;
char **ptr;
int base;
{long val;
Register int c;
int xx, neg = 0;
/* in case no number is formed */
if (ptr != (char **) 0)
*ptr = str;
/* base is invalid -- should be a fatal error */
if (base < 0 || base > MBASE)
return (0);
if (!isalnum(c = *str))
{while (isspace(c))
c = *++str;
switch (c)
{case '-' : neg++;
case '+' : c = *++str;};}; /* fall-through */
if (base == 0)
if (c != '0')
base = 10;
else if (str[1] == 'x' || str[1] == 'X')
base = 16;
else
base = 8;
/* for any base > 10, the digits incrementally following
* 9 are assumed to be "abc...z" or "ABC...Z"
*/
if (!isalnum(c) || (xx = ishexdigit(c)) >= base)
return(0); /* no number formed */
/* skip over leading "0x" or "0X" */
if ((base == 16) && (c == '0') && isxdigit(str[2]) &&
((str[1] == 'x') || (str[1] == 'X')))
c = *(str += 2);
/* accumulate neg avoids surprises near MAXLONG */
for (val = -ishexdigit(c);
isalnum(c = *++str) && (xx = ishexdigit(c)) < base; )
val = base * val - xx;
if (ptr != (char **) 0)
*ptr = str;
return(neg ? val : -val);}
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
/* _SC_OTOL - string representation of octal number to long int converter */
long int _SC_otol(str)
char *str;
{return(STRTOL(str, NULL, 8));}
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
/* _SC_HTOL - string representation of hex number to long int converter */
long int _SC_htol(str)
char *str;
{return(STRTOL(str, NULL, 16));}
/*--------------------------------------------------------------------------*/
/* STRING SORT ROUTINES */
/*--------------------------------------------------------------------------*/
/* SC_STRING_SORT - sort an array of character pointers
* - by what they point to
*/
void SC_string_sort(v, n)
char **v;
int n;
{int gap, i, j;
char *temp;
for (gap = n/2; gap > 0; gap /= 2)
for (i = gap; i < n; i++)
for (j = i-gap; j >= 0; j -= gap)
{if (strcmp(v[j], v[j+gap]) <= 0)
break;
temp = v[j];
v[j] = v[j+gap];
v[j+gap] = temp;};
return;}
/*--------------------------------------------------------------------------*/
/* LEXICAL SCANNER SUPPORT ROUTINES */
/*--------------------------------------------------------------------------*/
/* SC_OPEN_LEXICAL_STREAM - open up a stream for lexical scanning
* - Arguments:
* -
* - NAME - name of file or NULL for stdin
* - INBFSZ - byte size of input buffer or
* - MAXLINE if 0
* - STRBFSZ - byte size of string buffer or
* - MAX_LEN_BUFFER if 0
* - SCAN - scanning function such as yylex (int)
* - INPUT - input function (int)
* - SC_lex_getc used if NULL
* - OUTPUT - output function (int)
* - SC_lex_putc used if NULL
* - UNPUT - unput function (int)
* - SC_lex_push used if NULL
* - WRAP - wrap function (int)
* - _SC_wrap used if NULL
* - MORE - more function (int)
* - LESS - less function (int)
*/
SC_lexical_stream *SC_open_lexical_stream(name, inbfsz, strbfsz,
scan, input, output,
unput, wrap, more, less)
char *name;
int inbfsz, strbfsz;
PFInt scan, input, output, unput, wrap, more, less;
{SC_lexical_stream *str;
str = FMAKE(SC_lexical_stream, "SC_OPEN_LEXICAL_STREAM:str");
/* set up the file */
if (name != NULL)
{str->name = SC_strsavef(name,
"char*:SC_OPEN_LEXICAL_STREAM:name");
str->file = io_open(name, "r");}
else
{str->name = NULL;
str->file = stdin;};
/* allocate the token space */
str->n_tokens_max = 50;
str->n_tokens = 0;
str->tokens = FMAKE_N(SC_lexical_token, 50,
"SC_OPEN_LEXICAL_STREAM:tokens");
/* allocate the buffers */
if (inbfsz != 0)
{str->in_bf = FMAKE_N(char, inbfsz,
"SC_OPEN_LEXICAL_STREAM:in_bf");
str->out_bf = FMAKE_N(char, inbfsz,
"SC_OPEN_LEXICAL_STREAM:out_bf");}
else
{str->in_bf = FMAKE_N(char, MAXLINE,
"SC_OPEN_LEXICAL_STREAM:in_bf");
str->out_bf = FMAKE_N(char, MAXLINE,
"SC_OPEN_LEXICAL_STREAM:out_bf");}
if (strbfsz != 0)
str->str_bf = FMAKE_N(char, strbfsz,
"SC_OPEN_LEXICAL_STREAM:str_bf");
else
str->str_bf = FMAKE_N(char, MAX_LEX_BUFFER,
"SC_OPEN_LEXICAL_STREAM:str_bf");
str->str_ptr = str->str_bf;
str->in_ptr = str->in_bf;
str->out_ptr = str->out_bf;
/* set up the assocaited functions */
str->scan = scan;
str->more = more;
str->less = less;
if (input != NULL)
str->inp = input;
else
str->inp = _SC_lex_getc;
if (output != NULL)
str->out = output;
else
str->out = _SC_lex_putc;
if (input != NULL)
str->unp = unput;
else
str->unp = _SC_lex_push;
if (wrap != NULL)
str->wrap = wrap;
else
str->wrap = _SC_lex_wrap;
return(str);}
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
/* SC_CLOSE_LEXICAL_STREAM - close an SC_lexical_stream and clean up the
* - memory
*/
void SC_close_lexical_stream(str)
SC_lexical_stream *str;
{if (str->name != NULL)
{io_close(str->file);
SFREE(str->name);};
SFREE(str->tokens);
SFREE(str->in_bf);
SFREE(str->out_bf);
SFREE(str->str_bf);
SFREE(str);
return;}
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
/* SC_SCAN - using an automatically generated lexical scanner
* - scan the given input string and break it down into
* - its lexical tokens
* - STR is the SC_lexical_stream
* - if RD is TRUE, read a line from STR before scanning
* - return the number of tokens read and available
* - return -1 if RD is TRUE and EOF has been found
*/
int SC_scan(str, rd)
SC_lexical_stream *str;
int rd;
{int sz;
char *in, *out;
if (str->tokens == NULL)
return(-1);
memset(str->tokens, 0, str->n_tokens_max*sizeof(SC_lexical_token));
in = str->in_bf;
out = str->out_bf;
str->i_token = 0;
str->n_tokens = 0;
str->str_ptr = str->str_bf;
str->in_ptr = in;
str->out_ptr = out;
if (rd)
{sz = SC_arrlen(out);
memset(out, 0, sz);
sz = SC_arrlen(in);
if (GETLN(in, sz, str->file) == NULL)
return(-1);};
SC_current_lexical_stream = str;
SC_LEX_SCAN(str);
SC_current_lexical_stream = NULL;
return(str->n_tokens);}
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
/* SC_GET_NEXT_LEXICAL_TOKEN - get the next lexical token from the
* - given lexical stream
* - return NULL if no tokens available
* - NOTE: do we want to distinguish between
* - EOL, EOF, and an error or
* - should we leave that to the
* - lexical scanning rules?
*/
SC_lexical_token *SC_get_next_lexical_token(str)
SC_lexical_stream *str;
{SC_lexical_token *tok;
/* if no more tokens in the buffer, scan some more from the stream */
if (str->i_token >= str->n_tokens)
{SC_scan(str, TRUE);
if (str->n_tokens == 0)
return(NULL);};
tok = &str->tokens[str->i_token++];
return(tok);}
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
/* SC_LEX_PUSH_TOKEN - called by automatically generated lexical scanners
* - such as FLEX to push a token onto an array
*/
#ifdef PCC
void SC_lex_push_token(type, va_alist)
int type;
va_dcl
#endif
#ifdef ANSI
void SC_lex_push_token(int type, ...)
#endif
{int indx;
char *s;
SC_lexical_stream *str;
SC_VA_START(type);
str = SC_current_lexical_stream;
indx = str->n_tokens++;
str->tokens[indx].type = type;
switch (type)
{case SC_CMMNT_TOK :
case SC_STRING_TOK :
case SC_IDENT_TOK :
case SC_KEY_TOK :
case SC_OPER_TOK :
case SC_PRED_TOK :
case SC_DELIM_TOK :
case SC_WSPC_TOK :
case SC_HOLLER_TOK :
s = SC_VA_ARG(char *);
strcpy(str->str_ptr, s);
str->tokens[indx].val.s = str->str_ptr;
str->str_ptr += strlen(s) + 1;
break;
case SC_DINT_TOK :
case SC_HINT_TOK :
case SC_OINT_TOK :
str->tokens[indx].val.l = SC_VA_ARG(long);
break;
case SC_REAL_TOK :
str->tokens[indx].val.d = SC_VA_ARG(double);
break;
default :
break;};
SC_VA_END;
if (str->n_tokens >= str->n_tokens_max)
{str->n_tokens_max += 50;
REMAKE_N(str->tokens, SC_lexical_token, str->n_tokens_max);};
return;}
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
/* _SC_LEX_PUTC - a function to attach to the lexical scanner output macro */
static int _SC_lex_putc(c)
int c;
{*SC_current_lexical_stream->out_ptr++ = c;
return(c);}
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
/* _SC_LEX_GETC - a function to attach to the lexical scanner input macro */
static int _SC_lex_getc()
{return(*SC_current_lexical_stream->in_ptr++);}
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
/* _SC_LEX_PUSH - a function to attach to the lexical scanner unput macro */
static int _SC_lex_push(c)
int c;
{int nb;
SC_lexical_stream *str;
str = SC_current_lexical_stream;
nb = str->in_ptr - str->in_bf;
if (nb > 0)
{*(--str->in_ptr) = c;};
return(c);}
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
/* _SC_LEX_WRAP - a default EOF handler for lexical scanners */
static int _SC_lex_wrap()
{return(1);}
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
|