#include        <stdio.h>
#include        <stdlib.h>
#include        <stdtypes.h>
#include        <strings.h>
#include        <scanner.h>
#include        <isam.h>
#include        <tabutil.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, just(3), ws(256);

    while (*(work = ifile.line()))
    {
      s = work;
      s.left_just(reclen);
      d.restart();
      of->zero();
      w = d.get();
      while (w.not_empty())
      {
        const int st = w.get_int();
        const int len = w.get_int();
        name = w.get();
        if (name != "-")
        {
          ws = s.mid(st, len);
          just = w.get();
          if (just == "Z") ws.right_just(len, '0');
          else
            if (just == "R") ws.right_just(len);
            else
              if (just == "L") ws.left_just(len);
          of->put(name, ws);
        }
        w = d.get();
      }
      of->write();
      if (of->bad()) of->rewrite();
    }
    delete of;
  }
  free_global_vars();
}
