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

      MATSCAN.C

This is a function for summarizing the contents of a matfile.
There is also a main() function calling matscan(), for making
a dos command to summarize the contents of the matfile named
on the command line.

            Eric Firing
            88-02-12

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

#include "common.h"    /* free(), exit() */
#include "matfile.h"

#if PROTOTYPE_ALLOWED
void matscan(FILE *fp)
#else
void matscan(fp)
FILE *fp;
#endif
{
   char type;         /* type of matrix */
   char pname[40];    /* matrix name */
   int mrows;         /* row dimension */
   int ncols;         /* column dimension */
   int imagf;         /* imaginary flag */
   char *preal,       /* pointer to real data */
        *pimag;       /* pointer to imag data */
   int  i, j;

   putchar('\n');
   while(!loadmat(fp, &type, pname, &mrows, &ncols, &imagf, &preal, &pimag))
   {
      printf("type: %c, name: %15s, rows: %3d, cols: %3d, complex?: %c\n",
               type, pname, mrows, ncols, (imagf ? 'y' : 'n'));
      if (type == 't')
      {
        printf("  Text is: ");
        for(i=0;i<mrows;i++) {
        printf("\n    ");
        for(j=0;j<ncols;j++)
          printf("%c",preal[i+j*mrows]);
	   }  
       printf("\n");
   }
   free(preal);
   if (imagf)
     free(pimag);
   }
}

#ifdef EXE
#if PROTOTYPE_ALLOWED
void main(int argc, char **argv)
#else
void main(argc, argv)
int argc;
char **argv;
#endif
{
   FILE *fp;

   if (argc != 2)
   {
      printf("\nNeed only the matfile name on the command line.\n");
      exit(-1);
   }
   fp = fopen(argv[1], "rb");
   if (fp == NULL)
   {
      printf("\nCan't open file: %s\n", argv[1]);
      exit(-1);
   }
   matscan(fp);
   fclose(fp);
}
#endif
