File: tab2txt.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 (150 lines) | stat: -rw-r--r-- 3,323 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
139
140
141
142
143
144
145
146
147
148
149
150
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include "pinyin.h"
#include "safestring.h"

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

int LoadTable(char* pathname)
{
  FILE *stream;
  char str[250], *strpy, *strhz;
  int i=1, j=0, lastpy=0, curpy;

  if ( (stream = fopen( pathname, "r" )) == NULL )
  {
    fprintf(stderr,"%s file not found\n",pathname);
    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]-'a';
      if (curpy != lastpy) j = 0;
      safe_strncpy(pytab[curpy][j].py, strpy, MAX_PY_LEN);
      pytab[curpy][j].key = i;
      lastpy = curpy;

      i++,j++;
    }
  }
  fclose(stream);
  return 0;
}

int PrintOutPhrase(char *infile,char *pathname)
{
  FILE *out,*in;
  u_char len;
  u_char key[MAX_PHRASE_LEN+1],str[2*MAX_PHRASE_LEN+1],
           tmppy[10],pinyin[70];
  char tab='\011',*p;
  int i,j,k,size,l,m,fsize;
  u_char freq;
  Phrase *p0;
  u_short count,keyint,keytmp;
  char *file;

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

  if ( (in = fopen( infile, "rb" )) == NULL )
  {
    fprintf(stderr,"%s cant open.\n",infile);
    exit(1);
  }

  if (fseek(in,-4,SEEK_END) == -1 ||
      fread(&fsize,sizeof(int),1,in) != 1 ||
      fsize != ftell(in))   // error!!
  {
      fprintf(stderr,"%s is not a valid pinyin phrase file.\n",infile);
      exit(1);
  }

  if ( (file = (char*)malloc(fsize)) == NULL)
  {
       fprintf(stderr,"Not enough memory!\n");
       exit(1);
  }

  fseek(in,0,SEEK_SET);
  fread(file,fsize,1,in);

  p = file;
  for(i = 1; i < MAX_PY_NUM; i++)
  {
     count = *(u_short *)p;
     p += sizeof(u_short);
     if (count == 0) continue;

     for(j = 0; j < count; j++)
     {
         p0 = (Phrase *)p; 
         len = p0->len;
         pinyin[0] = '\0';  
 	 for(k=0; k<len; k++)
         {
	    keyint = (u_short)p0->key[0];
	    keyint = (keyint<<(8-k)) & 0x0100;
	    keyint += (u_short)p0->key[k+1];
	    for(l=0; l<26; l++)
	      for(m=0; keytmp=pytab[l][m].key; m++)
	        if (keyint == keytmp)
                {
		   snprintf(tmppy,sizeof(tmppy),"%s ",pytab[l][m].py);
                   break;
                }
	    if (strlen(pinyin) + strlen(tmppy) + 1 < sizeof(pinyin))
		strcat(pinyin,tmppy);
	    else {
		fprintf(stderr, "buffer overrun\n");
		abort();
	    }
	 }

         for(k = 0; k < p0->count; k++)
         {
	     if (sizeof(str) < (size_t)(2 * len + 1)) {
		 fprintf(stderr, "buffer overrun\n");
		 abort();
	     } else
		 memcpy(str,p0->key+len+1+(2*len+1)*k,2*len);
             str[2*len] = '\0';
             freq = p0->key[len+1+(2*len+1)*k+2*len];
             if (len > 1)  // single char ignored
             fprintf(out,"%s %s %d\n",str,pinyin,freq);
         }
         p += 2+len+1+(2*len+1)*p0->count;
     }
  } // MAX_PY

  fclose(in);
  fclose(out);
  return 1;
}

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

  return 0;  // no error
}