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

typedef struct {int pnt, len, hlen;} Hrec;
#define HEAD_SIZE 102
#define FALSE 0

int idump(char *fname);
int main(int argc, char *argv[])
{
	bswap(argv[1]);
	idump(argv[1]);
}
int cvtorder(int x);
int idump(char *fname)
{
	Hrec h[110];
	int idx;
	int i,n;

	idx = open(fname,O_RDONLY | O_BINARY ,0);
	if (idx<0) return FALSE;
	n = read(idx,h,HEAD_SIZE * sizeof(Hrec));
	close(idx);
	for (i=0; i<HEAD_SIZE; i++) {
		printf("Point [%2d] %6d len %6d hlen %6d \n",i,h[i].pnt,h[i].len,h[i].hlen); 
	}
}
void fixint(char *x);
int bswap(char *fname)
{
	char bf[HEAD_SIZE * 4 * 3];
	int idx;
	int i,n;

	idx = open(fname,O_RDONLY | O_BINARY ,0);
	if (idx<0) return FALSE;
	n = read(idx,bf,HEAD_SIZE * sizeof(Hrec));
	close(idx);

	for (i=0; i<HEAD_SIZE * 3 ; i++) {
	 	fixint(bf+i*4);
	}

	idx = open("out.idx",O_CREAT | O_WRONLY | O_BINARY ,0);
	if (idx<0) return FALSE;
	n = write(idx,bf,HEAD_SIZE * sizeof(Hrec));
	close(idx);
}
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];
 }
