/*
	Copyright (C) 1994,1995	 Edward Der-Hua Liu, Hsin-Chu, Taiwan
*/

#include <stdio.h>
#include <memory.h>
#include <sys/types.h>
#include "xi.h"
#include "constant.h"

unsigned char *bf;

extern void *malloc(size_t size);
extern void free(void *ptr);
extern void SORTFUNC(void *base, size_t nmemb, size_t size,
		int (*compar)(const void *, const void *));
extern void ser_b5(u_char *ch, u_char *tt);
extern int unlink(const char *pathname);


#define KeyNum (27)
u_long MaxPress=5;
unsigned char keymap[]=" abcdefghijklmnopqrstuvwxyz";
unsigned char tran[]="  日月金木水火土竹戈十大中一弓人心手口尸廿山女田難卜重";

#define swap(a,b) { tt=a; a=b; b=tt; }

typedef struct {
	u_char key[4];
	u_char ch[2];
	u_short oseq;
} ITE;

int qcmp(a, b)
  ITE *a, *b;
{
	if (CONVT(a->key) > CONVT(b->key)) return 1;
	if (CONVT(a->key) < CONVT(b->key)) return -1;
	if (a->oseq > b->oseq) return 1;
	if (a->oseq < b->oseq) return -1;
	return 0;
}

int main()
{
FILE *fp,*fw;
unsigned long kk;
unsigned long k1,k2,k3,k4,k5,tt;
unsigned long /*q,r,*/ofs;
unsigned char c1,c2;
unsigned long counter,ckey[5],cwordstart;
int i,/*j,*/fsize;
ITE *itar;
/*unsigned short idx1[27];*/
/*char def1[27];*/
u_short orgseq=0;
  
puts("%NoBosh 1");
puts("%ename changjei");
puts("%prompt 【倉頡】");
puts("%selkey 1234567890");
puts("%dupsel 9");
puts("%keyname begin");
for(i=1;i<=25;i++)
	printf("%c %c%c\n", keymap[i], tran[i*2], tran[i*2+1]);
puts("%keyname end");
puts("%quick begin");
puts("%quick end");
puts("%sel1st begin");
puts("%sel1st end");

if ((fp=fopen("changjei.tab","r"))!=NULL) {

	if ((bf=(unsigned char *)malloc(40000))==NULL) {
		puts("malloc err");
		exit(1);
	}

	fsize=fread(bf,1,40000,fp);
	fclose(fp);

	if ((fp=fopen("cj.tmp1","w"))==NULL) {
  		puts("create");
  		exit(-1);
	}

	orgseq=0;
	for(i=0;i<fsize;i+=3) {
	/*       kk=*(unsigned long *)(&bf[i]) & 0xffffff; */
		kk=((u_long)bf[i+2]<<16) |(u_long)(bf[i+1]<<8) |(u_long)bf[i];   /* avoid endian */


       		k5=kk%27;
       		kk/=27;
       		k4=kk%27;
       		kk/=27;
       		k3=kk%27;
       		kk/=27;
       		k2=kk%27;
       		k1=kk/27;
       		if (k1>27) break;  /* end of table mark */

       		if (k5!=0)
	 		swap(k2,k5)
       		else
       		if (k4!=0)
			swap(k2,k4)
       		else
       		if (k3!=0)
	 		swap(k2,k3)
	 	kk=k5|k4<<6|k3<<12|k2<<18|k1<<24;
		fwrite(&kk,4,1,fp);

		ofs=i/3;
		ofs+=0x8800;
		c1=ofs>>8;
		c2=ofs&255;
		fwrite(&c1,1,1,fp);
		fwrite(&c2,1,1,fp);
		fwrite(&orgseq,2,1,fp);
		orgseq++;
	}
	fclose(fp);
	fsize/=3;

	if( (fp=fopen("cj.tmp1","r"))==NULL ) {
		fprintf(stderr,"Can't open cj.tmp1\n");
		exit(1);
	}
	
	free(bf);
	i=0;
	if ((itar=(ITE *)malloc((fsize+1)*sizeof(ITE)))==NULL) {
		puts("-- malloc err 2");
		exit(-1);
	}

	fsize=fread(itar, sizeof(ITE), fsize, fp);

	SORTFUNC(itar,fsize,sizeof(ITE),qcmp);

	for(i=0;i<fsize;i++) {
  		u_long kk=CONVT(itar[i].key);
  		char tt[3];

		ser_b5(itar[i].ch, tt);
  		k1=(kk>>24)&0x1f;
  		k2=(kk>>18)&0x1f;
  		k3=(kk>>12)&0x1f;
  		k4=(kk>>6)&0x1f;
  		k5=(kk)&0x1f;
		#define tk(kk) (keymap[kk])

		printf("%c", tk(k1));
		if (k2) printf("%c", tk(k2));
		if (k3) printf("%c", tk(k3));
		if (k4) printf("%c", tk(k4));
		if (k5) printf("%c", tk(k5));
		printf(" %s\n",tt );
	}
	unlink("cj.tmp1");
	exit(0);
}
else if((fp=fopen("changjei.uil","r"))!=NULL) {
	
	if((bf=(unsigned char *)malloc(2001))==NULL) {
		fprintf(stderr,"Allocate Memory Error[bf]\n");
		exit(1);
	}

	fsize=0;
	orgseq=0;
	
	if((fw=fopen("cj.tmp1","w"))==NULL) {
		fprintf(stderr,"Can't create cj.tmp1\n");
		exit(1);
	}

	while(!feof(fp)) {
		fgets(bf,2000,fp);
		if((bf[0]=='[')&&(bf[3]=='t')) break;
		else continue;
	}

	while(!feof(fp)) {
		fgets(bf,2000,fp);
		/*fprintf(stderr,"%s",bf);*/
		
		cwordstart=0;
		while(( bf[cwordstart]!='=')&&( cwordstart<2002 )) {
			if( bf[cwordstart]=='\0' ) {cwordstart=0;break;}	
			cwordstart++;
		}
		if( cwordstart==0 ) break;
	
		for(counter=0;counter<cwordstart;counter+=2) {
			for(kk=2;kk<53;kk+=2) 
				if((tran[kk]==bf[counter])&&(tran[kk+1]==bf[counter+1])) break;
			ckey[counter>>1]=(kk+1)>>1;
		}
		while(counter<10) {
			ckey[counter>>1]=0;
			counter+=2;	
		}
			
		kk=ckey[4]|ckey[3]<<6|ckey[2]<<12|ckey[1]<<18|ckey[0]<<24;	
		for( counter=cwordstart+1;(bf[counter]>0x40)&&(bf[counter]>0x40);counter+=2 ) {
			orgseq=(bf[counter]<<8)+bf[counter+1];

			fwrite(&kk,4,1,fw);
			fwrite(&bf[counter+1],1,1,fw);
			fwrite(&bf[counter],1,1,fw);
			fwrite(&orgseq,2,1,fw);
			orgseq++;
			fsize++;
		}	
	}

	fclose(fp);
	fclose(fw);

	if( (fp=fopen("cj.tmp1","r"))==NULL ) {
		fprintf(stderr,"Can't open cj.tmp1\n");
		exit(1);
	}

	free(bf);
	i=0;
	if ((itar=(ITE *)malloc((fsize+1)*sizeof(ITE)))==NULL) {
		puts("-- malloc err 2");
		exit(-1);
	}

	fsize=fread(itar, sizeof(ITE), fsize, fp);

	SORTFUNC(itar,fsize,sizeof(ITE),qcmp);

	for(i=0;i<fsize;i++) {
  		u_long kk=CONVT(itar[i].key);

  		k1=(kk>>24)&0x1f;
  		k2=(kk>>18)&0x1f;
  		k3=(kk>>12)&0x1f;
  		k4=(kk>>6)&0x1f;
  		k5=(kk)&0x1f;
		#define tk(kk) (keymap[kk])

		printf("%c", tk(k1));
		if (k2) printf("%c", tk(k2));
		if (k3) printf("%c", tk(k3));
		if (k4) printf("%c", tk(k4));
		if (k5) printf("%c", tk(k5));
		printf(" %c%c\n",itar[i].ch[1],itar[i].ch[0] );
	}
	unlink("cj.tmp1");
	exit(0);
}
else {
	fprintf(stderr,"Can't open changjei.tab[changjei.uil]\n");
	exit(1);
}
}
