File: lib2sim.c

package info (click to toggle)
cce 0.36-1.1
  • links: PTS
  • area: main
  • in suites: potato, woody
  • size: 4,168 kB
  • ctags: 1,407
  • sloc: ansic: 15,193; makefile: 83; sh: 23
file content (120 lines) | stat: -rw-r--r-- 2,481 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <stdio.h>
#include <string.h>
#include "safestring.h"

#define MAX_PY_NUM 420
#define MAX_EACH_PY 38
#define MAX_EACH_HZ 241

/* Important,need change according to assoc.h */
#define MAX_PHRASE_LEN 6

typedef struct {
  unsigned short key;
  char py[7];
} PinYin;

PinYin pytab[26][MAX_EACH_PY];
char hztab[MAX_PY_NUM][MAX_EACH_HZ]; 

int LoadTable()
{

  FILE *stream;
  char str[250], *strpy, *strhz;
  int i=0,j=0;
  int tmp,curpy;
  PinYin *pyt=(PinYin *)pytab;

  if( (stream = fopen( "/usr/local/lib/pyinput.dic/table", "r" )) == NULL ){
    fprintf(stderr,"/usr/local/lib/pyinput.dic/table not found\n");
    exit(1);
  }

  while( !feof( stream )) {
    if( fgets(str,250,stream)!=NULL){
      strpy = strtok(str, " \f\n\r\t\v");
      strhz = strtok(NULL, " \f\n\r\t\v");

      safe_strncpy(hztab[i], strhz, MAX_EACH_HZ);

      curpy=strpy[0]-97;
      if(curpy!=tmp) {j=0;}
      safe_strncpy((pyt+curpy*MAX_EACH_PY+j)->py, strpy, 7);
      (pyt+curpy*MAX_EACH_PY+j)->key=i+1;
      tmp=curpy;

      i++,j++;
    }
  }

  fclose(stream);
  return (0);
}

int lib2sim(inname,outname)
     char *inname;
     char *outname;
{
  FILE *stream,*out;
  unsigned char clen;
  int len;
  unsigned char key[MAX_PHRASE_LEN+1],str[2*MAX_PHRASE_LEN+1];
  char tab='\011';
  int i,j,k;
  PinYin *pyt=(PinYin *)pytab;
  unsigned short keyint,keytmp;

  if( (stream = fopen( inname, "r" )) == NULL ){
    fprintf(stderr,"%s cant open.\n",inname);
    exit(1);
  }

  if( (out = fopen( outname, "wb" )) == NULL ){
    fprintf(stderr,"%s cant open.\n",outname);
    exit(1);
  }

  while( !feof( stream )) {
    if(fread(&clen,sizeof(char),1,stream)){
      len=(int)clen;
      if( len > 0 && fread(key,sizeof(char),len+1,stream) &&
	  fread(str,sizeof(char),len*2,stream) ) {
	*(str+len*2)='\0';
	fprintf(out,"%s ",str);
	for(k=0;k<len;k++){
	  keyint=(unsigned short)key[0];
	  keyint=(keyint<<(8-k)) & 0x0100;
	  keyint += (unsigned short)key[k+1];
	  for(i=0;i<26;i++){
	    for(j=0;(keytmp=(pyt+i*MAX_EACH_PY+j)->key);j++){
	      if(keyint==keytmp){
		fprintf(out,"%s",(pyt+i*MAX_EACH_PY+j)->py);
		if(k!=len-1)
		  fprintf(out," ");
		else
		  fprintf(out,"\n");
	      }
	    }
	  }
	}
      }
    }
  }
  fclose(stream);
  fclose(out);
  return 1;
}

void main(argc,argv)
     int argc;
     char **argv;
{
  if(argc != 3) {
    fprintf(stderr,"usage: %s <input_name> <output_name>\n",argv[0]);
    return;
  }
  LoadTable();
  lib2sim(argv[1],argv[2]);
  return;
}