/******************************************************************************
*
*  NSSDC/CDF                            Header file for CDF distribution.
*
*  Version 2.7, 6-Jun-96, Hughes STX.
*
*  Modification history:
*
*   V1.0  20-Jul-91, J Love     Original version (for CDF V2.1).
*   V2.0  17-May-92, J Love     IBM PC & HP-UX port.  CDF V2.2.
*   V2.1  15-Sep-92, J Love     CDF V2.3 (shareable/zVar/NeXT).
*   V2.2  30-Oct-92, J Love     Added variable values display to CDFinquire.
*   V2.3  25-Jan-94, J Love     CDF V2.4.
*   V2.3a  8-Feb-94, J Love     DEC Alpha/OpenVMS port.
*   V2.4  13-Dec-94, J Love     CDF V2.5.
*   V2.5  20-Jan-95, J Love     `Trailer' function prototype.
*   V2.5a 19-Jan-95, J Love     IRIX 6.0 (64-bit).
*   V2.5b 28-Feb-95, J Love     `IsGraphChr' function prototype.  Solaris 2.3
*                               IDL i/f.  Pass `char' as `int'.
*   V2.6   6-Apr-95, J Love     POSIX.
*   V2.6a 18-Apr-95, J Love     More POSIX.
*   V2.6b  9-May-95, J Love     Virtual memory under Microsoft C 7.00.  Added
*				`EPOCHdataType'.  EPOCH styles.
*   V2.6c 13-Jun-95, J Love	EPOCH custom format.  Linux.
*   V2.6d 25-Aug-95, J Love	More system include files for UNIX.  Moved
*				`ASSIGNnotNULL' from `cdflib.h'.  CDFexport-
*				related changes.  More virtual memory for
*				Microsoft C 7.00.
*   V2.6e 13-Oct-95, J Love	AIX: `term.h' replaced by `select.h'.
*   V2.7   6-Jun-96, J Love	QNX port.
*
******************************************************************************/

#if !defined(CDFDISTh_INCLUDEd__)
#define CDFDISTh_INCLUDEd__

/*****************************************************************************
* Definitions for misguided compilers, etc.
*****************************************************************************/

#if defined(MSDOS)              /* Microsoft C. */
#define dos
#define MICROSOFTC
#if _MSC_VER == 600
#define MICROSOFTC_600
#endif
#if _MSC_VER == 700
#define MICROSOFTC_700
#endif
#endif

#if defined(__MSDOS__)          /* Borland C. */
#define dos
#define BORLANDC
#endif

#if defined(__SALFORD__)        /* Salford C. */
#define dos 1
#define SALFORDC 1
#endif

#if defined(dos)
#define IBMPC
#endif

#if defined(NeXT)
#define Mach
#endif

#if defined(AIX)
#define unix
#endif

#if defined(linux)
#define IBMPC
#endif

#if defined(__QNX__)
#define IBMPC
#define unix
#endif

#if defined(__alpha)
#if defined(__osf__)
#define alphaosf
#else
#if defined(vms)
#define alphavms
#if __D_FLOAT
#define alphavmsD
#else
#define alphavmsG
#endif
#else		/* "vms" not defined if "-W ansi89" used. */
#define posixSHELL
#define posixSHELLalpha
#if __D_FLOAT
#define posixSHELLalphaD
#else
#define posixSHELLalphaG
#endif
#endif
#endif
#endif

#if defined(vax)
#if defined(vms)
#define vaxvms
#else           /* "vms" not defined if "-W ansi89" used. */
#define posixSHELL
#define posixSHELLvax
#endif
#endif

#if defined(macintosh)          /* Macintosh MPW */
#define MPW_C
#define mac
#endif

#if defined(THINK_C)            /* Macintosh Think C */
#define mac
#endif

/******************************************************************************
* Determine variable argument list type - `stdarg' or `varargs'.  Currently,
* SunOS doesn't have `stdarg.h'.
******************************************************************************/

#if defined(vms)
#define STDARG
#endif

#if defined(dos)
#define STDARG
#endif

#if defined(unix) || defined(posixSHELL)
#if defined(__STDC__)
#define STDARG
#else
#define VARARGS
#endif
#endif

#if defined(mac)
#define STDARG
#endif

/******************************************************************************
* System include files.
******************************************************************************/

#if defined(vms)
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <math.h>
#include <unixio.h>
#include <types.h>
#include <descrip.h>
#include <rmsdef.h>
#include <climsgdef.h>
#include <smgdef.h>
#include <ssdef.h>
#include <stat.h>
#include <lib$routines.h>
#if defined(VARARGS)
#include <varargs.h>
#else
#include <stdarg.h>
#endif
#endif

#if defined(unix) || defined(posixSHELL)
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <math.h>
#include <pwd.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#if defined(VARARGS)
#include <varargs.h>
#else
#include <stdarg.h>
#endif
#if defined (AIX) || defined(__QNX__)
#include <sys/select.h>
#endif
#if defined(Mach)
#include <sys/dir.h>
#else
#include <dirent.h>
#endif
#endif

#if defined(dos)
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <math.h>
#if defined(BORLANDC)
#include <dir.h>
#endif
#if !defined(SALFORDC)
#include <dos.h>
#endif
#include <conio.h>
#include <sys\types.h>
#include <sys\stat.h>
#if defined(VARARGS)
#include <varargs.h>
#else
#include <stdarg.h>
#endif
#endif

#if defined(mac)
#if defined(MPW_C)
#include <StdLib.h>
#include <StdIO.h>
#include <String.h>
#include <CType.h>
#include <Time.h>
#include <Math.h>
#include <Types.h>
#if defined(STDARG)
#include <StdArg.h>
#else
#endif
#include <Files.h>
#include <Quickdraw.h>
#include <OSEvents.h>
#include <Events.h>
#include <Fonts.h>
#include <Windows.h>
#include <Menus.h>
#include <Dialogs.h>
#include <Textedit.h>
#include <Desk.h>
#include <Resources.h>
#include <StandardFile.h>
#include <ToolUtils.h>
#endif
#if defined(THINK_C)
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <math.h>
#include <stat.h>
#if defined(STDARG)
#include <stdarg.h>
#else
#endif
#endif
#endif

/******************************************************************************
* If using the POSIX-compliant C comiler on an HP, redefine the macros that
* have been undefined by the system include files.
******************************************************************************/

#if defined(HP)
#if defined(_POSIX_SOURCE)
#define unix
#define hpux
#endif
#endif

/******************************************************************************
* Typedefs.
******************************************************************************/

#if defined(alphaosf) || defined(IRIX64bit)
   /***************************************************************************
   * `long' is 64 bits on a DEC Alpha/OSF1 and an SGi/IRIX 6.x using the `-64'
   * option (64-bit objects).  `int' is 32 bits in these cases.
   ***************************************************************************/
   typedef int Int32;
   typedef unsigned int uInt32;
#else
   typedef long Int32;
   typedef unsigned long uInt32;
#endif

typedef short Int16;
typedef unsigned short uInt16;

#if defined(AIX) || defined(dos) || defined(sgi) || defined(__QNX__)
   /***************************************************************************
   * `char' is unsigned by default under AIX, MS-DOS, IRIX, and QNX...
   ***************************************************************************/
   typedef signed char Schar;
#else
   /***************************************************************************
   * ...and signed by default everywhere else.
   ***************************************************************************/
   typedef char Schar;
#endif

typedef unsigned char Uchar;

#if !defined(mac)
  /****************************************************************************
  * `Byte' is already defined in `types.h' on a Macintosh (MPW C & Think C).
  ****************************************************************************/
  typedef unsigned char Byte;
#endif

typedef int Logical;

/******************************************************************************
* Non-system include files.
******************************************************************************/

#include "cdf.h"
#include "config.h"

/******************************************************************************
* Configuration-dependent system include files.
******************************************************************************/

#if defined(MICROSOFTC_700)
#if INCLUDEvMEMORY
#include <vmemory.h>
#endif
#endif

/*****************************************************************************
* Floating-point encodings.
*   1..........Sun, SGi, IBM-RS, HP, NeXT, Macintosh
*   2..........DECstation, IBM-PC, DEC Alpha (OSF/1)
*   3..........VAX, DEC Alpha (OpenVMS - D_FLOAT)
*   4..........DEC Alpha (OpenVMS - G_FLOAT)
*****************************************************************************/

#if defined(sun)||defined(MIPSEB)||defined(IBMRS)||defined(HP)||defined(NeXT)||defined(mac)
#define FP1cpu
#endif

#if defined(MIPSEL)||defined(IBMPC)||defined(alphaosf)
#define FP2cpu
#endif

#if defined(vax)||defined(alphavmsD)||defined(posixSHELLalphaD)
#define FP3cpu
#endif

#if defined(alphavmsG)||defined(posixSHELLalphaG)
#define FP4cpu
#endif

/*****************************************************************************
* VA_START.
*     This is necessary because of some HP9000s which don't like it when
* `va_start' is passed an element of a structure.  It is apparently related
* to the version of HP-UX being used (other HP9000s are happy with the
* structure element being passed).
*****************************************************************************/

#if defined(hpux)
#define VA_START(ap) { \
va_list _apT_; \
va_start (_apT_); \
ap = _apT_; \
}
#else
#define VA_START(ap) va_start(ap)
#endif

/*****************************************************************************
* Sizes/limits.
*****************************************************************************/

#define DU_MAX_PATH_LEN 255
#define MAX_aMODE_LEN   3

#if defined(BORLANDC) || defined(MICROSOFTC)
#define LIMITof64K      1
#else
#define LIMITof64K      0
#endif

/*****************************************************************************
* ASCII characters.
*   Beware when using NL and CR with MPW C.  Their values are reversed from
* all of the other platforms/compilers.
*****************************************************************************/

#define Nl      '\n'            /* Newline. */
#define Cr      '\r'            /* Carriage return. */
#define Bs      '\b'            /* Backspace. */
#define Ht	'\t'		/* Horizontal tab. */
#define NUL     '\0'            /* Null character. */

/*****************************************************************************
* C Run-time Library redirections.
*****************************************************************************/

#if defined(dos)
#define cfree free
#endif

#if defined(unix)
#if (defined(sun) && !defined(SOLARIS)) || defined(sgi)
#define memmove(dst,src,nbytes) bcopy(src,dst,nbytes)
#endif
#endif

/******************************************************************************
* CDFstatus checking macros.
******************************************************************************/

#define StatusOK(status) ((CDFstatus) status > CDF_WARN)
#define StatusBAD(status) ((CDFstatus) status < CDF_WARN)
#define StatusERROR(status) ((CDFstatus) status < CDF_WARN)
#define StatusWARN(status) \
(CDF_WARN < (CDFstatus) status && (CDFstatus) status < CDF_OK)
#define StatusINFO(status) ((CDFstatus) status > CDF_OK)

/*****************************************************************************
* TRUE/FALSE (for where they don't exist).
*****************************************************************************/

#if !defined(TRUE)
#define TRUE 1
#endif

#if !defined(FALSE)
#define FALSE 0
#endif

/*****************************************************************************
* Bit macros.
*****************************************************************************/

#define BITSET(a,bit) ((a & (1 << bit)) ? TRUE : FALSE)
#define BITCLR(a,bit) ((!(a & (1 << bit))) ? TRUE : FALSE)

#define SETBIT(a,bitN) a = a | (1 << bitN)
#define CLRBIT(a,bitN) a = a & ~(1 << bitN)
#define FLPBIT(a,bitN) a = a ^ (1 << bitN)

/*****************************************************************************
* Minimum/Maximum macros.
*****************************************************************************/

#if !defined(MINIMUM)
#define MINIMUM(a,b) ((a) < (b) ? (a) : (b))
#endif

#if !defined(MAXIMUM)
#define MAXIMUM(a,b) ((a) > (b) ? (a) : (b))
#endif

/******************************************************************************
* BOO.
*   Choose one of two things based on a boolean value.
******************************************************************************/

#define BOO(b,t,f) (b ? t : f)

/******************************************************************************
* NULstring.
*    Macro to check if a null-string (length of zero).
******************************************************************************/

#define NULstring(string) (string[0] == NUL)

/******************************************************************************
* MakeNUL.
*    Macro which makes a string a null-string (length of 0).
******************************************************************************/

#define MakeNUL(string) strcpy(string,"")

/******************************************************************************
* ROWmajor/COLmajor.
******************************************************************************/

#define ROWmajor(majority) (majority == ROW_MAJOR)
#define COLmajor(majority) (majority == COL_MAJOR)

/******************************************************************************
* STRINGdataType.
*    Macro to check if a character string data type.
******************************************************************************/

#define STRINGdataType(dataType) \
(dataType == CDF_CHAR || dataType == CDF_UCHAR)

/******************************************************************************
* EPOCHdataType.
*    Macro to check if an EPOCH data type.
******************************************************************************/

#define EPOCHdataType(dataType) (dataType == CDF_EPOCH)

/******************************************************************************
* EofS (End of String).
*   Macro which expands to a pointer to the NUL character at the end of a
* character string.
******************************************************************************/

#define EofS(string) &string[strlen(string)]

/******************************************************************************
* ASSIGNnotNULL.
*     Dereference a pointer and assign a value if the pointer is not NULL.
* This is used in those cases where the caller of a routine has the option
* of passing in a NULL pointer for those parameters for which a returned
* value is not desired.
******************************************************************************/

#define ASSIGNnotNULL(ptr,value) if (ptr != NULL) *ptr = value;

/******************************************************************************
* Entry type constants/macros.
******************************************************************************/

#define gENTRYt         1
#define rENTRYt         2
#define zENTRYt         3

#define E3(type,itemG,itemR,itemZ) \
(type == gENTRYt ? itemG : \
 (type == rENTRYt ? itemR : \
  (type == zENTRYt ? itemZ : 0)))

#define E3p(E,g,r,z) \
(E == g ? gENTRYt : (E == r ? rENTRYt : (E == z ? zENTRYt : 0)))

#define ONEof3(I,a,b,c) ((I == a) || (I == b) || (I == c))

/******************************************************************************
* Gq.
******************************************************************************/

#define Gq(G,g,v) (G ? g : v)

/******************************************************************************
* Zq.
******************************************************************************/

#define Zq(Z,z,r) (Z ? z : r)

/******************************************************************************
* TOObigIBMpc.  Check for overflow on IBM PCs (running MS-DOS) because of 64Kb
* limit on segment sizes.
******************************************************************************/

#define TOObigIBMpc(size) ((long) size > 65535L)

/******************************************************************************
* ARRAYtoARRAY.
******************************************************************************/

#define ARRAYtoARRAY(dst,src,count) \
{int _i_; for (_i_ = 0; _i_ < count; _i_++) dst[_i_] = src[_i_];}

/******************************************************************************
* vSTATS structures.
******************************************************************************/

typedef struct vSTATSstruct {
  int maxBuffers;       /* Maximum number of cache buffers to be used. */
  int nBuffers;         /* Actual number of cache buffers being used. */
  long nV_reads;        /* Number of calls to `V_read'. */
  long nV_writes;       /* Number of calls to `V_write'. */
  long nBlockReads;     /* Number of file blocks which have been read. */
  long nBlockWrites;    /* Number of file blocks which have been written. */
  long nPageIns;        /* Number of blocks paged in from the file. */
  long nPageOuts;       /* Number of blocks paged out to the file. */
} vSTATS;

/******************************************************************************
* Function prototypes for routines contained in the CDF library but also used
* by the toolkit/toolbox/IDL.
******************************************************************************/

STATICforIDL int FreeMemory PROTOARGs((
  void *ptr, void (*fatalFnc) PROTOARGs((char *msg))
));
STATICforIDL void *CallocateMemory PROTOARGs((
  size_t nObjects, size_t objSize, void (*fatalFnc) PROTOARGs((char *msg))
));
STATICforIDL void *AllocateMemory PROTOARGs((
  size_t nBytes, void (*fatalFnc) PROTOARGs((char *msg))
));
STATICforIDL void *ReallocateMemory PROTOARGs((
  void *ptr, size_t nBytes, void (*fatalFnc) PROTOARGs((char *msg))
));
STATICforIDL char *strcpyX PROTOARGs((char *dst, char *src, size_t max));
STATICforIDL char *strcatX PROTOARGs((char *dst, char *src, size_t max));
STATICforIDL char *catchrX PROTOARGs((char *dst, int src, size_t max));
STATICforIDL char *prependX PROTOARGs((char *dst, char *src, size_t max));
STATICforIDL double parseEPOCH1 PROTOARGs((char *inString));
STATICforIDL double parseEPOCH2 PROTOARGs((char *inString));
STATICforIDL double parseEPOCH3 PROTOARGs((char *inString));
STATICforIDL int CDFelemSize PROTOARGs((long));
STATICforIDL void ClearBytes PROTOARGs((
  void *buffer, int firstByte, int lastByte
));
STATICforIDL Logical IsReg PROTOARGs((char *));
STATICforIDL void ExpandPath PROTOARGs((
  char *shortPath, char longPath[DU_MAX_PATH_LEN]
));
STATICforIDL void TimeStamp PROTOARGs((char *));
STATICforIDL int strcmpITB PROTOARGs((char *, char *));
STATICforIDL Logical NegativeZeroReal8 PROTOARGs((double *value));
STATICforIDL Logical NegativeZeroReal4 PROTOARGs((float *value));
STATICforIDL void Uppercase PROTOARGs((char *string));
STATICforIDL void Lowercase PROTOARGs((char *string));
STATICforIDL void ROWtoCOL PROTOARGs((
  void *iBuffer, void *oBuffer, long numDims, long dimSizes[], long nValueBytes
));
STATICforIDL void COLtoROW PROTOARGs((
  void *iBuffer, void *oBuffer, long numDims, long dimSizes[], long nValueBytes
));
STATICforIDL void INCRindicesROW PROTOARGs((
  long numDims, long *dimSizes, long *indices
));
STATICforIDL void INCRindicesCOL PROTOARGs((
  long numDims, long *dimSizes, long *indices
));
STATICforIDL Logical Trailer PROTOARGs((char *string, char *trailer));
STATICforIDL int ToLowerChr PROTOARGs((int c));
STATICforIDL int ToUpperChr PROTOARGs((int c));
STATICforIDL int IsPrintChr PROTOARGs((int c));
STATICforIDL int IsUpperChr PROTOARGs((int c));
STATICforIDL int IsLowerChr PROTOARGs((int c));
STATICforIDL int IsAlphaChr PROTOARGs((int c));
STATICforIDL int IsDigitChr PROTOARGs((int c));
STATICforIDL int IsSpaceChr PROTOARGs((int c));
STATICforIDL int IsGraphChr PROTOARGs((int c));
STATICforIDL CDFid Int32toCDFid PROTOARGs((Int32 id));
STATICforIDL Int32 CDFidToInt32 PROTOARGs((CDFid id));
STATICforIDL Logical sX PROTOARGs((CDFstatus cStatus, CDFstatus *pStatus));

#if defined(mac)
STATICforIDL Logical MacDirSpecified PROTOARGs((
  char *path, short *vRefNum, long *dirID
));
STATICforIDL char *PstrcpyX PROTOARGs((char *dst, char *src, size_t max));
STATICforIDL char *PstrcatX PROTOARGs((char *dst, char *src, size_t max));
STATICforIDL char *PprependX PROTOARGs((char *dst, char *src, size_t max));
#if defined(MPW_C)
STATICforIDL unsigned char *CtoPstr PROTOARGs((char *string));
STATICforIDL char *PtoCstr PROTOARGs((unsigned char *string));
#endif
#endif

/*****************************************************************************/

#endif
