#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include "dbf.h"

main(int argc, char **argv) {
int handle, i;
unsigned short num_fields, rec_num, rec_offset, width;
DBF_HEAD dbf_head;
FIELD_REC field_rec;
FIELD_LIST *field_list, *flist_ptr, *last_flist;
char more_fields, *record, *buffer, helper[100];

if (argc!=2) {
   printf("\nUsage:    dbread <filename>\n");
   exit(0);
   }
if ((handle=open(argv[1], O_RDONLY))<0) {
   printf("\nCan not open database file: '%s'\n", argv[1]);
   exit(0);
   }

if (read(handle, (char*)&dbf_head, sizeof(dbf_head))!=sizeof(DBF_HEAD)) {
   printf("\nError: Short file ... header dammaged!\n");
   exit(0);   
   }
printf("Database:      %s\n", argv[1]);
printf("DBF ID:        %d\n", dbf_head.dbf_id);
printf("Last update:   %d %d %d\n", dbf_head.last_update[0], dbf_head.last_update[1], dbf_head.last_update[2]);
printf("Data position: %d\n", dbf_head.data_offset);
printf("Record size:   %d\n", dbf_head.rec_size);
printf("Record number: %1d\n", dbf_head.last_rec);

/* Record counter */
last_flist=NULL;

num_fields=0;
more_fields=1;

printf("\n  NO   ");
while(more_fields) {
   more_fields=(read(handle, (char*)&field_rec, sizeof(FIELD_REC))==sizeof(FIELD_REC));

   if (more_fields) {
      more_fields=(field_rec.field_name[0]!=0x0D);
      if (more_fields) {
         num_fields++;
         flist_ptr = (FIELD_LIST*)malloc(sizeof(FIELD_LIST));
         memcpy((char*)&flist_ptr->field_rec, (char*)&field_rec, sizeof(FIELD_LIST));
         flist_ptr->next_flist=NULL;

	 if (last_flist) {
	    last_flist->next_flist=flist_ptr;
	    last_flist = flist_ptr;
	    }
	 else {
            field_list=flist_ptr;
            last_flist=field_list;
	    }


         switch(last_flist->field_rec.field_type) {
            case 'N':
               width=last_flist->field_rec.len_info.num_size.len;
               break;
            default:
               width=last_flist->field_rec.len_info.char_len;
            }
         strcpy(helper, field_rec.field_name);
         i=strlen(field_rec.field_name);
         memset(helper+i, ' ', width-i);
         helper[width]=0;
         printf("%s ", helper);

         }
      }
   }

printf("\n");
for(i=0; i<dbf_head.rec_size+num_fields+1; i++)
   printf("-");
printf("\n");

/* Prikazivanje podataka ... */
flist_ptr=field_list;
if (lseek(handle, (long)dbf_head.data_offset, 0)<0) {
   printf("Error: Seek error ... short data\n");
   exit(0);
   }

record=(char*)malloc(dbf_head.rec_size);
buffer=(char*)malloc(dbf_head.rec_size);

for (rec_num=1; rec_num<=dbf_head.last_rec; rec_num++) {
   read(handle, record, dbf_head.rec_size);
   printf("%4d ", rec_num);
   
   flist_ptr=field_list;
   
   printf("%c ", record[0]);
   rec_offset=1;
   
   while(flist_ptr) {
      switch(flist_ptr->field_rec.field_type) {
         case 'N':
            width=flist_ptr->field_rec.len_info.num_size.len;
            break;
         default:
            width=flist_ptr->field_rec.len_info.char_len;
         }
      strncpy(buffer, (char*)&record[rec_offset], width);
      buffer[width]=0;
      printf("%s ", buffer);
      rec_offset=rec_offset + width;
      flist_ptr = flist_ptr->next_flist;
      }
   printf("\n");
   }


close(handle);

}
