/*
 * calma.h --
 *
 * This file defines constants used internally by the calma
 * module, but not exported to the rest of the world.
 *
 *     ********************************************************************* 
 *     * Copyright (C) 1985, 1990 Regents of the University of California. * 
 *     * Permission to use, copy, modify, and distribute this              * 
 *     * software and its documentation for any purpose and without        * 
 *     * fee is hereby granted, provided that the above copyright          * 
 *     * notice appear in all copies.  The University of California        * 
 *     * makes no representations about the suitability of this            * 
 *     * software for any purpose.  It is provided "as is" without         * 
 *     * express or implied warranty.  Export of this software outside     * 
 *     * of the United States of America may require an export license.    * 
 *     *********************************************************************
 *
 * rcsid $Header: /ufs/repository/magic/calma/calmaInt.h,v 1.6 2006/07/08 19:04:34 fang Exp $
 */

#ifndef	__CALMA_CALMAINT_H__
#define	__CALMA_CALMAINT_H__

#include "misc/magic.h"
#include "utils/hash.h"
#include "database/database_types.h"

/* Record data types */
#define CALMA_NODATA	0	/* No data present */
#define CALMA_BITARRAY	1	/* Bit array */
#define CALMA_I2	2	/* 2 byte integer */
#define CALMA_I4	3	/* 4 byte integer */
#define CALMA_R4	4	/* 4 byte real */
#define CALMA_R8	5	/* 8 byte real */
#define CALMA_ASCII	6	/* ASCII string */

/* Record types */
#define CALMA_HEADER		0
#define CALMA_BGNLIB		1
#define CALMA_LIBNAME		2
#define CALMA_UNITS		3
#define CALMA_ENDLIB		4
#define CALMA_BGNSTR		5
#define CALMA_STRNAME		6
#define CALMA_ENDSTR		7
#define CALMA_BOUNDARY		8
#define CALMA_PATH		9
#define CALMA_SREF		10
#define CALMA_AREF		11
#define CALMA_TEXT		12
#define CALMA_LAYER		13
#define CALMA_DATATYPE		14
#define CALMA_WIDTH		15
#define CALMA_XY		16
#define CALMA_ENDEL		17
#define CALMA_SNAME		18
#define CALMA_COLROW		19
#define CALMA_TEXTNODE		20
#define CALMA_NODE		21
#define CALMA_TEXTTYPE		22
#define CALMA_PRESENTATION	23
#define CALMA_SPACING		24
#define CALMA_STRING		25
#define CALMA_STRANS		26
#define CALMA_MAG		27
#define CALMA_ANGLE		28
#define CALMA_UINTEGER		29
#define CALMA_USTRING		30
#define CALMA_REFLIBS		31
#define CALMA_FONTS		32
#define CALMA_PATHTYPE		33
#define CALMA_GENERATIONS	34
#define CALMA_ATTRTABLE		35
#define CALMA_STYPTABLE		36
#define CALMA_STRTYPE		37
#define CALMA_ELFLAGS		38
#define CALMA_ELKEY		39
#define CALMA_LINKTYPE		40
#define CALMA_LINKKEYS		41
#define CALMA_NODETYPE		42
#define CALMA_PROPATTR		43
#define CALMA_PROPVALUE		44
#define CALMA_BOX		45
#define CALMA_BOXTYPE		46
#define CALMA_PLEX		47
#define CALMA_BGNEXTN		48
#define CALMA_ENDEXTN		49
#define CALMA_TAPENUM		50
#define CALMA_TAPECODE		51
#define CALMA_STRCLASS		52
#define CALMA_RESERVED		53
#define CALMA_FORMAT		54
#define CALMA_MASK		55
#define CALMA_ENDMASKS		56
#define CALMA_LIBDIRSIZE	57
#define CALMA_SRFNAME		58
#define CALMA_LIBSECUR		59

#define	CALMA_NUMRECORDTYPES	60	/* Number of above types */

/* Flags for transforms */
#define	CALMA_STRANS_UPSIDEDOWN	0x8000	/* Mirror about X axis before rot */
#define	CALMA_STRANS_ROTATE	0x0002	/* Rotate by absolute angle */

/* Path types */
#define	CALMAPATH_SQUAREFLUSH	0	/* Square end flush with endpoint */
#define	CALMAPATH_ROUND		1	/* Round end */
#define	CALMAPATH_SQUAREPLUS	2	/* Square end plus half-width extent */

/* Largest calma layer or data type numbers */
/* Note:  Changed from 200 to 255.  GDSII 6.0 apparently says 0 to 63	*/
/* but some databases allow 0 to 255.  ---Tim Edwards 8/13/02		*/
#define	CALMA_LAYER_MAX	255

#define	CalmaIsValidLayer(n)	((n) >= 0 && (n) <= CALMA_LAYER_MAX)

/* Used to index hash tables of (layer, datatype) pairs */
typedef struct
{
    int		 clt_layer;
    int		 clt_type;
} CalmaLayerType;

/* Biggest calma string */
#define	CALMANAMELENGTH		32

/* Length of record header */
#define	CALMAHEADERLENGTH	4

/* ------------------------- Input macros ----------------------------- */

/* Globals for Calma reading */
extern FILE *calmaInputFile;
extern int calmaReadScale1;
extern int calmaReadScale2;
extern bool calmaLApresent;
extern int calmaLAnbytes;
extern int calmaLArtype;

/*
 * Macros for number representation conversion.
 */
#ifdef ibm032
#include <netinet/in.h>    /* as macros in in.h and don't exist as routines */
#endif

#ifndef	ntohl
# ifdef	IS_BIG_ENDIAN
# define ntohl(x)        (x)
# define ntohs(x)        (x)
# define htonl(x)        (x)
# define htons(x)        (x)
# endif 
#endif

typedef union { char uc[2]; unsigned short us; } TwoByteInt;
typedef union { char uc[4]; unsigned int ul; } FourByteInt;

/* Macro to read a 2-byte integer */
#define	READI2(z) \
	{ \
            TwoByteInt u; \
            u.uc[0] = getc(calmaInputFile); \
            u.uc[1] = getc(calmaInputFile); \
            (z) = (int) ntohs(u.us); \
	}

/* Macro to read a 4-byte integer */
#define	READI4(z) \
	{ \
            FourByteInt u; \
            u.uc[0] = getc(calmaInputFile); \
            u.uc[1] = getc(calmaInputFile); \
            u.uc[2] = getc(calmaInputFile); \
            u.uc[3] = getc(calmaInputFile); \
            (z) = (int) ntohl(u.ul); \
	}

/*
 * Macro to read a point from the input.
 * We take care of scaling by calmaReadScale1/calmaReadScale2.
 */
#define READPOINT(p) \
	{ \
	    READI4((p)->p_x); \
	    READI4((p)->p_y); \
	    (p)->p_x = (p)->p_x * calmaReadScale1 / calmaReadScale2; \
	    (p)->p_y = (p)->p_y * calmaReadScale1 / calmaReadScale2; \
	}

/* Macros for reading and unreading record headers */
#define READRH(nb, rt) \
	{ \
	    if (calmaLApresent) { \
		(nb) = calmaLAnbytes; \
		(rt) = calmaLArtype; \
		calmaLApresent = FALSE; \
	    } else { \
		READI2(nb); \
		if (feof(calmaInputFile)) nb = -1; \
		else { \
		    (rt) = getc(calmaInputFile); \
		    (void) getc(calmaInputFile); \
		} \
	    } \
	}

#define UNREADRH(nb, rt) \
	{ \
	    ASSERT(!calmaLApresent, "UNREADRH"); \
	    calmaLApresent = TRUE; \
	    calmaLAnbytes = (nb); \
	    calmaLArtype = (rt); \
	}

#define	PEEKRH(nb, rt) \
	{ \
	    READRH(nb, rt); \
	    UNREADRH(nb, rt); \
	}

/* Other commonly used globals */
extern HashTable calmaLayerHash;
extern int calmaElementIgnore[];
extern CellDef *calmaFindCell();
extern char *calmaRecordName();
extern void freeMagic(char* cp);

/* from CalmaRead.c: */
extern void calmaUnexpected(int wanted, int got);
#ifndef	NO_VARARGS
extern void calmaReadError(char* format, ...);
#else
extern void calmaReadError(char* format, 
	char* a1, char* a2, char* a3, char* a4, char* a5,
	char* a6, char* a7, char* a8, char* a9, char* a10);
#endif

/* from CalmaRdio.c: */
extern bool calmaReadI2Record(int type, int* pvalue);
extern bool calmaSkipExact(int type);
extern void calmaSkipSet(int* skipwhat);	/* was Void */
extern bool calmaSkipBytes(register int nbytes);
extern bool calmaReadTransform(Transform* ptrans, char* name);
extern bool calmaReadStringRecord(int type, char** str);
extern bool calmaReadI4Record(int type, int* pvalue);
extern bool calmaReadR8(double* pd);
extern bool calmaSkipTo(int what);

/* from CalmaRdcl.c: */
extern void calmaScalePoint(register Point* p, register int scale);
extern void calmaElementSref(void);
extern bool calmaParseStructure(void);
extern bool calmaParseElement(int* pnsrefs);

/* from CalmaRdpt.c: */
extern bool calmaReadPath(CIFPath** pathheadpp);
extern void calmaLayerError(char* mesg, int layer, int dt);
extern void calmaElementBoundary(void);
extern void calmaElementBox(void);
extern void calmaElementPath(void);
extern void calmaElementText(void);

/* from CalmaWrite.c: */
#if 0
// now local (static) to CalmaWrite.c
extern void calmaOutHeader(CellDef* rootDef, FILE* f);
extern void calmaProcessDef(CellDef* def, FILE* outf);
extern void calmaOutFunc(CellDef* def, FILE* f);
extern void calmaOutDate(time_t t, FILE* f);
extern void calmaOutStructName(int type, CellDef* def, FILE* f);
extern void calmaWriteLabelFunc(Label *lab, int type, FILE *f);
extern void calmaOutStringRecord(int type, 
	register char *str, register FILE *f);
#endif

/* ------------------- Imports from CIF reading ----------------------- */

extern CellDef *cifReadCellDef;
extern Plane *cifSubcellPlanes[];
extern Plane **cifCurReadPlanes;
extern HashTable CifCellTable;

#endif	/* __CALMA_CALMAINT_H__ */
