
#include "DICFileObj.h"
#include "DDLFileObj.h"
#include <iostream.h>

#define FALSE 0
int main() {
  char *diags=NULL;
  DICFileObj * fobjR = NULL;
  ISTable *format;
  ISTable *ddlformat;
  DDLFileObj * ddl = NULL;
  ISTable * tbl;
  ReVarCifArray<CifString> colName;
  ReVarCifArray<CifString> target;
  ReVarPCifArray<int> *listOut;
  int errCode;

  ddl = new DDLFileObj((char*)"ddl", WRITE_MODE, 80, "?", 1);
  ddlformat = new ISTable("ddlformat");
  ddl->Read((char*)"./test/ddl-mm",ddlformat, diags);
  cout<<"diags = ";
  if (diags != NULL)
  {
    cout<<diags;
  }
  cout<<endl;


  format = new ISTable("format");
  fobjR = new DICFileObj((char*)"./test/TESTFILE12", WRITE_MODE, 80, "?", 1);

  fobjR->Read((char*)"./test/dictionary.dic",ddl , format, diags);

  cout<<"diags = ";
  if (diags != NULL)
  {
    cout<<diags;
  }
  cout<<endl;

  fobjR->Compress(ddl);
  
  fobjR->Write("./test/view2.ocif");
  tbl=fobjR->GetTablePtr("item_aliases");
  colName.Add("name");
  colName.Add("dictionary");
  colName.Add("version");
  tbl->CreateIndex("index0",colName);

  target.Add("_atom_site.attached_hydrogens");
  target.Add("cif_core.dic");
  target.Add("2.0.1");

  listOut=tbl->Search(target,colName,errCode);

  int colIndex = tbl->GetColumnIndex("alias_name");
  CifString theCell;

  if (errCode>=0) {
    for(int i=0;i<(int)listOut->Length();i++) {
      theCell.Clear();
      tbl->GetCell(theCell,colIndex,(*listOut)[i]);
      cout<<theCell.Text()<<endl;
    }
  }

  if (listOut) delete listOut;
  fobjR->CloseFile(1);
  if (fobjR) delete fobjR;

  fobjR = new DICFileObj((char*)"./test/TESTFILE12", READ_MODE, 80, "?", 1);
  
  tbl=fobjR->GetTablePtr("item_aliases");

  listOut=tbl->Search(target,colName,errCode);

  colIndex = tbl->GetColumnIndex("alias_name");

  if (errCode>=0) {
    for(int i=0;i<(int)listOut->Length();i++) {
      theCell.Clear();
      tbl->GetCell(theCell,colIndex,(*listOut)[i]);
      cout<<theCell.Text()<<endl;
    }
  }

  fobjR->CloseFile(0);
  if (fobjR) delete fobjR;
  if (listOut) delete listOut;
  exit(0);

}

