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
|
/*
* Copyright (c) 2005 Smithsonian Astrophysical Observatory
*/
#include <funtoolsP.h>
/*
*
* private routines
*
*/
/* default data base files */
#define FUN_VIEWFILE ".funtools.vu"
#define FUN_VIEWHOME "$HOME/.funtools.vu"
/* default text options for database open */
#define FUN_VIEWEXT "[1,TEXT(alen=1024)]"
/* default is to allow multiple matches */
#define FUN_VIEW_MATCH_DEFAULT 1
/* if a list is specified, do we also match templates? */
#define FUN_VIEW_DOROW_DEFAULT 1
/* types of matches */
#define MATCH_VIEW 1
#define MATCH_FILE 2
#define MATCH_VTMP 4
#define MATCH_FTMP 8
/* this should match what is in fun_viewopen's alen above */
#define SZ_VIEW 1024
/* parser characteristics struct */
typedef struct _rowstruct{
int type;
char view[SZ_VIEW];
char file[SZ_VIEW];
char fmt[SZ_VIEW];
char cols[SZ_VIEW];
char filt[SZ_VIEW];
} *Row, RowRec;
typedef struct _viewstruct{
char *vname;
char *vroot;
int nlist;
int maxlist;
char **vlist;
Fun ifun;
Fun vfun;
int nrow;
int maxrow;
Row *rows;
Row file, filt, cols, fmt;
Row trow;
} *View, ViewRec;
#ifdef ANSI_FUNC
static int
pathcmp(char *s1, char *s2)
#else
static int
pathcmp(s1, s2)
char *s1;
char *s2;
#endif
{
char tbuf1[SZ_LINE];
char tbuf2[SZ_LINE];
char tbuf1a[SZ_LINE];
char tbuf2a[SZ_LINE];
/* resolve pathnames and expand environment variables */
ExpandEnv(s1, tbuf1, SZ_LINE);
ExpandEnv(s2, tbuf2, SZ_LINE);
ResolvePath(tbuf1, tbuf1a, SZ_LINE);
ResolvePath(tbuf2, tbuf2a, SZ_LINE);
/* now compare strings */
return strcmp(tbuf1a, tbuf2a);
}
#ifdef ANSI_FUNC
static int
ViewFree(View view)
#else
static int ViewFree(view)
View view;
#endif
{
int i;
/* sanity check */
if( !view ) return 0;
/* free up resources */
if( view->vfun ) FunClose(view->vfun);
if( view->vname ) xfree(view->vname);
if( view->vroot ) xfree(view->vroot);
if( view->trow ) xfree(view->trow);
if( view->rows ){
for(i=0; i<view->nrow; i++){
if( view->rows[i] ) xfree(view->rows[i]);
}
xfree(view->rows);
}
if( view->vlist ){
for(i=0; i<view->nlist; i++){
if( view->vlist[i] ) xfree(view->vlist[i]);
}
xfree(view->vlist);
}
xfree(view);
return 1;
}
#ifdef ANSI_FUNC
static View
ViewNew(Fun fun, char *vname, char *vptr, char *vlist, char *dbname, int dbmax)
#else
static View
ViewNew(fun, vname, vptr, vlist, dbname, dbmax)
Fun fun;
char *vname;
char *vptr;
char *vlist;
char *dbname;
int dbmax;
#endif
{
int ip=0;
char *s, *t;
char *rmode="r";
char *fmode="rV";
char *params=NULL;
char tbuf[SZ_LINE];
char dtype[SZ_LINE];
View view=NULL;
/* look for view file in the usual places */
if( (s=(char *)getenv("FUN_VIEWFILE")) && (t=Access(s, rmode)) ){
xfree(t);
goto gotv;
}
else if( (t=Access(FUN_VIEWFILE, rmode)) ){
xfree(t);
s = FUN_VIEWFILE;
goto gotv;
}
else if( (t=Access(FUN_VIEWHOME, rmode)) ){
xfree(t);
s = FUN_VIEWHOME;
goto gotv;
}
else{
return NULL;
}
gotv:
/* allocate the view struct */
if( !(view=(View)xcalloc(1, sizeof(ViewRec))) ){
gerror(stderr, "can't allocate view database struct\n");
return NULL;
}
/* allocate temp row */
if( !(view->trow=(Row)xcalloc(1, sizeof(RowRec))) ){
gerror(stderr, "can't allocate view temp row struct\n");
ViewFree(view);
return NULL;
}
/* add default text options to the filename, if needed */
if( strchr(s, '[') ){
strncpy(tbuf, s, SZ_LINE);
}
else{
snprintf(tbuf, SZ_LINE, "%s%s", s, FUN_VIEWEXT);
}
/* open the file */
if( !(view->vfun=FunOpen(tbuf, fmode, NULL)) ){
gerror(stderr, "can't open view database file: %s\n", s);
ViewFree(view);
return NULL;
}
/* select columns from view database */
snprintf(dtype, SZ_LINE-1, "%dA", SZ_VIEW);
FunColumnSelect(view->vfun, sizeof(RowRec), params,
"view", dtype, rmode, FUN_OFFSET(Row, view),
"file", dtype, rmode, FUN_OFFSET(Row, file),
"format", dtype, rmode, FUN_OFFSET(Row, fmt),
"columns", dtype, rmode, FUN_OFFSET(Row, cols),
"filter", dtype, rmode, FUN_OFFSET(Row, filt),
NULL);
/* save original view string */
view->vname = xstrdup(vname);
/* save view name without any bracket extension */
view->vroot = xstrdup(vptr);
if( (s=strchr(view->vroot, '[')) ) *s = '\0';
/* save original input Fun handle */
view->ifun = fun;
/* save vlist as separate strings */
if( vlist ){
newdtable(",");
while( word(vlist, tbuf, &ip) ){
if( view->nlist >= view->maxlist ){
view->maxlist += SZ_VIEW;
if( view->vlist )
view->vlist = (char **)xrealloc(view->vlist,
view->maxlist*sizeof(char **));
else
view->vlist = (char **)xmalloc(view->maxlist*sizeof(char **));
}
/* add this record to the array of matches */
view->vlist[view->nlist++] = xstrdup(tbuf);
}
freedtable();
}
/* temp row, in case template is matched as a file => use input file name */
strncpy(view->trow->file, vptr, SZ_VIEW-1);
if( (s=strrchr(view->trow->file, '[')) ){
/* remove extension from file (we'll put it back later, if necessary) */
*s = '\0';
}
/* return name of database used, if necessary */
if( dbname ){
FunInfoGet(view->vfun, FUN_FNAME, &s, 0);
strncpy(dbname, s, dbmax-1);
}
/* return the good news */
return view;
}
#ifdef ANSI_FUNC
static int
ViewMatchRow(View view, Row row)
#else
static int
ViewMatchRow(view, row)
View view;
Row row;
#endif
{
char *root=NULL;
/* sanity check */
if( !row || !view->vroot || !*view->vroot ) return 0;
/* root is file without path */
if( (root=strrchr(row->file, '/')) )
root++;
else
root=row->file;
/* look for various types of match */
if( *row->view && !strcmp(view->vroot, row->view) )
row->type |= MATCH_VIEW;
/* if not a vew, perhaps a view template */
else if( *row->view && tmatch(view->vroot, row->view) )
row->type |= MATCH_VTMP;
/* check for file, with and without path */
if( (*row->file &&!pathcmp(view->vroot, row->file)) ||
(root && *root && !strcmp(view->vroot, root)))
row->type |= MATCH_FILE;
/* if not a file, perhaps a file template */
else if( root && *root && tmatch(view->vroot, root) )
row->type |= MATCH_FTMP;
/* return the results */
return row->type;
}
#ifdef ANSI_FUNC
static int
ViewSaveRow(View view, Row row)
#else
static int
ViewSaveRow(view, row)
View view;
Row row;
#endif
{
/* make sure we have enough space */
if( view->nrow >= view->maxrow ){
view->maxrow += SZ_VIEW;
if( view->rows )
view->rows = (Row *)xrealloc(view->rows, view->maxrow*sizeof(Row));
else
view->rows = (Row *)xmalloc(view->maxrow*sizeof(Row));
}
/* add this record to the array of matches */
view->rows[view->nrow++] = row;
return view->nrow;
}
#ifdef ANSI_FUNC
static int
ViewMatchList(View view, Row row, int dorow)
#else
static int
ViewMatchList(view, row, dorow)
View view;
Row row;
int dorow;
#endif
{
int i;
/* sanity check */
if( !row || !view->vlist ) return 0;
/* look though all view strings in the list */
for(i=0; i<view->nlist; i++){
/* does list view match row view? */
if( !strcmp(view->vlist[i], row->view) ){
row->type |= MATCH_VIEW;
break;
}
}
/* also check row matches, if necessary */
if( dorow ) ViewMatchRow(view, row);
/* return the results */
return row->type;
}
#ifdef ANSI_FUNC
static int
ViewProcessMatches(View view, char *fname, int fmax)
#else
static int
ViewProcessMatches(view, fname, fmax)
View view;
char *fname;
int fmax;
#endif
{
int i;
int nv=0;
int nf=0;
int nvt=0;
int nft=0;
int fgot=-1;
int tgot=-1;
int domulti=FUN_VIEW_MATCH_DEFAULT;
char *s=NULL;
/* sanity check */
if( !view ) return 0;
/* do we match a single row or use multiple row to complete the spec? */
if( (s=getenv("FUN_VIEWMATCH")) ){
if( !strncmp(s, "m", 1) ){
domulti = 1;
}
else if( !strncmp(s, "s", 1) ){
domulti = 0;
}
}
/* collect separate parts of fname using increasingly general matching */
if( view->nrow ){
/* get match counts */
for(i=0; i<view->nrow; i++){
if( view->rows[i]->type & MATCH_VIEW )
nv++;
if( view->rows[i]->type & MATCH_VTMP )
nvt++;
if( view->rows[i]->type & MATCH_FILE )
nf++;
if( view->rows[i]->type & MATCH_FTMP )
nft++;
}
/* view matches are the most desirable */
if( nv ){
for(i=0; i<view->nrow; i++){
if( view->rows[i]->type & MATCH_VIEW ){
/* if we are using a list, we always take the original filename */
if( view->nlist )
view->file = view->trow;
else if( !view->file && *view->rows[i]->file )
view->file = view->rows[i];
if( !view->filt && *view->rows[i]->filt )
view->filt = view->rows[i];
if( !view->cols && *view->rows[i]->cols )
view->cols = view->rows[i];
if( !view->fmt && *view->rows[i]->fmt )
view->fmt = view->rows[i];
tgot = i;
/* we only use one, unless we are running off a list */
if( !view->nlist ) break;
}
}
}
/* if no view matches, try file matches */
else if( nf ){
for(i=0; i<view->nrow; i++){
if( view->rows[i]->type & MATCH_FILE ){
if( !view->file && *view->rows[i]->file )
view->file = view->rows[i];
if( !view->filt && *view->rows[i]->filt )
view->filt = view->rows[i];
if( !view->cols && *view->rows[i]->cols )
view->cols = view->rows[i];
if( !view->fmt && *view->rows[i]->fmt )
view->fmt = view->rows[i];
fgot = i;
break;
}
}
}
/* if we got a match, only do templates if multiple matches is true */
if( (tgot==-1 && fgot==-1) || domulti ){
for(i=0; i<view->nrow; i++){
if( tgot == i ) continue;
if( view->rows[i]->type & MATCH_VTMP ){
/* use the input file name in this case */
if( !view->file && *view->rows[i]->file )
view->file = view->trow;
if( !view->filt && *view->rows[i]->filt )
view->filt = view->rows[i];
if( !view->cols && *view->rows[i]->cols )
view->cols = view->rows[i];
if( !view->fmt && *view->rows[i]->fmt )
view->fmt = view->rows[i];
/* mark use of view template so we don't do file templates */
tgot = i;
/* stop at one if not domulti */
if( !domulti ) break;
}
}
}
/* if we got a match, only do templates if multiple matches is true */
if( (tgot==-1 && fgot==-1) || domulti ){
for(i=0; i<view->nrow; i++){
if( fgot == i ) continue;
if( view->rows[i]->type & MATCH_FTMP ){
/* use the input file name in this case */
if( !view->file && *view->rows[i]->file )
view->file = view->trow;
if( !view->filt && *view->rows[i]->filt )
view->filt = view->rows[i];
if( !view->cols && *view->rows[i]->cols )
view->cols = view->rows[i];
if( !view->fmt && *view->rows[i]->fmt )
view->fmt = view->rows[i];
/* stop at one if not domulti */
if( !domulti ) break;
}
}
}
/* now we can make up the fname from the pieces we have found */
if( view->file && *view->file->file ){
/* see if original input had an extension */
if( (s=strchr(view->vname, '[')) ){
/* if we can guess that original was a filter, don't add new one */
if( strstr(s,"ann") ||
strstr(s,"box") ||
strstr(s,"cir") ||
strstr(s,"ell") ||
strstr(s,"lin") ||
strstr(s,"pan") ||
strstr(s,"pie") ||
strstr(s,"poi") ||
strstr(s,"qtp") ||
strstr(s,"pol") ||
strstr(s,"fie") ||
strstr(s,"bpa") ||
strstr(s,"cpa") ||
strstr(s,"epa") ||
/* guess at some sort of filter operation */
strpbrk(s, "!&|~=<>+-/%^") ){
snprintf(fname, fmax, "%s%s", view->file->file, s);
}
/* otherwise add original + new filter, if we have one */
else{
if( view->filt && *view->filt->filt ){
snprintf(fname, fmax, "%s%s[%s]",
view->file->file, s, view->filt->filt);
}
/* just add original */
else{
snprintf(fname, fmax, "%s%s", view->file->file, s);
}
}
}
/* no original filter */
else{
/* otherwise new filter, if we have one */
if( view->filt && *view->filt->filt ){
snprintf(fname, fmax, "%s[%s]", view->file->file, view->filt->filt);
}
/* just the file name */
else{
snprintf(fname, fmax, "%s", view->file->file);
}
}
/* save other view info */
if( view->ifun ){
if( view->cols && *view->cols->cols )
view->ifun->vcols = xstrdup(view->cols->cols);
if( view->fmt && *view->fmt->fmt )
view->ifun->vfmt = xstrdup(view->fmt->fmt);
}
}
return 1;
}
else{
return 0;
}
}
/*
*
* public routines
*
*/
/*
*
* FunView -- translate view into a filename + view params
*
*/
#ifdef ANSI_FUNC
int
FunView(Fun fun, char *vname, char *vmode, char *fname, int fmax)
#else
int
FunView(fun, vname, vmode, fname, fmax)
Fun fun;
char *vname;
char *vmode;
char *fname;
int fmax;
#endif
{
int nev=0;
int got=0;
int dorow=FUN_VIEW_DOROW_DEFAULT;
char *s=NULL;
char *vptr=NULL;
char vlist[SZ_LINE];
Row row=NULL;
View view=NULL;
/* sanity checks */
if( !fname || !fmax ) return 0;
*fname = '\0';
if( !vname || !*vname ) return 0;
/* assume we don't have a view */
strncpy(fname, vname, fmax-1);
fname[fmax-1] = '\0';
/* sanity check -- this prevents infinite recusion below */
if( !fun || (vmode && strchr(vmode, 'V')) ) return 0;
/* see if a view ("v:...") was specified */
memset(vlist, 0, SZ_LINE);
vptr = vname;
if( !*vptr || (*vptr != 'v') ) return 0;
vptr++;
if( !*vptr || (*vptr != ':') ) return 0;
vptr++;
if( !*vptr ) return 0;
/* v::vid1,vid2...:file... */
if( (*vptr == ':') || (*vptr == '+') || (*vptr == '-') ){
if( *vptr == '+' ) dorow = 1;
else if( *vptr == '-' ) dorow = 0;
vptr++;
/* must have a closing ':' */
if( !*vptr || !(s = strchr(vptr, ':')) ) return 0;
/* copy the list */
strncpy(vlist, vptr, MIN(s-vptr,SZ_LINE-1));
/* bump past the ':' */
vptr = s+1;
}
if( !*vptr ) return 0;
/* now assume we don't match and want to use the supplied filename */
strncpy(fname, vptr, fmax-1);
fname[fmax-1] = '\0';
/* open view file */
if( !(view=ViewNew(fun, vname, vptr, vlist, NULL, 0)) ) goto done;
/* read rows from view database */
while( (row=FunTableRowGet(view->vfun, NULL, 1, NULL, &nev)) && nev ){
/* look for a match */
if( view->vlist ){
if( ViewMatchList(view, row, dorow) ){
ViewSaveRow(view, row);
/* clear row */
row = NULL;
}
}
else{
if( ViewMatchRow(view, row) ){
ViewSaveRow(view, row);
/* clear row */
row = NULL;
}
}
/* free record if it was not saved (and cleared) */
if( row ){
xfree(row);
row = NULL;
}
}
/* process view information and generate a new file name */
got = ViewProcessMatches(view, fname, fmax);
done:
/* clean up */
ViewFree(view);
if( row ) xfree(row);
/* return the news */
return got;
}
|