// display structure of a DBf table


#include <stdio.h>
#include <dos.h>
#include "dbftable.h"

const int MAX_FIELDS = 100;

void main(int argc, char **argv)
{
  if (argc > 1)
  {
    dbf_table t;
    if (t.open(argv[1], DBF_READ))
    {
      printf("\nDatabase           : %s\n", argv[1]);
      printf("Number of records  : %ld\n", t.number_of_records);
      printf("Date of last update: %02d/%02d/%04d\n\n", t.header_date.da_mon,
        t.header_date.da_day, t.header_date.da_year);
      printf("Field  Field Name  Type       Width  Dec\n");
      int nfields;
      const dbf_field_descriptor *field[MAX_FIELDS];
      for (nfields = 0; nfields < MAX_FIELDS; nfields++)
      {
        if ((field[nfields] = t.field_info(nfields)) == NULL) break;
      }
      for (int i = 0; i < nfields; i++)
      {
        printf("%5d  ", i);
        int j = 0;
        for (int k = 0; k < 12; k++)
        {
          int c = field[i]->name[j];
          if (c == 0)
            c = ' ';
          else
            j++;
          putchar(c);
        }
        const char *type;
        switch (field[i]->type)
        {
          case 'D':
            type = "Date     ";
            break;
          case 'C':
            type = "Character";
            break;
          case 'N':
            type = "Numeric  ";
            break;
          case 'L':
            type = "Logical  ";
            break;
          default:
            type = "Unknown  ";
        }
        printf("%s  %5d", type, field[i]->width);
        if (field[i]->type == 'N')
          printf("  %3d\n", field[i]->precision);
        else
          putchar('\n');
      }
    }
    t.close();
  }
}


