/*
 * FILE:        CTDEFS.H
 *
 * MODULE:      FactoryLink CT Definitions Header File
 *
 * PROGRAM:     FactoryLink Multi-Platform version 4.1
 *
 * AUTHOR:      Clay Waldrop, Jr.
 *
 * HISTORY:       DATE   AUTHOR  DESCRIPTION OF ACTION
 *              08/06/90   CW    Finished Multi-Platform version 1.0
 *                               for OS/2, UNIX, and VMS platforms.
 *              09/05/90   CW    Finished Multi-Platform version 2.0
 *                               for OS/2, UNIX, and VMS platforms.
 *		06/03/91   DM    Pulled out from old FLIB.H
 *              01/06/91   WO    changed fp in CT from FILE to CT_FP
 */
#ifndef _FL_CT_INCLUDED /* { */
#define	_FL_CT_INCLUDED 1

#ifdef OS2
#pragma pack(1)
#endif

#define	CTIOSIZE	8192

/* Configuration Table (CT) structures */

#define	OP_TYPE_READ		1
#define	OP_TYPE_CREATE		2
#define	OP_TYPE_UPDATE		3

typedef	struct	_ct_fp
{
	FILE	*afp;
	FILE	*cfp;
	FILE	*dfp;
	short	op_type;
	ulong	ctndx_seek;
	char 	arch_file[ MAX_FILE_NAME];
} CT_FP;

typedef struct	 			/* entire CT archive header */
{
	u16	magic;			/* magic number for CT file */
	u16	version;		/* version number (0x0100 = V 1.0) */
					/* (equals number of CTNDX's) */
} CTHDR_FL;

typedef struct _CTARC			/* entire CT archive header */
{
	CTHDR_FL cthdr;                 /* Header information */
	u32	 ncts;			/* number of CT's within archive */
					/* (equals number of CTNDX's) */
} CTARC;

typedef struct _CTARC16			/* entire CT archive header */
{
	CTHDR_FL cthdr;                 /* Header information */
	u16	 ncts;			/* number of CT's within archive */
					/* (equals number of CTNDX's) */
} CTARC16;

typedef struct _CTNDX			/* single CT index record */
{
	u16	type;			/* type of entry (numeric) */
	char	name[10];		/* name of entry (ASCII) */
	u32	offset;			/* file offset to header */
	u32	hdrlen;			/* header length, in bytes */
	u32	reclen;			/* record length, in bytes */
	u32	nrecs;			/* number of records */
} CTNDX;

typedef struct _CTNDX16			/* single CT index record */
{
	u16	type;			/* type of entry (numeric) */
	char	name[10];		/* name of entry (ASCII) */
	u32	offset;			/* file offset to header */
	u16	hdrlen;			/* header length, in bytes */
	u16	reclen;			/* record length, in bytes */
	u16	nrecs;			/* number of records */

} CTNDX16;

typedef struct _CT			/* CT control block */
{
	CT_FP	*fp;			/* file pointer for CT file */
	CTARC	ctarc;			/* CT archive header */
	CTNDX	ctndx;			/* current CT index record */
} CT;

#define	ct_get_ncts(p)	((p)->ctarc.ncts)
#define	ct_get_nrecs(p)	((p)->ctndx.nrecs)
#define	ct_get_type(p)	((p)->ctndx.type)
#define	ct_get_name(p)	((p)->ctndx.name)
#define	ct_get_hdrlen(p) ((p)->ctndx.hdrlen)
#define	ct_get_reclen(p) ((p)->ctndx.reclen)

/* --- CT Definitions --- */

/* Magic number for Configuration Tables (CT files) */

#define	CT_MAGIC		0x5443	/* this spells "CT" in ASCII */
#define CT_VERSION              0x0300  /* 32-bit version */
#define CT_VERSION16            0x0200  /* 16-bit version */

/* Return values for CT routines ........ probable cause of error */

#define CT_CANNOT_OPEN_FILE	1	/* file is missing */
#define CT_CANNOT_CLOSE_FILE	2	/* program bug */
#define CT_FILE_NOT_OPEN	3	/* program bug */
#define CT_SEEK_ERROR		4	/* wrong file size */
#define CT_READ_ERROR		5	/* wrong file size */
#define CT_WRITE_ERROR		6	/* drive not ready or disk full */
#define CT_BAD_MAGIC		7	/* file corrupted */
#define CT_BAD_DATA		8	/* file corrupted */
#define CT_NULL_POINTER		9	/* program bug */
#define CT_BAD_INDEX		10	/* CT doesn't exist */
#define CT_BAD_RECORD		11	/* CT record doesn't exist */
#define CT_BAD_VERSION		12	/* CT operation invalid for CT version */



typedef int (*PCHKFILEPROC)(char *);
typedef int (*PCHKPASSWDPROC)();

/* CT Read Routines */
#ifdef __cplusplus
extern "C" {
#endif

PCHKFILEPROC ct_set_checkfilefp(PCHKFILEPROC);
int	ENTRY ct_open(CT FAR *ctp, char FAR *dirp, char FAR *namep);
int	ENTRY ct_read_index(CT FAR *ctp, uint ndx);
int	ENTRY ct_read_hdr(CT FAR *ctp, void FAR *hdrp);
int	ENTRY ct_read_rec(CT FAR *ctp, void FAR *recp, uint rec);
int	ENTRY ct_read_recs(CT FAR *ctp, void FAR *recp, uint rec, uint recs);
int	ENTRY ct_calc_offset(CT FAR *ctp, uint ndx);
int	ENTRY ct_close(CT FAR *ctp);

/* CT Write Routines */

int	ENTRY ct_create(CT FAR *ctp, char FAR *dirp, char FAR *namep);
int	ENTRY ct_update(CT FAR *ctp, char FAR *dirp, char FAR *namep);
int	ENTRY ct_write_index(CT FAR *ctp, uint ndx);
int	ENTRY ct_write_hdr(CT FAR *ctp, void FAR *hdrp);
int	ENTRY ct_write_rec(CT FAR *ctp, void FAR *recp, uint rec);
int	ENTRY ct_write_recs(CT FAR *ctp, void FAR *recp, uint rec, uint recs);
int	ENTRY ct_find_index(CT FAR *ctp, char FAR *name);
int	ENTRY ct_find_name_type(CT FAR *ctp, char FAR *name, 
		uint FAR *type, uint occurrence);

int	ENTRY ct_select_index(CT FAR *ctp, uint ndx);
int	ENTRY ct_add_index(CT FAR *ctp, char FAR *name, 
		uint type, uint hdrlen, uint reclen);
int	ENTRY ct_del_index(CT FAR *ctp, uint index);

int     ENTRY ct_arcrec_disksize(CT FAR *ctp);
int     ENTRY ct_ndxrec_disksize(CT FAR *arcp);
int     ENTRY ct_fread_arc(CT *ctp, FILE *fp);
int     ENTRY ct_fread_ndx(CT *ctp, FILE *fp, CTNDX *dst);

#ifdef __cplusplus
}
#endif

#ifdef OS2
#pragma pack()
#endif

#endif /* } _FL_CT_INCLUDED */
