File: sim2lib1.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 (138 lines) | stat: -rw-r--r-- 2,672 bytes parent folder | download
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#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 sim2lib(inname,outname)
     char *inname;
     char *outname;
{
  FILE *stream,*out;
  int i,j;
  char str[250],strtmp[3];
  int len,result;
  char *pdest;
  unsigned char key[MAX_PHRASE_LEN+1],clen;
  short pykey[MAX_PHRASE_LEN];
  int count;

  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);
  }

  strtmp[2]='\0';
  while( !feof( stream )) {
    if( fgets(str,250,stream)!=NULL){
      len=strlen(str)/2;

      if(len > MAX_PHRASE_LEN) continue;

      for(i=0;i<len;i++){
	strtmp[0]=str[2*i];
	strtmp[1]=str[2*i+1];
	count=0;
	for(j=0;j<MAX_PY_NUM;j++) {
	  pdest = strstr( hztab[j],strtmp);
	  result = pdest- hztab[j];
	  if(pdest!= NULL && !(result%2) ){
	    pykey[i]=j+1;
	    count++;
	  }
	}
	if(count!=1) break;
      }
      if(count!=1){
	*(str+strlen(str)-1)='\0';
	printf("%s(%s %d)\n",str,strtmp,count);
	continue;
      }
      for(i=0;i<len;i++)
	key[i+1]=pykey[i] & 0xff;

      key[0]='\0';
      for(i=0;i<len;i++)
	key[0] |= (pykey[i] & 0x0100) >> (8-i);

      clen=(unsigned char)len;

      fwrite(&clen,sizeof(char),1,out);
      fwrite(key,sizeof(char),len+1,out);
      fwrite(str,sizeof(char),len*2,out);

    }
  }
   
  fclose(stream);
  fclose(out);
  return (0);
}

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