#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include "ndb_cifparse.h"
#include "btree.h"
#include <sys/types.h>
#include <time.h>

int main(int argc, char *argv[])
{
  CifHandle cif1,ddl,cif2;
  CifHandle format;
  FILE *fp1,*fp2,*fp3;
  FILE *msg;
  time_t start,end;

  ndb_open_log(argv[0],"cifparse.log");
    ndb_set_debug_level(10);

  ndb_cif_init(&ddl);
  ndb_cif_init(&cif1);
  ndb_cif_init(&format);
  
 fp1=fopen(argv[1],"r"); 
 fp2=fopen("test/ddl-mm","r");

/* 
	reads dictionary with format 
	this function first reads ddl, then dictionary, and generates implicit rows
	using informations from ddl, check number of rows, existance of category ...
*/
 ndb_cif_read_dic_with_format(&cif1,fp1,&ddl,fp2,&format);
 printf("DICTIONARY IS READ\n");
 fclose(fp1);
 ndb_cif_rewind_file(&ddl);
 ndb_cif_rewind_file(&cif1); 
 ndb_cif_rewind_file(&format); 

/*
  takes of redudant rows from dictionary
*/
 ndb_cif_compress_file(&cif1,&ddl);
 printf("DICTIONARY IS COMPRESSED\n");

/*
  writes down cif1 structure in the dictionary format
*/
 fp1=fopen("test/test.odic","w");
time(&start);
 ndb_cif_write_dic_by_format(&cif1,fp1,&ddl,&format,1);
time(&end);
 fclose(fp1); 
printf("DICTIONARY IS WRITEN for %f secondes\n",difftime(end,start));
 ndb_cif_close(&ddl);
 
/*
  reads dictionary without format
*/
 fp1=fopen("test/test.odic","r");
 ndb_cif_init(&cif2);
 ndb_cif_init(&ddl);
 ndb_cif_read_dic(&cif2,fp1,&ddl,fp2);

/*
  compares two cif structures
  cif1 - build from original dictionary using ndb_cif_read_dic_with_format function
  cif2 - build from test.odic using function ndb_cif_read_dic.
         test.odic is output from ndb_cif_write_dic_by_format for cif1
  in compare.log is diagnosis for the comparison
*/
 msg=fopen("compare.log","w");
 ndb_cif_compare(cif1,"file1",cif2,"file2",msg);

/*
  writes both structures in mmcif format
*/
 fclose(fp2);
 fp2=fopen("test/file1.odic","w");
 fp3=fopen("test/file2.odic","w");
 ndb_cif_write_file(&cif2,fp3);
 ndb_cif_write_file(&cif1,fp2);
 fclose(fp3);
 fclose(fp2);
 
 ndb_cif_close(&ddl);
 
 fclose(msg);
 fclose(fp1); 
 ndb_cif_close(&cif2);
 
 ndb_cif_close(&cif1);
 ndb_cif_close(&format);
 ndb_close_log();
 return (0);
}



