/*****************************************************************************
*                                                                            *
*      COMMON OCEANOGRAPHIC DATA ACCESS SYSTEM (CODAS)                       *
*                                                                            *
*      WRITTEN BY:  RAMON CABRERA                                            *
*                   JOINT INSTITUTE FOR MARINE AND ATMOSPHERIC RESEARCH      *
*                   1000 POPE ROAD  MSB 312                                  *
*                   HONOLULU, HI 96822                                       *
*                                                                            *
*      VERSION:     3.10                                                     *
*                                                                            *
*      DATE:        APRIL 1989, AUGUST 1995                                  *
*                                                                            *
*****************************************************************************/
/*

       FILE:  DBEXT.H

              CONSTANT, TYPE, & STRUCTURE DECLARATIONS
              EXTERNAL TO THE DATABASE SYSTEM

              The constants, types, and structures in this file are
              generally useful and may be included by producers and
              users in their database loading and retrieval programs.

*/

#ifndef dbext_included
#define dbext_included

#ifndef dbhost_included
#include "dbhost.h"        /* HOST_BYTES_PER_WORD, TRUNCATION_TOWARD_ZERO */
#endif
/*---------------------------------------------------------------------------*/
/*
       BASIC TYPES FOR SYSTEMS WITH 4-BYTE WORDS
*/
#if (HOST_BYTES_PER_WORD == 4)
typedef short SHORT;
typedef unsigned short USHORT;
#define MAXINT 0x7FFFFFFF
#define MAXUINT 0xFFFFFFFF
#else
/*
       BASIC TYPES FOR SYSTEMS WITH 2-BYTE WORDS
*/
#if (HOST_BYTES_PER_WORD == 2)
typedef int SHORT;
typedef unsigned int USHORT;
#define MAXINT 0x7FFF
#define MAXUINT 0xFFFF
#endif
#endif

typedef char CHAR, BYTE;
typedef unsigned char UBYTE;
typedef long LONG;
typedef unsigned long ULONG;
typedef float FLOAT;
typedef double DOUBLE;
typedef struct
{
   FLOAT RE, IM;
} COMPLEX;
/*
       CODES OF VALUE TYPES
*/
#define BYTE_VALUE_CODE     0
#define UBYTE_VALUE_CODE    1
#define CHAR_VALUE_CODE     2
#define SHORT_VALUE_CODE    3
#define USHORT_VALUE_CODE   4
#define LONG_VALUE_CODE     5
#define ULONG_VALUE_CODE    6
#define FLOAT_VALUE_CODE    7
#define DOUBLE_VALUE_CODE   8
#define COMPLEX_VALUE_CODE  9
#define TEXT_VALUE_CODE    10
#define STRUCT_VALUE_CODE  11
/*
       EXTREME VALUES OF BASIC TYPES
*/
#define MAXUBYTE     (UBYTE)    0xFF
#define MINUBYTE     (UBYTE)    0x00
#define MAXCHAR      (CHAR)     0x7F
#define MAXBYTE      (BYTE)     0x7F
#define MINCHAR      (CHAR)    -0x80
#define MINBYTE      (BYTE)    -0x80
#define MAXUSHORT    (USHORT)   0xFFFF
#define MINUSHORT    (USHORT)   0x0000
#define MAXSHORT     (SHORT)    0x7FFF
#define MINSHORT     (SHORT)   -0x8000
#define MAXULONG     (ULONG)    0xFFFFFFFFL
#define MINULONG     (ULONG)    0x00000000L
#define MAXLONG      (LONG)     0x7FFFFFFFL
#define MINLONG      (LONG)    -0x80000000L
#define MINFLOAT     (FLOAT)    1.0E-38
#ifdef MAXFLOAT                      /* Sun <math.h> has its own MAXFLOAT */
#undef MAXFLOAT
#endif
#define MAXFLOAT     (FLOAT)    1.0E38
#define MAXDOUBLE    (DOUBLE)   1.0E38 /* adjusted for DECVAX D-floating format */

#define BADBYTE      MAXBYTE
#define BADCHAR      MAXCHAR
#define BADUBYTE     MAXUBYTE
#define BADSHORT     MAXSHORT
#define BADUSHORT    MAXUSHORT
#define BADINT       MAXINT
#define BADUINT      MAXUINT
#define BADLONG      MAXLONG
#define BADULONG     MAXULONG
#define BADFLOAT     MAXFLOAT
#define BADDOUBLE    MAXDOUBLE
#define ADJ_BADFLOAT (FLOAT) 9.99999E37 /* need to adjust BADFLOAT for roundoff error */
/*
       SPECIAL DATA STRUCTURES
*/
typedef struct
{
   CHAR instrument_name[8], unused[24];
} DATASET_ID_TYPE;

typedef struct
{
   CHAR country[2], institution[2], platform[2], instrument[4], unused[22];
} PRODUCER_ID_TYPE;

typedef struct
{
   USHORT year, month, day, hour, minute, second;
} YMDHMS_TIME_TYPE;

typedef struct
{
   SHORT degree, minute, second, hundredth;
} DMSH_POSITION_TYPE;

typedef struct
{
   SHORT min_depth, max_depth;
} DEPTH_RANGE_TYPE;

typedef CHAR FILE_NAME_TYPE[160];

/*
       STRUCTURE DEFINITIONS & FORMATS
*/
typedef struct
{
    CHAR      name[20],
              units[12];
    SHORT     value_type,
              count;
    CHAR      format_ptr[4];
} STRUCT_DEF_ELEM_TYPE;

#define STRUCT_DEF_ELEM_SIZE sizeof(STRUCT_DEF_ELEM_TYPE)
#define EXPECTED_STRUCT_DEF_ELEM_SIZE 40

typedef struct
{
    CHAR      name[20];
    LONG      nelem;
    CHAR      padding[16];
} STRUCT_DEF_HDR_TYPE;

#define STRUCT_DEF_HDR_SIZE sizeof(STRUCT_DEF_HDR_TYPE)
#define EXPECTED_STRUCT_DEF_HDR_SIZE 40
#define LINK_PTR  padding[0]   /* for use during access */
#define ORDER_PTR padding[4]
#define NPRINT    padding[8]

typedef union
{
    STRUCT_DEF_HDR_TYPE  hdr;
    STRUCT_DEF_ELEM_TYPE elem;
} STRUCT_DEF_ENTRY_TYPE;

#define STRUCT_DEF_ENTRY_SIZE sizeof(STRUCT_DEF_ENTRY_TYPE)

typedef struct
{
   char *fmt_string;
   int  pause;
   unsigned int (*print_function)();
} FORMAT_SPEC;

typedef struct
{
   char *name;
   int nelem;
} FORMAT_HDR_TYPE;

typedef struct
{
   char *name;
   FORMAT_SPEC *format_ptr;
} FORMAT_ELEM_TYPE;

typedef union
{
   FORMAT_HDR_TYPE hdr;
   FORMAT_ELEM_TYPE elem;
} FORMAT_ENTRY_TYPE;
/*
       BIT MASKS
*/
#define BIT0  0x1L
#define BIT1  0x2L
#define BIT2  0x4L
#define BIT3  0x8L
#define BIT4  0x10L
#define BIT5  0x20L
#define BIT6  0x40L
#define BIT7  0x80L
#define BIT8  0x100L
#define BIT9  0x200L
#define BIT10 0x400L
#define BIT11 0x800L
#define BIT12 0x1000L
#define BIT13 0x2000L
#define BIT14 0x4000L
#define BIT15 0x8000L
#define BIT16 0x10000L
#define BIT17 0x20000L
#define BIT18 0x40000L
#define BIT19 0x80000L
#define BIT20 0x100000L
#define BIT21 0x200000L
#define BIT22 0x400000L
#define BIT23 0x800000L
#define BIT24 0x1000000L
#define BIT25 0x2000000L
#define BIT26 0x4000000L
#define BIT27 0x8000000L
#define BIT28 0x10000000L
#define BIT29 0x20000000L
#define BIT30 0x40000000L
#define BIT31 0x80000000L
/*
       USEFUL MACROS AND CONSTANTS
*/
#ifndef PI
#define PI (3.141593)
#endif
#define TWOPI (6.283186)
#define deg_to_rad(x)        ((x)*(0.01745329))
#define rad_to_deg(x)        ((x)*(57.29578))
#define min_val(x, y)        ((x)<(y)?(x):(y))
#define max_val(x, y)        ((x)>(y)?(x):(y))
#define ordered(x1, x2, x3)  ((x1)<=(x2)<=(x3))
#define sign_of(x)           ((x)>=0?(1):(-1))
#define abs_val(x)           ((x)>=0?(x):(-(x)))
/* Divide and modulo operations that return results for a positive remainder */
#define div_pr(n,d)          ((n)%(d)<0?(n)/(d)-1:(n)/(d))
#define mod_pr(n,d)          ((n)%(d)<0?(n)%(d)+(d):(n)%(d))
/* Divide and modulo operations that return results for a negative remainder */
#define div_nr(n,d)          ((((n)/(d)<0)&&((n)%(d)>0))?(n)/(d)+1:(n)/(d))
#define mod_nr(n,d)          ((((n)/(d)<0)&&((n)%(d)>0))?(n)%(d)-(d):(n)%(d))
#if (TRUNCATION_TOWARD_0)
#define round(x)             ((x)>=0?(x)+0.4999999:(x)-0.4999999)
#else
#define round(x)             ((x)+0.4999999)
#endif
/*   Try taking this out--it causes trouble
#if (HOST_BYTES_PER_WORD == 4)
#define labs abs
#endif
*/

#define to_hundredths_sec_s(x) ((x)&0x8000?(x)^0x8000:(x)*100)

#define DIR_TIME_IN_SECONDS    0
#define DIR_TIME_IN_HUNDREDTHS 2
/*
   SCREEN DISPLAY MODES
*/
#define GO    0      /* print succeeding screen line immediately */
#define PAUSE 1      /* wait till user presses <enter> key       */
/*
       DATABASE DEFINITIONS
*/
#define OPTIMUM_PROFILES_PER_BLOCK  400
/*
       SEARCH CODES
*/
#define TIME_SEARCH          1
#define BLOCK_PROFILE_SEARCH 2
/*
       MEMORY MODE CODES
*/
#define DIR_ON_DISK   0
#define DIR_IN_MEMORY 1
/*
       VARIABLE ACCESS CODES
*/
#define UNUSED                  0      /*jr+*/
#define BLOCK_VAR_ACCESS_CODE   1
#define PROFILE_VAR_ACCESS_CODE 2
/*
       DATABASE ACCESS MODES
*/
#define READ_ONLY        0
#define READ_WRITE       1
/*
       DATA TYPES IN DATA BASE
*/
#define DATASET_ID              100
#define PRODUCER_ID             101
#define TIME                    102
#define POSITION                103
#define DATA_MASK               104
#define DATA_PROC_MASK          105
#define DEPTH_RANGE             106

#define DATABASE_VERSION        200
#define PRODUCER_HOST           201
#define DATABASE_NUMBER         202
#define BLOCK_PROFILE_INDEX     203

#define BLOCK_DIR_FILE          300
#define BLOCK_FILE              301
#define PRODUCER_DEF_FILE       302
#define TEMP_DATA_FILE          303
#define BLOCK_DIR_HDR           304
#define BLOCK_DIR               305
#define BLOCK_HDR               306
#define DATA_LIST               307
#define PROFILE_DIR             308
#define DATA_DIR                309
#define PROFILE_DATA            310
#define PRODUCER_DEF            311
#define STRUCTURE_DEF           312      /*jr+*/
#define STRUCTURE_FORMATS       313
#define BLOCK_FTR               314
/*
       SYSTEM IO ERROR CODES

#define EOF                    -1            already defined in STDIO
*/
#define UNABLE_TO_OPEN         -2
#define FILE_DOES_NOT_EXIST    -3
#define FILE_ALREADY_EXISTS    -4
#define FILE_PROTECTION_ERROR  -5
#define READ_ERROR             -6
#define WRITE_ERROR            -7
#define SEEK_ERROR             -8
#define CLOSE_ERROR            -9
#define INSUFFICIENT_MEMORY    -10
/*
       ACCESS ERROR CODES
*/
#define INVALID_DATABASE_NUMBER         1
#define DB_IS_NOT_OPEN                  2
#define DB_ALREADY_OPEN                 3
#define NO_OPEN_FOR_WRITE               4
#define NO_OPEN_FOR_CREATE              5
#define OPEN_FOR_CREATE                 6
#define DATABASE_IS_EMPTY               7
#define INCORRECT_DATABASE_VERSION      8
#define UNEXPECTED_STRUCTURE_SIZE       9

#define NO_BLOCK_IS_OPEN               11
#define NO_PROFILE_IS_OPEN             12
#define NO_NEW_BLOCK_IS_OPEN           13
#define NO_NEW_PROFILE_IS_OPEN         14
#define NEW_BLOCK_IS_OPEN              15
#define NEW_PROFILE_IS_OPEN            16

#define BLOCK_DIR_IS_FULL              21
#define BLOCK_IS_FULL                  22
#define INSUFFICIENT_SPACE             23

#define BLOCK_DIR_IS_EMPTY             26
#define PROFILE_DIR_IS_EMPTY           27
#define DATA_LIST_IS_EMPTY             28

#define INVALID_TYPE_REQUEST           31
#define INVALID_TIME                   32
#define INVALID_POSITION               33
#define INVALID_VALUE_TYPE             34
#define INVALID_ACCESS_TYPE            35
#define NO_SUCH_BLOCK_PROFILE          36
#define INVALID_BUFFER_SIZE            37
#define INVALID_DIRECTORY_TYPE         38
#define MAXIMUM_SIZE_EXCEEDED          39

#define SEARCH_BEYOND_END              41
#define SEARCH_BEFORE_BEGINNING        42
#define SEARCH_ERROR                   43 /* 1999/01/20 Pierre Jaccard */

#define ZERO_SCALE                     51
#define INVALID_SCALE                  52 /* 1999/01/20 Pierre Jaccard */

#define PATHNAME_TOO_LONG              61
#define DBNAME_TOO_LONG                62

#define UNDEFINED_STRUCTURE            70
#define UNKNOWN_HOST_ERROR             71
#define INCOMPATIBLE_HOST_ERROR        72
#define STRUCTURE_NOT_PRESENT          73
#define NO_SUCH_ELEMENT                74

#define FORMAT_ERROR                   80

#define INVALID_NUMBER                 98 /* 1999/01/20 Pierre Jaccard */
#define CONVERT_ERROR                  99
#endif /* ifndef dbext_included */
