
#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;

  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/TESTFILE13", WRITE_MODE, 80, "?", 0);

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

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

  fobjR->Compress(ddl);

/**************** Adding new definitions *****************/
  int colIndex;
  CifString cs;

  tbl=fobjR->GetTablePtr("category");
  cout<<tbl->GetNumRows()<<endl;
  tbl->AddRow();
  cout<<tbl->GetNumRows()<<endl;
  cout<<tbl->GetLastRowIndex()<<endl;

  colIndex = tbl->GetColumnIndex("implicit_key");
  cs=fobjR->GetDataBlock();
  tbl->UpdateCell(cs,colIndex,tbl->GetLastRowIndex());
  /* GetLastRowIndex() is different from GetNumRows() because some rows might be deleted,
     they actually are marked as deleted, but they are still in a table
  */

  colIndex = tbl->GetColumnIndex("mandatory_code");
  cs="no";
  tbl->UpdateCell(cs,colIndex,tbl->GetLastRowIndex());

  colIndex = tbl->GetColumnIndex("id");
  cs="category_1";
  tbl->UpdateCell(cs,colIndex,tbl->GetLastRowIndex());

  colIndex = tbl->GetColumnIndex("description");
  cs="Description for category_1";
  tbl->UpdateCell(cs,colIndex,tbl->GetLastRowIndex());
  fobjR->WriteTable(tbl,fobjR->GetDataBlock(),"category");
  

  tbl=fobjR->GetTablePtr("category_key");
  tbl->AddRow();

  colIndex = tbl->GetColumnIndex("name");
  cs="category_1.item_1";
  tbl->UpdateCell(cs,colIndex,tbl->GetLastRowIndex());

  colIndex = tbl->GetColumnIndex("id");
  cs="category_1";
  tbl->UpdateCell(cs,colIndex,tbl->GetLastRowIndex());

  fobjR->WriteTable(tbl,fobjR->GetDataBlock(),"category_key");

  tbl=fobjR->GetTablePtr("item");
  tbl->AddRow();

  colIndex = tbl->GetColumnIndex("name");
  cs="item_1";
  tbl->UpdateCell(cs,colIndex,tbl->GetLastRowIndex());

  colIndex = tbl->GetColumnIndex("mandatory_code");
  cs="no";
  tbl->UpdateCell(cs,colIndex,tbl->GetLastRowIndex());

  colIndex = tbl->GetColumnIndex("category_id");
  cs="category_1";
  tbl->UpdateCell(cs,colIndex,tbl->GetLastRowIndex());

  fobjR->WriteTable(tbl,fobjR->GetDataBlock(),"item");
  
/*********************************************************/

  fobjR->Write("./test/view13.ocif");
  fobjR->Write("./test/view13.dic",ddl,format);

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

