#include <stdio.h>
#include "dbftable.h"

static char HELP[] =
  "\n"
  "dbf2html DBFfilespecs\n"
  "\n"
  "generates an HTML table containing the contents of all non-deleted\n"
  "records in the specified DBF table. The field names are used as table\n"
  "headers. The result is sent to the standard output.\n";

static void string(const char *s)
{
  fputs(s, stdout);
}

int main(int argc, char **argv)
{
  if (argc < 2)
  {
    fputs(HELP, stderr);
    return 1;
  }
  for (int i = 1; i < argc; i++)
  {
    dbf_table t;
    if (t.open(argv[1], DBF_READ))
    {
      const dbf_field_descriptor *f;
      puts("<TABLE BORDER=1>");
      string("<TR>");
      for (int j = 0; (f = t.field_info(j)) != NULL; j++)
        printf("<TH>%s</TH>", f->name);
      puts("</TR>");
      for (long n = 1; n <= t.number_of_records; n++)
      {
        t.record_number = n;
        if (!t.deleted)
        {
          string("<TR>");
          for (int j = 0; (f = t.field_info(j)) != NULL; j++)
          {
            switch (f->type)
            {
              case 'D':
              {
                struct date x, defdate;
                defdate.da_mon = 0;
                string("<TD ALIGN=CENTER>");
                if (t.get(x, f, defdate))
                {
                  if (x.da_mon == 0)
                    string("&nbsp;");
                  else
                    printf("%02d/%02d/%04d", x.da_mon, x.da_day, x.da_year);
                }
                else
                  string("??/??/????");
                break;
              }
              case 'N':
              {
                __int64 x;
                string("<TD ALIGN=RIGHT>");
                if (t.get(x, f))
                {
                  int digits = f->width;
                  if (f->precision != 0)
                    digits--;
                  char s[32];
                  __int64 y = x < 0 ? -x : x;
                  int i = digits;
                  do
                  {
                    s[--i] = y % 10 + '0';
                    y /= 10;
                  } while (y != 0 && i != 0 || i + f->precision >= digits);
                  if (x < 0)
                    putchar('-');
                  for (; i < digits; i++)
                  {
                    putchar(s[i]);
                    if (f->precision != 0 && i + f->precision + 1 == digits)
                      putchar('.');
                  }
                }
                else
                  string("?????");
                break;
              }
              case 'C':
              {
                char x[255];
                string("<TD ALIGN=LEFT>");
                if (!t.get(x, sizeof(x), f))
                {
                  x[0] = ' ';
                  x[1] = 0;
                }
                char *s = x;
                while (*s == ' ')
                  s++;
                if (*s == 0)
                  string("&nbsp;");
                else
                {
                  char *e = s;
                  while (*e != 0)
                    e++;
                  while (e[-1] == ' ')
                    e--;
                  *e = 0;
                  string(s);
                }
                break;
              }
              case 'L':
              {
                bool x;
                string("<TD ALIGN=CENTER>");
                string(t.get(x, f) ? (x ? "true" : "false") : "?????");
                break;
              }
              default:
                string("<TD ALIGN=CENTER>");
                string("?????");
            }
            string("</TD>");
          }
          puts("</TR>");
        }
      }
      puts("</TABLE>\n");
    }
  }
  return 0;
}



