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
|
/*
* Copyright (c) 1999-2003 Smithsonian Astrophysical Observatory
*/
#include <funtoolsP.h>
#define SLEN 32
/*
*
* _FunKeyword -- look for a keyword=value string, or an environment variable
*
*/
#ifdef ANSI_FUNC
int _FunKeyword(char *buf, char *key, char *env, char *vbuf, int vlen)
#else
int _FunKeyword(buf, key, env, vbuf, vlen)
char *buf;
char *key;
char *env;
char *vbuf;
int vlen;
#endif
{
char *eptr;
/* look for the keyword in the input string */
if( keyword(buf, key, vbuf, vlen) )
return 1;
else{
/* look for an environment variable, if necessary */
if( env && (eptr = (char *)getenv(env)) ){
strncpy(vbuf, eptr, vlen);
return 1;
}
else
return 0;
}
}
/*
*
* _FunParseSection -- parse section information
* The possibilities are:
*
* name[xy0:xy1,block]
* name[x0:x1,y0:y1,block]
* name[x0:x1,*,block]
* name[*,y0:y1,block]
* name[*,block]
*
* where the min and max values can be ints or "*" for default. A single
* "*" can be used instead of val:val, as shown.
*
* name[dim@cen1@cen2,block]
* name[dim1@cen1,dim2@cen2,block]
*
* The values for dim, dim1, dim2, cen1, cen2 must be specified or else
* the expression does not make sense!
*
* In all cases, block is optional and defaults to 1. An 's' or 'a' can be
* appended to signify "sum" or "average" blocking
*
*
*/
#ifdef ANSI_FUNC
void
_FunParseSection(Fun fun, char *section,
int *x0, int *x1, int *y0, int *y1,
int *block, int *btype, char *tail, int maxlen)
#else
void _FunParseSection(fun, section, x0, x1, y0, y1, block, btype, tail, maxlen)
Fun fun;
char *section;
int *x0;
int *x1;
int *y0;
int *y1;
int *block;
int *btype;
char *tail;
int maxlen;
#endif
{
char *f;
char *tbuf;
char *s;
char s1[SLEN], s2[SLEN], s3[SLEN], s4[SLEN], s5[SLEN];
int i;
int n;
int got;
int len;
int itype;
int type1, type2;
double dim1=0.0, dim2=0.0;
double tx0, tx1, ty0, ty1;
double t0, t1;
double cen1=0.0, cen2=0.0;
double nan;
/* start out impossible */
*x0 = *y0 = MININT;
*x1 = *y1 = MAXINT;
*block = 1;
*btype = FUN_SUM;
/* 0 means section, 1 means dim@cen */
itype = 0;
/* get NaN value */
nan = getnand();
tx0 = tx1 = ty0 = ty1 = nan;
/* flag no conversion */
n = 0;
/* make sure we have something to work with */
if( (section == NULL) || (*section == '\0') )
return;
/* basic work buffers */
tbuf = (char *)xmalloc(strlen(section)+1);
/* this will be our pointer into the section buffer */
f = section;
/* maxlen generally is 1 more than we can handle */
maxlen--;
/* skip past the open bracket, but save it in the tail */
if( *f == '[' ){
f++;
strcpy(tbuf, "[");
}
/* look for different ways of specifying the section -- order counts! */
if(sscanf(f, "%[-0-9.*] : %[-0-9.*] , %[-0-9.*] : %[-0-9.*] , %[0-9as]%n",
s1, s2, s3, s4, s5, &n) == 5){
/* sanity check */
if( fun->dims == 1 ){
gerror(stderr, "can't specify a 2D section with a 1D image\n");
}
tx0 = (*s1 == '*') ? nan : atof(s1);
tx1 = (*s2 == '*') ? nan : atof(s2);
ty0 = (*s3 == '*') ? nan : atof(s3);
ty1 = (*s4 == '*') ? nan : atof(s4);
*block = MAX(1, strtol(s5, &s, 0));
if( s && *s && (tolower(*s) == 'a') )
*btype = FUN_AVG;
f += n;
}
else if(sscanf(f, "%[-0-9.*] : %[-0-9.*] , %[-0-9.*] : %[-0-9.*]%n",
s1, s2, s3, s4, &n) == 4){
/* sanity check */
if( fun->dims == 1 ){
gerror(stderr, "can't specify a 2D section with a 1D image\n");
}
tx0 = (*s1 == '*') ? nan : atof(s1);
tx1 = (*s2 == '*') ? nan : atof(s2);
ty0 = (*s3 == '*') ? nan : atof(s3);
ty1 = (*s4 == '*') ? nan : atof(s4);
f += n;
}
else if(sscanf(f, "%[-0-9.*] : %[-0-9.*] , %[*] , %[0-9as]%n",
s1, s2, s3, s4, &n) == 4){
/* sanity check */
if( fun->dims == 1 ){
gerror(stderr, "can't specify a 2D section with a 1D image\n");
}
tx0 = (*s1 == '*') ? nan : atof(s1);
tx1 = (*s2 == '*') ? nan : atof(s2);
*block = MAX(1, strtol(s4, &s, 0));
if( s && *s && (tolower(*s) == 'a') )
*btype = FUN_AVG;
f += n;
}
else if(sscanf(f, "%[-0-9.*] : %[-0-9.*] , %[*]%n",
s1, s2, s3, &n) == 3){
/* sanity check */
if( fun->dims == 1 ){
gerror(stderr, "can't specify a 2D section with a 1D image\n");
}
tx0 = (*s1 == '*') ? nan : atof(s1);
tx1 = (*s2 == '*') ? nan : atof(s2);
f += n;
}
else if(sscanf(f, "%[*] , %[-0-9.*] : %[-0-9.*] , %[0-9as]%n",
s1, s2, s3, s4, &n) == 4){
/* sanity check */
if( fun->dims == 1 ){
gerror(stderr, "can't specify a 2D section with a 1D image\n");
}
ty0 = (*s3 == '*') ? nan : atof(s2);
ty1 = (*s4 == '*') ? nan : atof(s3);
*block = MAX(1, strtol(s4, &s, 0));
if( s && *s && (tolower(*s) == 'a') )
*btype = FUN_AVG;
f += n;
}
else if(sscanf(f, "%[*] , %[-0-9.*] : %[-0-9.*]%n",
s1, s2, s3, &n) == 3){
/* sanity check */
if( fun->dims == 1 ){
gerror(stderr, "can't specify a 2D section with a 1D image\n");
}
ty0 = (*s3 == '*') ? nan : atof(s3);
ty1 = (*s4 == '*') ? nan : atof(s4);
f += n;
}
else if(sscanf(f, "%[-0-9.*] : %[-0-9.*] , %[0-9as]%n",
s1, s2, s3, &n) == 3){
tx0 = (*s1 == '*') ? nan : atof(s1);
tx1 = (*s2 == '*') ? nan : atof(s2);
ty0 = tx0;
ty1 = tx1;
*block = MAX(1, strtol(s3, &s, 0));
if( s && *s && (tolower(*s) == 'a') )
*btype = FUN_AVG;
f += n;
}
else if(sscanf(f, "%[-0-9.*] : %[-0-9.*]%n",
s1, s2, &n) == 2){
tx0 = (*s1 == '*') ? nan : atof(s1);
tx1 = (*s2 == '*') ? nan : atof(s2);
ty0 = tx0;
ty1 = tx1;
f += n;
}
else if(sscanf(f, "%[*] , %[*] , %[0-9as]%n",
s1, s2, s3, &n) == 3){
/* sanity check */
if( fun->dims == 1 ){
gerror(stderr, "can't specify a 2D section with a 1D image\n");
}
*block = MAX(1, strtol(s3, &s, 0));
if( s && *s && (tolower(*s) == 'a') )
*btype = FUN_AVG;
f += n;
}
else if(sscanf(f, "%[*] , %[0-9as]%n",
s1, s2, &n) == 2){
*block = MAX(1, strtol(s2, &s, 0));
if( s && *s && (tolower(*s) == 'a') )
*btype = FUN_AVG;
f += n;
}
else if(sscanf(f, "%[*] , %[*]%n",
s1, s2, &n) == 2){
/* sanity check */
if( fun->dims == 1 ){
gerror(stderr, "can't specify a 2D section with a 1D image\n");
}
f += n;
}
#ifdef BREAKS_STRING_FILTERING
else if(sscanf(f, "%[*]%n",
s1, &n) == 1){
f += n;
}
#endif
/* process dimension@center specifications */
else if(sscanf(f, "%[0-9.*] @ %[-0-9.*] , %[0-9.*] @ %[-0-9.*] , %[0-9as]%n",
s1, s2, s3, s4, s5, &n) == 5){
/* sanity check */
if( fun->dims == 1 ){
gerror(stderr, "can't specify a 2D section with a 1D image\n");
}
dim1 = atof(s1);
cen1 = atof(s2);
dim2 = atof(s3);
cen2 = atof(s4);
*block = MAX(1, strtol(s5, &s, 0));
if( s && *s && (tolower(*s) == 'a') )
*btype = FUN_AVG;
f += n;
itype++;
}
else if(sscanf(f, "%[0-9.*] @ %[-0-9.*] , %[0-9.*] @ %[-0-9.*]%n",
s1, s2, s3, s4, &n) == 4){
/* sanity check */
if( fun->dims == 1 ){
gerror(stderr, "can't specify a 2D section with a 1D image\n");
}
dim1 = atof(s1);
cen1 = atof(s2);
dim2 = atof(s3);
cen2 = atof(s4);
f += n;
itype++;
}
else if(sscanf(f, "%[0-9.*] @ %[-0-9.*] @ %[-0-9.*] , %[0-9as]%n",
s1, s2, s3, s4, &n) == 4){
/* sanity check */
if( fun->dims == 1 ){
gerror(stderr, "can't specify 2D section with a 1D image\n");
}
dim1 = atof(s1);
cen1 = atof(s2);
dim2 = dim1;
cen2 = atof(s3);
*block = MAX(1, strtol(s4, &s, 0));
if( s && *s && (tolower(*s) == 'a') )
*btype = FUN_AVG;
f += n;
itype++;
}
else if(sscanf(f, "%[0-9.*] @ %[-0-9.*] @ %[-0-9.*]%n",
s1, s2, s3, &n) == 3){
/* sanity check */
if( fun->dims == 1 ){
gerror(stderr, "can't specify 2D section with a 1D image\n");
}
dim1 = atof(s1);
cen1 = atof(s2);
dim2 = dim1;
cen2 = atof(s3);
f += n;
itype++;
}
else if(sscanf(f, "%[0-9.*] @ %[-0-9.*]%n",
s1, s2, &n) == 2){
dim1 = atof(s1);
cen1 = atof(s2);
dim2 = dim1;
cen2 = cen1;
f += n;
itype++;
}
/* if we are processing dim@center, we need to calculate section values */
if( itype ){
tx0 = cen1 - ((dim1+1)/2) + 1;
ty0 = cen2 - ((dim2+1)/2) + 1;
/* this method maintains the center and changes the dimensions */
/* Frank, Eric, and John all prefer this method, so that the user
gets the center he asked for, even if the image is reduced */
tx1 = cen1 + (dim1/2);
ty1 = cen2 + (dim2/2);
}
/* if we converted, then a 'p' => physical, 'i' => image coords
(image is the default) -- we need image */
if( n && (*f == 'p') ){
f++;
if( fun ){
switch(fun->type){
case FUN_IMAGE:
if( !isnand(tx0) && !isnand(ty0) ){
ft_phy2img(fun->header, tx0, ty0, &t0, &t1);
tx0 = t0;
ty0 = t1;
}
if( !isnand(tx1) && !isnand(ty1) ){
ft_phy2img(fun->header, tx1, ty1, &t0, &t1);
tx1 = t0;
ty1 = t1;
}
break;
case FUN_TABLE:
case FUN_EVENTS:
type1 = MAX(0,fun->header->table->col[fun->bin[0]].type);
type2 = MAX(0,fun->header->table->col[fun->bin[1]].type);
if( !isnand(tx0) ) tx0 = tlp2i(tx0, fun->min1, fun->binsiz1, type1);
if( !isnand(tx1) ) tx1 = tlp2i(tx1, fun->min1, fun->binsiz1, type1);
if( !isnand(ty0) ) ty0 = tlp2i(ty0, fun->min2, fun->binsiz2, type2);
if( !isnand(ty1) ) ty1 = tlp2i(ty1, fun->min2, fun->binsiz2, type2);
break;
case FUN_ARRAY:
break;
}
}
}
/* now we can integerize and set the output values in image coords */
if( !isnand(tx0) )
*x0 = (int)tx0;
else
*x0 = -1;
if( !isnand(tx1) )
*x1 = (int)tx1;
else
*x1 = -1;
if( !isnand(ty0) )
*y0 = (int)ty0;
else
*y0 = -1;
if( !isnand(ty1) )
*y1 = (int)ty1;
else
*y1 = -1;
/* at this point, we are processing tail */
if( *f == ',' )
f++;
if( *f != '\0' ){
strcat(tbuf, f);
}
strncpy(tail, tbuf, maxlen);
tail[maxlen] = '\0';
/* get rid of any empty brackets */
len = strlen(tail);
for(i=0; i<len; i++){
if( (tail[i] == '[') && (i+2 < len) && (tail[i+1] == ']') ){
strcpy(tbuf, &(tail[i+2]));
strcpy(&(tail[i]), tbuf);
}
}
/* but don't bother passing back completely null brackets */
len = strlen(tail);
for(got=0, i=0; i<len; i++){
if( (tail[i] != '[') && (tail[i] != ']') && (tail[i] != ' ') ){
got = 1;
break;
}
}
if( !got ) *tail = '\0';
/* clean up */
if( tbuf != NULL )
xfree(tbuf);
}
/*
*
* FunPrimaryExtension -- write a dummy primary header for a FITS extension
*
*/
#ifdef ANSI_FUNC
int
_FunPrimaryExtension(Fun fun)
#else
int _FunPrimaryExtension(fun)
Fun fun;
#endif
{
FITSHead header, merge;
/* exit if we already have written the header (or if we have no funtools) */
if( !_FunValid(fun) || fun->primio || (gtell(fun->gio)>0) )
return 0;
/* create a temp fitsy header */
if( !(header = ft_headinit(NULL, 0)) )
return 0;
/* add the essential cards to indicate a FITS file with extensions */
ft_headsetl(header, "SIMPLE", 0, 1, "FITS STANDARD", 1);
ft_headseti(header, "BITPIX", 0, 8, "Binary data", 1);
ft_headseti(header, "NAXIS", 0, 0, "No image array present", 1);
ft_headsetl(header, "EXTEND", 0, 1, "Standard extensions might follow", 1);
ft_syncdata(header);
/* merge these new params in with any we added earlier using FunParamPut */
if( fun->primary ){
merge = ft_headmerge(header, fun->primary, 0);
ft_syncdata(merge);
ft_headwrite(fun->gio, merge);
ft_headfree(merge, 1);
}
/* should not happen, but ... */
else{
ft_headwrite(fun->gio, header);
}
/* free up space */
ft_headfree(header, 1);
/* wrote the primary header */
fun->primio++;
return 1;
}
/*
*
* _FunColumnDims -- read a string that details the tlmin, tlmax, binsiz, and
* tscale, tzero for a column; string is of the form:
*
* [:tlmin[:tmlax[:binsiz]]][;tscal:tzero]
*
* If only one arg is given, its assumed to be the overall image dimension,
* and tlmin, tlmax defaults are calculated based on data type.
*
* We allow binsiz to be 0 as a flag that it was not set. The software that
* uses binsize (filter/tl.c) will use 1.0 instead of 0.0.
*
*
*/
#ifdef ANSI_FUNC
int
_FunColumnDims(char *s, int type, double *tlmin, double *tlmax,
double *binsiz, int *dims,
double *tscale, double *tzero, int *scaled)
#else
int _FunColumnDims(s, type, tlmin, tlmax, binsiz, dims, tscale, tzero, scaled)
char *s;
int type;
double *tlmin;
double *tlmax;
double *binsiz;
int *dims;
double *tscale;
double *tzero;
int *scaled;
#endif
{
int i, got, got2;
char *v;
char vbuf[SZ_LINE];
double val[3];
/* intiialize */
*tlmin = 0.0;
*tlmax = 0.0;
*binsiz = 1.0;
*dims = 0;
*tscale = 1.0;
*tzero = 0.0;
*scaled = 0;
/* make sure we have something to do */
if( !s || !*s )
return 0;
/* get image dimensions and bin size */
for(i=0, got=0; i<3; i++, got++){
if( *s != ':' )
break;
/* skip past ':' */
s++;
/* fill buffer with next value */
*vbuf = '\0';
for(v=vbuf; *s && (*s != ',') && (*s != ':') && (*s != ';'); )
*v++ = *s++;
*v = '\0';
/* convert string to double */
val[i] = atof(vbuf);
}
switch(got){
case 0:
break;
case 1:
switch(type){
case 'B':
case 'I':
case 'U':
case 'J':
case 'K':
case 'V':
*tlmin = 1.0;
*tlmax = val[0];
*dims = 1;
break;
case 'E':
case 'D':
*tlmin = 0.5;
*tlmax = val[0]+0.5;
*dims = 1;
break;
default:
*tlmin = 1.0;
*tlmax = val[0];
*dims = 1;
break;
}
break;
case 2:
*tlmin = val[0];
*tlmax = val[1];
*dims = 1;
break;
case 3:
default:
*tlmin = val[0];
*tlmax = val[1];
*binsiz = val[2];
*dims = 1;
break;
}
/* check for next section: tscale, tzero */
if( *s == ';' ){
*s = ':';
for(i=0, got2=0; i<3; i++, got2++){
if( *s != ':' )
break;
/* skip past ':' */
s++;
/* fill buffer with next value */
*vbuf = '\0';
for(v=vbuf; *s && (*s != ',') && (*s != ':') && (*s != ';'); )
*v++ = *s++;
*v = '\0';
/* convert string to double */
val[i] = atof(vbuf);
}
switch(got2){
case 0:
break;
case 1:
*tscale = val[0];
*tzero = 0.0;
*scaled = 1;
break;
case 2:
*tscale = val[0];
*tzero = val[1];
*scaled = 1;
break;
}
}
return got;
}
/*
*
* _FunColumnType -- read a string that details the type for a column
* string is of the form:
*
* [@][<n>]<type>[[poff]][:[tlmin[:tmlax[:binsiz]]]]
*
* where @ means ptype (pointer type) is true
*
*/
#ifdef ANSI_FUNC
void
_FunColumnType(char *s, int *type, int *n,
double *tlmin, double *tlmax, double *binsiz, int *dims,
double *tscale, double *tzero, int *scaled,
int *ptype, int *poff)
#else
void _FunColumnType(s, type, n, tlmin, tlmax, binsiz, dims,
tscale, tzero, scaled, ptype, poff)
char *s;
int *type;
int *n;
double *tlmin;
double *tlmax;
double *binsiz;
int *dims,
double *tscale;
double *tzero;
int *scaled;
int *ptype;
int *poff;
#endif
{
int ival;
int boff=0;
char *t;
/* initialize values */
*n = 1;
*ptype = 0;
*poff = 0;
*type = '?';
*tlmin = 0.0;
*tlmax = 0.0;
*binsiz = 1.0;
*dims = 0;
*tscale = 1.0;
*tzero = 0.0;
*scaled = 0;
/* make sure we have something to do */
if( !s || !*s )
return;
/* look for indication that this is a pointer */
if( *s == '@' ){
*ptype = COL_PTR;
s++;
}
/* get optional count */
ival = strtol(s, &t, 10);
if( s != t ){
*n = ival;
s = t;
}
/* type character comes next */
*type = *s;
if( *type ){
if( islower(*type) )
*type = toupper(*type);
s++;
}
/* we might have an offset specified as well */
if( *s == '[' ){
s++;
/* 'B' means its a byte offset */
if( (*s == 'B') || (*s == 'b') ){
boff = 1;
s++;
}
ival = strtol(s, &t, 10);
if( s != t ){
/* negative offset flags byte offset (not great, but ...) */
if( boff )
*poff = -ival;
/* positive offset flags data-type offset */
else
*poff = ival;
s = t;
}
if( *s == ']' )
s++;
}
/* parse the rest of the string into tlmin, tlmax, binsiz, etc. */
(void)_FunColumnDims(s, *type, tlmin, tlmax, binsiz, dims,
tscale, tzero, scaled);
}
/*
*
* _FunFile -- parse next filename from a string
*
*/
#ifdef ANSI_FUNC
int
_FunFile (char *lbuf, char *tbuf, int len, int *lptr)
#else
int _FunFile(lbuf, tbuf, len, lptr)
char *lbuf;
char *tbuf;
int len;
int *lptr;
#endif
{
int ip;
int i;
int bracklev=0;
int atstart=0;
char quotes;
/* null out the output string */
*tbuf = '\0';
/* if no string was specified, just return */
if( lbuf == NULL )
return(0);
/* just a more convenient pointer ... */
ip = *lptr;
/* if we are at the end of string, just return */
if( lbuf[ip] == '\0' )
return(0);
/* start of the string can be "list:" for a list of files, else its a
single file; we'll do the actual check after the whitespace */
if( ip == 0 )
atstart=1;
/* skip over white space */
while( isspace((int)lbuf[ip]) ){
if( lbuf[ip] == '\0' ){
*lptr = ip;
return(0);
}
else
ip++;
}
/* now check for list, but only at the start */
if( atstart ){
if( strncasecmp(&lbuf[ip], "list:", 5) ){
/* not a list, return full string */
i = strlen(&lbuf[ip]);
if( i < len ){
strncpy(tbuf, &lbuf[ip], i);
ip += i;
goto done;
}
else{
ip += i;
goto error;
}
}
else{
/* its a list, skip prefix */
ip += 5;
/* skip over white space ... again */
while( isspace((int)lbuf[ip]) ){
if( lbuf[ip] == '\0' ){
*lptr = ip;
return(0);
}
else
ip++;
}
}
}
/* check for an explicit quote */
quotes = '\0';
if( lbuf[ip] == '"' ){
quotes = '"';
}
if( lbuf[ip] == '\'' ){
quotes = '\'';
}
/* grab next token */
if( quotes != '\0' ){
/* bump past quotes */
ip++;
/* grab up to next quotes -- but skip escaped quotes */
for(i=0; lbuf[ip] != '\0'; i++, ip++){
if( (lbuf[ip] == quotes) && (lbuf[ip-1] != '\\') )
break;
else{
if( i >= len ) goto error;
tbuf[i] = lbuf[ip];
}
}
}
else{
/* grab up to next whitespace */
i = 0;
again:
for(; lbuf[ip] && (!isspace((int)lbuf[ip]) || (bracklev>0)); i++, ip++){
if( i >= len ) goto error;
tbuf[i] = lbuf[ip];
if( lbuf[ip] == '[' )
bracklev++;
else if( lbuf[ip] == ']' )
bracklev--;
}
/* skip white space */
while( lbuf[ip] && isspace((int)lbuf[ip]) )
ip++;
/* if the next char is '[', we are not done with this filename */
if( lbuf[ip] == '[' )
goto again;
/* back up so we process this character next time */
else
ip--;
}
done:
/* bump past delimiter (but not null terminator) */
if( lbuf[ip] )
ip++;
/* null terminate */
tbuf[i] = '\0';
/* got something */
*lptr = ip;
return(1);
/* file name is too long */
error:
gerror(stderr, "filename is larger than max allowable (%d)\n", len);
*lptr = ip;
return(0);
}
|