
#ifndef matfile_included
#define matfile_included
#ifndef dbhost_included
#include "dbhost.h"   /* PROTOTYPE_ALLOWED, HOST_ENVIRONMENT */
#endif

#define PC 0000
#define M68000 1000
#define VAX_D 2000
/* #define VAX_G 3000 -- not currently supported in CODAS3 */

/*-----------------------------------------------------------------------
    EDIT FOLLOWING LINE(S) TO DEFINE HOST ENVIRONMENT OTHER THAN LISTED
  -----------------------------------------------------------------------*/
#if HOST_ENVIRONMENT == PC_COMPATIBLE_HOST
#define MACHINE_TYPE PC
#define NaN_d_0 0L
#define NaN_d_1 -524288L  /* 0xFFFF80000 */
#else
#if HOST_ENVIRONMENT == SUN3_COMPATIBLE_HOST
#define MACHINE_TYPE M68000
#define NaN_d_0 0x7FFFFFFFL
#define NaN_d_1 0xFFFFFFFFL
#else
#if HOST_ENVIRONMENT == VAXD_COMPATIBLE_HOST
#define MACHINE_TYPE VAX_D
#define NaN_d_0 0x1D97FF5DL
#define NaN_d_1 0x687CD88DL
#endif
#endif
#endif

/* We need to find out what these should be for the other machines. */

#define REAL_ROWS 0
#define IMAG_ROWS 1
#define REAL_COLS 10
#define IMAG_COLS 11

#define COLUMN_WISE 0
#define ROW_WISE 100

#define type_M(x) ( (x).type        / 1000 * 1000 )   /* Machine type */
#define type_O(x) ( (x).type % 1000 /  100 *  100 )   /* Orientation */
#define type_P(x) ( (x).type %  100 /   10 *   10 )   /* Precision */
#define type_T(x) ( (x).type %   10               )   /* Text */

/* These functions for working with matfiles are in MATFIL_S.LIB */

typedef struct {
     long type;   /* type */
     long mrows;  /* row dimension */
     long ncols;  /* column dimension */
     long imagf;  /* flag indicating imag part */
     long namlen; /* name length (including NULL) */
} Fmatrix;

typedef struct {
   FILE *fp;
   long Fmatrix_origin;   /* offset into file */
   int  count_offset;     /* offset of count from Fmatrix_origin */
   long  count;           /* number of rows or columns written */
   int  nbytes;           /* bytes per data item */
   int  n_rc;             /* number of rows or columns */
} MAT_ARRAY_FP_TYPE;

#if PROTOTYPE_ALLOWED
/* SAVEMATX.C */
double *matstring(char *s, int *n);
char *transpose(char pdata[], int Crows, int Ccols, int nbytes);
void savemat(
         FILE *fp, /* File pointer */
         char type,
         char *pname,    /* pointer to matrix name */
         int mrows,      /* row dimension */
         int ncols,      /* column dimension */
         int imagf,      /* imaginary flag */
         char *preal,  /* pointer to real data */
         char *pimag); /* pointer to imag data */

/* SAVEMATC.C */
void tonan_f(double *dest, float *source, int n);
void tonan_d(double *dest, double *source, int n);
void tonan_mask_d(double *dest, double *source, int *mask, int minmask, int n);

MAT_ARRAY_FP_TYPE *start_mat_array(
   FILE *fp,       /* File pointer */
   char type,      /* d, f, l, i, u, t:
                        double, float, long, short, ushort, text */
   char *pname,    /* pointer to matrix name */
   char rc_flag,   /* r if rows will be added, c otherwise */
   int n_rc);       /* number of columns if rows will be added,
                                          or the converse */

void add_to_mat_array(
   MAT_ARRAY_FP_TYPE *mat_ptr,
   char *data_ptr);

void end_mat_array(
   MAT_ARRAY_FP_TYPE *mat_ptr);


/* LOADMATX.C */
int loadmat(
         FILE *fp,       /* File pointer */
         char *type,     /* Type flag: d, f, l, i, u, t */
         char *pname,    /* pointer to matrix name */
         int *mrows,     /* row dimension */
         int *ncols,     /* column dimension */
         int *imagf,     /* imaginary flag */
         char **preal,  /* pointer to real data */
         char **pimag);  /* pointer to imag data */

char *convert_mat_string(double *s, int n);

/* MATSCAN.C */
void matscan(FILE *fp);
#if COMPILER == TURBO_C
/* isnan.c */
int isnan(double d);
#endif
#else

/* SAVEMATX.C */
double *matstring();
char *transpose();
void savemat();
/* SAVEMATC.C */
void tonan_f();
void tonan_d();
void tonan_mask_d();
MAT_ARRAY_FP_TYPE *start_mat_array();
void add_to_mat_array();
void end_mat_array();
/* LOADMATX.C */
int loadmat();
char *convert_mat_string();
/* MATSCAN.C */
void matscan();
#ifdef LACKS_ISNAN
/* isnan.c */
int isnan();
#endif

#endif

#endif /* ifndef matfile_included */
