home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Applications / UIFlow 1.0.1 / UIFlow Source / VSet2.0 / Src / vg.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-20  |  15.0 KB  |  680 lines  |  [TEXT/????]

  1. /*****************************************************************************
  2. *              NCSA HDF Vset release 2.1
  3. *                    May 1991
  4. *
  5. * NCSA HDF Vset release 2.1 source code and documentation are in the public
  6. * domain.  Specifically, we give to the public domain all rights for future
  7. * licensing of the source code, all resale rights, and all publishing rights.
  8. * We ask, but do not require, that the following message be included in all
  9. * derived works:
  10. * Portions developed at the National Center for Supercomputing Applications at
  11. * the University of Illinois at Urbana-Champaign.
  12. * THE UNIVERSITY OF ILLINOIS GIVES NO WARRANTY, EXPRESSED OR IMPLIED, FOR THE
  13. * SOFTWARE AND/OR DOCUMENTATION PROVIDED, INCLUDING, WITHOUT LIMITATION,
  14. * WARRANTY OF MERCHANTABILITY AND WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE
  15. *****************************************************************************
  16. * Likkai Ng NCSA May 1991
  17. *
  18. * vg.c
  19. * Part of the HDF VSet interface
  20. * This file contains routine to handle VDATAs.
  21. *
  22. * Most routines return -1 (FAIL) on error. 
  23. * VSattach returns NULL on error.
  24. *
  25. * PRIVATE functions manipulate vsdir and are used only within this file.
  26. * PRIVATE data structures in here pertain to vdata in vsdir only.
  27. *
  28. **************************************************************************/
  29.  
  30. #include <ctype.h> 
  31. #include "vg.h"
  32.  
  33. PRIVATE int newref_count = -1; /* used by Vnewref only */
  34.  
  35. /* ------------------------------------------------------------------
  36. *    Vnewref
  37. *    utility routine. returns a unique reference number.
  38. *
  39. *    NOTE: should be modified to call DFnewref!!!
  40. *  currently looks once in vsdir and vgdir, and find the highest ref.
  41. *    also, the parameter f isn't used right now.
  42. *
  43. *    RETURNS a unique ref (+ve integer) ,
  44. *    RETURNS -1 if error
  45. *
  46. *  undocumented
  47. *
  48. */
  49.  
  50. int vnewref (f)            /*@@*/
  51.  
  52. DF  *f;
  53. {
  54. #if 0
  55.     int t;
  56.  
  57.  
  58.     if (f==NULL) return(FAIL);
  59.     if (newref_count == -1) {
  60.         for(t=1;t<65300;t++) {
  61.             if      ( -1 != vexistvg(f,t))   continue;
  62.             else if ( -1 != vexistvs(f,t))   continue;
  63.             else break;
  64.         }
  65.         newref_count = t;
  66.     }
  67.  
  68.     newref_count++;
  69.     if (newref_count > 65300) return(FAIL);
  70.  
  71.     return( newref_count - 1 );
  72. #endif
  73.  
  74. #if 1
  75.     return( DFnewref(f) );
  76. #endif
  77.  
  78. } /* vnewref */
  79.  
  80. /* ================================================================== */
  81.  
  82. /* matchnocase -  (PRIVATE) compares 2 strings, ignoring case 
  83. *                   returns 1 if match, else 0
  84. */
  85.  
  86. PRIVATE int matchnocase (strx,stry)     /*@@*/
  87.  
  88. char *strx,*stry;
  89. {
  90.     int     i,nx,ny,tx,ty;
  91.     char     *sx, *sy;
  92.  
  93.     nx = strlen(strx);
  94.     ny = strlen(stry);
  95.     if ( nx != ny) return(0);  /* different lengths */
  96.  
  97.     for (sx=strx, sy=stry, i=0;i<nx;i++,sx++,sy++) {
  98.         tx= *sx; 
  99.         ty= *sy;
  100.         if (islower(tx)) tx=toupper(tx);
  101.         if (islower(ty)) ty=toupper(ty);
  102.         if (tx != ty)       return(0);
  103.     }
  104.  
  105.     return(1);
  106.  
  107. } /* matchnocase */
  108.  
  109.  
  110.  
  111. /* ------------------------------------------------------------------
  112. * VSelts
  113. * returns the number of elements in the VDATA vs 
  114. * returns -1  on error.
  115. *
  116. * undocumented
  117. * 28-MAR-91 Jason NG NCSA
  118. *
  119. */
  120.  
  121. PUBLIC int32 VSelts (vs)                       /*@-@*/
  122.  
  123. VDATA *vs;
  124. {
  125.     return( (vs->otag==VSDESCTAG) ?  vs->nvertices : -1);
  126.  
  127. } /* VSelts */
  128.  
  129.  
  130.  
  131. /* ------------------------------------------------------------------
  132. *    VSgetinterlace 
  133. *  returns the interlace (in the file) of the vdata vs.
  134. *  returns -1 on error.
  135. *
  136. *  undocumented
  137. *
  138. */
  139.  
  140. PUBLIC int VSgetinterlace (vs)              /*@-@*/
  141.  
  142. VDATA *vs;
  143. {
  144.     return( (vs==NULL) ? FAIL : vs->interlace );
  145.  
  146. } /* VSgetinterlace */
  147.  
  148.  
  149.  
  150. /* ------------------------------------------------------------------
  151. *    VSsetinterlace 
  152. *     sets the vdata's interlace to full or none.
  153. *    returns -1 on error.
  154. */
  155.  
  156. PUBLIC int VSsetinterlace (vs, interlace)      /*@-@*/
  157.  
  158. VDATA *vs;
  159. int interlace;
  160. {
  161.     if (vs==NULL)
  162.         return(FAIL);
  163.     if ( vs->access == 'r' ||            /* only for write-only files */
  164.     vs->nvertices > 0)             /*may not change interlace if data exists*/
  165.         return(FAIL);
  166.  
  167.     /* currently only 2 kinds allowed */
  168.  
  169.     if ( interlace==FULL_INTERLACE || 
  170.         interlace==NO_INTERLACE ) {
  171.         vs->interlace = interlace;
  172.         return(1); /* ok */
  173.     }
  174.     else 
  175.         return(FAIL);                            /* bad interlace type */
  176.  
  177. } /* VSsetinterlace */
  178.  
  179.  
  180. /* ------------------------------------------------------------------
  181. *    VSgetfields 
  182. *  returns the fieldnames in a vdata.
  183. *  RETURNS  -1 on error, else the no of fields in the vdata.
  184. *
  185. *    undocumented
  186. *
  187. */
  188.  
  189. PUBLIC int VSgetfields (vs,fields)        /*@-@*/
  190.  
  191. VDATA *vs;
  192. char  *fields;                            /* fieldnames are returned in this var */
  193. {
  194.     int i;
  195.     if (vs==NULL) return(FAIL);
  196.  
  197.     fields[0] = '\0';
  198.     for (i=0;i<vs->wlist.n;i++) { /* build the comma-separated string */
  199.         strcat(fields,vs->wlist.name[i]);
  200.         if ( i < vs->wlist.n - 1 )
  201.             strcat(fields,",");
  202.     }
  203.  
  204.     return(vs->wlist.n);
  205.  
  206. } /* VSgetfields */
  207.  
  208. /* ------------------------------------------------------------------
  209. *    VSfexist
  210. *     tests for existence of 1 or more fields in a vdata.
  211. *
  212. *    RETURNS -1 if flase, or error
  213. *    RETURNS 1 if true
  214. */
  215.  
  216. PUBLIC int VSfexist (vs, fields)          /*@-@*/
  217.  
  218. VDATA *vs;
  219. char    *fields;
  220. {
  221.     char           **av, *s;
  222.     int            ac,i,j,found;
  223.     VWRITELIST    *w;
  224.  
  225.     if (vs==NULL) return(FAIL);
  226.  
  227.     /* call scanattrs to parse the string */
  228.  
  229.     if (scanattrs(fields,&ac,&av) < 0) return(FAIL); /* bad attrs string */
  230.     if (ac<1) return(FAIL);                                 /* no attrs */
  231.  
  232.     /* now check in vs's field table */
  233.  
  234.     w = &vs->wlist;
  235.     for (i=0;i<ac;i++) {
  236.         for (found=0,s=av[i],j=0;j<w->n;j++) {
  237.             if ( matchnocase(s,w->name[j]) ) {
  238.                 found=1;
  239.                 break;
  240.             }
  241.         }
  242.         if (!found) return(FAIL);
  243.     }
  244.  
  245.     return(1);
  246.  
  247. } /* VSfexist */
  248.  
  249.  
  250. /* ================================================================== */
  251. /*
  252. *    VSsizeof - computes the byte size of the field(s) of a vdata.
  253. *             - Note that the size is the actual size for the local machine.
  254. *
  255. *                - RETURNS -1 on error, else the field(s) size (+ve integer).
  256. */
  257.  
  258. PUBLIC int VSsizeof (vs, fields)           /*@-@*/
  259.  
  260. VDATA *vs;
  261. char  *fields;
  262. {
  263.     int     totalsize, ac, i,j,found;
  264.     char   **av;
  265.  
  266.     if (vs==NULL)                              return(FAIL);
  267.  
  268.     if (scanattrs(fields,&ac,&av) < 0) return(FAIL);     /* bad attrs string */
  269.     if (ac<1)                                  return(FAIL);     /* no attrs */
  270.  
  271.     if (vjv) { 
  272.         sprintf(sjs,"#VSsizeof: fields are [%s]\n",fields);
  273.         zj; 
  274.     }
  275.  
  276.     totalsize=0;
  277.     for (i=0;i<ac;i++) {
  278.         for (found=0,j=0;j<vs->wlist.n;j++)
  279.             if (!strcmp(av[i], vs->wlist.name[j])) {  /* check fields in vs */
  280.                 totalsize += vs->wlist.esize[j];
  281.                 found=1;
  282.                 break;
  283.             }
  284.  
  285.         if (!found) {
  286.             sprintf(sjs,"@VSsizeof:[%s] not in vs\n",av[i]);
  287.             zj;
  288.             return(FAIL);
  289.         }
  290.     }
  291.  
  292.     return(totalsize);
  293.  
  294. } /* VSsizeof */
  295.  
  296.  
  297. /* ================================================================== */
  298.  
  299. /*
  300. *    VSdump - prints contents of a vdata (for debugging) 
  301. *                no return codes.
  302. */
  303.  
  304. void VSdump (vs)                           /*@@*/
  305.  
  306. VDATA *vs;
  307. {
  308.     VWRITELIST    *w;
  309.     int             i;
  310.  
  311.     if (vs==NULL) {
  312.         sprintf(sjs,"@VSdump: vs is null\n");
  313.         zj;
  314.         return;
  315.     }
  316.  
  317.     sprintf(sjs,"@tag=%d ref=%d i=%d ",vs->otag, vs->oref,vs->interlace);
  318.     zj;
  319.     sprintf(sjs,"@nv=%d\n ",vs->nvertices);
  320.     zj;
  321.  
  322.     w = (VWRITELIST*) &vs->wlist;
  323.     sprintf(sjs,"@vsize(HDF)=%d fields=%d [%s]\n",w->ivsize,w->n,vs->vsname);
  324.     zj;
  325.  
  326.     for (i=0;i<w->n;i++)
  327.     { 
  328.         sprintf(sjs,"@<%s>      type:%d esize=%d isize=%d off=%d\n",
  329.             w->name[i], w->type[i], w->esize[i],w->isize[i],w->off[i]);
  330.         zj;
  331.     }
  332.  
  333.     sprintf(sjs,"\n");
  334.     zj;
  335.  
  336. } /* VSdump */
  337.  
  338.  
  339. /* ======================================================= */
  340. /*
  341. *    VSsetname - give a name to a vdata.
  342. *              - NO RETURN VALUES
  343. *              - truncates to max length of VSNAMELENMAX
  344. *      
  345. */
  346.  
  347. PUBLIC void VSsetname (vs, vsname)           /*@-@*/
  348.  
  349. VDATA *vs;
  350. char    *vsname;
  351.  
  352. {
  353.     if (vs == NULL) return;
  354.     if ( strlen(vsname) > VSNAMELENMAX) {
  355.         strncpy(vs->vsname, vsname,VSNAMELENMAX);
  356.         vs->vsname[VSNAMELENMAX]='\0';
  357.     }
  358.     else 
  359.         strcpy(vs->vsname, vsname);
  360.     vs->marked = 1;
  361.     return;
  362.  
  363. } /* VSsetname */
  364.  
  365. /* ======================================================= */
  366. /*
  367. *    VSsetclass- assigns a class name to a vdata.
  368. *              - NO RETURN VALUES
  369. *              - truncates to max length of VSNAMELENMAX
  370. *      
  371. */
  372.  
  373. PUBLIC void VSsetclass (vs, vsclass)           /*@-@*/
  374.  
  375. VDATA *vs;
  376. char    *vsclass;
  377.  
  378. {
  379.     if (vs == NULL) return;
  380.     if ( strlen(vsclass) > VSNAMELENMAX) {
  381.         strncpy(vs->vsclass, vsclass,VSNAMELENMAX);
  382.         vs->vsclass[VSNAMELENMAX]='\0';
  383.     }
  384.     else 
  385.         strcpy(vs->vsclass, vsclass);
  386.     vs->marked = 1;
  387.     return;
  388.  
  389. } /* VSsetclass*/
  390.  
  391. /* ======================================================= */
  392.  
  393. /*
  394. *    VSgetname - gets the vdata's name.
  395. *                 - NO RETURN VALUES
  396. */
  397.  
  398. PUBLIC void VSgetname (vs, vsname)           /*@-@*/
  399.  
  400. VDATA *vs;
  401. char    *vsname;
  402. {
  403.     if (vs != NULL) strcpy(vsname, vs->vsname);
  404.     return;
  405.  
  406. } /* VSgetname */
  407.  
  408. /* ======================================================= */
  409.  
  410. /*
  411. *    VSgetclass - gets the vdata's class name.
  412. *                 - NO RETURN VALUES
  413. */
  414.  
  415. PUBLIC void VSgetclass (vs, vsclass)           /*@-@*/
  416.  
  417. VDATA *vs;
  418. char    *vsclass;
  419. {
  420.     if (vs != NULL) strcpy(vsclass, vs->vsclass);
  421.     return;
  422.  
  423. } /* VSgetclass */
  424.  
  425.  
  426. /* ================================================================== */
  427. /*
  428. *    VSinquire - gets info about a vdata vs:
  429. *
  430. *                  nvertices:     no of vertices in it.
  431. *                  interlace:     its interlcae
  432. *                  fields :         a comma separated string listing the field(s). 
  433. *                                    (eg "PX,PY")
  434. *                  eltsize:        size of element (all field(s)) on local machine.
  435. *                  vsname:         vdata's name, if any.
  436. *
  437. *    RETURNS -1 if error
  438. *    RETURNS 1 if ok
  439. *
  440. */
  441.  
  442.  
  443. PUBLIC int VSinquire (vs, nelt, interlace, fields, eltsize, vsname)   /*@-@*/
  444.  
  445. VDATA     *vs;
  446. char      *fields, *vsname;
  447. int         *nelt, *interlace, *eltsize;
  448.  
  449. {
  450.     if (vs==NULL) return(FAIL);
  451.     VSgetfields (vs,fields);
  452.  
  453.     *nelt       = vs->nvertices;
  454.     *interlace  = vs->interlace;
  455.     *eltsize    =  VSsizeof (vs,fields);
  456.     strcpy(vsname,vs->vsname);
  457.  
  458.     return(1); /* ok */
  459.  
  460.  
  461. } /* VSinquire */
  462.  
  463. /* ================================================================== */
  464. /*
  465. * VSlone - returns an array of refs of all lone vdatas in the file.
  466. *           - returns -1 if error
  467. *          - otherwise returns the total number of lone vdatas in the file 
  468. *
  469. *            If idarray is too small, routine will only fill idarray with up
  470. *             to asize worth of refs.
  471. *
  472. *            INPUT idarray: user supplies  an int array.
  473. *           INPUT asize: integer specifying how many ints in idarray[];
  474. *            INPUT f: HDF file pointer.
  475. *
  476. */
  477.  
  478. PUBLIC int VSlone(f, idarray, asize) 
  479.  
  480. DF * f;
  481. int idarray[];             /* array to contain the refs */
  482. int asize;            /* input: size of idarray */
  483. {
  484. int      * lonevdata; /* local working area: stores flags of vdatas */
  485. int         i,vgid, vsid, vstag;
  486. VGROUP     * vg;
  487. int         dum;
  488. int         nlone; /* total number of lone vdatas */
  489.  
  490.  
  491. /* -- allocate space for vdata refs, init to zeroes -- */
  492. if (NULL == (lonevdata = (int*) DFIgetspace( 65000 * sizeof(int)))) 
  493.    RTNEG ("VSlone: space too low. VSlone not executed\n");
  494. for(i=0;i<65000;i++) lonevdata[i] = 0;
  495.  
  496. /* -- look for all vdatas in the file, and flag (1) each -- */
  497. vsid = -1;
  498. dum=0;
  499. while(1) {
  500.     if ( -1 == (vsid = VSgetid (f, vsid))) break; /* no more vdatas */
  501.     lonevdata[vsid ] = 1;
  502.     }
  503.  
  504. /* -- Look through all vgs, searching for vdatas -- */
  505. /* -- increment its index in lonevdata if found -- */
  506. dum=0;
  507. vgid = -1;
  508. while(1) {
  509.     if ( -1 == (vgid = Vgetid (f, vgid))) break; /* no more vgroups */
  510.     vg = (VGROUP*) Vattach(f,vgid,"r");
  511.     for (i=0; i< Vntagrefs(vg); i++) {
  512.         Vgettagref (vg, i, &vstag, &vsid);
  513.         if (vstag==VSDESCTAG) 
  514.             { lonevdata[vsid]++; dum++;  }
  515.         }
  516.     Vdetach(vg);
  517.     }
  518.  
  519. /* -- check in lonevdata: it's a lone vdata if its flag is still 1 -- */
  520. nlone = 0;
  521. for(i=0;i<65000;i++) {
  522.     if (1 == lonevdata[i]) {
  523.          if (nlone < asize) { /* insert into idarray up till asize */
  524.             idarray[nlone] = i;
  525.             }
  526.         nlone ++;
  527.         }
  528.    }
  529. DFIfreespace(lonevdata);
  530.  
  531. return(nlone); /* return the TOTAL # of lone vdatas */
  532.  
  533. } /* VSlone */
  534.  
  535. /* ================================================================== */
  536. /*
  537. * Vlone  - returns an array of refs of all lone vgroups in the file.
  538. *           - returns -1 if error
  539. *          - otherwise returns the total number of lone vgroups in the file 
  540. *
  541. *            If idarray is too small, routine will only fill idarray with up
  542. *             to asize worth of refs.
  543. *
  544. *            INPUT idarray: user supplies  an int array.
  545. *           INPUT asize: integer specifying how many ints in idarray[];
  546. *            INPUT f: HDF file pointer.
  547. *
  548. */
  549.  
  550. PUBLIC int Vlone(f, idarray, asize) 
  551.  
  552. DF * f;
  553. int idarray[];             /* array to contain the refs */
  554. int asize;            /* input: size of idarray */
  555. {
  556. int         * lonevg; /* local working area: stores flags of vgroups */
  557. int        i;
  558. int         vgid, vstag, id;
  559. VGROUP     * vg;
  560. int         dum;
  561. int         nlone; /* total number of lone vgroups */
  562.  
  563.  
  564. /* -- allocate space for vgroup refs, init to zeroes -- */
  565. if (NULL == (lonevg = (int*) DFIgetspace( 65000 * sizeof(int)))) 
  566.    RTNEG ("Vlone: space too low. Vlone not executed\n");
  567. for(i=0;i<65000;i++) lonevg[i] = 0;
  568.  
  569. /* -- look for all vgroups in the file, and flag (1) each -- */
  570. id = -1;
  571. dum=0;
  572. while(1) {
  573.     if ( -1 == (id = Vgetid (f, id))) break; /* no more vgroups */
  574.     lonevg[ id ] = 1;
  575.     }
  576.  
  577. /* -- Look through all vgs, searching for vgroups -- */
  578. /* -- increment its index in lonevg if found -- */
  579. dum=0;
  580. vgid = -1;
  581. while(1) {
  582.     if ( -1 == (vgid = Vgetid (f, vgid))) break; /* no more vgroups */
  583.    printf("Vlone: vgid=%d..attach",vgid);
  584.     vg = (VGROUP*) Vattach(f,vgid,"r");
  585.    printf("..attach done\n");
  586.     id = -1;
  587.     for (i=0; i< Vntagrefs(vg); i++) {
  588.         Vgettagref (vg, i, &vstag, &id);
  589.         if (vstag==VGDESCTAG) { lonevg[id]++; dum++;  }
  590.         }
  591.     Vdetach(vg);
  592.     }
  593.  
  594. /* -- check in lonevg: it's a lone vgroup if its flag is still 1 -- */
  595. nlone = 0;
  596. for(i=0;i<65000;i++) {
  597.     if (1 == lonevg[i]) {
  598.          if (nlone < asize) { /* insert into idarray up till asize */
  599.             idarray[nlone] = i;
  600.             }
  601.         nlone ++;
  602.         }
  603.    }
  604. DFIfreespace(lonevg);
  605.  
  606. return(nlone); /* return the TOTAL # of lone vgroups */
  607.  
  608. } /* Vlone */
  609.  
  610.  
  611. /* ================================================================== */
  612. /* new jan 3 1991 */
  613. /* looks in the file and returns the ref of the vgroup with name vgname */
  614. /* 
  615. * returns -1 if not found, or error.
  616. * otherwise, returns the vgroup's ref (a +ve integer).
  617. */
  618.  
  619. int Vfind (f, vgname)
  620. DF * f;
  621. char * vgname;
  622. {
  623.   int vgid = -1;
  624.   VGROUP* vg;
  625.   char name[512];
  626.  
  627.     while ( -1 != (vgid=Vgetid(f, vgid)) ) {
  628.         vg = (VGROUP*) Vattach(f,vgid,"r");
  629.         if (vg==NULL) return(-1);             /* error */
  630.         Vgetname(vg, name);
  631.         Vdetach (vg);
  632.         if (!strcmp(vgname,name)) return (vg->oref);  /* found the vgroup */
  633.       }
  634.       return(-1); /* not found */
  635.  
  636. } /* Vfind */
  637.  
  638. /* ================================================================== */
  639. /* new jan 3 1991 */
  640. /* looks in the file and returns the ref of the vdata with name vsname */
  641. /* 
  642. * returns -1 if not found, or error.
  643. * otherwise, returns the vdata's ref (a +ve integer).
  644. */
  645.  
  646. int VSfind (f, vsname)
  647. DF * f;
  648. char * vsname;
  649. {
  650.   int vsid = -1;
  651.   VDATA * vs;
  652.   char name[512];
  653.  
  654.     while ( -1 != (vsid=VSgetid(f, vsid)) ) {
  655.         vs = (VDATA*) VSattach(f,vsid,"r");
  656.         if (vs==NULL) return(-1);             /* error */
  657.         VSgetname(vs, name);
  658.         VSdetach (vs);
  659.         if (!strcmp(vsname, name)) return (vs->oref);  /* found the vdata */
  660.       }
  661.       return(-1); /* not found */
  662.  
  663. } /* VSfind */
  664.  
  665. /* ================================================================== */
  666.  
  667. /*
  668. * Vsetzap: Useless now. Maintained for back compatibility.
  669. */
  670.  
  671. Vsetzap() {
  672.     if (vjv) { sprintf(sjs,"Vsetzap: defunct\n"); zj; }
  673. }
  674. /* ================================================================== */
  675.