forked from zhujian198/unispim
-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathspw.c
1919 lines (1711 loc) · 99.7 KB
/
spw.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/** 用户自造词模块
* 用户自造词的读取以及检索功能。
* 用户自造词简称“短语”, spw(special word)
*
* 1. 20000条用户自造词
* 2. 每条可以达到1024个字节
* 3. 短语格式:
* name = item1
*
* 格式定义
* 项名称:英文字母打头,可以是大写字母,下划线可以使用。
* 如:hello, he_ll, Hell_
* 用等号表达后续的内容,如:china=中华人民共和国
* 项的名字可以重复:如:
* china=中国
* china=中华人民共和国
* 内容约束:
* 短语名称:第一列必须是英文字母并且满足名称的要求;如:^ abcd=不是合法短语名称,作为内容处理。
* 短语名称定义行中如果没有内容,则这个回车被忽略;如:abcd= 这个回车将被忽略
* 后续的内容行中的空格作为内容进行处理(不忽略);如:abcd=ab def ggef
* 后续内容最后的空行将被忽略(一般用于进行短语的分隔)。abcd=
* ABCD
* <-这个空行将被忽略
* <-这个空行将被忽略
* efgh=abddefef
*
* 内容中可以出现=,只要等号前面不是短语名称的合法组合。如:abcd=e=mc2。
*
* SPW的存储设计
* 从文件中逐条读取数据,存放在spw_buffer中,该缓冲区大小为1M(char),使用spw_index指向
* spw_buffer的位置。
* 缓冲区的结构为:短语名称,短语内容。这些字符串必须以0为结尾。
*
* SPW的特殊设计:
* 采纳sougou拼音的日期与时间输入方式进行候选选择,方便用户
* rq_, RQ, Rq, rQ->日期输入
* 2007-4-26,2007.4.26,2007-04-26,2007年4月26日,二〇〇七年四月二十六日,
* sj_, SJ, Sj, sJ->时间输入
* 2007-4-26 10:49,10:49,10:49:47, 2007年4月26日10:49:44,
*
*/
/* 装载用户自造词库,如果已经在内存,则返回SPW词库指针,否则
* 进行内存的加载。
* 参数:
* spw_file_name 自造词库文件名
* 返回:
* 装载成功:spw词库指针
* 失败:0
*/
#include <zi.h>
#include <assert.h>
#include <string.h>
#include <spw.h>
#include <config.h>
#include <utility.h>
#include <win32/pim_ime.h>
#include <tchar.h>
#include <pim_resource.h>
#include <fontcheck.h>
#include <gbk_map.h>
#include <share_segment.h>
#include <context.h>
#include <shlwapi.h>
static TCHAR *spw_buffer = 0;
static TCHAR *spw_share_name = TEXT("HYPIM_SPW_SHARED_NAME");
static const TCHAR digit_hz_string[][4] =
{
TEXT("〇"), TEXT("一"), TEXT("二"), TEXT("三"), TEXT("四"), TEXT("五"),
TEXT("六"), TEXT("七"), TEXT("八"), TEXT("九"), TEXT("十")
};
//日期输入前缀
static const TCHAR date_spw_string[][8] =
{ TEXT("rq_"), TEXT("rq:"), TEXT("RQ"), TEXT("Rq"), TEXT("rQ"), TEXT("date"), };
//时间输入前缀
static const TCHAR time_spw_string[][8] =
{ TEXT("sj_"), TEXT("sj:"), TEXT("SJ"), TEXT("Sj"), TEXT("sJ"), TEXT("time"), };
static const TCHAR DEFAULT_SPW_FILENAME[][MAX_FILE_NAME_LENGTH]={
SYS_SPW_FILE_NAME,
};
static const TCHAR special_hint[][MAX_SPW_HINT_STRING] =
{
TEXT("i"), TEXT("小写数字单位模式(I为大写)"),
TEXT("U"), TEXT("小写数字单位模式(I为大写)"),
TEXT("U+"), TEXT("UNICODE字符输入模式"),
TEXT("I"), TEXT("大写数字单位模式(i/U为小写)"),
TEXT("E"), TEXT("输入程序(文档)名,按TAB输入参数,按空格执行"),
TEXT("u"), TEXT("输入程序(文档)名,按TAB输入参数,按空格执行"),
TEXT("v"), TEXT("英文输入模式,TAB为输入空格"),
TEXT("B"), TEXT("笔画模式:h横、s竖、p撇、n捺、z折、d点"),
};
static const TCHAR special_hint_sp[][MAX_SPW_HINT_STRING] =
{
TEXT("i"), TEXT("小写数字单位模式(I为大写)"),
TEXT("U"), TEXT("小写数字单位模式(I为大写)"),
TEXT("U+"), TEXT("UNICODE字符输入模式"),
TEXT("I"), TEXT("大写数字单位模式(U为小写)"),
TEXT("E"), TEXT("输入程序(文档)名,按TAB输入参数,按空格执行"),
TEXT("u"), TEXT("输入程序(文档)名,按TAB输入参数,按空格执行"),
TEXT("v"), TEXT("英文输入模式,TAB为输入空格"),
TEXT("B"), TEXT("笔画模式:h横、s竖、p撇、n捺、z折、d点"),
};
static TCHAR *hz_xx_numbers = TEXT("〇一二三四五六七八九十");
static TCHAR *hz_dx_numbers = TEXT("零壹贰叁肆伍陆柒捌玖拾");
static TCHAR *i_numcadidates[] =
{
TEXT("一二三四五六七八九十"),
TEXT("①②③④⑤⑥⑦⑧⑨⑩"),
TEXT("ⅰⅱⅲⅳⅴⅵⅶⅷⅸⅹ"),
TEXT("➊➋➌➍➎➏➐➑➒➓"),
TEXT("⒈⒉⒊⒋⒌⒍⒎⒏⒐⒑"),
TEXT("⑴⑵⑶⑷⑸⑹⑺⑻⑼⑽"),
TEXT("壹贰叁肆伍陆柒捌玖拾"),
TEXT("ⅠⅡⅢⅣⅤⅥⅦⅧⅨⅩ"),
TEXT("㈠㈡㈢㈣㈤㈥㈦㈧㈨㈩"),
TEXT("㊀㊁㊂㊃㊄㊅㊆㊇㊈㊉")
};
static TCHAR *I_numcadidates[] =
{
TEXT("壹贰叁肆伍陆柒捌玖拾"),
TEXT("ⅠⅡⅢⅣⅤⅥⅦⅧⅨⅩ"),
TEXT("㈠㈡㈢㈣㈤㈥㈦㈧㈨㈩"),
TEXT("㊀㊁㊂㊃㊄㊅㊆㊇㊈㊉"),
TEXT("一二三四五六七八九十"),
TEXT("①②③④⑤⑥⑦⑧⑨⑩"),
TEXT("ⅰⅱⅲⅳⅴⅵⅶⅷⅸⅹ"),
TEXT("➊➋➌➍➎➏➐➑➒➓"),
TEXT("⒈⒉⒊⒋⒌⒍⒎⒏⒐⒑"),
TEXT("⑴⑵⑶⑷⑸⑹⑺⑻⑼⑽")
};
static TCHAR *ext_unit[] =
{
TEXT(""), TEXT("万"), TEXT("亿"), TEXT("万亿"), TEXT("亿亿"), TEXT("万亿亿"),
TEXT("亿亿亿"), TEXT("万亿亿亿"), TEXT("亿亿亿亿"),
};
static TCHAR *base_xx_unit[] = { TEXT(""), TEXT("十"), TEXT("百"), TEXT("千"), };
static TCHAR *base_dx_unit[] = { TEXT(""), TEXT("拾"), TEXT("佰"), TEXT("仟"), };
static TCHAR *digital_xx_string[] =
{
TEXT("零"), TEXT("一"), TEXT("二"), TEXT("三"), TEXT("四"), TEXT("五"),
TEXT("六"), TEXT("七"), TEXT("八"), TEXT("九")
};
static TCHAR *digital_dx_string[] =
{ TEXT("零"), TEXT("壹"), TEXT("贰"), TEXT("叁"), TEXT("肆"), TEXT("伍"),
TEXT("陆"), TEXT("柒"), TEXT("捌"), TEXT("玖")
};
static TCHAR *money_unit[] = {TEXT("整"), TEXT("角"), TEXT("分")};
//转换数字串,简单转换(0-〇,1-一,2-二,3-三...)
void GetSimpleNumberString(const TCHAR *input, TCHAR *buffer, int length, int dx_number, int chinese_number)
{
TCHAR *numbers, *p, *q, ch;
int i;
if (dx_number)
numbers = hz_dx_numbers;
else
numbers = hz_xx_numbers;
for (p = buffer, i = 0; p < buffer + length - 2 && i < (int)_tcslen(input); i++)
{
ch = input[i];
if (ch >= '0' && ch <= '9' && chinese_number)
{
q = &numbers[(ch - '0')];
*p = *q;
p ++;
}
else if(ch == '.'){
*p = TEXT('点');
p ++;
}
else
{
*p = ch;
p++;
}
}
if (p != buffer)
*p = 0;
}
int LetterModeEnabled(TCHAR Letter)
{
if (!pim_config->B_mode_enabled && Letter == 'B')
return 0;
if (!pim_config->i_mode_enabled && (Letter == 'i' || Letter == 'U'))
return 0;
if (!pim_config->I_mode_enabled && Letter == 'I')
return 0;
if (!pim_config->u_mode_enabled && (Letter == 'u' || Letter == 'E'))
return 0;
if (!pim_config->v_mode_enabled && Letter == 'v')
return 0;
return 1;
}
/** 获得短语输入的Hint信息
*/
const TCHAR *GetSPWHintString(const TCHAR *input_string)
{
int i;
if (!input_string)
return 0;
if (pim_config->pinyin_mode == PINYIN_QUANPIN)
{
for (i = 0; i < sizeof(special_hint) / _SizeOf(special_hint[0]) / sizeof(TCHAR); i += 2)
{
if (!LetterModeEnabled(input_string[0]))
continue;
if (!_tcscmp(input_string, special_hint[i]))
return special_hint[i + 1];
}
}
else if(pim_config->pinyin_mode == PINYIN_SHUANGPIN && input_string[0] != 'u')
{
for (i = 0; i < sizeof(special_hint_sp) / _SizeOf(special_hint_sp[0]) / sizeof(TCHAR); i += 2)
{
if (!LetterModeEnabled(input_string[0]))
continue;
if (!_tcscmp(input_string, special_hint_sp[i]))
return special_hint_sp[i + 1];
}
}
return 0;
}
/** 判断是否为日期输入的前缀
*/
static int IsDatePrefix(const TCHAR *input_string)
{
int i;
for (i = 0; i < sizeof(date_spw_string) / _SizeOf(date_spw_string[0]) / sizeof(TCHAR); i++)
if (!_tcscmp(input_string, date_spw_string[i]))
return 1;
return 0;
}
/** 判断是否为时间输入前缀
*/
static int IsTimePrefix(const TCHAR *input_string)
{
int i;
for (i = 0; i < sizeof(time_spw_string) / _SizeOf(time_spw_string[0]) / sizeof(TCHAR); i++)
if (!_tcscmp(input_string, time_spw_string[i]))
return 1;
return 0;
}
/** 生成日期的候选
* 返回:候选数目
*/
static int GenerateDateCandidate(CANDIDATE *candidate_array, int array_length)
{
static TCHAR date_candidate_string[10][0x20];
static int i;
int year, month, day, hour, minute, second, msecond;
GetTimeValue(&year, &month, &day, &hour, &minute, &second, &msecond);
//0.2007-04-26,1.2007.4.26,2.07-04-26, 3.2007年04月26日,4.二〇〇七年四月二十六日,
_stprintf_s(date_candidate_string[0], _SizeOf(date_candidate_string[0]), TEXT("%d-%02d-%02d"), year, month, day);
_stprintf_s(date_candidate_string[1], _SizeOf(date_candidate_string[0]), TEXT("%d%02d%02d"), year, month, day);
_stprintf_s(date_candidate_string[2], _SizeOf(date_candidate_string[0]), TEXT("%02d-%02d-%02d"), year % 100, month, day);
_stprintf_s(date_candidate_string[3], _SizeOf(date_candidate_string[0]), TEXT("%d年%d月%d日"), year, month, day);
date_candidate_string[4][0] = 0;
_tcscat_s(date_candidate_string[4], _SizeOf(date_candidate_string[0]), digit_hz_string[(year / 1000) % 10]);
_tcscat_s(date_candidate_string[4], _SizeOf(date_candidate_string[0]), digit_hz_string[(year / 100) % 10]);
_tcscat_s(date_candidate_string[4], _SizeOf(date_candidate_string[0]), digit_hz_string[(year / 10) % 10]);
_tcscat_s(date_candidate_string[4], _SizeOf(date_candidate_string[0]), digit_hz_string[year % 10]);
_tcscat_s(date_candidate_string[4], _SizeOf(date_candidate_string[0]), TEXT("年"));
if (month >= 10)
_tcscat_s(date_candidate_string[4], _SizeOf(date_candidate_string[0]), TEXT("十"));
if (month % 10)
_tcscat_s(date_candidate_string[4], _SizeOf(date_candidate_string[0]), digit_hz_string[month % 10]);
_tcscat_s(date_candidate_string[4], _SizeOf(date_candidate_string[0]), TEXT("月"));
if (day >= 10)
{
if (day >= 20)
_tcscat_s(date_candidate_string[4], _SizeOf(date_candidate_string[0]), digit_hz_string[day / 10]);
_tcscat_s(date_candidate_string[4], _SizeOf(date_candidate_string[0]), TEXT("十"));
}
if (day % 10)
_tcscat_s(date_candidate_string[4], _SizeOf(date_candidate_string[0]), digit_hz_string[day % 10]);
_tcscat_s(date_candidate_string[4], _SizeOf(date_candidate_string[0]), TEXT("日"));
for (i = 0; i < 5 && i < array_length; i++)
{
candidate_array[i].type = CAND_TYPE_SPW;
candidate_array[i].spw.type = SPW_STRING_NORMAL;
candidate_array[i].spw.string = date_candidate_string[i];
candidate_array[i].spw.hint = 0;
candidate_array[i].spw.length = (int)_tcslen(date_candidate_string[i]);
}
return i;
}
/** 生成时间的候选
* 返回:候选数目
*/
static int GenerateTimeCandidate(CANDIDATE *candidate_array, int array_length)
{
static TCHAR time_candidate_string[10][0x20];
static int i;
int year, month, day, hour, minute, second, msecond;
GetTimeValue(&year, &month, &day, &hour, &minute, &second, &msecond);
//0.2007-4-26 10:49,1.10:49,2.10:49:47, 3.2007年4月26日10:49:44,
_stprintf_s(time_candidate_string[0], _SizeOf(time_candidate_string[0]), TEXT("%02d:%02d"), hour, minute);
_stprintf_s(time_candidate_string[1], _SizeOf(time_candidate_string[0]), TEXT("%d-%d-%d %02d:%02d"), year, month, day, hour, minute);
_stprintf_s(time_candidate_string[2], _SizeOf(time_candidate_string[0]), TEXT("%02d:%02d:%02d"), hour, minute, second);
_stprintf_s(time_candidate_string[3], _SizeOf(time_candidate_string[0]), TEXT("%d年%d月%d日 %02d:%02d:%02d"), year, month, day, hour, minute, second);
for (i = 0; i < 4 && i < array_length; i++)
{
candidate_array[i].type = CAND_TYPE_SPW;
candidate_array[i].spw.type = SPW_STRING_NORMAL;
candidate_array[i].spw.string = time_candidate_string[i];
candidate_array[i].spw.hint = 0;
candidate_array[i].spw.length = (int)_tcslen(time_candidate_string[i]);
}
return i;
}
/** 向短语缓冲区内插入短语。
* 参数:
* name 短语名称
* content 短语内容
* 返回:
* 失败:0
* 成功:1
*/
int InsertSpw(const TCHAR *name, TCHAR *hint, TCHAR *content)
{
int name_length, content_length, hint_length;
int i, index = 0;
assert(name && content);
name_length = (int)_tcslen(name);
content_length = (int)_tcslen(content);
hint_length = (int)_tcslen(hint);
//检查是否会发生越界
if (share_segment->spw_length + name_length + content_length + 2 > SPW_BUFFER_SIZE)
{
Log(LOG_ID, L"短语缓冲区长度不足,不能插入短语,length=%d, name=%s, content=%s", share_segment->spw_length, name, content);
return 0;
}
if (share_segment->spw_count >= SPW_MAX_ITEMS)
{
Log(LOG_ID, L"短语个数超过限制,不能插入短语,count=%d, name=%s, content=%s", share_segment->spw_count, name, content);
return 0;
}
//如果长度小于128,则只用0d标识回车
if (content_length < 128)
{
for (i = 0, index = 0; content[i]; i++)
if (content[i] != 0xa)
content[index++] = content[i];
content[index] = 0;
content_length = index;
}
if (!name[0])
return 1; //不存在名称,不插入,也不以错误返回。
share_segment->spw_index[share_segment->spw_count++] = share_segment->spw_length;
_tcscpy(spw_buffer + share_segment->spw_length, name);
share_segment->spw_length += name_length + 1;
if (hint_length)
{
_tcscpy(spw_buffer + share_segment->spw_length, hint);
share_segment->spw_length += hint_length + 1;
hint[0] = 0;
}
if (!hint_length && content_length && content[0] == SPW_HINT_LEFT_CHAR)
{
_tcscpy(spw_buffer + share_segment->spw_length, SPW_HINT_NULL_STR);
share_segment->spw_length += (int)_tcslen(SPW_HINT_NULL_STR) + 1;
}
_tcscpy(spw_buffer + share_segment->spw_length, content);
share_segment->spw_length += content_length + 1;
return 1;
}
/** 获得数字的字符串,如:20005.00617 -> 两万零五点零零六一七
*/
void GetComplexNumberString(const TCHAR *input, TCHAR *buffer, int length, int dx_number)
{
TCHAR tmp[0x10];
TCHAR xs_string[0x100]; //小数字符串
TCHAR zs_string[0x100]; //整数字符串
TCHAR no_string[0x100]; //数字字符串
int zs_count; //整数计数
int i, dot_index, input_length;
int is_zero, last_zero, all_zero;
int has_prefix_zero;
TCHAR **base_unit, **digital_string;
if (!input || !*input || !buffer || !length)
return;
if (dx_number)
{
base_unit = base_dx_unit;
digital_string = digital_dx_string;
}
else
{
base_unit = base_xx_unit;
digital_string = digital_xx_string;
}
*buffer = no_string[0] = zs_string[0] = xs_string[0] = 0;
//检查符号
if (input[0] == '-')
{
_tcscpy(no_string, TEXT("负"));
input++;
}
//删除前导0
has_prefix_zero = 0;
while(*input && *input == '0')
{
input++;
has_prefix_zero = 1;
}
//检查合法性
input_length = (int)_tcslen(input);
dot_index = -1;
for (i = 0; i < input_length; i++)
{
if (input[i] >= '0' && input[i] <= '9')
continue;
if (input[i] != '.') //小数点
return;
if (dot_index != -1) //两个以上小数点
return;
dot_index = i;
}
if (dot_index == -1) //没有小数点
dot_index = input_length;
//将小数部分提取
for (i = dot_index; i < input_length && _tcslen(xs_string) < _SizeOf(xs_string) - 4; i++)
_tcscat(xs_string, input[i] == '.' ? TEXT("点") : digital_string[input[i] - '0']);
//将整数部分提取
zs_count = dot_index;
if (zs_count > 4 * (int)sizeof(ext_unit) / sizeof(ext_unit[0])) //判断是否越过最大边界
zs_count = 4 * (int)sizeof(ext_unit) / sizeof(ext_unit[0]) - 1;
//形成整数字符串, 4个一组进行赋值
last_zero = 0, all_zero = 1;
for (i = dot_index - zs_count; i < zs_count; i++)
{
int index = zs_count - 1 - i;
tmp[0] = 0;
is_zero = input[i] == '0';
if (!is_zero)
{
if (last_zero)
_tcscat(tmp, digital_string[0]);
if (index % 4 != 1 || input[i] != '1' || i) //10只在前面没有值的时候为“十”
_tcscat(tmp, digital_string[input[i] - '0']);
_tcscat(tmp, base_unit[index % 4]);
all_zero = 0;
}
//到“万”位
if (index % 4 == 0 && !all_zero)
{
_tcscat(tmp, ext_unit[index / 4]);
all_zero = 1;
}
last_zero = is_zero;
_tcscat(zs_string, tmp);
}
//检查是否前面都为零
if (!zs_string[0] && has_prefix_zero)
_tcscpy(zs_string, TEXT("零"));
_tcscat(no_string, zs_string);
_tcscat(no_string, xs_string);
_tcsncpy(buffer, no_string, length / 2 * 2); //避免越界
}
//private method
//append candidate
void AppendSPWCandidate(CANDIDATE *candidates, int *count, int length, const TCHAR *buffer){
int i;
//判断是否有重复候选
for(i = 0 ; i < (*count); i++){
if(!_tcscmp(candidates[i].spw.string, buffer))
return;
}
candidates[*count].type = CAND_TYPE_SPW;
candidates[*count].spw.type = SPW_STRING_SPECIAL;
candidates[*count].spw.length = length;
candidates[*count].spw.string = buffer;
(*count)++;
}
/*--sunmd
字符串截取函数
*/
void substr(TCHAR *dest, const TCHAR* src, unsigned int start, unsigned int cnt)
{
_tcsnccpy(dest, src + start, cnt);
dest[cnt] = 0;
}
/**--sunmd
*获得时间的字符串,如:
* "i20:09"-> 二十点九分、20点09分;
* "i20:"->二十点、20点;
* "i20:09:09"->二十点九分九秒、20点09分09秒
*/
void GetTimeString(const TCHAR *input, TCHAR *buffer, TCHAR *buffer1, TCHAR *buffer2, CANDIDATE *candidates, int *count, int dx_number)
{
TCHAR chrSplit = ':';
TCHAR *right_string,*left_string;
TCHAR sHour[10];
TCHAR sMinute[10];
TCHAR sSecond[10];
TCHAR temp[0x100];
if (!input || !*input || !buffer)
return;
*buffer = *buffer1 = *buffer2 = sHour[0] = sMinute[0] = sSecond[0] = 0;
left_string = _tcschr(input,chrSplit);
right_string = _tcsrchr(input,chrSplit);
//有两个分隔符时
if (left_string != right_string)
{
if (_tcslen(right_string) > 1)
substr(sSecond, right_string, 1, (int)_tcslen(right_string) - 1);
substr(sMinute, left_string, 1, (int)_tcslen(left_string) - (int)_tcslen(right_string) - 1);
substr(sHour, input, 0, (int)_tcslen(input) - (int)_tcslen(left_string));
}
else
{//只有一个分隔符
if (_tcslen(left_string) > 1)
substr(sMinute, left_string, 1, (int)_tcslen(left_string) - 1);
substr(sHour, input, 0, (int)_tcslen(input) - (int)_tcslen(left_string));
}
if(!sHour[0])
return;
if (!dx_number)
{
_tcscat(buffer, sHour);
_tcscat(buffer, TEXT("点"));
_tcscat(buffer1, sHour);
_tcscat(buffer1, TEXT("时"));
if(sMinute[0])
{
_tcscat(buffer, sMinute);
_tcscat(buffer, TEXT("分"));
_tcscat(buffer1, sMinute);
_tcscat(buffer1, TEXT("分"));
}
if(sSecond[0])
{
_tcscat(buffer, sSecond);
_tcscat(buffer, TEXT("秒"));
_tcscat(buffer1, sSecond);
_tcscat(buffer1, TEXT("秒"));
}
else
{
_tcscat(buffer2, sHour);
_tcscat(buffer2, TEXT("分"));
if(sMinute[0]){
_tcscat(buffer2, sMinute);
_tcscat(buffer2, TEXT("秒"));
}
}
}
else
{
GetComplexNumberString(sHour, temp, _SizeOf(temp), 0);
_tcscat(buffer, temp);
_tcscat(buffer, TEXT("点"));
_tcscat(buffer1, temp);
_tcscat(buffer1, TEXT("时"));
if (sMinute[0])
{
GetComplexNumberString(sMinute, temp, _SizeOf(temp), 0);
_tcscat(buffer, temp);
_tcscat(buffer, TEXT("分"));
_tcscat(buffer1, temp);
_tcscat(buffer1, TEXT("分"));
}
if(sSecond[0])
{
GetComplexNumberString(sSecond, temp, _SizeOf(temp), 0);
_tcscat(buffer, temp);
_tcscat(buffer, TEXT("秒"));
_tcscat(buffer1, temp);
_tcscat(buffer1, TEXT("秒"));
}
else
{
GetComplexNumberString(sHour, temp, _SizeOf(temp), 0);
_tcscat(buffer2, temp);
_tcscat(buffer2, TEXT("分"));
if(sMinute[0]){
GetComplexNumberString(sMinute, temp, _SizeOf(temp), 0);
_tcscat(buffer2, temp);
_tcscat(buffer2, TEXT("秒"));
}
}
}
//大写:如"i20:2:3"-》二十点二分三秒
if (buffer[0])
AppendSPWCandidate(candidates, count, (int)_tcslen(buffer), buffer);
//大写:如"i20:2:3"-》二十时二分三秒
if(buffer1[0])
AppendSPWCandidate(candidates, count, (int)_tcslen(buffer1), buffer1);
//大写:如"i20:2"-》二十分二秒
if(buffer2[0])
AppendSPWCandidate(candidates, count, (int)_tcslen(buffer2), buffer2);
}
/**--sunmd
*获得日期的字符串,如:
* "i2005-09"-> 二〇〇五年九月、2005年9月;
* "i2009-"->二〇〇九年、2009年;
* "i2009-09-09"->二〇〇九年九月九日、2009年9月9日
* "i20090807"->二〇〇九年八月七日、2009年08月07日
*/
void GetDateString(const TCHAR *input, TCHAR *buffer, TCHAR *buffer1, CANDIDATE *candidates, int *count, int dx_number)
{
TCHAR chrSplit;
TCHAR *right_string, *left_string;
TCHAR sYear[10]; //年份
TCHAR sMonth[10]; //月份
TCHAR sDay[10]; //日期
TCHAR temp[10];
int iYear;
if (!input || !*input || !buffer )
return;
*buffer = *buffer1 = sYear[0] = sMonth[0] = sDay[0]=0;
if (_tcschr(input, '-'))
chrSplit = '-';
else if (_tcschr(input, '/'))
chrSplit = '/';
else
chrSplit = '0';
//带分隔符的日期,如2009/09/09、2009-09-09
if (chrSplit != '0')
{
left_string = _tcschr(input, chrSplit);
right_string = _tcsrchr(input, chrSplit);
//有两个分隔符时
if(left_string != right_string)
{
if((int)_tcslen(right_string) > 1)
substr(sDay, right_string, 1, (int)_tcslen(right_string) - 1);
substr(sMonth, left_string, 1, (int)_tcslen(left_string) - (int)_tcslen(right_string) - 1);
substr(sYear, input, 0, (int)_tcslen(input) - (int)_tcslen(left_string));
}
//只有一个分隔符
else
{
if((int)_tcslen(left_string)>1)
substr(sMonth, left_string, 1, (int)_tcslen(left_string) - 1);
substr(sYear, input, 0, (int)_tcslen(input) - (int)_tcslen(left_string));
}
}
//不带分隔符的日期,如20090807
else
{
substr(sYear, input, 0, 4);
substr(sMonth, input, 4, 2);
substr(sDay, input, 6, 4);
}
if(!sYear[0])
return;
iYear = _tstoi(sYear);
if (!dx_number)
{
_tcscat(buffer, sYear);
_tcscat(buffer, TEXT("年"));
if(sMonth[0])
{
_tcscat(buffer, sMonth);
_tcscat(buffer, TEXT("月"));
}
if(sDay[0])
{
_tcscat(buffer, sDay);
_tcscat(buffer, TEXT("日"));
}
else{
if(iYear>0 && iYear<=12){
_tcscat(buffer1, sYear);
_tcscat(buffer1, TEXT("月"));
if(sMonth[0]){
_tcscat(buffer1, sMonth);
_tcscat(buffer1, TEXT("日"));
}
}
}
}
else
{
GetSimpleNumberString(sYear, temp, _SizeOf(temp), 0, 1);
_tcscat(buffer, temp);
_tcscat(buffer, TEXT("年"));
if(sMonth[0])
{
GetComplexNumberString(sMonth, temp, _SizeOf(temp), 0);
_tcscat(buffer, temp);
_tcscat(buffer, TEXT("月"));
}
if(sDay[0])
{
GetComplexNumberString(sDay, temp, _SizeOf(temp), 0);
_tcscat(buffer, temp);
_tcscat(buffer, TEXT("日"));
}
else{
if(iYear>0 && iYear<=12){
GetComplexNumberString(sYear, temp, _SizeOf(temp), 0);
_tcscat(buffer1, temp);
_tcscat(buffer1, TEXT("月"));
if(sMonth[0]){
GetComplexNumberString(sMonth, temp, _SizeOf(temp), 0);
_tcscat(buffer1, temp);
_tcscat(buffer1, TEXT("日"));
}
}
}
}
if (buffer1[0])
AppendSPWCandidate(candidates, count, (int)_tcslen(buffer1), buffer1);
if (buffer[0])
AppendSPWCandidate(candidates, count, (int)_tcslen(buffer), buffer);
}
/* --sunmd
* 获得元角分的字符串,如:
* i123.-->一百二十三元、壹佰贰拾叁元、壹佰贰拾叁圆
* i123.0或i123.00-->一百二十三元整、壹佰贰拾叁元整、壹佰贰拾叁圆整
* i123.5-->一百二十三元五角、壹佰贰拾叁元伍角、壹佰贰拾叁圆伍角
* i123.50-->一百二十三元五角整、壹佰贰拾叁元伍角整、壹佰贰拾叁圆伍角整
* i123.51-->一百二十三元五角一分、壹佰贰拾叁元伍角壹分、壹佰贰拾叁圆伍角壹分
*/
void GetMoneyNumberString(const TCHAR *input, TCHAR *buffer, int length, int dx_number)
{
TCHAR tmp[0x10];
TCHAR xs_string[0x100]; //小数字符串
TCHAR zs_string[0x100]; //整数字符串
TCHAR no_string[0x100]; //数字字符串
int zs_count; //整数计数
int i,j, dot_index, input_length;
int is_zero, last_zero, all_zero;
int has_prefix_zero;
TCHAR **base_unit, **digital_string, *base_money_unit;
if (!input || !*input || !buffer || !length)
return;
base_money_unit=TEXT("元");
if (dx_number)
{
base_unit = base_dx_unit;
if(dx_number==2)
base_money_unit=TEXT("圆");
digital_string = digital_dx_string;
}
else
{
base_unit = base_xx_unit;
digital_string = digital_xx_string;
}
*buffer = no_string[0] = zs_string[0] = xs_string[0] =tmp[0]= 0;
//删除前导0
has_prefix_zero = 0;
while(*input && *input == '0')
{
input++;
has_prefix_zero = 1;
}
//判断“.”所在位置
input_length = (int)_tcslen(input);
for (i = 0; i < input_length; i++)
{
if (input[i] >= '0' && input[i] <= '9')
continue;
dot_index = i;
break;
}
//如果第一位是“.”,则会生成“零元”
if (dot_index==0)
has_prefix_zero=1;
//将小数部分提取
j = input_length - dot_index;
if (input[dot_index] == '.')
{
if ((j == 2 && input[dot_index + 1] == '0') ||
(j==3 && input[dot_index + 1] == '0' && input[dot_index + 2] == '0'))
{//i123.0或者i123.00
_tcscat(xs_string, base_money_unit);
_tcscat(xs_string, money_unit[0]);
}
else if (j == 3 && input[dot_index + 1] != '0' && input[dot_index + 2] == '0')
{//i123.50
_tcscat(xs_string, base_money_unit);
_tcscat(xs_string, digital_string[input[dot_index+1] - '0']);
_tcscat(xs_string, money_unit[1]);
_tcscat(xs_string, money_unit[0]);
}
else if (j == 3 && input[dot_index + 1] == '0' && input[dot_index + 2] != '0')
{//i123.01
_tcscat(xs_string, base_money_unit);
_tcscat(xs_string, digital_string[0]);
_tcscat(xs_string, digital_string[input[dot_index + 2] - '0']);
_tcscat(xs_string, money_unit[2]);
}
else
{
for (i = dot_index;i<input_length && _tcslen(xs_string) < _SizeOf(xs_string) - 4; i++)
{
_tcscat(xs_string, input[i] == '.' ? base_money_unit : digital_string[input[i] - '0']);
if (input[i] != '.')
_tcscat(xs_string,money_unit[i-dot_index]);
}
}
}
//将整数部分提取
zs_count = dot_index;
if (zs_count > 4 * (int)sizeof(ext_unit) / sizeof(ext_unit[0])) //判断是否越过最大边界
zs_count = 4 * (int)sizeof(ext_unit) / sizeof(ext_unit[0]) - 1;
//形成整数字符串, 4个一组进行赋值
last_zero = 0, all_zero = 1;
for (i = dot_index - zs_count; i < zs_count; i++)
{
int index = zs_count - 1 - i;
tmp[0] = 0;
is_zero = input[i] == '0';
if (!is_zero)
{
if (last_zero)
_tcscat(tmp, digital_string[0]);
if (index % 4 != 1 || input[i] != '1' || i || dx_number) //10只在前面没有值的时候为“十”
_tcscat(tmp, digital_string[input[i] - '0']);
_tcscat(tmp, base_unit[index % 4]);
all_zero = 0;
}
//到“万”位
if (index % 4 == 0 && !all_zero)
{
_tcscat(tmp, ext_unit[index / 4]);
all_zero = 1;
}
last_zero = is_zero;
_tcscat(zs_string, tmp);
}
//检查是否前面都为零
if (!zs_string[0] && has_prefix_zero)
_tcscpy(zs_string, TEXT("零"));
_tcscat(no_string, zs_string);
_tcscat(no_string, xs_string);
_tcsncpy(buffer, no_string, length / 2 * 2); //避免越界
}
/*
--sunmd
判断i输入的是否为时间,条件如下:
(1)输入串中包含1到2个“:”
(2)输入串中,除“:”外,其他都是数字
(3)“:”的前面最少有1个数字,最多有2位数字
(4)“:”的后面最多有两位数字
(5)如:"i12:13:24-"->“十二点十三分二十四秒”、“12点13分24秒”,"i09:9"->"九点九分"、“09点9分”
*/
int IsTime4IPre(const TCHAR *input)
{
TCHAR *right_string, *left_string;
TCHAR chrSplit = ':';
int iDisSplit = 0;
int iTemp = 0;
left_string = _tcschr(input, chrSplit);
right_string = _tcsrchr(input, chrSplit);
//存在“:”
if (!left_string)
return 0;
//第一位不能是“:”
if (input[1] == chrSplit)
return 0;
//第一个“:”前面不能超过两位数字
if (_tcslen(input) - _tcslen(left_string) > 3)
return 0;
if (_tcslen(right_string) > 3)
return 0;
//多于一个":"
if (left_string != right_string)
{
*left_string++;
//多于两个':'
if (_tcschr(left_string,chrSplit) != right_string)
return 0;
iDisSplit = (int)_tcslen(left_string) - (int)_tcslen(right_string);
//两个‘:’相邻
if (iDisSplit == 0)
return 0;
//两个'-'相隔太远,大于3
if (iDisSplit > 2)
return 0;
}
//除“:”外,应该全部为数字