#include        <stdio.h>
#include        <stdlib.h>
#include        <stdtypes.h>
#include        <strings.h>
#include        <scanner.h>
#include        <isam.h>
#include        <tabutil.h>
#include        <utility.h>

int main(int argc,char** argv)

{
  init_global_vars();
  if (argc > 2)
  {
    argv++;
    TScanner dfile(*argv);
    TLocalisamfile* of;
    TString s(dfile.line());
    TToken_string w(s);
    const char* work = w.get();
    const int nf = atoi(work);
    const TString nt(work);
    const int reclen = w.get_int();

    if (nf) of = new TLocalisamfile(nf);
    else of = new TTable(nt);
    
    TToken_string d(40, ',');

    while (*(work = dfile.line()))
      d.add(work);
    
    argv++;
    TScanner ifile(*argv);

    TString name, ws(256), depcom(10);
    int ncodi = 2, ncodd = 2;

    while (*(work = ifile.line()))
    {
      s = work;
      s.left_just(reclen);
      d.restart();
      w = d.get();
      int st = w.get_int();
      int len = w.get_int();
      name = w.get();
      ws = s.mid(st, len);
      if (ws != depcom)
      {
        if (depcom.not_empty()) of->rewrite();
        ncodi = 2;
        ncodd = 2;
        of->zero();
        of->put(name, ws);
        of->read();
        if (of->good()) depcom = ws;
        else depcom = "";
      }
      w = d.get();
      st = w.get_int();
      len = w.get_int();
      name = s.mid(st, len);
      w = d.get();
      st = w.get_int();
      len = w.get_int();
      ws = s.mid(st, len);
      if (name == "UI")
        name = format("UFFIVA%1d", ncodi++);
      else
        name = format("UFFIIDD%1d", ncodd++);
      of->put(name, ws);
    }
    if (depcom.not_empty()) of->rewrite();
    delete of;
  }
  free_global_vars();
}
