home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / compstol / cmpsttl1.lha / CompositeTool / v1.1.dist / HDF / dfgroup.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-05-08  |  10.2 KB  |  337 lines

  1. /*****************************************************************************
  2. *              NCSA HDF version 3.00
  3. *                December, 1989
  4. *
  5. * NCSA HDF Version 3.00 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. /*-----------------------------------------------------------------------------
  17.  * File:    dfgroup.c
  18.  * Purpose: Low level functions for implementing groups
  19.  * Invokes: df.c df.h
  20.  * Contents: 
  21.  *  DFdiread: read in the data identifier list from the group
  22.  *  DFdiget: get next data identifier from list
  23.  *  DFdisetup: get ready to store a list of data identifiers to write out
  24.  *  DFdiput: add a data identifier to the list to be written out
  25.  *  DFdiwrite: write out the list of data identifiers
  26.  *  DFDIputgroup: write out a group (array of tag/refs)
  27.  *  DFDIgetgroup: read in a group (array of tag/refs)
  28.  * Remarks: A group is a way of associating data elements with each other.
  29.  *          It is a tag whose data is a list of tag/refs
  30.  *          Each tag/ref combination is called a data identifier (DI).
  31.  *---------------------------------------------------------------------------*/
  32.  
  33.  
  34. #include "df.h"
  35.  
  36.  
  37. static DFdi *Dilist=NULL;    /* list of tag/refs constituting group */
  38. static int Dinlist;             /* no of tag/refs in list */
  39. static int Ndi;                 /* current position in list */
  40.  
  41. /*-----------------------------------------------------------------------------
  42.  * Name:    DFdiread
  43.  * Purpose: Read a list of DIs into memory
  44.  * Inputs:  dfile: HDF file pointer
  45.  *          tag, ref: id of group which is to be read in
  46.  * Returns: 0 on success, -1 on failure with DFerror set
  47.  * Users:   HDF systems programmers, DF8getrig, other routines
  48.  * Invokes: DFIcheck, DFIfind, DFgetelement
  49.  * Remarks: assumes tag is a group
  50.  *---------------------------------------------------------------------------*/
  51.  
  52. int DFdiread(dfile, tag, ref)
  53. DF *dfile;
  54. uint16 tag, ref;        /* tag, ref of group */
  55. {
  56.     DFdle *dlep;
  57.     int cdd;
  58.     int32 length;
  59.  
  60.     if (DFIcheck(dfile))
  61.         return(-1);
  62.  
  63.     /* find group */
  64.     if (DFIfind(dfile, tag, ref, 1, 0, 0, &dlep, &cdd) <0) {
  65.         DFerror = DFE_NOMATCH;
  66.         return(-1);
  67.     }
  68.  
  69.     /* get space for group */
  70.     length = dlep->dd[cdd].length;
  71.     if (Dilist) DFIfreespace((char*)Dilist); /* ensure earlier allocs freed */
  72.     Dilist = (DFdi *) DFIgetspace((unsigned)length);
  73.     if (!Dilist) {
  74.         DFerror = DFE_NOSPACE;
  75.         return(-1);
  76.     }
  77.  
  78.     Dinlist = length / 4;    /* 4==sizeof DFdi */
  79.     Ndi = 0;            /* no DIs returned so far */
  80.  
  81.     /* read in group */
  82.     if (DFgetelement(dfile, tag, ref, (char*) Dilist)<0) {
  83.         DFIfreespace((char*)Dilist);
  84.         Dilist = NULL;        /* flag value */
  85.         return(-1);
  86.     }
  87.     return(0);
  88. }
  89.  
  90. /*-----------------------------------------------------------------------------
  91.  * Name:    DFdiget
  92.  * Purpose: reaturn next DI from list
  93.  * Inputs:  di: space to return DI
  94.  * Returns: 0 on success, -1 on failure with DFerror set
  95.  * Users:   HDF systems programmers, DF8getrig, other routines
  96.  * Invokes: none
  97.  * Remarks: frees Dilist space when all DIs returned
  98.  *---------------------------------------------------------------------------*/
  99.  
  100. int DFdiget(di)
  101. DFdi *di;
  102. {
  103.     if (Ndi>=Dinlist)
  104.     return(-1);
  105.  
  106. #ifdef DF_STRUCTOK
  107.     *di = Dilist[Ndi++];    /* return next DI on list */
  108. #else /*DF_STRUCTOK*/
  109.     {
  110.         register char *p;
  111.     /* compute address of Ndi'th di */
  112.         p = (char *) Dilist + 4 * Ndi++;
  113.         UINT16READ(p, di->tag);
  114.         UINT16READ(p, di->ref);
  115.     }
  116. #endif /*DF_STRUCTOK*/
  117.     
  118.     if (Ndi==Dinlist) {
  119.         DFIfreespace((char*)Dilist); /* if all returned, free storage */
  120.         Dilist = NULL;        /* flag value */
  121.     }
  122.     return(0);
  123. }
  124.  
  125.  
  126. /*-----------------------------------------------------------------------------
  127.  * Name:    DFdisetup
  128.  * Purpose: setup space for storing a list of DIs to be written out
  129.  * Inputs:  maxsize: maximum number of DIs expected in the list
  130.  * Returns: 0 on success, -1 on failure with DFerror set
  131.  * Users:   HDF systems programmers, DF8putrig, other routines
  132.  * Invokes: none
  133.  * Remarks: This call should go away sometime.  Need better way to allocate
  134.  *          space, possibly just use a big block of static space
  135.  *---------------------------------------------------------------------------*/
  136.  
  137. int DFdisetup(maxsize)
  138. int maxsize;
  139. {
  140.     if (Dilist) DFIfreespace((char*)Dilist);
  141.     Dilist = (DFdi *) DFIgetspace((unsigned)(maxsize * 4));
  142.                 /* 4==sizeof(DFdi) */
  143.     if (!Dilist) {
  144.         DFerror = DFE_NOSPACE;
  145.         return(-1);
  146.     }
  147.     Dinlist = maxsize;        /* maximum size of list */
  148.     Ndi = 0;                    /* current size of list */
  149.     return(0);
  150. }
  151.  
  152. /*-----------------------------------------------------------------------------
  153.  * Name:    DFdiput
  154.  * Purpose: add a DI to the list to be written out
  155.  * Inputs:  tag, ref: DI to add
  156.  * Returns: 0 on success, -1 on failure with DFerror set
  157.  * Users:   HDF systems programmers, DF8putrig, other routines
  158.  * Invokes: none
  159.  * Remarks: arg is tag/ref rather than DI for convenience
  160.  *---------------------------------------------------------------------------*/
  161.  
  162. int DFdiput(tag, ref)
  163. uint16 tag, ref;
  164. {
  165.     register char *p;
  166.  
  167.     if (Ndi>=Dinlist) {
  168.         DFerror = DFE_NOTENOUGH;
  169.         return(-1);
  170.     }
  171.  
  172. #ifdef DF_STRUCTOK
  173.     Dilist[Ndi].tag = tag;
  174.     Dilist[Ndi++].ref = ref;
  175. #else /*DF_STRUCTOK*/
  176.     /* compute address of Ndi'th di to put tag/ref in */
  177.     p = (char *) Dilist + 4 * Ndi++;
  178.     UINT16WRITE(p, tag);
  179.     UINT16WRITE(p, ref);
  180. #endif /*DF_STRUCTOK*/
  181.  
  182.     return(0);
  183. }
  184.  
  185. /*-----------------------------------------------------------------------------
  186.  * Name:    DFdiwrite
  187.  * Purpose: Write DI list out to HDF file
  188.  * Inputs:  dfile: HDF file pointer
  189.  *          tag, ref: tag and ref of group whose contents is the list
  190.  * Returns: 0 on success, -1 on failure with DFerror set
  191.  * Users:   HDF systems programmers, DF8putrig, other routines
  192.  * Invokes: none
  193.  * Remarks: frees storage for Dilist
  194.  *---------------------------------------------------------------------------*/
  195.  
  196. int DFdiwrite(dfile, tag, ref)
  197. DF *dfile;
  198. uint16 tag, ref;
  199. {
  200.     int ret;            /* return value */
  201.  
  202.     if (DFIcheck(dfile))
  203.         return(-1);
  204.  
  205.     ret = DFputelement(dfile, tag, ref, (char*)Dilist,(int32)Ndi*4);
  206.                 /* 4==sizeof(DFdi) */
  207.     DFIfreespace((char*)Dilist);
  208.     Dilist = NULL;        /* flag value */
  209.     Dinlist = Ndi = 0;
  210.     return(ret);
  211. }
  212.  
  213. /*-----------------------------------------------------------------------------
  214.  * Name:    DFDIgetgroup
  215.  * Purpose: Read array of tag/refs from file
  216.  * Inputs:  filename: name of HDF file to read from
  217.  *          diarray: array to put tag/refs in
  218.  *          maxdis: maximum number of DIs that may be returned
  219.  *          groupdi: tag/ref of group to read
  220.  * Returns: number of DIs read on success, -1 on failure with DFerror set
  221.  * Users:   HDF systems programmers, other routines
  222.  * Invokes: none
  223.  * Remarks: none
  224.  *---------------------------------------------------------------------------*/
  225.  
  226. int DFDIgetgroup(filename, diarray, maxdis, groupdi)
  227. char *filename;
  228. int maxdis;
  229.  DFdi diarray[], *groupdi;
  230. {
  231.     DF *dfile;
  232.     int cdd, ret;
  233.     DFdle *dlep;
  234.     
  235.     DFerror = DFE_NOERROR;
  236.  
  237.     if (!filename[0]) {
  238.         DFerror = DFE_BADPTR;
  239.         return(-1);
  240.     }
  241.  
  242.     dfile = DFopen(filename, DFACC_ALL, -1);
  243.     if (dfile==NULL) return(-1);
  244.  
  245.     if (DFIfind(dfile, groupdi->tag, groupdi->ref, 1, 0, 0, &dlep, &cdd)<0)
  246.         return(DFIerr(dfile));
  247.  
  248.     if (DFaccess(dfile, groupdi->tag, groupdi->ref, "r")<0)
  249.         return(DFIerr(dfile));
  250.  
  251.     ret = dlep->dd[cdd].length / 4; /* 4==sizeof(DFdi) */
  252.     if (maxdis < ret) ret = maxdis;
  253.  
  254. #ifdef DF_STRUCTOK
  255.     if (DFread(dfile, diarray, (int32) (ret * sizeof( DFdi)))<0)
  256.         return(DFIerr(dfile));
  257. #else /*DF_STRUCTOK*/
  258.     {
  259.         int i;
  260.         char *p;
  261.  
  262.         p = (char *) DFtbuf;
  263.         if (DFread(dfile, p, (int32) ret*4)<0)
  264.             return(DFIerr(dfile));
  265.  
  266.         for (i=0; i<ret; i++) {
  267.             UINT16READ(p, diarray[i].tag);
  268.             UINT16READ(p, diarray[i].ref);
  269.         }
  270.     }
  271. #endif /*DF_STRUCTOK*/
  272.     
  273.     if (ret<maxdis) {
  274.         DFerror = DFE_NOTENOUGH;
  275.         return(DFIerr(dfile));
  276.     }
  277.     return(DFclose(dfile));
  278. }
  279.     
  280. /*-----------------------------------------------------------------------------
  281.  * Name:    DFDIputgroup
  282.  * Purpose: Write array of tag/refs to file
  283.  * Inputs:  filename: name of HDF file to write to
  284.  *          diarray: array of tag/refs to write out
  285.  *          ndis: number of DIs in array
  286.  *          groupdi: tag/ref of group to write array as
  287.  * Returns: 0 on success, -1 on failure with DFerror set
  288.  * Users:   HDF systems programmers, other routines
  289.  * Invokes: none
  290.  * Remarks: The group is always appended to an existing file
  291.  *---------------------------------------------------------------------------*/
  292.  
  293. int DFDIputgroup(filename, diarray, ndis, groupdi)
  294. char *filename;
  295. int ndis;
  296. DFdi diarray[], *groupdi;
  297. {
  298.     DF *dfile;
  299.     int ret;
  300.     
  301.     DFerror = DFE_NOERROR;
  302.  
  303.     if (!filename[0]) {
  304.         DFerror = DFE_BADPTR;
  305.         return(-1);
  306.     }
  307.  
  308.     dfile = DFopen(filename, DFACC_ALL, -1);
  309.     if (dfile==NULL) return(-1);
  310.  
  311. #ifdef DF_STRUCTOK
  312.     ret = DFputelement(dfile, groupdi->tag, groupdi->ref, diarray,
  313.                (int32) (ndis * 4));
  314. #else /*DF_STRUCTOK*/
  315.     {
  316.         int i;
  317.         char *p;
  318.  
  319.         p = (char *) DFtbuf;
  320.         for (i=0; i<ndis; i++) {
  321.             UINT16WRITE(p, diarray[i].tag);
  322.             UINT16WRITE(p, diarray[i].ref);
  323.         }
  324.         ret = DFputelement(dfile, groupdi->tag, groupdi->ref, p,
  325.                (int32)ndis*sizeof(DFdi));
  326.     }
  327. #endif /*DF_STRUCTOK*/
  328.     
  329.     if (ret<0) return(DFIerr(dfile));
  330.     return(DFclose(dfile));
  331. }
  332.