#ifndef __EXTCTYPE_H
#define __EXTCTYPE_H


#ifndef __STDTTYPES_H
#include <stdtypes.h>
#endif

/* @M */
#ifdef __GNUC__
#define __PACKED__ __attribute__ ((packed))
#define __ALIGNED__ __attribute__ ((aligned(2))) 
#define __PACK_1__

#else
#define __PACKED__
#define __ALIGNED__

#endif

/* @DES Tipi e macro da programmi C */
/* @M */
#define MaxFields               100
#define MKFields                10
#define MaxKeys                 8
#define NORDIR                  0
#define COMDIR                  1
#define MaxArray                80
#define BPageLen                512
#define PHLen                   20
#define AreaLen                 (BPageLen - PHLen)
#define PDLen                   28
#define FHRLen                  32
#define MaxPD                           ((BPageLen - FHRLen) / PDLen)
#define NOFLDS 10
#define MOSTMEM 51200
#define LEASTMEM 10240

#if !defined(__STDIO_H) && !defined(_INC_STDIO)
#include <stdio.h>
#endif

/* @END */

/* @T  */
typedef long    TrDate;
typedef char    PathSt[42];
typedef char    Str80[82];
typedef long    RecNoType;
typedef char    *RecType;
typedef char    FieldName[12] ;
typedef char    TKey[MaxArray + 2];
typedef char    AreaKey[AreaLen];
typedef char    DataArea[BPageLen];
typedef struct  {
  char  nome[52] __PACKED__ ;
  char  indirizzo[32] __PACKED__ ;
  char  citta[32] __PACKED__ ;
  char  cap[6] __PACKED__ ;
  char  partiva[12] __PACKED__ ;
  char  codfisc[18] __PACKED__ ;
  short numero __PACKED__ ;
  short tab0[20] __PACKED__ ;
  long  tab1 __PACKED__ ;
  char  nome1[52] __PACKED__ ;
  char  indirizzo1[32] __PACKED__ ;
  char  citta1[32] __PACKED__ ;
  char  cap1[6] __PACKED__ ;
  char  partiva1[12] __PACKED__ ;
  char  codfisc1[18] __PACKED__ ;
} TDitta;

typedef struct  {
  int                   F;
  FILE*                 Fd;
  int                   IOR;
  int                   LenRec;
  int                   BaseFil;
  unsigned              LockMode;
  RecNoType             lpos;
  PathSt                name;
} SecDef ;

typedef struct
{
  char                                  SysName[42] __PACKED__ ;
  RecNoType                             EOD __PACKED__ ;
  RecNoType                             EOX __PACKED__ ;
  UINT16                                LenR __PACKED__ ;
  RecNoType                             Flags __PACKED__ ;
  char                                  Des[42] __PACKED__ ;
  char                                  FCalc[32] __PACKED__ ;
  char                                  GenPrompt[32] __PACKED__ ;
} FileDes;

typedef struct
{
  FieldName                             Name __PACKED__ ;
  byte                                          TypeF __PACKED__;
  byte                                          Len __PACKED__ ;
  byte                                          Dec __PACKED__ ;
#ifdef __PACK_1__
  byte                                          junk __PACKED__ ;
#endif
  UINT16                                        RecOff __PACKED__ ;
} RecFieldDes ;


typedef struct
{
  word DupKeys __ALIGNED__ ;
  byte NkFields __ALIGNED__ ;
  byte FieldSeq[MKFields] __PACKED__ ;
  byte FromCh[MKFields] __PACKED__ ;
  byte ToCh[MKFields] __PACKED__ ;
} KeyDes ;


typedef struct
{
  INT16                                         NFields __PACKED__ ;
  RecFieldDes                   Fd[MaxFields] __PACKED__ ;
  byte                                          SortFd[MaxFields] __PACKED__ ;
  INT16                                         NKeys __PACKED__ ;
  KeyDes                                        Ky[MaxKeys] __PACKED__ ;
}       RecDes ;

typedef struct
{
  RecNoType             RootBlock __PACKED__ ;
  INT16                         KeyLen __PACKED__ ;
  bool                          DupKeys __PACKED__ ;
#ifdef __PACK_1__
  byte                            junk;
#endif
  INT16                         NKNode __PACKED__ ;
  INT16                         MaxKNode __PACKED__ ;
  INT16                         MinKNode __PACKED__ ;
  RecNoType             PEOD __PACKED__ ;
  RecNoType             PEOX __PACKED__ ;
  INT16                         Flags[3] __PACKED__ ;
} PD ;

typedef PD              PathsArray[MaxPD];
typedef struct
{
  byte                          TPage __PACKED__ ;
#ifdef __PACK_1__
  byte                            junk;
#endif
  INT16                         PathNumber __PACKED__ ;
  INT16                         Flags __PACKED__ ;
  RecNoType             Father __PACKED__ ;
  RecNoType             Prev __PACKED__ ;
  RecNoType             Next __PACKED__ ;
  INT16                         NKey __PACKED__ ;
} PHR ;

typedef struct
{
  RecNoType             FFBlock __PACKED__ ;
  RecNoType             CABlock __PACKED__ ;
  RecNoType             NBEOX __PACKED__ ;
  RecNoType             NBEOD __PACKED__ ;
  INT16                         Filler[3] __PACKED__ ;
  INT16                         Flags[4] __PACKED__ ;
  INT16                         DPN __PACKED__ ;
} FHR ;

typedef struct
{
  FHR                                   Header __PACKED__ ;
  PathsArray    Paths __PACKED__ ;
} CA ;

typedef union
{
  struct
  {
    PHR                 PageHeader __PACKED__ ;
    AreaKey     AreaForKey __PACKED__ ;
  } PA __PACKED__ ;
  CA                                            CtrlArea __PACKED__ ;
  DataArea                      BuffIO __PACKED__ ;
} Page;

typedef struct
{
  SecDef                        Fil;
  FHR                                   FHead;
  INT16                         PN;
  PathsArray    Base;
  TKey                          Key;
  RecNoType             Ind;
  INT16                         Pos;
  RecNoType             CurPag;
} FilCtrl ;


typedef struct
{
  int                                   fhnd;
  FileDes                               *d;
  RecDes                                *r;
  RecNoType                              RecNo;
  INT16                                 ln;
  INT16                                 ft; 
  int                                   knum;
} isdef ;


typedef isdef* isfdptr;


struct s_prm {
  int rc_len;
  struct  {
    int f_pos;
    int f_len;
    char ad;
  } s_fld [NOFLDS];
};

/* @END */
#endif // __EXTCTYPE_H
