#include <stdio.h>

typedef struct {
  int files;
  char **filenames;
  struct {
    int file:8;
    int offset:24;
  } allkanji[94*94];
} indexinfo;
#if 0
main(ac,ag)
char **ag;
{
  char buf[4096];
  int code=strtol(ag[1],NULL,16);
  read_index(ag[2]);
  searchcode(code,buf);
  puts(buf);
}
#endif
read_index(indexfile)
char *indexfile;
{
  FILE *fd;
  char buf[256];
  int len,allfile,i;

  if((fd=fopen(indexfile,"r"))==NULL){
    fprintf(stderr,"Index file %s is not foune\n",indexfile);
    exit(1);
  }
  fgets(buf,256,fd);
  if(strncmp(buf,"TYPE1",5)){
    fprintf(stderr,"%s is not a index file\n",indexfile);
    exit(1);
  }
  allfile=atoi(buf+6);
  filenames=malloc(allfile*sizeof(char *));
  for(i=0;i<allfile;i++){
    fgets(buf,256,fd);
    len=strlen(buf);
    buf[len-1]=0;
    filenames[i]=malloc(len);
    strcpy(filenames[i],buf,len-1);
  }
  fread(allkanji,1,sizeof(allkanji),fd);
  fclose(fd);
}
searchcode(code,buf)
char *buf;
{
  FILE *fd;
  int koffset=(((code>>8)&255)-0x21)*94+((code&255)-0x21);
  if((fd=fopen(filenames[allkanji[koffset].file],"r"))==NULL){
    fprintf(stderr,"Can't open file %s\n",filenames[allkanji[koffset].file]);
    exit(1);
  }
  fseek(fd,allkanji[koffset].offset,0);
  fgets(buf,4096,fd);
  fclose(fd);
}
