#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <io.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>

#define IDXSIZE 20
typedef struct MREC {int ipnt; short fid; short gid; int num; int len; int pnt; int jump; struct MREC *next;} Mrec;
#define FALSE 0
#define XMAX_GROUPS 60000 /* Cannot be increased easily */

#define BFSZ 600
void frec_to_mrec(char *bf, int pnt, Mrec *m)
{
	m->ipnt = pnt;
	memcpy(&m->fid,bf,2);
	memcpy(&m->gid,bf+2,2);
	memcpy(&m->num,bf+4,4);
	memcpy(&m->pnt,bf+8,4);
	memcpy(&m->len,bf+12,4);
	memcpy(&m->jump,bf+16,4);
}

int idump(char *fname);
int main(int argc, char *argv[])
{
	/* bswap(argv[1]); */
	idump(argv[1]);
	return 0;
}
int cvtorder(int x);
int idump(char *fname)
{
	char bf[1000];
	int pnt;
	Mrec m;
	FILE *f;
	int p;

	f = fopen("dbfirst.idx","rb");
	if (f==NULL) {perror("Couldn't read file"); return 0;}
	for (p=0;(!feof(f)) && ((p/4)<XMAX_GROUPS);p+= 4) {
		if (fread(&pnt,4,1,f)<1) break;
		if (pnt>=0) printf("group [%x] pnt %x \n",p/4,pnt);
	}
	fclose(f);
	f = fopen("dbi.idx","rb");
	for (p=0;!feof(f);p+= IDXSIZE) {
		if (fread(bf,IDXSIZE,1,f)<1) break;
		frec_to_mrec(bf,p,&m);
		printf("[%x] fid=%d %d:%d pnt[%x:%d] j=%x\n",
			p,m.fid,m.gid,m.num,m.pnt,m.len,m.jump);

	}
	perror("End reading");
	fclose(f);
}
void fixint(char *x);
void fixint(char *x)
{
 	char bf[4];
	bf[0] = x[0];
	bf[1] = x[1];
	bf[2] = x[2];
	bf[3] = x[3];

	x[0] = bf[3];
	x[1] = bf[2];
	x[2] = bf[1];
	x[3] = bf[0];
 }
