/***************************************************************
                         FLATFILE.C
                         August, 1987
                         modified October 17, 1987

     Routines for reading simple arrays:

FUNCTIONS:
     int read_array_?(fp, array, n, format)

PROTOTYPES:  flatfile.h

VARIATIONS:
     ? can be 1       for any char or byte type,
              2       for any 2-byte int type,
              4       for any 4-byte int type,
              float,
              double.

ARGUMENTS:
     fp        is a pointer to type FILE, already open;
     array     is the address of an array of the appropriate type;
     n         is the number of elements to be printed or read;
     ncol      is the number of columns for printing only;
     format    is a standard printf format specification string:
                    e.g. " %10.3f "

RETURNS:
     read_array_?   ----> number of fields read.

**********************************************************************/

#include "common.h"
#include "vector.h"

#if PROTOTYPE_ALLOWED
int read_array_1(FILE *fp_in, char array[], int n, char format[])
#else
int read_array_1( fp_in, array, n, format)
FILE *fp_in;
char array[];
int n;
char format[];
#endif
{
     int i;
     int count = 0;
     int ok = 1;
     for (i=0; i<n && (ok != EOF); i++)
     {
          ok = fscanf(fp_in, format, array+i);
          if (ok==1) count++;
     }
     return( count );
}

#if PROTOTYPE_ALLOWED
int read_array_2(FILE *fp_in, int array[], int n, char format[])
#else
int read_array_2( fp_in, array, n, format)
FILE *fp_in;
int array[];
int n;
char format[];
#endif
{
     int i;
     int count = 0;
     int ok = 1;
     for (i=0; i<n && (ok != EOF); i++)
     {
          ok = fscanf(fp_in, format, array+i);
          if (ok==1) count++;
     }
     return( count );
}

#if PROTOTYPE_ALLOWED
int read_array_4(FILE *fp_in, long array[], int n, char format[])
#else
int read_array_4( fp_in, array, n, format)
FILE *fp_in;
long array[];
int n;
char format[];
#endif
{
     int i;
     int count = 0;
     int ok = 1;
     for (i=0; i<n && (ok != EOF); i++)
     {
          ok = fscanf(fp_in, format, array+i);
          if (ok==1) count++;
     }
     return( count );
}

#if PROTOTYPE_ALLOWED
int read_array_float(FILE *fp_in, float array[], int n, char format[])
#else
int read_array_float( fp_in, array, n, format)
FILE *fp_in;
float array[];
int n;
char format[];
#endif
{
     int i;
     int count = 0;
     int ok = 1;
     for (i=0; i<n && (ok != EOF); i++)
     {
          ok = fscanf(fp_in, format, array+i);
          if (ok==1) count++;
     }
     return( count );
}

#if PROTOTYPE_ALLOWED
int read_array_double(FILE *fp_in, double array[], int n, char format[])
#else
int read_array_double( fp_in, array, n, format)
FILE *fp_in;
double array[];
int n;
char format[];
#endif
{
     int i;
     int count = 0;
     int ok = 1;
     for (i=0; i<n && (ok != EOF); i++)
     {
          ok = fscanf(fp_in, format, array+i);
          if (ok==1) count++;
     }
     return( count );
}
#if 0

#include "prtarray.h"   /* print_array_*() */

#if PROTOTYPE_ALLOWED
void main(void)
#else
void main()
#endif
{
     long larray[10];
     float farray[10];
     int i;

     for (i=0; i<10; i++)  larray[i] = i;
     print_array_4(stdout, larray, 10, 5, "%10ld ");
     for (i=0; i<10; i++)  farray[i] = (float) i;
     print_array_float(stdout, farray, 10, 5, "%10.2f ");
}
#endif
