LCOV - code coverage report
Current view: top level - frmts/hdf4/hdf-eos - EHapi.c (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 242 509 47.5 %
Date: 2026-09-18 08:43:21 Functions: 16 17 94.1 %

          Line data    Source code
       1             : /*****************************************************************************
       2             :  *
       3             :  * This module has a number of additions and improvements over the original
       4             :  * implementation to be suitable for usage in GDAL HDF driver.
       5             :  *
       6             :  * Andrey Kiselev <dron@ak4719.spb.edu> is responsible for all the changes.
       7             :  ****************************************************************************/
       8             : 
       9             : /*
      10             : Copyright (C) 1996 Hughes and Applied Research Corporation
      11             : 
      12             : Permission to use, modify, and distribute this software and its documentation
      13             : for any purpose without fee is hereby granted, provided that the above
      14             : copyright notice appear in all copies and that both that copyright notice and
      15             : this permission notice appear in supporting documentation.
      16             : */
      17             : 
      18             : #include "cpl_port.h"
      19             : #include <errno.h>
      20             : #include <limits.h>
      21             : #include "mfhdf.h"
      22             : #include "HdfEosDef.h"
      23             : #include "hfile.h"
      24             : 
      25             : /* Set maximum number of HDF-EOS files to HDF limit (MAX_FILE) */
      26             : #define NEOSHDF MAX_FILE
      27             : static intn  EHXmaxfilecount = 0;
      28             : static uint8 *EHXtypeTable = NULL;
      29             : static uint8 *EHXacsTable = NULL;
      30             : static int32 *EHXfidTable = NULL;
      31             : static int32 *EHXsdTable = NULL;
      32             : 
      33             : /* define a macro for the string size of the utility strings and some dimension
      34             :    list strings. The value in previous versions of this code may not be
      35             :    enough in some cases. The length now is 512 which seems to be more than
      36             :    enough to hold larger strings. */
      37             : 
      38             : #define UTLSTR_MAX_SIZE 512
      39             : #define UTLSTRSIZE  32000
      40             : 
      41             : #define EHIDOFFSET 524288
      42             : 
      43             : #define HDFEOSVERSION 2.12
      44             : #define HDFEOSVERSION1 "2.12"
      45             : #include "HDFEOSVersion.h"
      46             : 
      47             : #define MAX_RETRIES 10
      48             : 
      49             : /* Function Prototypes */
      50             : static intn EHreset_maxopenfiles(intn);
      51             : static intn EHget_maxopenfiles(intn *, intn *);
      52             : static intn EHget_numfiles(void);
      53             : 
      54             : /*----------------------------------------------------------------------------|
      55             : |  BEGIN_PROLOG                                                               |
      56             : |                                                                             |
      57             : |  FUNCTION: EHopen                                                           |
      58             : |                                                                             |
      59             : |  DESCRIPTION: Opens HDF-EOS file and returns file handle                    |
      60             : |                                                                             |
      61             : |                                                                             |
      62             : |  Return Value    Type     Units     Description                             |
      63             : |  ============   ======  =========   =====================================   |
      64             : |  fid            int32               HDF-EOS file ID                         |
      65             : |                                                                             |
      66             : |  INPUTS:                                                                    |
      67             : |  filename       char                Filename                                |
      68             : |  access         intn                HDF access code                         |
      69             : |                                                                             |
      70             : |  OUTPUTS:                                                                   |
      71             : |             None                                                            |
      72             : |                                                                             |
      73             : |  NOTES:                                                                     |
      74             : |                                                                             |
      75             : |                                                                             |
      76             : |   Date     Programmer   Description                                         |
      77             : |  ======   ============  =================================================   |
      78             : |  Jun 96   Joel Gales    Original Programmer                                 |
      79             : |  Jul 96   Joel Gales    Add file id offset EHIDOFFSET                       |
      80             : |  Aug 96   Joel Gales    Add "END" statement to structural metadata          |
      81             : |  Sep 96   Joel Gales    Reverse order of Hopen and SDstart statements       |
      82             : |                         for RDWR and READ access                            |
      83             : |  Oct 96   Joel Gales    Trap CREATE & RDWR (no write permission)            |
      84             : |                         access errors                                       |
      85             : |  Apr 97   Joel Gales    Fix problem with RDWR open when file previously     |
      86             : |                         open for READONLY access                            |
      87             : |                                                                             |
      88             : |  END_PROLOG                                                                 |
      89             : -----------------------------------------------------------------------------*/
      90             : int32
      91          46 : EHopen(const char *filename, intn access)
      92             : 
      93             : {
      94             :     intn            i;    /* Loop index */
      95          46 :     intn            status = 0; /* routine return status variable */
      96             :     intn            dum;  /* Dummy variable */
      97          46 :     intn            curr_max = 0; /* maximum # of HDF files to open */
      98          46 :     intn            sys_limit = 0;  /* OS limit for maximum # of opened files */
      99             : 
     100          46 :     int32           HDFfid = 0; /* HDF file ID */
     101          46 :     int32           fid = -1; /* HDF-EOS file ID */
     102          46 :     int32           sdInterfaceID = 0;  /* HDF SDS interface ID */
     103             :     int32           attrIndex;  /* Structural Metadata attribute index */
     104             : 
     105          46 :     uint8           acs = 0;  /* Read (0) / Write (1) access code */
     106             : 
     107             :     char           *testname; /* Test filename */
     108             :     char            errbuf[256];/* Error report buffer */
     109             :     char           *metabuf;  /* Pointer to structural metadata buffer */
     110             :     char            hdfeosVersion[32];  /* HDFEOS version string */
     111             : 
     112             :     intn            retryCount;
     113             : 
     114             :     /* Request the system allowed number of opened files */
     115             :     /* and increase HDFEOS file tables to the same size  */
     116             :     /* ------------------------------------------------- */
     117          46 :     if (EHget_maxopenfiles(&curr_max, &sys_limit) >= 0
     118          46 :         && curr_max < sys_limit)
     119             :     {
     120          40 :         if (EHreset_maxopenfiles(sys_limit) < 0)
     121             :         {
     122           0 :       HEpush(DFE_ALROPEN, "EHopen", __FILE__, __LINE__);
     123           0 :       HEreport("Can't set maximum opened files number to \"%d\".\n", curr_max);
     124           0 :       return -1;
     125             :         }
     126             :     }
     127             : 
     128             :     /* Setup file interface */
     129             :     /* -------------------- */
     130          46 :     if (EHget_numfiles() < EHXmaxfilecount)
     131             :     {
     132             : 
     133             :   /*
     134             :    * Check that file has not been previously opened for write access if
     135             :    * current open request is not READONLY
     136             :    */
     137          46 :   if (access != DFACC_READ)
     138             :   {
     139             :       /* Loop through all files */
     140             :       /* ---------------------- */
     141           0 :       for (i = 0; i < EHXmaxfilecount; i++)
     142             :       {
     143             :     /* if entry is active file opened for write access ... */
     144             :     /* --------------------------------------------------- */
     145           0 :     if (EHXtypeTable[i] != 0 && EHXacsTable[i] == 1)
     146             :     {
     147             :         /* Get filename (testname) */
     148             :         /* ----------------------- */
     149           0 :         Hfidinquire(EHXfidTable[i], &testname, &dum, &dum);
     150             : 
     151             : 
     152             :         /* if same as filename then report error */
     153             :         /* ------------------------------------- */
     154           0 :         if (strcmp(testname, filename) == 0)
     155             :         {
     156           0 :       status = -1;
     157           0 :       fid = -1;
     158           0 :       HEpush(DFE_ALROPEN, "EHopen", __FILE__, __LINE__);
     159           0 :       HEreport("\"%s\" already open.\n", filename);
     160           0 :       break;
     161             :         }
     162             :     }
     163             :       }
     164             :   }
     165          46 :   if (status == 0)
     166             :   {
     167             :       /* Create HDF-EOS file */
     168             :       /* ------------------- */
     169          46 :       switch (access)
     170             :       {
     171           0 :       case DFACC_CREATE:
     172             : 
     173             :     /* Get SDS interface ID */
     174             :     /* -------------------- */
     175           0 :     sdInterfaceID = SDstart(filename, DFACC_CREATE);
     176             : 
     177             :     /* If SDstart successful ... */
     178             :     /* ------------------------- */
     179           0 :     if (sdInterfaceID != -1)
     180             :     {
     181             :         /* Set HDFEOS version number in file */
     182             :         /* --------------------------------- */
     183           0 :         snprintf(hdfeosVersion, sizeof(hdfeosVersion), "%s%s", "HDFEOS_V",
     184             :           HDFEOSVERSION1);
     185           0 :         SDsetattr(sdInterfaceID, "HDFEOSVersion", DFNT_CHAR8,
     186           0 :             (int)strlen(hdfeosVersion), hdfeosVersion);
     187             : 
     188             : 
     189             :         /* Get HDF file ID */
     190             :         /* --------------- */
     191           0 :         HDFfid = Hopen(filename, DFACC_RDWR, 0);
     192             : 
     193             :         /* Set open access to write */
     194             :         /* ------------------------ */
     195           0 :         acs = 1;
     196             : 
     197             :         /* Setup structural metadata */
     198             :         /* ------------------------- */
     199           0 :         metabuf = (char *) calloc(UTLSTRSIZE, 1);
     200           0 :         if(metabuf == NULL)
     201             :         {
     202           0 :       HEpush(DFE_NOSPACE,"EHopen", __FILE__, __LINE__);
     203           0 :       return(-1);
     204             :         }
     205             : 
     206           0 :         strcpy(metabuf, "GROUP=SwathStructure\n");
     207           0 :         strcat(metabuf, "END_GROUP=SwathStructure\n");
     208           0 :         strcat(metabuf, "GROUP=GridStructure\n");
     209           0 :         strcat(metabuf, "END_GROUP=GridStructure\n");
     210           0 :         strcat(metabuf, "GROUP=PointStructure\n");
     211           0 :         strcat(metabuf, "END_GROUP=PointStructure\n");
     212           0 :         strcat(metabuf, "END\n");
     213             : 
     214             :         /* Write Structural metadata */
     215             :         /* ------------------------- */
     216           0 :         SDsetattr(sdInterfaceID, "StructMetadata.0",
     217             :             DFNT_CHAR8, UTLSTRSIZE, metabuf);
     218           0 :         free(metabuf);
     219             :     } else
     220             :     {
     221             :         /* If error in SDstart then report */
     222             :         /* ------------------------------- */
     223           0 :         fid = -1;
     224           0 :         status = -1;
     225           0 :         HEpush(DFE_FNF, "EHopen", __FILE__, __LINE__);
     226           0 :         snprintf(errbuf, sizeof(errbuf), "%s%s%s", "\"", filename,
     227             :           "\" cannot be created.");
     228           0 :         HEreport("%s\n", errbuf);
     229             :     }
     230             : 
     231           0 :     break;
     232             : 
     233             :     /* Open existing HDF-EOS file for read/write access */
     234             :     /* ------------------------------------------------ */
     235           0 :       case DFACC_RDWR:
     236             : 
     237             :     /* Get HDF file ID */
     238             :     /* --------------- */
     239             : #ifndef _PGS_OLDNFS
     240             : /* The following loop around the function Hopen is intended to deal with the NFS cache
     241             :    problem when opening file fails with errno = 150 or 151. When NFS cache is updated,
     242             :    this part of change is no longer necessary.              10/18/1999   */
     243           0 :                 retryCount = 0;
     244           0 :                 HDFfid = -1;
     245           0 :                 while ((HDFfid == -1) && (retryCount < MAX_RETRIES))
     246             :                 {
     247           0 :                 HDFfid = Hopen(filename, DFACC_RDWR, 0);
     248           0 :                 if((HDFfid == -1) && (errno == 150 || errno == 151))
     249             :                     {
     250           0 :                     HEpush(DFE_FNF, "EHopen", __FILE__, __LINE__);
     251           0 :                     snprintf(errbuf, sizeof(errbuf), "\"%s\" cannot be opened for READ/WRITE access, will retry %d times.", filename,  (MAX_RETRIES - retryCount - 1));
     252           0 :                     HEreport("%s\n", errbuf);
     253             :                     }
     254           0 :                 retryCount++;
     255             :                 }
     256             : #else
     257             :                 HDFfid = Hopen(filename, DFACC_RDWR, 0);
     258             : #endif
     259             : 
     260             :     /* If Hopen successful ... */
     261             :     /* ----------------------- */
     262           0 :     if (HDFfid != -1)
     263             :     {
     264             :         /* Get SDS interface ID */
     265             :         /* -------------------- */
     266           0 :         sdInterfaceID = SDstart(filename, DFACC_RDWR);
     267             : 
     268             :                     /* If SDstart successful ... */
     269             :                     /* ------------------------- */
     270           0 :                     if (sdInterfaceID != -1)
     271             :                     {
     272             :                        /* Set HDFEOS version number in file */
     273             :                        /* --------------------------------- */
     274             : 
     275           0 :           attrIndex = SDfindattr(sdInterfaceID, "HDFEOSVersion");
     276           0 :           if (attrIndex == -1)
     277             :       {
     278           0 :         snprintf(hdfeosVersion, sizeof(hdfeosVersion), "%s%s", "HDFEOS_V",
     279             :           HDFEOSVERSION1);
     280           0 :         SDsetattr(sdInterfaceID, "HDFEOSVersion", DFNT_CHAR8,
     281           0 :             (int)strlen(hdfeosVersion), hdfeosVersion);
     282             :       }
     283             :            /* Set open access to write */
     284             :            /* ------------------------ */
     285           0 :            acs = 1;
     286             : 
     287             :            /* Get structural metadata attribute ID */
     288             :            /* ------------------------------------ */
     289           0 :            attrIndex = SDfindattr(sdInterfaceID, "StructMetadata.0");
     290             : 
     291             :            /* Write structural metadata if it doesn't exist */
     292             :            /* --------------------------------------------- */
     293           0 :            if (attrIndex == -1)
     294             :            {
     295           0 :         metabuf = (char *) calloc(UTLSTRSIZE, 1);
     296           0 :         if(metabuf == NULL)
     297             :         {
     298           0 :             HEpush(DFE_NOSPACE,"EHopen", __FILE__, __LINE__);
     299           0 :             return(-1);
     300             :         }
     301             : 
     302           0 :         strcpy(metabuf, "GROUP=SwathStructure\n");
     303           0 :         strcat(metabuf, "END_GROUP=SwathStructure\n");
     304           0 :         strcat(metabuf, "GROUP=GridStructure\n");
     305           0 :         strcat(metabuf, "END_GROUP=GridStructure\n");
     306           0 :         strcat(metabuf, "GROUP=PointStructure\n");
     307           0 :         strcat(metabuf, "END_GROUP=PointStructure\n");
     308           0 :         strcat(metabuf, "END\n");
     309             : 
     310           0 :         SDsetattr(sdInterfaceID, "StructMetadata.0",
     311             :           DFNT_CHAR8, UTLSTRSIZE, metabuf);
     312           0 :         free(metabuf);
     313             :            }
     314             :                     } else
     315             :                     {
     316             :                         /* If error in SDstart then report */
     317             :                         /* ------------------------------- */
     318           0 :                         fid = -1;
     319           0 :                         status = -1;
     320           0 :                         HEpush(DFE_FNF, "EHopen", __FILE__, __LINE__);
     321           0 :                         snprintf(errbuf, sizeof(errbuf), "%s%s%s", "\"", filename,
     322             :                             "\" cannot be opened for read/write access.");
     323           0 :                         HEreport("%s\n", errbuf);
     324             :                     }
     325             :     } else
     326             :     {
     327             :         /* If error in Hopen then report */
     328             :         /* ----------------------------- */
     329           0 :         fid = -1;
     330           0 :         status = -1;
     331           0 :         HEpush(DFE_FNF, "EHopen", __FILE__, __LINE__);
     332           0 :         snprintf(errbuf, sizeof(errbuf), "%s%s%s", "\"", filename,
     333             :           "\" cannot be opened for RDWR access.");
     334           0 :         HEreport("%s\n", errbuf);
     335             :     }
     336             : 
     337           0 :     break;
     338             : 
     339             : 
     340             :     /* Open existing HDF-EOS file for read-only access */
     341             :     /* ----------------------------------------------- */
     342          46 :       case DFACC_READ:
     343             : 
     344             :     /* Get HDF file ID */
     345             :     /* --------------- */
     346             : #ifndef _PGS_OLDNFS
     347             : /* The following loop around the function Hopen is intended to deal with the NFS cache
     348             :    problem when opening file fails with errno = 150 or 151. When NFS cache is updated,
     349             :    this part of change is no longer necessary.              10/18/1999   */
     350          46 :                 retryCount = 0;
     351          46 :                 HDFfid = -1;
     352          92 :                 while ((HDFfid == -1) && (retryCount < MAX_RETRIES))
     353             :                 {
     354          46 :                 HDFfid = Hopen(filename, DFACC_READ, 0);
     355          46 :                 if((HDFfid == -1) && (errno == 150 || errno == 151))
     356             :                     {
     357           0 :                     HEpush(DFE_FNF, "EHopen", __FILE__, __LINE__);
     358           0 :                     snprintf(errbuf, sizeof(errbuf), "\"%s\" cannot be opened for READONLY access, will retry %d times.", filename,  (MAX_RETRIES - retryCount - 1));
     359           0 :                     HEreport("%s\n", errbuf);
     360             :                     }
     361          46 :                 retryCount++;
     362             :                 }
     363             : #else
     364             :                 HDFfid = Hopen(filename, DFACC_READ, 0);
     365             : #endif
     366             : 
     367             :     /* If file does not exist report error */
     368             :     /* ----------------------------------- */
     369          46 :     if (HDFfid == -1)
     370             :     {
     371           0 :         fid = -1;
     372           0 :         status = -1;
     373           0 :         HEpush(DFE_FNF, "EHopen", __FILE__, __LINE__);
     374           0 :             snprintf(errbuf, sizeof(errbuf), "\"%s\" (opened for READONLY access) does not exist.", filename);
     375           0 :         HEreport("%s\n", errbuf);
     376             :     } else
     377             :     {
     378             :         /* If file exists then get SD interface ID */
     379             :         /* --------------------------------------- */
     380          46 :         sdInterfaceID = SDstart(filename, DFACC_RDONLY);
     381             : 
     382             :                         /* If SDstart successful ... */
     383             :                         /* ------------------------- */
     384          46 :                         if (sdInterfaceID != -1)
     385             :                         {
     386             : 
     387             :                /* Set open access to read-only */
     388             :                /* ---------------------------- */
     389          46 :                acs = 0;
     390             :              } else
     391             :                         {
     392             :                             /* If error in SDstart then report */
     393             :                             /* ------------------------------- */
     394           0 :                             fid = -1;
     395           0 :                             status = -1;
     396           0 :                             HEpush(DFE_FNF, "EHopen", __FILE__, __LINE__);
     397           0 :                             snprintf(errbuf, sizeof(errbuf), "%s%s%s", "\"", filename,
     398             :                             "\" cannot be opened for read access.");
     399           0 :                             HEreport("%s\n", errbuf);
     400             :                         }
     401             :     }
     402             : 
     403          46 :     break;
     404             : 
     405           0 :       default:
     406             :     /* Invalid Access Code */
     407             :     /* ------------------- */
     408           0 :     fid = -1;
     409           0 :     status = -1;
     410           0 :     HEpush(DFE_BADACC, "EHopen", __FILE__, __LINE__);
     411           0 :     HEreport("Access Code: %d (%s).\n", access, filename);
     412             :       }
     413             : 
     414           0 :   }
     415             :     } else
     416             :     {
     417             :   /* Too many files opened */
     418             :   /* --------------------- */
     419           0 :   status = -1;
     420           0 :   fid = -1;
     421           0 :   HEpush(DFE_TOOMANY, "EHopen", __FILE__, __LINE__);
     422           0 :   HEreport("No more than %d files may be open simultaneously (%s).\n",
     423             :      EHXmaxfilecount, filename);
     424             :     }
     425             : 
     426             : 
     427             : 
     428             : 
     429          46 :     if (status == 0)
     430             :     {
     431             :   /* Initialize Vgroup Access */
     432             :   /* ------------------------ */
     433          46 :   Vstart(HDFfid);
     434             : 
     435             : 
     436             :   /* Assign HDFEOS fid # & Load HDF fid and sdInterfaceID tables */
     437             :   /* ----------------------------------------------------------- */
     438          52 :   for (i = 0; i < EHXmaxfilecount; i++)
     439             :   {
     440          52 :       if (EHXtypeTable[i] == 0)
     441             :       {
     442          46 :     fid = i + EHIDOFFSET;
     443          46 :     EHXacsTable[i] = acs;
     444          46 :     EHXtypeTable[i] = 1;
     445          46 :     EHXfidTable[i] = HDFfid;
     446          46 :     EHXsdTable[i] = sdInterfaceID;
     447          46 :     break;
     448             :       }
     449             :   }
     450             : 
     451             :     }
     452          46 :     return (fid);
     453             : }
     454             : 
     455             : 
     456             : 
     457             : 
     458             : /*----------------------------------------------------------------------------|
     459             : |  BEGIN_PROLOG                                                               |
     460             : |                                                                             |
     461             : |  FUNCTION: EHchkfid                                                         |
     462             : |                                                                             |
     463             : |  DESCRIPTION: Checks for valid file id and returns HDF file ID and          |
     464             : |               SD interface ID                                               |
     465             : |                                                                             |
     466             : |                                                                             |
     467             : |  Return Value    Type     Units     Description                             |
     468             : |  ============   ======  =========   =====================================   |
     469             : |  status         intn                return status (0) SUCCEED, (-1) FAIL    |
     470             : |                                                                             |
     471             : |  INPUTS:                                                                    |
     472             : |  fid            int32               HDF-EOS file ID                         |
     473             : |  name           char                Structure name                          |
     474             : |                                                                             |
     475             : |  OUTPUTS:                                                                   |
     476             : |  HDFfid         int32               HDF File ID                             |
     477             : |  sdInterfaceID  int32               SDS interface ID                        |
     478             : |  access         uint8               access code                             |
     479             : |                                                                             |
     480             : |  NOTES:                                                                     |
     481             : |                                                                             |
     482             : |                                                                             |
     483             : |   Date     Programmer   Description                                         |
     484             : |  ======   ============  =================================================   |
     485             : |  Jun 96   Joel Gales    Original Programmer                                 |
     486             : |  Jul 96   Joel Gales    set status=-1 if failure                            |
     487             : |  Jul 96   Joel Gales    Add file id offset EHIDOFFSET                       |
     488             : |                                                                             |
     489             : |  END_PROLOG                                                                 |
     490             : -----------------------------------------------------------------------------*/
     491             : intn
     492         266 : EHchkfid(int32 fid, const char *name, int32 * HDFfid, int32 * sdInterfaceID,
     493             :    uint8 * access)
     494             : 
     495             : {
     496         266 :     intn            status = 0; /* routine return status variable */
     497             :     intn            fid0; /* HDFEOS file ID - Offset */
     498             : 
     499             : 
     500             :     /* Check for valid HDFEOS file ID range */
     501             :     /* ------------------------------------ */
     502         266 :     if (fid < EHIDOFFSET || fid > EHXmaxfilecount + EHIDOFFSET)
     503             :     {
     504           0 :   status = -1;
     505           0 :   HEpush(DFE_RANGE, "EHchkfid", __FILE__, __LINE__);
     506           0 :   HEreport("Invalid file id: %d.  ID must be >= %d and < %d (%s).\n",
     507             :      fid, EHIDOFFSET, EHXmaxfilecount + EHIDOFFSET, name);
     508             :     } else
     509             :     {
     510             :   /* Compute "reduced" file ID */
     511             :   /* ------------------------- */
     512         266 :   fid0 = fid % EHIDOFFSET;
     513             : 
     514             : 
     515             :   /* Check that HDFEOS file ID is active */
     516             :   /* ----------------------------------- */
     517         266 :   if (EHXtypeTable[fid0] == 0)
     518             :   {
     519           0 :       status = -1;
     520           0 :       HEpush(DFE_GENAPP, "EHchkfid", __FILE__, __LINE__);
     521           0 :       HEreport("File id %d not active (%s).\n", fid, name);
     522             :   } else
     523             :   {
     524             :       /*
     525             :        * Get HDF file ID, SD interface ID and file access from external
     526             :        * arrays
     527             :        */
     528         266 :       *HDFfid = EHXfidTable[fid0];
     529         266 :       *sdInterfaceID = EHXsdTable[fid0];
     530         266 :       *access = EHXacsTable[fid0];
     531             :   }
     532             :     }
     533             : 
     534         266 :     return (status);
     535             : }
     536             : 
     537             : 
     538             : 
     539             : 
     540             : /*----------------------------------------------------------------------------|
     541             : |  BEGIN_PROLOG                                                               |
     542             : |                                                                             |
     543             : |  FUNCTION: EHidinfo                                                         |
     544             : |                                                                             |
     545             : |  DESCRIPTION: Gets Hopen and SD interface IDs from HDF-EOS id               |
     546             : |                                                                             |
     547             : |                                                                             |
     548             : |  Return Value    Type     Units     Description                             |
     549             : |  ============   ======  =========   =====================================   |
     550             : |  status         intn                return status (0) SUCCEED, (-1) FAIL    |
     551             : |                                                                             |
     552             : |  INPUTS:                                                                    |
     553             : |  fid            int32               HDF-EOS file ID                         |
     554             : |                                                                             |
     555             : |  OUTPUTS:                                                                   |
     556             : |  HDFfid         int32               HDF File ID                             |
     557             : |  sdInterfaceID  int32               SDS interface ID                        |
     558             : |                                                                             |
     559             : |  NOTES:                                                                     |
     560             : |                                                                             |
     561             : |                                                                             |
     562             : |   Date     Programmer   Description                                         |
     563             : |  ======   ============  =================================================   |
     564             : |  Jul 96   Joel Gales    Original Programmer                                 |
     565             : |                                                                             |
     566             : |  END_PROLOG                                                                 |
     567             : -----------------------------------------------------------------------------*/
     568             : intn
     569           1 : EHidinfo(int32 fid, int32 * HDFfid, int32 * sdInterfaceID)
     570             : 
     571             : {
     572           1 :     intn            status = 0; /* routine return status variable */
     573             :     uint8           dum;  /* Dummy variable */
     574             : 
     575             :     /* Call EHchkfid to get HDF and SD interface IDs */
     576             :     /* --------------------------------------------- */
     577           1 :     status = EHchkfid(fid, "EHidinfo", HDFfid, sdInterfaceID, &dum);
     578             : 
     579           1 :     return (status);
     580             : }
     581             : 
     582             : /*----------------------------------------------------------------------------|
     583             : |  BEGIN_PROLOG                                                               |
     584             : |                                                                             |
     585             : |  FUNCTION: EHparsestr                                                       |
     586             : |                                                                             |
     587             : |  DESCRIPTION: String Parser Utility                                         |
     588             : |                                                                             |
     589             : |                                                                             |
     590             : |  Return Value    Type     Units     Description                             |
     591             : |  ============   ======  =========   =====================================   |
     592             : |  count          int32               Number of string entries                |
     593             : |                                                                             |
     594             : |  INPUTS:                                                                    |
     595             : |  instring       const char          Input string                            |
     596             : |  delim          const char          string delimiter                        |
     597             : |                                                                             |
     598             : |  OUTPUTS:                                                                   |
     599             : |  pntr           char *              Pointer array to beginning of each      |
     600             : |                                     string entry                            |
     601             : |  len            int32               Array of string entry lengths           |
     602             : |                                                                             |
     603             : |  NOTES:                                                                     |
     604             : |                                                                             |
     605             : |                                                                             |
     606             : |   Date     Programmer   Description                                         |
     607             : |  ======   ============  =================================================   |
     608             : |  Jun 96   Joel Gales    Original Programmer                                 |
     609             : |  Aug 96   Joel Gales    NULL pointer array returns count only               |
     610             : |                                                                             |
     611             : |  END_PROLOG                                                                 |
     612             : -----------------------------------------------------------------------------*/
     613             : int32
     614         128 : EHparsestr(const char *instring, const char delim,
     615             :            char *pntr[], size_t pntrsize,
     616             :            int32 len[], size_t lensize)
     617             : {
     618             :     size_t           i;   /* Loop index */
     619         128 :     size_t           prevDelimPos = 0;  /* Previous delimiter position */
     620             :     size_t           count; /* Number of elements in string list */
     621             :     size_t           slen;  /* String length */
     622             : 
     623             :     char           *delimitor;  /* Pointer to delimiter */
     624             : 
     625             : 
     626             :     /* Get length of input string list & Point to first delimiter */
     627             :     /* ---------------------------------------------------------- */
     628         128 :     slen = (int)strlen(instring);
     629         128 :     delimitor = strchr(instring, delim);
     630             : 
     631             :     /* If NULL string set count to zero otherwise set to 1 */
     632             :     /* --------------------------------------------------- */
     633         128 :     count = (slen == 0) ? 0 : 1;
     634             : 
     635             : 
     636             :     /* if string pointers are requested set first one to beginning of string */
     637             :     /* --------------------------------------------------------------------- */
     638         128 :     if (pntr != NULL && pntrsize)
     639             :     {
     640         120 :   pntr[0] = (char *)instring;
     641             :     }
     642             :     /* If delimiter not found ... */
     643             :     /* ---------------------------- */
     644         128 :     if (delimitor == NULL)
     645             :     {
     646             :   /* if string length requested then set to input string length */
     647             :   /* ---------------------------------------------------------- */
     648          17 :   if (len != NULL && lensize)
     649             :   {
     650           9 :       len[0] = (int32)slen;
     651             :   }
     652             :     } else
     653             :   /* Delimiters Found */
     654             :   /* ---------------- */
     655             :     {
     656             :         /* Loop through all characters in string */
     657             :         /* ------------------------------------- */
     658        1673 :         for (i = 1; i < slen; i++)
     659             :         {
     660             :             /* If character is a delimiter ... */
     661             :             /* ------------------------------- */
     662        1562 :             if (instring[i] == delim)
     663             :             {
     664             : 
     665             :             /* If string pointer requested */
     666             :             /* --------------------------- */
     667         111 :             if (pntr != NULL)
     668             :             {
     669             :                 /* if requested then compute string length of entry */
     670             :                 /* ------------------------------------------------ */
     671         111 :                 if (len != NULL)
     672             :                 {
     673         111 :                     if (count - 1 >= lensize)
     674           0 :                         return -1;
     675         111 :                     len[count - 1] = (int32)(i - prevDelimPos);
     676             :                 }
     677             :                 /* Point to beginning of string entry */
     678             :                 /* ---------------------------------- */
     679         111 :                 if (count >= pntrsize)
     680           0 :                     return -1;
     681         111 :                 pntr[count] = (char *)instring + i + 1;
     682             :             }
     683             :             /* Reset previous delimiter position and increment counter */
     684             :             /* ------------------------------------------------------- */
     685         111 :             prevDelimPos = i + 1;
     686         111 :             count++;
     687             :             }
     688             :         }
     689             : 
     690             :         /* Compute string length of last entry */
     691             :         /* ----------------------------------- */
     692         111 :         if (pntr != NULL && len != NULL)
     693             :         {
     694         111 :             if (count == 0 || (size_t)(count - 1) >= lensize)
     695           0 :                 return -1;
     696         111 :             len[count - 1] = (int32)(i - prevDelimPos);
     697             :         }
     698             :     }
     699             : 
     700         128 :     return (int32)(count);
     701             : }
     702             : 
     703             : 
     704             : 
     705             : 
     706             : /*----------------------------------------------------------------------------|
     707             : |  BEGIN_PROLOG                                                               |
     708             : |                                                                             |
     709             : |  FUNCTION: EHstrwithin                                                      |
     710             : |                                                                             |
     711             : |  DESCRIPTION: Searches for string within target string                      |
     712             : |                                                                             |
     713             : |                                                                             |
     714             : |  Return Value    Type     Units     Description                             |
     715             : |  ============   ======  =========   =====================================   |
     716             : |  indx           int32               Element index (0 - based)               |
     717             : |                                                                             |
     718             : |  INPUTS:                                                                    |
     719             : |  target         const char          Target string                           |
     720             : |  search         const char          Search string                           |
     721             : |  delim          const char          Delimiter                               |
     722             : |                                                                             |
     723             : |  OUTPUTS:                                                                   |
     724             : |             None                                                            |
     725             : |                                                                             |
     726             : |  NOTES:                                                                     |
     727             : |                                                                             |
     728             : |                                                                             |
     729             : |   Date     Programmer   Description                                         |
     730             : |  ======   ============  =================================================   |
     731             : |  Jun 96   Joel Gales    Original Programmer                                 |
     732             : |  Jan 97   Joel Gales    Change ptr & slen to dynamic arrays                 |
     733             : |                                                                             |
     734             : |  END_PROLOG                                                                 |
     735             : -----------------------------------------------------------------------------*/
     736             : int32
     737           8 : EHstrwithin(const char *target, const char *search, const char delim)
     738             : {
     739           8 :     intn            found = 0;  /* Target string found flag */
     740             : 
     741             :     int32           indx; /* Loop index */
     742             :     int32           nentries; /* Number of entries in search string */
     743             :     int32          *slen; /* Pointer to string length array */
     744             : 
     745             :     char          **ptr;  /* Pointer to string pointer array */
     746             :     char            buffer[128];/* Buffer to hold "test" string entry */
     747             : 
     748             : 
     749             :     /* Count number of entries in search string list */
     750             :     /* --------------------------------------------- */
     751           8 :     nentries = EHparsestr(search, delim, NULL, 0, NULL, 0);
     752           8 :     if (nentries == 0)
     753           8 :         return -1;
     754             : 
     755             : 
     756             :     /* Allocate string pointer and length arrays */
     757             :     /* ----------------------------------------- */
     758           0 :     ptr = (char **) calloc(nentries, sizeof(char *));
     759           0 :     if(ptr == NULL)
     760             :     {
     761           0 :   HEpush(DFE_NOSPACE,"EHstrwithin", __FILE__, __LINE__);
     762           0 :   return(-1);
     763             :     }
     764           0 :     slen = (int32 *) calloc(nentries, sizeof(int32));
     765           0 :     if(slen == NULL)
     766             :     {
     767           0 :   HEpush(DFE_NOSPACE,"EHstrwithin", __FILE__, __LINE__);
     768           0 :   free(ptr);
     769           0 :   return(-1);
     770             :     }
     771             : 
     772             : 
     773             :     /* Parse search string */
     774             :     /* ------------------- */
     775           0 :     nentries = EHparsestr(search, delim, ptr, nentries, slen, nentries);
     776           0 :     if (nentries < 0)
     777             :     {
     778           0 :         free(ptr);
     779           0 :         free(slen);
     780           0 :         HEpush(DFE_NOSPACE,"EHstrwithin", __FILE__, __LINE__);
     781           0 :         return(-1);
     782             :     }
     783             : 
     784             : 
     785             :     /* Loop through all elements in search string list */
     786             :     /* ----------------------------------------------- */
     787           0 :     for (indx = 0; indx < nentries; indx++)
     788             :     {
     789             :   /* Copy string entry into buffer */
     790             :   /* ----------------------------- */
     791           0 :   memcpy(buffer, ptr[indx], slen[indx]);
     792           0 :   buffer[slen[indx]] = 0;
     793             : 
     794             : 
     795             :   /* Compare target string with string entry */
     796             :   /* --------------------------------------- */
     797           0 :   if (strcmp(target, buffer) == 0)
     798             :   {
     799           0 :       found = 1;
     800           0 :       break;
     801             :   }
     802             :     }
     803             : 
     804             :     /* If not found set return to -1 */
     805             :     /* ----------------------------- */
     806           0 :     if (found == 0)
     807             :     {
     808           0 :   indx = -1;
     809             :     }
     810           0 :     free(slen);
     811           0 :     free(ptr);
     812             : 
     813           0 :     return (indx);
     814             : }
     815             : 
     816             : 
     817             : 
     818             : 
     819             : /*----------------------------------------------------------------------------|
     820             : |  BEGIN_PROLOG                                                               |
     821             : |                                                                             |
     822             : |  FUNCTION: EHgetid                                                          |
     823             : |                                                                             |
     824             : |  DESCRIPTION: Get Vgroup/Vdata ID from name                                 |
     825             : |                                                                             |
     826             : |                                                                             |
     827             : |  Return Value    Type     Units     Description                             |
     828             : |  ============   ======  =========   =====================================   |
     829             : |  outID          int32               Output ID                               |
     830             : |                                                                             |
     831             : |  INPUTS:                                                                    |
     832             : |  fid            int32               HDF-EOS file ID                         |
     833             : |  vgid           int32               Vgroup ID                               |
     834             : |  objectname     const char          object name                             |
     835             : |  code           intn                object code (0 - Vgroup, 1 - Vdata)     |
     836             : |  access         const char          access ("w/r")                          |
     837             : |                                                                             |
     838             : |                                                                             |
     839             : |  OUTPUTS:                                                                   |
     840             : |             None                                                            |
     841             : |                                                                             |
     842             : |  NOTES:                                                                     |
     843             : |                                                                             |
     844             : |                                                                             |
     845             : |   Date     Programmer   Description                                         |
     846             : |  ======   ============  =================================================   |
     847             : |  Jun 96   Joel Gales    Original Programmer                                 |
     848             : |                                                                             |
     849             : |  END_PROLOG                                                                 |
     850             : -----------------------------------------------------------------------------*/
     851             : int32
     852           1 : EHgetid(int32 fid, int32 vgid, const char *objectname, intn code,
     853             :         const char *access)
     854             : {
     855             :     intn            i;    /* Loop index */
     856             : 
     857             :     int32           nObjects; /* # of objects in Vgroup */
     858             :     int32          *tags; /* Pnt to Vgroup object tags array */
     859             :     int32          *refs; /* Pnt to Vgroup object refs array */
     860             :     int32           id;   /* Object ID */
     861           1 :     int32           outID = -1; /* Desired object ID */
     862             : 
     863             :     char            name[128];  /* Object name */
     864             : 
     865             : 
     866             :     /* Get Number of objects */
     867             :     /* --------------------- */
     868           1 :     nObjects = Vntagrefs(vgid);
     869             : 
     870             :     /* If objects exist ... */
     871             :     /* -------------------- */
     872           1 :     if (nObjects != 0)
     873             :     {
     874             : 
     875             :   /* Get tags and references of objects */
     876             :   /* ---------------------------------- */
     877           0 :   tags = (int32 *) malloc(sizeof(int32) * nObjects);
     878           0 :   if(tags == NULL)
     879             :   {
     880           0 :       HEpush(DFE_NOSPACE,"EHgetid", __FILE__, __LINE__);
     881           0 :       return(-1);
     882             :   }
     883           0 :   refs = (int32 *) malloc(sizeof(int32) * nObjects);
     884           0 :   if(refs == NULL)
     885             :   {
     886           0 :       HEpush(DFE_NOSPACE,"EHgetid", __FILE__, __LINE__);
     887           0 :       free(tags);
     888           0 :       return(-1);
     889             :   }
     890             : 
     891           0 :   Vgettagrefs(vgid, tags, refs, nObjects);
     892             : 
     893             : 
     894             :   /* Vgroup ID Section */
     895             :   /* ----------------- */
     896           0 :   if (code == 0)
     897             :   {
     898             :       /* Loop through objects */
     899             :       /* -------------------- */
     900           0 :       for (i = 0; i < nObjects; i++)
     901             :       {
     902             : 
     903             :     /* If object is Vgroup ... */
     904             :     /* ----------------------- */
     905           0 :     if (*(tags + i) == DFTAG_VG)
     906             :     {
     907             : 
     908             :         /* Get ID and name */
     909             :         /* --------------- */
     910           0 :         id = Vattach(fid, *(refs + i), access);
     911           0 :         VgetnameSafe(id, name, sizeof(name));
     912             : 
     913             :         /* If name equals desired object name get ID */
     914             :         /* ----------------------------------------- */
     915           0 :         if (strcmp(name, objectname) == 0)
     916             :         {
     917           0 :       outID = id;
     918           0 :       break;
     919             :         }
     920             :         /* If not desired object then detach */
     921             :         /* --------------------------------- */
     922           0 :         Vdetach(id);
     923             :     }
     924             :       }
     925           0 :   } else if (code == 1)
     926             :   {
     927             : 
     928             :       /* Loop through objects */
     929             :       /* -------------------- */
     930           0 :       for (i = 0; i < nObjects; i++)
     931             :       {
     932             : 
     933             :     /* If object is Vdata ... */
     934             :     /* ---------------------- */
     935           0 :     if (*(tags + i) == DFTAG_VH)
     936             :     {
     937             : 
     938             :         /* Get ID and name */
     939             :         /* --------------- */
     940           0 :         id = VSattach(fid, *(refs + i), access);
     941           0 :         VSgetname(id, name);
     942             : 
     943             :         /* If name equals desired object name get ID */
     944             :         /* ----------------------------------------- */
     945           0 :         if (EHstrwithin(objectname, name, ',') != -1)
     946             :         {
     947           0 :       outID = id;
     948           0 :       break;
     949             :         }
     950             :         /* If not desired object then detach */
     951             :         /* --------------------------------- */
     952           0 :         VSdetach(id);
     953             :     }
     954             :       }
     955             :   }
     956           0 :   free(tags);
     957           0 :   free(refs);
     958             :     }
     959           1 :     return (outID);
     960             : }
     961             : 
     962             : 
     963             : 
     964             : 
     965             : /*----------------------------------------------------------------------------|
     966             : |  BEGIN_PROLOG                                                               |
     967             : |                                                                             |
     968             : |  FUNCTION: EHgetmetavalue                                                   |
     969             : |                                                                             |
     970             : |  DESCRIPTION: Returns metadata value                                        |
     971             : |                                                                             |
     972             : |                                                                             |
     973             : |  Return Value    Type     Units     Description                             |
     974             : |  ============   ======  =========   =====================================   |
     975             : |  status         intn                return status (0) SUCCEED, (-1) FAIL    |
     976             : |                                                                             |
     977             : |  INPUTS:                                                                    |
     978             : |  metaptrs        char               Begin and end of metadata section       |
     979             : |  parameter      char                parameter to access                     |
     980             : |                                                                             |
     981             : |  OUTPUTS:                                                                   |
     982             : |  metaptr        char                Ptr to (updated) beginning of metadata  |
     983             : |  retstr         char                return string containing value          |
     984             : |                                                                             |
     985             : |  NOTES:                                                                     |
     986             : |                                                                             |
     987             : |                                                                             |
     988             : |   Date     Programmer   Description                                         |
     989             : |  ======   ============  =================================================   |
     990             : |  Jun 96   Joel Gales    Original Programmer                                 |
     991             : |  Jan 97   Joel Gales    Check string pointer against end of meta section    |
     992             : |                                                                             |
     993             : |  END_PROLOG                                                                 |
     994             : -----------------------------------------------------------------------------*/
     995             : intn
     996         940 : EHgetmetavalue(char *metaptrs[], const char *parameter, char *retstr)
     997             : {
     998         940 :     intn            status = 0; /* routine return status variable */
     999             : 
    1000             :     int32           slen; /* String length */
    1001             :     char           *newline;  /* Position of new line character */
    1002             :     char           *sptr; /* string pointer within metadata */
    1003             : 
    1004             : 
    1005             :     /* Get string length of parameter string + 1 */
    1006             :     /* ----------------------------------------- */
    1007         940 :     slen = (int)strlen(parameter) + 1;
    1008             : 
    1009             : 
    1010             :     /* Build search string (parameter string + "=") */
    1011             :     /* -------------------------------------------- */
    1012         940 :     strcpy(retstr, parameter);
    1013         940 :     strcat(retstr, "=");
    1014             : 
    1015             : 
    1016             :     /* Search for string within metadata (beginning at metaptrs[0]) */
    1017             :     /* ------------------------------------------------------------ */
    1018         940 :     sptr = strstr(metaptrs[0], retstr);
    1019             : 
    1020             : 
    1021             :     /* If string found within desired section ... */
    1022             :     /* ------------------------------------------ */
    1023         940 :     if (sptr != NULL && sptr < metaptrs[1])
    1024             :     {
    1025             :   /* Store position of string within metadata */
    1026             :   /* ---------------------------------------- */
    1027         940 :   metaptrs[0] = sptr;
    1028             : 
    1029             :   /* Find newline "\n" character */
    1030             :   /* --------------------------- */
    1031         940 :   newline = strchr(metaptrs[0], '\n');
    1032         940 :   if (newline == NULL)
    1033             :   {
    1034           0 :     retstr[0] = 0;
    1035           0 :     return -1;
    1036             :   }
    1037             : 
    1038             :   /* Copy from "=" to "\n" (exclusive) into return string */
    1039             :   /* ---------------------------------------------------- */
    1040         940 :   memcpy(retstr, metaptrs[0] + slen, newline - metaptrs[0] - slen);
    1041             : 
    1042             :   /* Terminate return string with null */
    1043             :   /* --------------------------------- */
    1044         940 :   retstr[newline - metaptrs[0] - slen] = 0;
    1045             :     } else
    1046             :     {
    1047             :   /*
    1048             :    * if parameter string not found within section, null return string
    1049             :    * and set status to -1.
    1050             :    */
    1051           0 :   retstr[0] = 0;
    1052           0 :   status = -1;
    1053             :     }
    1054             : 
    1055         940 :     return (status);
    1056             : }
    1057             : 
    1058             : 
    1059             : 
    1060             : 
    1061             : /*----------------------------------------------------------------------------|
    1062             : |  BEGIN_PROLOG                                                               |
    1063             : |                                                                             |
    1064             : |  FUNCTION: EHmetagroup                                                      |
    1065             : |                                                                             |
    1066             : |  DESCRIPTION: Returns pointers to beginning and end of metadata group       |
    1067             : |                                                                             |
    1068             : |                                                                             |
    1069             : |  Return Value    Type     Units     Description                             |
    1070             : |  ============   ======  =========   =====================================   |
    1071             : |  metabuf        char                Pointer to HDF-EOS object in metadata   |
    1072             : |                                                                             |
    1073             : |  INPUTS:                                                                    |
    1074             : |  sdInterfaceID  int32               SDS interface ID                        |
    1075             : |  structname     char                HDF-EOS structure name                  |
    1076             : |  structcode     char                Structure code ("s/g/p")                |
    1077             : |  groupname      char                Metadata group name                     |
    1078             : |                                                                             |
    1079             : |  OUTPUTS:                                                                   |
    1080             : |  metaptrs       char                pointers to begin and end of metadata   |
    1081             : |                                                                             |
    1082             : |  NOTES:                                                                     |
    1083             : |                                                                             |
    1084             : |                                                                             |
    1085             : |   Date     Programmer   Description                                         |
    1086             : |  ======   ============  =================================================   |
    1087             : |  Jun 96   Joel Gales    Original Programmer                                 |
    1088             : |  Aug 96   Joel Gales    Make metadata ODL compliant                         |
    1089             : |                                                                             |
    1090             : |  END_PROLOG                                                                 |
    1091             : -----------------------------------------------------------------------------*/
    1092             : char           *
    1093         193 : EHmetagroup(int32 sdInterfaceID, const char *structname, const char *structcode,
    1094             :       const char *groupname, char *metaptrs[])
    1095             : {
    1096             :     intn            i;    /* Loop index */
    1097             : 
    1098             :     int32           attrIndex;  /* Structural metadata attribute index */
    1099             :     int32           nmeta;  /* Number of UTLSTRSIZE byte metadata sections */
    1100             : 
    1101             :     char           *metabuf;  /* Pointer (handle) to structural metadata */
    1102         193 :     char           *endptr = NULL;  /* Pointer to end of metadata section */
    1103             :     char           *metaptr;  /* Metadata pointer */
    1104             :     char           *prevmetaptr;/* Previous position of metadata pointer */
    1105             :     char           *utlstr;     /* Utility string */
    1106             : 
    1107             : 
    1108             : 
    1109             :      /* Allocate memory for utility string */
    1110             :      /* ---------------------------------- */
    1111         193 :     utlstr = (char *) calloc(UTLSTR_MAX_SIZE,sizeof(char));
    1112         193 :     if(utlstr == NULL)
    1113             :     {
    1114           0 :         HEpush(DFE_NOSPACE,"EHEHmetagroup", __FILE__, __LINE__);
    1115             : 
    1116           0 :         return( NULL);
    1117             :     }
    1118             :     /* Determine number of structural metadata "sections" */
    1119             :     /* -------------------------------------------------- */
    1120         193 :     nmeta = 0;
    1121             :     while (1)
    1122             :     {
    1123             :   /* Search for "StructMetadata.x" attribute */
    1124             :   /* --------------------------------------- */
    1125         386 :   snprintf(utlstr, UTLSTR_MAX_SIZE, "%s%d", "StructMetadata.", (int)nmeta);
    1126         386 :   attrIndex = SDfindattr(sdInterfaceID, utlstr);
    1127             : 
    1128             : 
    1129             :   /* If found then increment metadata section counter else exit loop */
    1130             :   /* --------------------------------------------------------------- */
    1131         386 :   if (attrIndex != -1)
    1132             :   {
    1133         193 :       nmeta++;
    1134             :   } else
    1135             :   {
    1136         193 :       break;
    1137             :   }
    1138             :     }
    1139             : 
    1140         193 :     if (nmeta > INT_MAX / UTLSTRSIZE )
    1141             :     {
    1142           0 :         HEpush(DFE_NOSPACE,"EHEHmetagroup", __FILE__, __LINE__);
    1143             : 
    1144           0 :         return( NULL);
    1145             :     }
    1146             : 
    1147             :     /* Allocate space for metadata (in units of UTLSTRSIZE bytes) */
    1148             :     /* ----------------------------------------------------- */
    1149         193 :     metabuf = (char *) calloc(UTLSTRSIZE * nmeta, 1);
    1150             : 
    1151         193 :     if(metabuf == NULL)
    1152             :     {
    1153           0 :   HEpush(DFE_NOSPACE,"EHmetagroup", __FILE__, __LINE__);
    1154           0 :   free(utlstr);
    1155           0 :   return(metabuf);
    1156             :     }
    1157             : 
    1158             : 
    1159             :     /* Read structural metadata */
    1160             :     /* ------------------------ */
    1161         386 :     for (i = 0; i < nmeta; i++)
    1162             :     {
    1163         193 :   snprintf(utlstr, UTLSTR_MAX_SIZE, "%s%d", "StructMetadata.", i);
    1164         193 :   attrIndex = SDfindattr(sdInterfaceID, utlstr);
    1165         193 :   int metalen = (int)strlen(metabuf);
    1166         193 :   SDreadattr(sdInterfaceID, attrIndex, metabuf + metalen);
    1167             :     }
    1168             : 
    1169             :     /* Find HDF-EOS structure "root" group in metadata */
    1170             :     /* ----------------------------------------------- */
    1171             : 
    1172             :     /* Setup proper search string */
    1173             :     /* -------------------------- */
    1174         193 :     if (strcmp(structcode, "s") == 0)
    1175             :     {
    1176          49 :   strcpy(utlstr, "GROUP=SwathStructure");
    1177         144 :     } else if (strcmp(structcode, "g") == 0)
    1178             :     {
    1179         144 :   strcpy(utlstr, "GROUP=GridStructure");
    1180           0 :     } else if (strcmp(structcode, "p") == 0)
    1181             :     {
    1182           0 :   strcpy(utlstr, "GROUP=PointStructure");
    1183             :     }
    1184             :     /* Use string search routine (strstr) to move through metadata */
    1185             :     /* ----------------------------------------------------------- */
    1186         193 :     metaptr = strstr(metabuf, utlstr);
    1187             : 
    1188             : 
    1189             : 
    1190             :     /* Save current metadata pointer */
    1191             :     /* ----------------------------- */
    1192         193 :     prevmetaptr = metaptr;
    1193             : 
    1194             : 
    1195             :     /* First loop for "old-style" (non-ODL) metadata string */
    1196             :     /* ---------------------------------------------------- */
    1197         193 :     if (strcmp(structcode, "s") == 0)
    1198             :     {
    1199          49 :   snprintf(utlstr, UTLSTR_MAX_SIZE, "%s%s", "SwathName=\"", structname);
    1200         144 :     } else if (strcmp(structcode, "g") == 0)
    1201             :     {
    1202         144 :   snprintf(utlstr, UTLSTR_MAX_SIZE, "%s%s", "GridName=\"", structname);
    1203           0 :     } else if (strcmp(structcode, "p") == 0)
    1204             :     {
    1205           0 :   snprintf(utlstr, UTLSTR_MAX_SIZE, "%s%s", "PointName=\"", structname);
    1206             :     }
    1207             :     /* Do string search */
    1208             :     /* ---------------- */
    1209         193 :     if (metaptr)
    1210         193 :         metaptr = strstr(metaptr, utlstr);
    1211             : 
    1212             : 
    1213             :     /*
    1214             :      * If not found then return to previous position in metadata and look for
    1215             :      * "new-style" (ODL) metadata string
    1216             :      */
    1217         193 :     if (metaptr == NULL && prevmetaptr)
    1218             :     {
    1219           0 :   snprintf(utlstr, UTLSTR_MAX_SIZE, "%s%s", "GROUP=\"", structname);
    1220           0 :   metaptr = strstr(prevmetaptr, utlstr);
    1221             :     }
    1222             :     /* Find group within structure */
    1223             :     /* --------------------------- */
    1224         193 :     if (metaptr && groupname != NULL)
    1225             :     {
    1226         177 :   snprintf(utlstr, UTLSTR_MAX_SIZE, "%s%s", "GROUP=", groupname);
    1227         177 :   metaptr = strstr(metaptr, utlstr);
    1228             : 
    1229         177 :   snprintf(utlstr, UTLSTR_MAX_SIZE, "%s%s", "\t\tEND_GROUP=", groupname);
    1230         177 :   if (metaptr)
    1231         171 :       endptr = strstr(metaptr, utlstr);
    1232             :   else
    1233           6 :       endptr = NULL;
    1234          16 :     } else if (metaptr)
    1235             :     {
    1236             :   /* If groupname == NULL then find end of structure in metadata */
    1237             :   /* ----------------------------------------------------------- */
    1238          16 :   snprintf(utlstr, UTLSTR_MAX_SIZE, "%s", "\n\tEND_GROUP=");
    1239          16 :   endptr = strstr(metaptr, utlstr);
    1240             :     }
    1241             : 
    1242             : 
    1243             :     /* Return beginning and ending pointers */
    1244             :     /* ------------------------------------ */
    1245         193 :     metaptrs[0] = metaptr;
    1246         193 :     metaptrs[1] = endptr;
    1247             : 
    1248         193 :     free(utlstr);
    1249             : 
    1250         193 :     return (metabuf);
    1251             : }
    1252             : 
    1253             : 
    1254             : /*----------------------------------------------------------------------------|
    1255             : |  BEGIN_PROLOG                                                               |
    1256             : |                                                                             |
    1257             : |  FUNCTION: EHattr                                                           |
    1258             : |                                                                             |
    1259             : |  DESCRIPTION: Reads/Writes attributes for HDF-EOS structures                |
    1260             : |                                                                             |
    1261             : |                                                                             |
    1262             : |  Return Value    Type     Units     Description                             |
    1263             : |  ============   ======  =========   =====================================   |
    1264             : |  status         intn                return status (0) SUCCEED, (-1) FAIL    |
    1265             : |                                                                             |
    1266             : |  INPUTS:                                                                    |
    1267             : |  fid            int32               HDF-EOS file ID                         |
    1268             : |  attrVgrpID     int32               Attribute Vgroup ID                     |
    1269             : |  attrname       char                attribute name                          |
    1270             : |  numbertype     int32               attribute HDF numbertype                |
    1271             : |  count          int32               Number of attribute elements            |
    1272             : |  wrcode         char                Read/Write Code "w/r"                   |
    1273             : |  datbuf         void                I/O buffer                              |
    1274             : |                                                                             |
    1275             : |                                                                             |
    1276             : |  OUTPUTS:                                                                   |
    1277             : |  datbuf         void                I/O buffer                              |
    1278             : |                                                                             |
    1279             : |  NOTES:                                                                     |
    1280             : |                                                                             |
    1281             : |                                                                             |
    1282             : |   Date     Programmer   Description                                         |
    1283             : |  ======   ============  =================================================   |
    1284             : |  Jun 96   Joel Gales    Original Programmer                                 |
    1285             : |  Oct 96   Joel Gales    Pass Vgroup id as routine parameter                 |
    1286             : |  Oct 96   Joel Gales    Remove Vdetach call                                 |
    1287             : |                                                                             |
    1288             : |  END_PROLOG                                                                 |
    1289             : -----------------------------------------------------------------------------*/
    1290             : intn
    1291           1 : EHattr(int32 fid, int32 attrVgrpID, const char *attrname, int32 numbertype,
    1292             :        int32 count, const char *wrcode, VOIDP datbuf)
    1293             : 
    1294             : {
    1295           1 :     intn            status = 0; /* routine return status variable */
    1296             :     int32           vdataID;  /* Attribute Vdata ID */
    1297             : 
    1298             :     /*
    1299             :      * Attributes are stored as Vdatas with name given by the user, class:
    1300             :      * "Attr0.0" and fieldname: "AttrValues"
    1301             :      */
    1302             : 
    1303             : 
    1304             :     /* Get Attribute Vdata ID and "open" with appropriate I/O code */
    1305             :     /* ----------------------------------------------------------- */
    1306           1 :     vdataID = EHgetid(fid, attrVgrpID, attrname, 1, wrcode);
    1307             : 
    1308             :     /* Write Attribute Section */
    1309             :     /* ----------------------- */
    1310           1 :     if (strcmp(wrcode, "w") == 0)
    1311             :     {
    1312             :   /* Create Attribute Vdata (if it doesn't exist) */
    1313             :   /* -------------------------------------------- */
    1314           0 :   if (vdataID == -1)
    1315             :   {
    1316           0 :       vdataID = VSattach(fid, -1, "w");
    1317           0 :       VSsetname(vdataID, attrname);
    1318           0 :       VSsetclass(vdataID, "Attr0.0");
    1319             : 
    1320           0 :       VSfdefine(vdataID, "AttrValues", numbertype, count);
    1321           0 :       Vinsert(attrVgrpID, vdataID);
    1322             :   }
    1323             :   /* Write Attribute */
    1324             :   /* --------------- */
    1325           0 :   VSsetfields(vdataID, "AttrValues");
    1326           0 :   (void) VSsizeof(vdataID, (char*) "AttrValues");
    1327           0 :   VSwrite(vdataID, datbuf, 1, FULL_INTERLACE);
    1328             : 
    1329           0 :   VSdetach(vdataID);
    1330             :     }
    1331             :     /* Read Attribute Section */
    1332             :     /* ---------------------- */
    1333           1 :     if (strcmp(wrcode, "r") == 0)
    1334             :     {
    1335             :   /* If attribute doesn't exist report error */
    1336             :   /* --------------------------------------- */
    1337           1 :   if (vdataID == -1)
    1338             :   {
    1339           1 :       status = -1;
    1340           1 :       HEpush(DFE_GENAPP, "EHattr", __FILE__, __LINE__);
    1341           1 :       HEreport("Attribute %s not defined.\n", attrname);
    1342             :   } else
    1343             :   {
    1344           0 :       VSsetfields(vdataID, "AttrValues");
    1345           0 :       (void) VSsizeof(vdataID, (char*) "AttrValues");
    1346           0 :       VSread(vdataID, datbuf, 1, FULL_INTERLACE);
    1347           0 :       VSdetach(vdataID);
    1348             :   }
    1349             :     }
    1350           1 :     return (status);
    1351             : }
    1352             : 
    1353             : 
    1354             : 
    1355             : 
    1356             : /*----------------------------------------------------------------------------|
    1357             : |  BEGIN_PROLOG                                                               |
    1358             : |                                                                             |
    1359             : |  FUNCTION: EHattrinfo                                                       |
    1360             : |                                                                             |
    1361             : |  DESCRIPTION: Returns numbertype and count of given HDF-EOS attribute       |
    1362             : |                                                                             |
    1363             : |                                                                             |
    1364             : |  Return Value    Type     Units     Description                             |
    1365             : |  ============   ======  =========   =====================================   |
    1366             : |  status         intn                return status (0) SUCCEED, (-1) FAIL    |
    1367             : |                                                                             |
    1368             : |  INPUTS:                                                                    |
    1369             : |  fid            int32               HDF-EOS file ID                         |
    1370             : |  attrVgrpID     int32               Attribute Vgroup ID                     |
    1371             : |  attrname       char                attribute name                          |
    1372             : |                                                                             |
    1373             : |  OUTPUTS:                                                                   |
    1374             : |  numbertype     int32               attribute HDF numbertype                |
    1375             : |  count          int32               Number of attribute elements            |
    1376             : |                                                                             |
    1377             : |  NOTES:                                                                     |
    1378             : |                                                                             |
    1379             : |                                                                             |
    1380             : |   Date     Programmer   Description                                         |
    1381             : |  ======   ============  =================================================   |
    1382             : |  Jun 96   Joel Gales    Original Programmer                                 |
    1383             : |  Oct 96   Joel Gales    Pass Vgroup id as routine parameter                 |
    1384             : |  Oct 96   Joel Gales    Remove Vdetach call                                 |
    1385             : |                                                                             |
    1386             : |  END_PROLOG                                                                 |
    1387             : -----------------------------------------------------------------------------*/
    1388             : intn
    1389           0 : EHattrinfo(int32 fid, int32 attrVgrpID, const char *attrname, int32 * numbertype,
    1390             :      int32 * count)
    1391             : 
    1392             : {
    1393           0 :     intn            status = 0; /* routine return status variable */
    1394             :     int32           vdataID;  /* Attribute Vdata ID */
    1395             : 
    1396             :     /* Get Attribute Vdata ID */
    1397             :     /* ---------------------- */
    1398           0 :     vdataID = EHgetid(fid, attrVgrpID, attrname, 1, "r");
    1399             : 
    1400             :     /* If attribute not defined then report error */
    1401             :     /* ------------------------------------------ */
    1402           0 :     if (vdataID == -1)
    1403             :     {
    1404           0 :   status = -1;
    1405           0 :   HEpush(DFE_GENAPP, "EHattr", __FILE__, __LINE__);
    1406           0 :   HEreport("Attribute %s not defined.\n", attrname);
    1407             :     } else
    1408             :     {
    1409             :   /* Get attribute info */
    1410             :   /* ------------------ */
    1411           0 :   VSsetfields(vdataID, "AttrValues");
    1412           0 :   *count = VSsizeof(vdataID, (char*) "AttrValues");
    1413           0 :   *numbertype = VFfieldtype(vdataID, 0);
    1414           0 :   VSdetach(vdataID);
    1415             :     }
    1416             : 
    1417           0 :     return (status);
    1418             : }
    1419             : 
    1420             : 
    1421             : 
    1422             : 
    1423             : 
    1424             : /*----------------------------------------------------------------------------|
    1425             : |  BEGIN_PROLOG                                                               |
    1426             : |                                                                             |
    1427             : |  FUNCTION: EHattrcat                                                        |
    1428             : |                                                                             |
    1429             : |  DESCRIPTION: Returns a listing of attributes within an HDF-EOS structure   |
    1430             : |                                                                             |
    1431             : |                                                                             |
    1432             : |  Return Value    Type     Units     Description                             |
    1433             : |  ============   ======  =========   =====================================   |
    1434             : |  nattr          int32               Number of attributes in swath struct    |
    1435             : |                                                                             |
    1436             : |  INPUTS:                                                                    |
    1437             : |  fid            int32               HDF-EOS file ID                         |
    1438             : |  attrVgrpID     int32               Attribute Vgroup ID                     |
    1439             : |  structcode     char                Structure Code ("s/g/p")                |
    1440             : |                                                                             |
    1441             : |  OUTPUTS:                                                                   |
    1442             : |  attrnames      char                Attribute names in swath struct         |
    1443             : |                                     (Comma-separated list)                  |
    1444             : |  strbufsize     int32               Attributes name list string length      |
    1445             : |                                                                             |
    1446             : |  NOTES:                                                                     |
    1447             : |                                                                             |
    1448             : |                                                                             |
    1449             : |   Date     Programmer   Description                                         |
    1450             : |  ======   ============  =================================================   |
    1451             : |  Jun 96   Joel Gales    Original Programmer                                 |
    1452             : |  Oct 96   Joel Gales    Pass Vgroup id as routine parameter                 |
    1453             : |  Oct 96   Joel Gales    Remove Vdetach call                                 |
    1454             : |                                                                             |
    1455             : |  END_PROLOG                                                                 |
    1456             : -----------------------------------------------------------------------------*/
    1457             : int32
    1458           7 : EHattrcat(int32 fid, int32 attrVgrpID, char *attrnames, int32 * strbufsize)
    1459             : {
    1460             :     intn            i;            /* Loop index */
    1461             : 
    1462             :     int32           nObjects;         /* # of objects in Vgroup */
    1463             :     int32          *tags;         /* Pnt to Vgroup object tags array */
    1464             :     int32          *refs;         /* Pnt to Vgroup object refs array */
    1465             :     int32           vdataID;          /* Attribute Vdata ID */
    1466             : 
    1467           7 :     int32           nattr = 0;          /* Number of attributes */
    1468             :     int32           slen;         /* String length */
    1469             : 
    1470             :     char            name[80];         /* Attribute name */
    1471             :     static const char indxstr[] = "INDXMAP:"; /* Index Mapping reserved
    1472             :                                                  string */
    1473             :     static const char fvstr[] = "_FV_"; /* Flag Value reserved string */
    1474             :     static const char bsom[] = "_BLKSOM:";/* Block SOM Offset reserved string */
    1475             : 
    1476             : 
    1477             :     /* Set string buffer size to 0 */
    1478             :     /* --------------------------- */
    1479           7 :     *strbufsize = 0;
    1480             : 
    1481             : 
    1482             :     /* Get number of attributes within Attribute Vgroup */
    1483             :     /* ------------------------------------------------ */
    1484           7 :     nObjects = Vntagrefs(attrVgrpID);
    1485             : 
    1486             : 
    1487             :     /* If attributes exist ... */
    1488             :     /* ----------------------- */
    1489           7 :     if (nObjects > 0)
    1490             :     {
    1491             :   /* Get tags and references of attribute Vdatas */
    1492             :   /* ------------------------------------------- */
    1493           0 :   tags = (int32 *) malloc(sizeof(int32) * nObjects);
    1494           0 :   if(tags == NULL)
    1495             :   {
    1496           0 :       HEpush(DFE_NOSPACE,"EHattrcat", __FILE__, __LINE__);
    1497           0 :       return(-1);
    1498             :   }
    1499           0 :   refs = (int32 *) malloc(sizeof(int32) * nObjects);
    1500           0 :   if(refs == NULL)
    1501             :   {
    1502           0 :       HEpush(DFE_NOSPACE,"EHattrcat", __FILE__, __LINE__);
    1503           0 :       free(tags);
    1504           0 :       return(-1);
    1505             :   }
    1506             : 
    1507           0 :   Vgettagrefs(attrVgrpID, tags, refs, nObjects);
    1508             : 
    1509             :   /* Get attribute vdata IDs and names */
    1510             :   /* --------------------------------- */
    1511           0 :   for (i = 0; i < nObjects; i++)
    1512             :   {
    1513           0 :       vdataID = VSattach(fid, *(refs + i), "r");
    1514           0 :       VSgetname(vdataID, name);
    1515             : 
    1516             :       /*
    1517             :        * Don't return fill value, index mapping & block SOM attributes
    1518             :        */
    1519           0 :       if (memcmp(name, indxstr, strlen(indxstr)) != 0 &&
    1520           0 :     memcmp(name, fvstr, strlen(fvstr)) != 0 &&
    1521           0 :     memcmp(name, bsom, strlen(bsom)) != 0)
    1522             :       {
    1523             :     /* Increment attribute counter and add name to list */
    1524             :     /* ------------------------------------------------ */
    1525           0 :     nattr++;
    1526           0 :     if (attrnames != NULL)
    1527             :     {
    1528           0 :         if (nattr == 1)
    1529             :         {
    1530           0 :       strcpy(attrnames, name);
    1531             :         } else
    1532             :         {
    1533           0 :       strcat(attrnames, ",");
    1534           0 :       strcat(attrnames, name);
    1535             :         }
    1536             :     }
    1537             :     /* Increment attribute names string length */
    1538             :     /* --------------------------------------- */
    1539           0 :     slen = (nattr == 1) ? (int)strlen(name) : (int)strlen(name) + 1;
    1540           0 :     *strbufsize += slen;
    1541             :       }
    1542           0 :       VSdetach(vdataID);
    1543             :   }
    1544           0 :   free(tags);
    1545           0 :   free(refs);
    1546             :     }
    1547           7 :     return (nattr);
    1548             : }
    1549             : 
    1550             : 
    1551             : 
    1552             : /*----------------------------------------------------------------------------|
    1553             : |  BEGIN_PROLOG                                                               |
    1554             : |                                                                             |
    1555             : |  FUNCTION: EHinquire                                                        |
    1556             : |                                                                             |
    1557             : |  DESCRIPTION: Returns number and names of HDF-EOS structures in file        |
    1558             : |                                                                             |
    1559             : |                                                                             |
    1560             : |  Return Value    Type     Units     Description                             |
    1561             : |  ============   ======  =========   =====================================   |
    1562             : |  nobj           int32               Number of HDF-EOS structures in file    |
    1563             : |                                                                             |
    1564             : |  INPUTS:                                                                    |
    1565             : |  filename       char                HDF-EOS filename                        |
    1566             : |  type           char                Object Type ("SWATH/GRID/POINT")        |
    1567             : |                                                                             |
    1568             : |  OUTPUTS:                                                                   |
    1569             : |  objectlist     char                List of object names (comma-separated)  |
    1570             : |  strbufsize     int32               Length of objectlist                    |
    1571             : |                                                                             |
    1572             : |  NOTES:                                                                     |
    1573             : |                                                                             |
    1574             : |                                                                             |
    1575             : |   Date     Programmer   Description                                         |
    1576             : |  ======   ============  =================================================   |
    1577             : |  Jun 96   Joel Gales    Original Programmer                                 |
    1578             : |                                                                             |
    1579             : |  END_PROLOG                                                                 |
    1580             : -----------------------------------------------------------------------------*/
    1581             : int32
    1582          50 : EHinquire(const char *filename, const char *type, char *objectlist, int32 * strbufsize)
    1583             : {
    1584             :     int32           HDFfid; /* HDF file ID */
    1585             :     int32           vgRef;  /* Vgroup reference number */
    1586             :     int32           vGrpID; /* Vgroup ID */
    1587          50 :     int32           nobj = 0; /* Number of HDFEOS objects in file */
    1588             :     int32           slen; /* String length */
    1589             : 
    1590             :     char            name[512];  /* Object name */
    1591             :     char            class[80];  /* Object class */
    1592             : 
    1593             : 
    1594             :     /* Open HDFEOS file of read-only access */
    1595             :     /* ------------------------------------ */
    1596          50 :     HDFfid = Hopen(filename, DFACC_READ, 0);
    1597             : 
    1598             : 
    1599             :     /* Start Vgroup Interface */
    1600             :     /* ---------------------- */
    1601          50 :     Vstart(HDFfid);
    1602             : 
    1603             : 
    1604             :     /* If string buffer size is requested then zero out counter */
    1605             :     /* -------------------------------------------------------- */
    1606          50 :     if (strbufsize != NULL)
    1607             :     {
    1608          50 :   *strbufsize = 0;
    1609             :     }
    1610             :     /* Search for objects from beginning of HDF file */
    1611             :     /* -------------------------------------------- */
    1612          50 :     vgRef = -1;
    1613             : 
    1614             :     /* Loop through all objects */
    1615             :     /* ------------------------ */
    1616             :     while (1)
    1617             :     {
    1618             :   /* Get Vgroup reference number */
    1619             :   /* --------------------------- */
    1620         309 :   vgRef = Vgetid(HDFfid, vgRef);
    1621             : 
    1622             :   /* If no more then exist search loop */
    1623             :   /* --------------------------------- */
    1624         309 :   if (vgRef == -1)
    1625             :   {
    1626          50 :       break;
    1627             :   }
    1628             :   /* Get Vgroup ID, name, and class */
    1629             :   /* ------------------------------ */
    1630         259 :   vGrpID = Vattach(HDFfid, vgRef, "r");
    1631         259 :   VgetnameSafe(vGrpID, name, sizeof(name));
    1632             : #if LIBVER_MAJOR == 4 && LIBVER_MINOR >= 4
    1633             :     size_t sz = sizeof(class);
    1634             :     Vgetclass(vGrpID, class, &sz);
    1635             : #else
    1636         259 :   Vgetclass(vGrpID, class);
    1637             : #endif
    1638             : 
    1639             :   /* If object of desired type (SWATH, POINT, GRID) ... */
    1640             :   /* -------------------------------------------------- */
    1641         259 :   if (strcmp(class, type) == 0)
    1642             :   {
    1643             : 
    1644             :       /* Increment counter */
    1645             :       /* ----------------- */
    1646          30 :       nobj++;
    1647             : 
    1648             : 
    1649             :       /* If object list requested add name to list */
    1650             :       /* ----------------------------------------- */
    1651          30 :       if (objectlist != NULL)
    1652             :       {
    1653          12 :     if (nobj == 1)
    1654             :     {
    1655          12 :         strcpy(objectlist, name);
    1656             :     } else
    1657             :     {
    1658           0 :         strcat(objectlist, ",");
    1659           0 :         strcat(objectlist, name);
    1660             :     }
    1661             :       }
    1662             :       /* Compute string length of object entry */
    1663             :       /* ------------------------------------- */
    1664          30 :       slen = (nobj == 1) ? (int)strlen(name) : (int)strlen(name) + 1;
    1665             : 
    1666             : 
    1667             :       /* If string buffer size is requested then increment buffer size */
    1668             :       /* ------------------------------------------------------------- */
    1669          30 :       if (strbufsize != NULL)
    1670             :       {
    1671          30 :     *strbufsize += slen;
    1672             :       }
    1673             :   }
    1674             :   /* Detach Vgroup */
    1675             :   /* ------------- */
    1676         259 :   Vdetach(vGrpID);
    1677             :     }
    1678             : 
    1679             :     /* "Close" Vgroup interface and HDFEOS file */
    1680             :     /* ---------------------------------------- */
    1681          50 :     Vend(HDFfid);
    1682          50 :     Hclose(HDFfid);
    1683             : 
    1684          50 :     return (nobj);
    1685             : }
    1686             : 
    1687             : 
    1688             : 
    1689             : /*----------------------------------------------------------------------------|
    1690             : |  BEGIN_PROLOG                                                               |
    1691             : |                                                                             |
    1692             : |  FUNCTION: EHclose                                                          |
    1693             : |                                                                             |
    1694             : |  DESCRIPTION: Closes HDF-EOS file                                           |
    1695             : |                                                                             |
    1696             : |                                                                             |
    1697             : |  Return Value    Type     Units     Description                             |
    1698             : |  ============   ======  =========   =====================================   |
    1699             : |  status         intn                return status (0) SUCCEED, (-1) FAIL    |
    1700             : |                                                                             |
    1701             : |  INPUTS:                                                                    |
    1702             : |  fid            int32               HDF-EOS File ID                         |
    1703             : |                                                                             |
    1704             : |  OUTPUTS:                                                                   |
    1705             : |             None                                                            |
    1706             : |                                                                             |
    1707             : |  NOTES:                                                                     |
    1708             : |                                                                             |
    1709             : |                                                                             |
    1710             : |   Date     Programmer   Description                                         |
    1711             : |  ======   ============  =================================================   |
    1712             : |  Jun 96   Joel Gales    Original Programmer                                 |
    1713             : |  Jul 96   Joel Gales    Add file id offset EHIDOFFSET                       |
    1714             : |  Aug 96   Joel Gales    Add HE error report if file id out of bounds        |
    1715             : |  Nov 96   Joel Gales    Add EHXacsTable array to "garbage collection"       |
    1716             : |                                                                             |
    1717             : |  END_PROLOG                                                                 |
    1718             : -----------------------------------------------------------------------------*/
    1719             : intn
    1720          46 : EHclose(int32 fid)
    1721             : {
    1722          46 :     intn            status = 0; /* routine return status variable */
    1723             : 
    1724             :     int32           HDFfid; /* HDF file ID */
    1725             :     int32           sdInterfaceID;  /* HDF SDS interface ID */
    1726             :     int32           fid0; /* HDF EOS file id - offset */
    1727             : 
    1728             : 
    1729             :     /* Check for valid HDFEOS file ID range */
    1730             :     /* ------------------------------------ */
    1731          46 :     if (fid >= EHIDOFFSET && fid < EHXmaxfilecount + EHIDOFFSET)
    1732             :     {
    1733             :   /* Compute "reduced" file ID */
    1734             :   /* ------------------------- */
    1735          46 :   fid0 = fid % EHIDOFFSET;
    1736             : 
    1737             : 
    1738             :   /* Get HDF file ID and SD interface ID */
    1739             :   /* ----------------------------------- */
    1740          46 :   HDFfid = EHXfidTable[fid0];
    1741          46 :   sdInterfaceID = EHXsdTable[fid0];
    1742             : 
    1743             :   /* "Close" SD interface, Vgroup interface, and HDF file */
    1744             :   /* ---------------------------------------------------- */
    1745          46 :   status = SDend(sdInterfaceID);
    1746          46 :   if (Vend(HDFfid) < 0)
    1747           0 :         status = -1;
    1748          46 :   if (Hclose(HDFfid) < 0)
    1749           0 :         status = -1;
    1750             : 
    1751             :   /* Clear out external array entries */
    1752             :   /* -------------------------------- */
    1753          46 :   EHXtypeTable[fid0] = 0;
    1754          46 :   EHXacsTable[fid0] = 0;
    1755          46 :   EHXfidTable[fid0] = 0;
    1756          46 :   EHXsdTable[fid0] = 0;
    1757          46 :         if (EHget_numfiles() == 0)
    1758             :         {
    1759          40 :             free(EHXtypeTable);
    1760          40 :             EHXtypeTable = NULL;
    1761          40 :             free(EHXacsTable);
    1762          40 :             EHXacsTable = NULL;
    1763          40 :             free(EHXfidTable);
    1764          40 :             EHXfidTable = NULL;
    1765          40 :             free(EHXsdTable);
    1766          40 :             EHXsdTable = NULL;
    1767          40 :             EHXmaxfilecount = 0;
    1768             :         }
    1769             :     } else
    1770             :     {
    1771           0 :   status = -1;
    1772           0 :   HEpush(DFE_RANGE, "EHclose", __FILE__, __LINE__);
    1773           0 :   HEreport("Invalid file id: %d.  ID must be >= %d and < %d.\n",
    1774             :      fid, EHIDOFFSET, EHXmaxfilecount + EHIDOFFSET);
    1775             :     }
    1776             : 
    1777          46 :     return (status);
    1778             : }
    1779             : 
    1780             : /*----------------------------------------------------------------------------|
    1781             : |  BEGIN_PROLOG                                                               |
    1782             : |                                                                             |
    1783             : |  FUNCTION: EHnumstr                                                         |
    1784             : |                                                                             |
    1785             : |  DESCRIPTION: Returns numerical type code of the given string               |
    1786             : |               representation.                                               |
    1787             : |                                                                             |
    1788             : |                                                                             |
    1789             : |  Return Value    Type     Units     Description                             |
    1790             : |  ============   ======  =========   =====================================   |
    1791             : |  numbertype     int32               numerical type code                     |
    1792             : |                                                                             |
    1793             : |  INPUTS:                                                                    |
    1794             : |  strcode        const char          string representation of the type code  |
    1795             : |                                                                             |
    1796             : |                                                                             |
    1797             : |  OUTPUTS:                                                                   |
    1798             : |             None                                                            |
    1799             : |                                                                             |
    1800             : |  NOTES:                                                                     |
    1801             : |                                                                             |
    1802             : |                                                                             |
    1803             : |   Date     Programmer   Description                                         |
    1804             : |  ======   ============  =================================================   |
    1805             : |  Nov 07   Andrey Kiselev  Original Programmer                               |
    1806             : |                                                                             |
    1807             : |  END_PROLOG                                                                 |
    1808             : -----------------------------------------------------------------------------*/
    1809             : int32
    1810         120 : EHnumstr(const char *strcode)
    1811             : {
    1812         120 :     if (strcmp(strcode, "DFNT_UCHAR8") == 0)
    1813           0 :         return DFNT_UCHAR8;
    1814         120 :     else if (strcmp(strcode, "DFNT_CHAR8") == 0)
    1815           0 :         return DFNT_CHAR8;
    1816         120 :     else if (strcmp(strcode, "DFNT_FLOAT32") == 0)
    1817         120 :         return DFNT_FLOAT32;
    1818           0 :     else if (strcmp(strcode, "DFNT_FLOAT64") == 0)
    1819           0 :         return DFNT_FLOAT64;
    1820           0 :     else if (strcmp(strcode, "DFNT_INT8") == 0)
    1821           0 :         return DFNT_INT8;
    1822           0 :     else if (strcmp(strcode, "DFNT_UINT8") == 0)
    1823           0 :         return DFNT_UINT8;
    1824           0 :     else if (strcmp(strcode, "DFNT_INT16") == 0)
    1825           0 :         return DFNT_INT16;
    1826           0 :     else if (strcmp(strcode, "DFNT_UINT16") == 0)
    1827           0 :         return DFNT_UINT16;
    1828           0 :     else if (strcmp(strcode, "DFNT_INT32") == 0)
    1829           0 :         return DFNT_INT32;
    1830           0 :     else if (strcmp(strcode, "DFNT_UINT32") == 0)
    1831           0 :         return DFNT_UINT32;
    1832             :     else
    1833           0 :         return DFNT_NONE;
    1834             : }
    1835             : 
    1836             : /*----------------------------------------------------------------------------|
    1837             : |  BEGIN_PROLOG                                                               |
    1838             : |                                                                             |
    1839             : |  FUNCTION: EHreset_maxopenfiles                                             |
    1840             : |                                                                             |
    1841             : |  DESCRIPTION: Change the allowed number of opened HDFEOS files.             |
    1842             : |                                                                             |
    1843             : |                                                                             |
    1844             : |  Return Value    Type     Units     Description                             |
    1845             : |  ============   ======  =========   =====================================   |
    1846             : |  numbertype     intn                The current maximum number of opened    |
    1847             : |                                     files allowed, or -1, if unable         |
    1848             : |                                     to reset it.                            |
    1849             : |                                                                             |
    1850             : |  INPUTS:                                                                    |
    1851             : |  strcode        intn                Requested number of opened files.       |
    1852             : |                                                                             |
    1853             : |                                                                             |
    1854             : |  OUTPUTS:                                                                   |
    1855             : |             None                                                            |
    1856             : |                                                                             |
    1857             : |  NOTES:                                                                     |
    1858             : |                                                                             |
    1859             : |                                                                             |
    1860             : |   Date        Programmer     Description                                    |
    1861             : |  ==========   ============   ============================================== |
    1862             : |  2013.04.03   Andrey Kiselev Original Programmer                            |
    1863             : |                                                                             |
    1864             : |  END_PROLOG                                                                 |
    1865             : -----------------------------------------------------------------------------*/
    1866             : static intn
    1867          40 : EHreset_maxopenfiles(intn req_max)
    1868             : {
    1869             :     intn    ret_value;
    1870             : 
    1871          40 :     if (req_max <= EHXmaxfilecount)
    1872           0 :         return EHXmaxfilecount;
    1873             : 
    1874             :     /* Fallback to built-in NEOSHDF constant if          */
    1875             :     /* SDreset_maxopenfiles() interface is not available */
    1876             :     /* ------------------------------------------------- */
    1877             : #ifdef HDF4_HAS_MAXOPENFILES
    1878          40 :     ret_value = SDreset_maxopenfiles(req_max);
    1879             : #else
    1880             :     ret_value = NEOSHDF;
    1881             : #endif /* HDF4_HAS_MAXOPENFILES */
    1882             : 
    1883          40 :     if (ret_value > 0)
    1884             :     {
    1885          40 :         EHXtypeTable = realloc(EHXtypeTable, ret_value * sizeof(*EHXtypeTable));
    1886          40 :         memset(EHXtypeTable + EHXmaxfilecount, 0,
    1887          40 :                (ret_value - EHXmaxfilecount) * sizeof(*EHXtypeTable));
    1888          40 :         EHXacsTable = realloc(EHXacsTable, ret_value * sizeof(*EHXacsTable));
    1889          40 :         memset(EHXacsTable + EHXmaxfilecount, 0,
    1890          40 :                (ret_value - EHXmaxfilecount) * sizeof(*EHXacsTable));
    1891          40 :         EHXfidTable = realloc(EHXfidTable, ret_value * sizeof(*EHXfidTable));
    1892          40 :         memset(EHXfidTable + EHXmaxfilecount, 0,
    1893          40 :                (ret_value - EHXmaxfilecount) * sizeof(*EHXfidTable));
    1894          40 :         EHXsdTable = realloc(EHXsdTable, ret_value * sizeof(*EHXsdTable));
    1895          40 :         memset(EHXsdTable + EHXmaxfilecount, 0,
    1896          40 :                (ret_value - EHXmaxfilecount) * sizeof(*EHXsdTable));
    1897          40 :         EHXmaxfilecount = ret_value;
    1898             :     }
    1899             : 
    1900          40 :     return ret_value;
    1901             : }
    1902             : 
    1903             : /*----------------------------------------------------------------------------|
    1904             : |  BEGIN_PROLOG                                                               |
    1905             : |                                                                             |
    1906             : |  FUNCTION: EHget_maxopenfiles                                               |
    1907             : |                                                                             |
    1908             : |  DESCRIPTION: Request the allowed number of opened HDFEOS files and maximum |
    1909             : |               number of opened files allowed in the system.                 |
    1910             : |                                                                             |
    1911             : |                                                                             |
    1912             : |  Return Value    Type     Units     Description                             |
    1913             : |  ============   ======  =========   =====================================   |
    1914             : |  status         intn                return status (0) SUCCEED, (-1) FAIL    |
    1915             : |                                                                             |
    1916             : |  INPUTS:                                                                    |
    1917             : |             None                                                            |
    1918             : |                                                                             |
    1919             : |                                                                             |
    1920             : |  OUTPUTS:                                                                   |
    1921             : |  curr_max       intn                Current number of open files allowed.   |
    1922             : |  sys_limit      intn                Maximum number of open files allowed    |
    1923             : |                                     in the system.                          |
    1924             : |                                                                             |
    1925             : |  NOTES:                                                                     |
    1926             : |                                                                             |
    1927             : |                                                                             |
    1928             : |   Date        Programmer     Description                                    |
    1929             : |  ==========   ============   ============================================== |
    1930             : |  2013.04.03   Andrey Kiselev Original Programmer                            |
    1931             : |                                                                             |
    1932             : |  END_PROLOG                                                                 |
    1933             : -----------------------------------------------------------------------------*/
    1934             : static intn
    1935          46 : EHget_maxopenfiles(intn *curr_max,
    1936             :        intn *sys_limit)
    1937             : {
    1938          46 :     intn ret_value = 0;
    1939             : 
    1940             : #ifdef HDF4_HAS_MAXOPENFILES
    1941          46 :     ret_value = SDget_maxopenfiles(curr_max, sys_limit);
    1942             : #else
    1943             :     *sys_limit = NEOSHDF;
    1944             : #endif /* HDF4_HAS_MAXOPENFILES */
    1945             : 
    1946          46 :     *curr_max = EHXmaxfilecount;
    1947             : 
    1948          46 :     return ret_value;
    1949             : }
    1950             : 
    1951             : /*----------------------------------------------------------------------------|
    1952             : |  BEGIN_PROLOG                                                               |
    1953             : |                                                                             |
    1954             : |  FUNCTION: EHget_numfiles                                                   |
    1955             : |                                                                             |
    1956             : |  DESCRIPTION: Request the number of HDFEOS files currently opened.          |
    1957             : |                                                                             |
    1958             : |                                                                             |
    1959             : |  Return Value    Type     Units     Description                             |
    1960             : |  ============   ======  =========   =====================================   |
    1961             : |  nfileopen      intn                Number of HDFEOS files already opened.  |
    1962             : |                                                                             |
    1963             : |  INPUTS:                                                                    |
    1964             : |             None                                                            |
    1965             : |                                                                             |
    1966             : |                                                                             |
    1967             : |  OUTPUTS:                                                                   |
    1968             : |             None                                                            |
    1969             : |                                     in the system.                          |
    1970             : |                                                                             |
    1971             : |  NOTES:                                                                     |
    1972             : |                                                                             |
    1973             : |                                                                             |
    1974             : |   Date        Programmer     Description                                    |
    1975             : |  ==========   ============   ============================================== |
    1976             : |  2013.04.03   Andrey Kiselev Original Programmer                            |
    1977             : |                                                                             |
    1978             : |  END_PROLOG                                                                 |
    1979             : -----------------------------------------------------------------------------*/
    1980             : static intn
    1981          92 : EHget_numfiles()
    1982             : {
    1983             :     intn            i;        /* Loop index */
    1984          92 :     intn            nfileopen = 0;  /* # of HDF files open */
    1985             : 
    1986          92 :     if (EHXtypeTable)
    1987             :     {
    1988             :         /* Determine number of files currently opened */
    1989             :         /* ------------------------------------------ */
    1990     1840090 :         for (i = 0; i < EHXmaxfilecount; i++)
    1991             :         {
    1992     1840000 :             nfileopen += EHXtypeTable[i];
    1993             :         }
    1994             :     }
    1995             : 
    1996          92 :     return nfileopen;
    1997             : }

Generated by: LCOV version 1.14