/*++++++++++++++
.IDENTIFICATION vtype.h
.LANGUAGE       C
.AUTHOR         Francois Ochsenbein [CDS]
.ENVIRONMENT    
.KEYWORDS       
.VERSION  1.0   29-Oct-1996
.VERSION  1.1   29-Aug-1997: TEXT attributes
.VERSION  1.2   29-Mar-1999: VOP_xxx Operators
.VERSION  1.3   11-Mar-2002: VM_MJD in dbType
.VERSION  1.4   01-Apr-2007: Require math.h
.VERSION  1.5   09-Feb-2010: VM_fixed (dbtype)
.COMMENTS       Definitions of Datatypes in VizieR
---------------*/

#ifndef VTYPE_DEF
#define VTYPE_DEF	0
#ifndef _ARGS
#ifdef __STDC__
#define _ARGS(A)      A       /* ANSI */
#else
#define _ARGS(A)      ()      /* Traditional */
#define const
#endif
#endif

#ifndef int4
#define int4 int
#endif

#ifndef int8
#define int8 long long
#endif

/* Use the "osmemory" routines */
#ifdef OSM
#ifndef malloc
#define malloc	osmalloc
#define free	osmfree
#define realloc	osmexpand
#define strdup	osmdup
#include <stdlib.h>
#endif
#endif

/*============================================================================
		Possible values as a single parameter
 *============================================================================*/

typedef union {
    double f ;
    long   l ;
    int4  i4 ;
    int8  i8 ;
    int i2[2] ;
    int   i ;
    char *s;
    char *pgm[sizeof(double)/sizeof(char *)] ;
    char m[sizeof(double)] ;
    void *b ;
    int (*a)() ;	/* Action */
} VALUE ;

/*============================================================================
		VizieR Special Constants
 *============================================================================*/
#ifndef I4NULL
#define I4NULL		0x80000000
#define I8NULL		0x8000000000000000    /*0x8000000000*/
#include <math.h>		/* isnan */
#define isI4NULL(v)	((v)==I4NULL)
#define isI8NULL(v)	((v)==I8NULL)
#define isNaN(af)       isnan(*(af)) 	/* in math.h */
#define toNaN(af)	*((long long*)(af)) = (-1LL)
/* Old definitions -- they should work too!
#define isNaN(af)	(*af) != (*af)
#define toNaN(af)       *(af) = -0./0.	-- The undef -- but not all bits to 1
*/
#endif

/* From 1970 to 2000 time */
#ifndef s2000
#define s2000 (10957*86400)
#endif

/* Length of one month in sec (exactly 1/12 of a year) */
#ifndef JMONTH
#define JMONTH	(36525*72)
#endif

/*============================================================================
		VizieR Data types definitions 
 *============================================================================*/
/* A complete definition is an 8-bit number.
   It contains basic data-type (0-7), 
   interpretation considerations,
   and unit considerations.
   Basic: note that length is required when isVstring is true.
*/
#define V_INTEGER    0     /* Integer (I*4)        */
#define V_DOUBLE     1     /* R*8                  */
#define V_FLOAT      1     /* R*8                  */
#define V_SPECIAL    2     /* e.g. DB-Date/Time    */
#define V_ACTION     3     /* Routine              */
#define V_STRING     4     /* Character String     */
#define V_BLOB	     5     /* Binary Large OBject  */
#define V_BIGINT     6     /* Integer (I*8)        */
#define isVstring(t)	   ((t)&V_STRING && t != V_BIGINT)
#define isVfloat(t)	   (((t)&7)==V_FLOAT)

/* Interpretation possibilities (normally type)    */
#define VM_INTERPRET   070 /* Interpretations----- */
#define VM_SEXAGESIMAL 010 /* Sexagesimal allowed  */
#define VM_DATIME      020 /* Date+Time   allowed  */
#define VM_ROMAN       030 /* Roman numeral        */
#define VM_ENUM        040 /* or comma-separated   */
#define VM_TEXT        060 /* Text in TeX/HTML...  */

/* Units considerations (recover interpret =dbtype */
#define VM_mas         010 /* Angles as mas        */
#define VM_fixed       010 /* Fixed-length string  */
#define VM_left        020 /* No leading blank(str)*/
#define VM_s           030 /* Time in seconds      */
#define VM_MJD         040 /* =JD-2400000.5        */
#define VM_s2000       050 /* Seconds since 2000   */
#define VM_s1980       060 /* Seconds since 1980   */
#define VM_s1970       070 /* Seconds since 1970   */

#define VM_OPTION     0300 /* Option ------------- */

/* When VM_INTERPRET = 000 */
#define VM_UPPER      0100 /* Uppercase string     */
#define VM_LOWER      0200 /* Lowercase string     */
#define VM_PERIODIC   0300 /* Cyclic value         */

/* When VM_INTERPRET = VM_SEXAGESIMAL ............ */
#define VM_TIME       0100 /* Represents a time    */
#define VM_RA         0300 /* Represents a RA      */

/* When VM_INTERPRET = VM_DATIME ................. */

/* When VM_INTERPRET = VM_ENUM ................... */
#define VM_COMBINED   0300 /* Enum as bit string   */

/* When VM_INTERPRET = VM_TEXT ................... */
#define VM_HTML       0100 /* A text in HTML       */
#define VM_TeX        0200 /* Enum as bit string   */
#define VM_sofTeX     0300 /* As the Abstracts     */

/* Combination  */
#define V_IPERIODIC  (V_INTEGER | VM_PERIODIC)
#define V_FPERIODIC  (V_FLOAT   | VM_PERIODIC)
#define V_RAdeg    (V_FPERIODIC | VM_SEXAGESIMAL | VM_RA)
#define V_RAmas    (V_IPERIODIC | VM_mas)
#define V_DEdeg    (V_DOUBLE  | VM_SEXAGESIMAL)
#define V_DEmas    (V_INTEGER | VM_mas)
#define V_TIME     (V_FLOAT   | VM_SEXAGESIMAL )
#define V_DATIME   (V_FLOAT   | VM_DATIME )
#define V_s2000    (V_INTEGER | VM_s2000)
#define V_s1970    (V_INTEGER | VM_s1970)
#define V_s1980    (V_INTEGER | VM_s1980)
#define V_JD	   (V_DOUBLE  | VM_DATIME)
#define V_ROMAN	   (V_INTEGER | VM_ROMAN)

/* Operator Definitions (as a control char)  */
#define VOP_LT		0x10	/* < */
#define VOP_EQ		0x11	/* = */
#define VOP_LIKE	0x12	/* ~ */
#define VOP_GT		0x13	/* > */
#define VOP_LP		0x14	/* ( */
#define VOP_RP		0x15	/* ) */
#define VOP_AND		0x18	/* & */
#define VOP_OR		0x19	/* | */
#define VOP_NOT		0x1a	/* ! */
#define VOP_OREQ	0x1b	/* ! */

/*============================================================================
		Declarations of aprintf routines
 *============================================================================*/

#include <stdio.h>
int  aprintf _ARGS((char *format, ...)) ;
int faprintf _ARGS((FILE *file, char *format, ...)) ;
int saprintf _ARGS((char *buffer, char *format, ...)) ;

#endif
