~ubuntu-branches/ubuntu/precise/hime/precise

« back to all changes in this revision

Viewing changes to src/pinyin.c

  • Committer: Package Import Robot
  • Author(s): Yao Wei (魏銘廷)
  • Date: 2012-01-14 00:24:08 UTC
  • Revision ID: package-import@ubuntu.com-20120114002408-e79gagbeg1rt8npv
Tags: upstream-0.9.9
Import upstream version 0.9.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2011 Edward Der-Hua Liu, Hsin-Chu, Taiwan
 
2
 *
 
3
 * This library is free software; you can redistribute it and/or
 
4
 * modify it under the terms of the GNU Lesser General Public
 
5
 * License as published by the Free Software Foundation; either
 
6
 * version 2.1 of the License, or (at your option) any later version.
 
7
 *
 
8
 * This library is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
11
 * Lesser General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU Lesser General Public
 
14
 * License along with this library; if not, write to the Free Software
 
15
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
16
 */
 
17
 
 
18
#include "hime.h"
 
19
#include "pho.h"
 
20
#include "tsin.h"
 
21
#include "gst.h"
 
22
#include "pho-status.h"
 
23
 
 
24
extern void key_typ_pho(phokey_t phokey, u_char rtyp_pho[]);
 
25
 
 
26
gboolean pin2juyin(gboolean full_match)
 
27
{
 
28
  int i;
 
29
 
 
30
  char pin[8];
 
31
  bzero(poo.typ_pho, sizeof(poo.typ_pho));
 
32
  if (poo.inph[0]=='`') {
 
33
    poo.typ_pho[0]=BACK_QUOTE_NO;
 
34
    poo.typ_pho[1]=poo.inph[1];
 
35
    return TRUE;
 
36
  }
 
37
 
 
38
  int inphN = strlen(poo.inph);
 
39
  for(i=0; i < pin_juyinN; i++) {
 
40
    bzero(pin, sizeof(pin));
 
41
    memcpy(pin,  pin_juyin[i].pinyin, sizeof(pin_juyin[0].pinyin));
 
42
 
 
43
    int pinN = strlen(pin);
 
44
    if (pinN < inphN)
 
45
      continue;
 
46
 
 
47
    if (full_match && pinN != inphN)
 
48
      continue;
 
49
 
 
50
    if (!memcmp(pin, poo.inph, inphN)) {
 
51
//      dbg("pin %s %s\n", pin, poo.inph);
 
52
//      prph(pin_juyin[i].key);
 
53
      break;
 
54
    }
 
55
  }
 
56
 
 
57
  if (i==pin_juyinN) {
 
58
    dbg("pin2juyin not found ..\n");
 
59
    return FALSE;
 
60
  }
 
61
 
 
62
  bzero(poo.typ_pho, sizeof(poo.typ_pho));
 
63
//  prph(pin_juyin[i].key); dbg(" %x ph\n", pin_juyin[i].key);
 
64
  key_typ_pho(pin_juyin[i].key, (u_char *)poo.typ_pho);
 
65
 
 
66
//  dbg("pin2juyin found %d\n", poo.typ_pho[0]);
 
67
 
 
68
  return TRUE;
 
69
}
 
70
 
 
71
 
 
72
gboolean inph_typ_pho_pinyin(int newkey)
 
73
{
 
74
  int i=0;
 
75
 
 
76
//  dbg("inph_typ_pho_pinyin '%c'\n", newkey);
 
77
 
 
78
  if (newkey != ' ') {
 
79
    char num = phkbm.phokbm[newkey][0].num;
 
80
    int typ = phkbm.phokbm[newkey][0].typ;
 
81
 
 
82
//    dbg("cccc num %d typ:%d\n", num, typ);
 
83
 
 
84
    if (typ==3) {
 
85
      pin2juyin(TRUE);
 
86
      poo.typ_pho[typ] = num;
 
87
//      tss.chpho[tss.c_idx].flag |=FLAG_CHPHO_PINYIN_TONE;
 
88
//      dbg("set %d\n",tss.c_idx);
 
89
//      if (!num)
 
90
//        poo.typ_pho[typ]= PHO_PINYIN_TONE1;
 
91
      return PHO_STATUS_OK_NEW | PHO_STATUS_TONE;
 
92
    }
 
93
 
 
94
//    dbg("'%c' %d\n", newkey, typ);
 
95
 
 
96
    for(i=0; i < 7; i++)
 
97
      if (!poo.inph[i])
 
98
        break;
 
99
    if (i==7)
 
100
      return FALSE;
 
101
    poo.inph[i] = newkey;
 
102
  }
 
103
 
 
104
  if (pin2juyin(newkey==' ')) {
 
105
//    dbg("zzzz\n");
 
106
    if (newkey==' ')
 
107
      return PHO_STATUS_OK_NEW;
 
108
    if (poo.typ_pho[0]==BACK_QUOTE_NO && poo.typ_pho[1])
 
109
      return PHO_STATUS_OK_NEW | PHO_STATUS_TONE;
 
110
//    dbg("ok %d\n", poo.typ_pho[0]);
 
111
    return PHO_STATUS_OK;
 
112
  }
 
113
 
 
114
//  dbg("yyy %d\n", i);
 
115
 
 
116
  poo.inph[i]=0;
 
117
  if (!i)
 
118
    return PHO_STATUS_REJECT;
 
119
 
 
120
  // v is not used as the first key
 
121
  int j;
 
122
  for(j=0; j < pin_juyinN; j++)
 
123
    if (pin_juyin[j].pinyin[0]==newkey)
 
124
      break;
 
125
 
 
126
  pin2juyin(FALSE);
 
127
  if (j==pin_juyinN)
 
128
    return PHO_STATUS_REJECT;
 
129
 
 
130
  bzero(poo.inph, sizeof(poo.inph));
 
131
  poo.inph[0]=newkey;
 
132
  return PHO_STATUS_OK_NEW|PHO_STATUS_PINYIN_LEFT;
 
133
}
 
134
 
 
135
extern int text_pho_N;
 
136
 
 
137
void load_pin_juyin()
 
138
{
 
139
  text_pho_N = 6;
 
140
  char pinfname[128];
 
141
 
 
142
  get_sys_table_file_name("pin-juyin.xlt", pinfname);
 
143
//  dbg("pinyin kbm %s\n", pinfname);
 
144
 
 
145
  FILE *fr;
 
146
  if ((fr=fopen(pinfname,"rb"))==NULL)
 
147
     p_err("Cannot open %s", pinfname);
 
148
 
 
149
  fread(&pin_juyinN, sizeof(short), 1, fr);
 
150
  pin_juyin = tmalloc(PIN_JUYIN, pin_juyinN);
 
151
  fread(pin_juyin, sizeof(PIN_JUYIN), pin_juyinN, fr);
 
152
  fclose(fr);
 
153
}