home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / hdf / unix / hdf3_2r2.lha / HDF3.2r2 / src / hfile.h < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-28  |  14.0 KB  |  405 lines

  1. /***************************************************************************
  2. *
  3. *
  4. *                         NCSA HDF version 3.2r2
  5. *                            October 30, 1992
  6. *
  7. * NCSA HDF Version 3.2 source code and documentation are in the public
  8. * domain.  Specifically, we give to the public domain all rights for future
  9. * licensing of the source code, all resale rights, and all publishing rights.
  10. *
  11. * We ask, but do not require, that the following message be included in all
  12. * derived works:
  13. *
  14. * Portions developed at the National Center for Supercomputing Applications at
  15. * the University of Illinois at Urbana-Champaign, in collaboration with the
  16. * Information Technology Institute of Singapore.
  17. *
  18. * THE UNIVERSITY OF ILLINOIS GIVES NO WARRANTY, EXPRESSED OR IMPLIED, FOR THE
  19. * SOFTWARE AND/OR DOCUMENTATION PROVIDED, INCLUDING, WITHOUT LIMITATION,
  20. * WARRANTY OF MERCHANTABILITY AND WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE
  21. *
  22. ****************************************************************************
  23. */
  24.  
  25. /*
  26. $Header: /hdf/hdf/v3.2r2/src/RCS/hfile.h,v 1.3 1992/10/23 00:14:11 koziol beta koziol $
  27.  
  28. $Log: hfile.h,v $
  29.  * Revision 1.3  1992/10/23  00:14:11  koziol
  30.  * Changed all DFIstr*() and DFImem*() calls to HDstr*() and HDmem*() calls
  31.  * #ifdef'd out the macros Jason defined for Hopen, Hclose, etc. for Vsets
  32.  * Replaced Vset VFREESPACE and VGETSPACE calls with actual calls to HDfreespace
  33.  * and HDgetspace
  34.  * Added a MS-Windows lower lower for file I/O (which may not be completely working
  35.  *
  36.  * Revision 1.2  1992/10/12  18:11:51  koziol
  37.  * Updated for v3.2r2 release
  38.  *
  39.  * Revision 1.1  1992/08/25  21:40:44  koziol
  40.  * Initial revision
  41.  *
  42. */
  43. /*+ hfile.h
  44. *** Header for hfile.c, routines for low level data element I/O
  45. +*/
  46.  
  47. #ifndef HFILE_H
  48. #define HFILE_H
  49.  
  50. /* maximum number of files (number of slots for file records) */
  51.  
  52. #ifndef MAX_FILE
  53. #   define MAX_FILE 16
  54. #endif
  55.  
  56. /* maximum number of access elements */
  57.  
  58. #ifndef MAX_ACC
  59. #   define MAX_ACC 256
  60. #endif
  61.  
  62. /* Magic cookie for HDF data files */
  63.  
  64. #define MAGICLEN 4             /* length */
  65. #define HDFMAGIC "\016\003\023\001" /* ^N^C^S^A */
  66.  
  67. /* version tags */
  68. /* Library version numbers */
  69.  
  70. #define LIBVER_MAJOR    3
  71. #define LIBVER_MINOR    2
  72. #define LIBVER_RELEASE  2
  73. #define LIBVER_STRING    "NCSA HDF Version 3.2 Release 2 Oct. 21, 1992"
  74. #define LIBVER_LEN  92  /* 4+4+4+80 = 92 */
  75. /* end of version tags */
  76.  
  77. /* FILELIB -- file library to use for file access: 1 stdio, 2 fcntl
  78.    default to stdio library */
  79.  
  80. #ifndef FILELIB
  81. #   define FILELIB UNIXBUFIO /* UNIX buffered I/O is the default */
  82. #endif
  83.  
  84. #if (FILELIB == UNIXBUFIO)
  85. /* using C buffered file I/O routines to access files */
  86. typedef FILE *hdf_file_t;
  87. #ifdef VMS
  88. /* For VMS, use "mbc=64" to improve performance     */
  89. #   define HI_OPEN(p, a)       (((a) & DFACC_WRITE) ? \
  90.                  fopen((p), "r+", "mbc=64") : fopen((p), "r", "mbc=64"))
  91. #else  /*  !VMS  */
  92. #   define HI_OPEN(p, a)       (((a) & DFACC_WRITE) ? \
  93.                         fopen((p), "r+") : fopen((p), "r"))
  94. #endif
  95. #   define HI_CREATE(p)        (fopen((p), "w+"))
  96. #   define HI_READ(f, b, n)    (((n) == fread((b), 1, (n), (f))) ? \
  97.                                 SUCCEED : FAIL)
  98. #   define HI_WRITE(f, b, n)   (((n) == fwrite((b), 1, (n), (f))) ? \
  99.                                 SUCCEED : FAIL)
  100. #   define HI_CLOSE(f) (fclose(f))
  101. #   define HI_FLUSH(f) (fflush(f))
  102. #   define HI_SEEK(f, o)       (fseek((f), (long)(o), SEEK_SET))
  103. #   define HI_SEEKEND(f) (fseek((f), (long)0, SEEK_END))
  104. #   define HI_TELL(f)  (ftell(f))
  105. #   define OPENERR(f)  ((f) == (FILE *)NULL)
  106. #endif /* FILELIB == UNIXBUFIO */
  107.  
  108. #if (FILELIB == UNIXUNBUFIO)
  109. /* using UNIX unbuffered file I/O routines to access files */
  110. typedef int hdf_file_t;
  111. #   define HI_OPEN(p, a)       (((a) & DFACC_WRITE) ? \
  112.                         open((p), O_RDWR) : open((p), O_RDONLY))
  113. #   define HI_CREATE(p)        (open((p), O_RDWR | O_CREAT | O_TRUNC))
  114. #   define HI_CLOSE(f) (close(f))
  115. #   define HI_FLUSH(f) (SUCCEED)
  116. #   define HI_READ(f, b, n)    (read((f), (b), (n)))
  117. #   define HI_WRITE(f, b, n)   (write((f), (b), (n)))
  118. #   define HI_SEEK(f, o)       (lseek((f), (off_t)(o), L_SET))
  119. #   define HI_SEEKEND(f) (lseek((f), (off_t)0, L_XTND))
  120. #   define HI_TELL(f)  (lseek((f), (off_t)0, L_INCR))
  121. #   define OPENERR(f)  (f < 0)
  122. #endif /* FILELIB == UNIXUNBUFIO */
  123.  
  124. #if (FILELIB == MACIO)
  125. /* using special routines to redirect to Mac Toolkit I/O */
  126. typedef short hdf_file_t;
  127. #   define HI_OPEN(x,y) mopen(x,y)
  128. #   define HI_CREATE(name) mopen(name, DFACC_CREATE)
  129. #   define HI_CLOSE(x) mclose(x)
  130. #   define HI_FLUSH(a) (SUCCEED)
  131. #   define HI_READ(a,b,c) mread(a, (char *) b, (int32) c)
  132. #   define HI_WRITE(a,b,c) mwrite(a, (char *) b, (int32) c)
  133. #   define HI_SEEK(x,y) mlseek(x, (int32 )y, 0)
  134. #   define HI_SEEKEND(x) mlseek(x, 0L, 2)
  135. #   define HI_TELL(x) mlseek(x,0L,1)
  136. #   define DF_OPENERR(f)    ((f) == -1)
  137. #   define OPENERR(f)  (f < 0)
  138. #endif /* FILELIB == MACIO */
  139.  
  140. #if (FILELIB == PCIO)
  141. /* using special PC functions to enable reading/writing large chunks */
  142. typedef FILE *hdf_file_t;
  143. #   define HI_OPEN(p, a)       (((a) & DFACC_WRITE) ? \
  144.                         fopen((p), "rb+") : fopen((p), "rb"))
  145. #   define HI_CREATE(p)        (fopen((p), "wb+"))
  146. /* Alias the HI_READ and HI_WRITE macros to functions which can handle */
  147. /*  32-bits of data to read/write */
  148. #   define HI_READ(f, b, n)    (((int32)(n) == HDfreadbig((b), (n), (f))) ? \
  149.                                 SUCCEED : FAIL)
  150. #   define HI_WRITE(f, b, n)   (((int32)(n) == HDfwritebig((b), (n), (f))) ? \
  151.                                 SUCCEED : FAIL)
  152. #   define HI_CLOSE(f) (fclose(f))
  153. #   define HI_FLUSH(f) (fflush(f))
  154. #   define HI_SEEK(f, o)       (fseek((f), (long)(o), SEEK_SET))
  155. #   define HI_SEEKEND(f) (fseek((f), (long)0, SEEK_END))
  156. #   define HI_TELL(f)  (ftell(f))
  157. #   define OPENERR(f)  ((f) == (FILE *)NULL)
  158. #endif /* FILELIB == PCIO */
  159.  
  160. #if (FILELIB == WINIO)
  161. /* using special MS Windows functions to enable reading/writing large chunks */
  162. typedef HFILE hdf_file_t;
  163. #   define HI_OPEN(p, a)       (((a) & DFACC_WRITE) ? \
  164.                         _lopen((p), READ_WRITE) : _lopen((p), READ))
  165. #   define HI_CREATE(p)        (_lcreat((p), 0))
  166. /* Alias the HI_READ and HI_WRITE macros to functions which can handle */
  167. /*  32-bits of data to read/write */
  168. #   define HI_READ(f, b, n)    (((int32)(n) == HDfreadbig((b), (n), (f))) ? \
  169.                                 SUCCEED : FAIL)
  170. #   define HI_WRITE(f, b, n)   (((int32)(n) == HDfwritebig((b), (n), (f))) ? \
  171.                                 SUCCEED : FAIL)
  172. #   define HI_CLOSE(f) (_lclose(f))
  173. #   define HI_FLUSH(f) (0)
  174. #   define HI_SEEK(f, o)       (_llseek((f), (long)(o), SEEK_SET))
  175. #   define HI_SEEKEND(f) (_llseek((f), (long)0, SEEK_END))
  176. #   define HI_TELL(f)  (_llseek((f),0l,SEEK_CUR))
  177. #   define OPENERR(f)  ((f) == (HFILE)HFILE_ERROR)
  178. #endif /* FILELIB == PCIO */
  179.  
  180. /* The internal structure used to keep track of the files opened: an
  181.    array of filerec_t structures, each has a linked list of ddblock_t.
  182.    Each ddblock_t struct points to an array of dd_t structs. */
  183.  
  184. /* record of each data decsriptor */
  185.  
  186. typedef struct dd_t {
  187.     uint16 tag;
  188.     uint16 ref;
  189.     int32 length;
  190.     int32 offset;
  191. } dd_t;
  192.  
  193. /* version tags */
  194. typedef struct version_t {
  195.     uint32 majorv;        /* major version number */
  196.     uint32 minorv;        /* minor version number */
  197.     uint32 release;        /* release number */
  198.     char string[81];        /* optional text description */
  199.     int16 modified;        /* indicates file was modified */
  200. } version_t;
  201.  
  202. /* record of a block of data descriptors, mirrors structure of a HDF file.  */
  203. typedef struct ddblock_t {
  204.     int16 ndds;                /* number of dd's in this block */
  205.     int32 nextoffset;          /* offset to the next ddblock in the file */
  206.     struct ddblock_t *next;    /* pointer to the next ddblock in memory */
  207.     struct ddblock_t *prev;    /* Pointer to previous ddblock. */
  208.     struct dd_t *ddlist;       /* pointer to array of dd's */
  209. } ddblock_t;
  210.  
  211. /* hashing information */
  212. #define HASH_MASK 0xff
  213. typedef struct tag_ref_list_str {
  214.   intn        tag;              /* tag for this element */
  215.   intn        ref;              /* ref for this element */
  216.   ddblock_t   *pblock;          /* ddblock this object is in */
  217.   int32       pidx;             /* this object's index into dd block */
  218.   struct tag_ref_list_str *next;  /* next one in the list */
  219.   VOIDP       my_data;          /* data other interfaces can add / query */
  220. } tag_ref_list, *tag_ref_list_ptr;
  221.  
  222. typedef struct filerec_t {
  223.     char *path;                 /* name of file */
  224.     hdf_file_t file;                /* either file descriptor or pointer */
  225.     intn access;                /* access mode */
  226.     intn refcount;              /* reference count / times opened */
  227.     struct ddblock_t *ddhead;   /* head of ddblock list */
  228.     struct ddblock_t *ddlast;   /* end of ddblock list */
  229.     uint16 maxref;              /* highest ref in this file */
  230.     intn attach;                /* number of access elts attached */
  231.  
  232.     /* version tag stuff */
  233.     intn version_set;
  234.     version_t version;        /* file version info */
  235.     
  236.     /* fast lookup of empty dd stuff */
  237.     struct ddblock_t *null_block; /* last block a NULL entry was found in */
  238.     int32             null_idx;   /* index into null_block of NULL entry */
  239.  
  240.     /* hash table stuff */
  241.     tag_ref_list_ptr hash[HASH_MASK + 1];  /* hashed table of tag / refs */
  242.  
  243. } filerec_t;
  244.  
  245. /* Each access element is associated with a tag/ref to keep track of
  246.    the dd it is pointing at.  To facilitate searching for next dd's,
  247.    instead of pointing directly to the dd, we point to the ddblock and
  248.    index into the ddlist of that ddblock. */
  249.  
  250. typedef struct accrec_t {
  251.     int32 file_id;              /* id of attached file */
  252.     struct ddblock_t *block;    /* ptr to ddblock that contains dd */
  253.     int32 idx;                  /* index of dd into *block */
  254.     int32 posn;                 /* seek position with respect to */
  255.                                 /* start of element */
  256.     int16 access;               /* access codes */
  257.     intn used;
  258.     int16 special;
  259.     VOIDP special_info;
  260.     int32 (**special_func)();
  261. } accrec_t;
  262.  
  263. /* a function table record for accessing special data elements.
  264.    special data elements of a key could be accessed through the list
  265.    of functions in array pointed to by tab. */
  266.  
  267. typedef struct functab_t {
  268.     int16 key;                 /* the key for this type of special elt */
  269.     int32 (**tab)();           /* table of accessing functions */
  270. } functab_t;
  271.  
  272. /* sizes of elements in a file.  This is necessary because
  273.    the size of variables need not be the same as in the file
  274.    (cannot use sizeof) */
  275.  
  276. #define DD_SZ 12               /* 2+2+4+4 */
  277. #define NDDS_SZ 2
  278. #define OFFSET_SZ 4
  279.  
  280. /* ndds (number of dd's in a block) default,
  281.    so user need not specify */
  282.  
  283. #ifndef DEF_NDDS
  284. #   define DEF_NDDS 16
  285. #endif
  286.  
  287. /* ndds minimum, to prevent excessive overhead of very small dd-blocks */
  288.  
  289. #ifndef MIN_NDDS
  290. #   define MIN_NDDS 4
  291. #endif
  292.  
  293. /* largest number that will fit into 16-bit word ref variable */
  294.  
  295. #define MAX_REF ((uint16)32767)
  296.  
  297. /* indices for special function table: the list of accessing functions
  298.    are ordered so that the functions are always in the same position
  299.    in a table */
  300.  
  301. #define SP_STREAD 0
  302. #define SP_STWRITE 1
  303. #define SP_SEEK 2
  304. #define SP_INQUIRE 3
  305. #define SP_READ 4
  306. #define SP_WRITE 5
  307. #define SP_END 6
  308.  
  309. /* macros */
  310.  
  311. /* each id, what ever the type, will be represented with a 32-bit word,
  312.    the most-significant 16 bits is a number unique for type.  The less-
  313.    significant 16 bits is an id unique to each type; in this, we use the
  314.    internal slot number. */
  315.  
  316. #define FIDTYPE 1
  317. #define AIDTYPE 2
  318. #define FSLOT2ID(s) ((((uint32)FIDTYPE & 0xffff) << 16) | ((s) & 0xffff))
  319. #define VALIDFID(i) (((((uint32)(i) >> 16) & 0xffff) == FIDTYPE) && \
  320.                     (((uint32)(i) & 0xffff) < MAX_FILE))
  321. #define FID2SLOT(i) (VALIDFID(i) ? (uint32)(i) & 0xffff : -1)
  322. #define FID2REC(i) ((VALIDFID(i) ? &(file_records[(uint32)(i) & 0xffff]) : NULL))
  323. #define ASLOT2ID(s) ((((uint32)AIDTYPE & 0xffff) << 16) | ((s) & 0xffff))
  324. #define VALIDAID(i) (((((uint32)(i) >> 16) & 0xffff) == AIDTYPE) && \
  325.                     (((uint32)(i) & 0xffff) < MAX_ACC) && \
  326.                     (access_records))
  327. #define AID2SLOT(i) (VALIDAID(i) ? (uint32)(i) & 0xffff : -1)
  328. #define AID2REC(i) ((VALIDAID(i) ? &(access_records[(uint32)(i) & 0xffff]) : NULL))
  329.  
  330. /* a tag is special if its tag belongs to a special set.  This test can be
  331.    just ((t)==SPECIAL1 || (t)==SPECIAL2), or a full blown function.
  332.    right now, no special tags yet */
  333.  
  334. #define BASETAG(t) (HDbase_tag(t))
  335. #define SPECIALTAG(t) (HDis_special_tag(t))
  336. #define MKSPECIALTAG(t) (HDmake_special_tag(t))
  337.  
  338. /* access records array.  defined in hfile.c */
  339.  
  340. extern accrec_t *access_records;
  341.  
  342. /* file records array.  defined in hfile.c */
  343.  
  344. extern filerec_t file_records[];
  345.  
  346. /* */
  347. #define FILE_NDDS(file_rec) ((file_rec)->ddlast->ndds)
  348.  
  349.  
  350. /* 
  351. ** Functions to get information of special elt from other access records.
  352. **   defined in hfile.c 
  353. ** These should really be HD... routines, but they are only used within
  354. **   the H-layer...
  355. */
  356.  
  357. extern int HIget_access_slot
  358.   PROTO((void));
  359.  
  360. extern int HIfind_dd
  361.   PROTO((uint16 look_tag, uint16 look_ref, ddblock_t **pblock, int32 *pidx));
  362.  
  363. extern int HInew_dd_block
  364.   PROTO((filerec_t *file_rec, int16 ndds, char *FUNC));
  365.  
  366. extern int HIupdate_dd
  367.   PROTO((filerec_t *file_rec, ddblock_t *block, int32 idx, char *FUNC));
  368.  
  369. extern VOIDP HIgetspinfo
  370.   PROTO((accrec_t *access_rec, uint16 tag, uint16 ref));
  371.  
  372. extern int HIlookup_dd
  373.   PROTO((filerec_t *file_rec, uint16 look_tag, uint16 look_ref,
  374.          ddblock_t **pblock, int32 *pidx));
  375.  
  376. extern int HIadd_hash_dd
  377.   PROTO((filerec_t *file_rec, uint16 look_tag, uint16 look_ref,
  378.          ddblock_t *pblock, int32 pidx));
  379.  
  380. extern int HIdel_hash_dd
  381.   PROTO((filerec_t *file_rec, uint16 look_tag, uint16 look_ref));
  382.  
  383. extern int32 HXIcloseAID
  384.     PROTO((accrec_t *access_rec));
  385.     
  386. #ifdef MAC
  387. extern hdf_file_t mopen
  388.     PROTO((char * filename, intn access));
  389.     
  390. extern int mlseek
  391.     PROTO((hdf_file_t rn, int32 n, intn m));
  392.  
  393. extern int32 mread
  394.     PROTO((hdf_file_t rn, char *buf, int32 n));
  395.  
  396. extern int32 mwrite
  397.     PROTO((hdf_file_t rn, char *buf, int32 n));
  398.  
  399. #endif
  400.  
  401. #endif /* HFILE_H */
  402.  
  403.  
  404.  
  405.