forked from zhujian198/unispim
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsymbol.c
232 lines (185 loc) · 11.4 KB
/
symbol.c
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/** 处理中文、英文符号模块
*/
#include <config.h>
#include <context.h>
#include <utility.h>
#include <symbol.h>
#include <tchar.h>
#include <share_segment.h>
#pragma data_seg(HYPIM_SHARED_SEGMENT)
//全角英文字符
TCHAR full_shape_string[][4] =
{
TEXT("a"), TEXT("b"), TEXT("c"), TEXT("d"), TEXT("e"), TEXT("f"), TEXT("g"), TEXT("h"), TEXT("i"), TEXT("j"), TEXT("k"), TEXT("l"), TEXT("m"), TEXT("n"), TEXT("o"), TEXT("p"), TEXT("q"), TEXT("r"), TEXT("s"), TEXT("t"), TEXT("u"), TEXT("v"), TEXT("w"), TEXT("x"), TEXT("y"), TEXT("z"),
TEXT("A"), TEXT("B"), TEXT("C"), TEXT("D"), TEXT("E"), TEXT("F"), TEXT("G"), TEXT("H"), TEXT("I"), TEXT("J"), TEXT("K"), TEXT("L"), TEXT("M"), TEXT("N"), TEXT("O"), TEXT("P"), TEXT("Q"), TEXT("R"), TEXT("S"), TEXT("T"), TEXT("U"), TEXT("V"), TEXT("W"), TEXT("X"), TEXT("Y"), TEXT("Z"),
TEXT("0"), TEXT("1"), TEXT("2"), TEXT("3"), TEXT("4"), TEXT("5"), TEXT("6"), TEXT("7"), TEXT("8"), TEXT("9"),
};
//单引号
TCHAR quotation1[2][SYMBOL_LENGTH] = { TEXT("‘"), TEXT("’")};
//双引号
TCHAR quotation2[2][SYMBOL_LENGTH] = { TEXT("“"), TEXT("”")};
//int q2_index = 0;
#pragma data_seg()
/** 判断是否为符号字符
*/
int IsSymbolChar(TCHAR ch)
{
int i;
for (i = 0; i < SYMBOL_NUMBER; i++)
if (share_segment->symbol_table[i].english_ch == ch)
return 1;
return 0;
}
/** 更新英文、中文符号表
*/
void UpdateSymbol(TCHAR *english_symbol, TCHAR *chinese_symbol)
{
int i;
//判断是否越界
if (_tcslen(english_symbol) > 2 || _tcslen(chinese_symbol) > SYMBOL_LENGTH)
return;
//寻找符号并更新
for (i = 2; i < SYMBOL_NUMBER; i++) //跳过头两个
if (share_segment->symbol_table[i].english_ch == english_symbol[0])
{
_tcscpy(share_segment->symbol_table[i].chinese_symbol, chinese_symbol);
return;
}
}
/** 装载符号定义文件
*/
int LoadSymbolData(const TCHAR *file_name)
{
TCHAR buffer[10 * 1024]; //10K的buffer
TCHAR line[0x100];
TCHAR *english_symbol, *chinese_symbol;
int char_count;
int length, index;
int i;
if (share_segment->symbol_loaded)
return 1;
//Copy Default symbol table
for (i = 0; i < SYMBOL_NUMBER; i++)
_tcscpy(share_segment->symbol_table[i].chinese_symbol, share_segment->symbol_table[i].default_chinese_symbol);
if (!(length = LoadFromFile(file_name, buffer, _SizeOf(buffer))))
return 0;
index = 1;
length = length / sizeof(TCHAR);
while(index < length)
{
//得到一行数据
char_count = 0;
while(char_count < _SizeOf(line) - 1 && index < length)
{
line[char_count++] = buffer[index++];
if (buffer[index - 1] == '\n')
break; //遇到回车结束
}
line[char_count] = 0; //得到一行数据
//除掉首尾的空白符号
TrimString(line);
if (line[0] == '/' && line[1]=='/') //注释行,跳过
continue;
english_symbol = _tcstok(line, TEXT(" "));
chinese_symbol = _tcstok(0, TEXT(" "));
if (!english_symbol || !chinese_symbol)
continue;
//TrimString(english_symbol);
//TrimString(chinese_symbol);
UpdateSymbol(english_symbol, chinese_symbol);
}
share_segment->symbol_loaded = 1;
return 1;
}
int FreeSymbolData()
{
share_segment->symbol_loaded = 0;
return 1;
}
/** 获得当前的符号
*/
const TCHAR *GetSymbol(PIMCONTEXT *context, TCHAR ch)
{
int i;
for (i = 0; i < SYMBOL_NUMBER; i++)
if (ch == share_segment->symbol_table[i].english_ch)
break;
if (i == SYMBOL_NUMBER)
{
//判断是否为全角字符
if (!(pim_config->hz_option & HZ_SYMBOL_HALFSHAPE))
{
if (ch == ' ')
return TEXT(" ");
if (ch >= 'a' && ch <= 'z')
return full_shape_string[ch - 'a'];
if (ch >= 'A' && ch <= 'Z')
return full_shape_string[26 + ch - 'A'];
if (ch >= '0' && ch <= '9')
return full_shape_string[52 + ch - '0'];
}
return 0; //没有找到,将要用原始的字符串来输出
}
if (!(pim_config->hz_option & HZ_SYMBOL_CHINESE) || (context->english_state == ENGLISH_STATE_INPUT))
return share_segment->symbol_table[i].english_symbol;
if ((context->input_mode & CHINESE_MODE) && (context->last_digital) &&
((pim_config->english_symbol_follow_number) || (pim_config->english_dot_follow_number && ch == '.')))
return share_segment->symbol_table[i].english_symbol;
if (i == 0) //单引号
return quotation1[share_segment->q1_index];
if (i == 1) //双引号
return quotation2[share_segment->q2_index];
//正常符号
return share_segment->symbol_table[i].chinese_symbol;
}
/** 输入了引号,包括单引号、双引号
*/
void CheckQuoteInput(HZ symbol)
{
if (symbol == *(HZ*)TEXT("“"))
share_segment->q2_index = 1;
else if (symbol == *(HZ*)TEXT("”"))
share_segment->q2_index = 0;
else if (symbol == *(HZ*)TEXT("‘"))
share_segment->q1_index = 1;
else if (symbol == *(HZ*)TEXT("’"))
share_segment->q1_index = 0;
}
int IsURLInput(const TCHAR *input)
{
static TCHAR *direct_inputs[] =
{
TEXT("www."), TEXT("ftp."), TEXT("bbs."), TEXT("forum."), TEXT("mail."), TEXT("blog."),
TEXT("ftp:"), TEXT("http:"), TEXT("https:"), TEXT("mailto:"), TEXT("file:"),
};
int i;
for (i = 0; i < sizeof(direct_inputs) / _SizeOf(direct_inputs[0]) / sizeof(TCHAR); i++)
if (!_tcsncmp(direct_inputs[i], input, _tcslen(direct_inputs[i])))
return 1;
return 0;
}
/** 获得全角字符串
*/
void GetFullShapes(const TCHAR *src, TCHAR *dest, int dest_len)
{
int i;
TCHAR c[2] = {0, 0};
if (IsURLInput(src))
return;
for (i = 0; i < (int)_tcslen(src); i++)
{
if (src[i] == ' ')
_tcscat_s(dest, dest_len, TEXT(" "));
else if (src[i] >= 'a' && src[i] <= 'z')
_tcscat_s(dest, dest_len, full_shape_string[src[i] - 'a']);
else if (src[i] >= 'A' && src[i] <= 'Z')
_tcscat_s(dest, dest_len, full_shape_string[26 + src[i] - 'A']);
else if (src[i] >= '0' && src[i] <= '9')
_tcscat_s(dest, dest_len, full_shape_string[52 + src[i] - '0']);
else
{
c[0] = src[i];
_tcscat_s(dest, dest_len, c);
}
}
}