📄 pinyin.c
字号:
/*** $Id: pinyin.c,v 1.13 2003/09/04 03:37:34 weiym Exp $**** pinyin.c: The GB2312 pinyin module.**** Porting to MiniGUI from CCE by Zheng Xiang**** Copyright (C) 2000 by Zheng Xiang (zx@minigui.org) ** Copyright (C) 2000 ~ 2002 Wei Yongming** Copyright (C) 2003 Feynman Software.** ** Current maintainer: Wei Yongming**** Create date: 2000/11/01*//* * Copyright 1999.1 by Li ZhenChun zhchli@163.net * * CCE - Console Chinese Environment - * Copyright (C) 1998-1999 Rui He (herui@cs.duke.edu) * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE TERRENCE R. LAMBERT BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * *//*** This program is free software; you can redistribute it and/or modify** it under the terms of the GNU General Public License as published by** the Free Software Foundation; either version 2 of the License, or** (at your option) any later version.**** This program is distributed in the hope that it will be useful,** but WITHOUT ANY WARRANTY; without even the implied warranty of** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the** GNU General Public License for more details.**** You should have received a copy of the GNU General Public License** along with this program; if not, write to the Free Software** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA*//*** TODO: */#include <stdio.h>#include <stdlib.h>#include <string.h>#include <ctype.h>
/*houhh 2006-11-22 1:07:30
#include <unistd.h>
#include <pwd.h>
#include <sys/types.h>*/
#ifdef WIN32
// #define inline
typedef unsigned char u_char;
//houhh 20061122...
#define F_OK 0
#define R_OK 4
#define W_OK 2
#define X_OK 8
#include <io.h>
#define access _access
#else
#include <ctype.h>
#include <unistd.h>
#include <pwd.h>
#endif
#include "common.h"#include "minigui.h"#include "gdi.h"#include "window.h"#include "endianrw.h"#include "hzinput.h"#include "pinyin.h"static void PinyinKeyPressed(InputModule *inmd, char ch, LPARAM lParam);static void SelectKeyPressed(InputModule *inmd, char ch, LPARAM lParam);static void CreatePyMsg(InputModule *inmd);static BOOL LoadPinyinTable (InputModule *inmd, char* pathname);static BOOL LoadUsrPhrase( InputModule *inmd, char *pathname);static BOOL LoadSysPhrase(InputModule *inmd, char *pathname);static inline u_char *GetPhrase(ChoiceItem *p);static inline u_char *GetFreq(ChoiceItem *p);static int ParsePy(InputModule *inmd, char *pybuf, PYString pinyin[]);static void FindMatchPhrase(InputModule *inmd,PYString pinyin[],int lenpy);static void FillForwardSelection(InputModule *inmd,int startpos);static void FillBackwardSelection(InputModule *inmd,int lastpos);static int EffectPyNum(PYString pinyin[],int len);static void SortOutput(InputModule *inmd);static int QueryPhrase(InputModule *inmd, u_char *key, int len);static void AdjustPhraseFreq (InputModule *inmd);static void SaveUsrPhraseToMem (InputModule *inmd,u_char *str,u_char *key,int len,int freq);static BOOL SaveUsrPhrase(InputModule *inmd, char *pathname);static BOOL SaveSysPhrase(InputModule *inmd, char *pathname);InputModule Pinyin_Module;
/*houhh 2006-11-22 1:08:46
static void get_user_phrase_file (const char* tabpath, char* file_name)
{
struct passwd *pwd;
if ((pwd = getpwuid (geteuid ())) != NULL) {
strcpy (file_name, pwd->pw_dir);
if (file_name [strlen (file_name) - 1] != '/')
strcat (file_name, "/");
strcat (file_name, ".usrphrase.tab");
}
else {
strcpy (file_name, tabpath);
strcat (file_name, "usrphrase.tab");
}
}*/static void get_user_phrase_file (const char* tabpath, char* file_name)
{
#ifdef WIN32 //modified by ty
strcpy (file_name, "E:\\Minigui_Modify\\miniguidll\\imetabs\\phrase.tab");
#else
struct passwd *pwd;
if ((pwd = getpwuid (geteuid ())) != NULL) {
strcpy (file_name, pwd->pw_dir);
if (file_name [strlen (file_name) - 1] != '/')
strcat (file_name, "/");
strcat (file_name, ".usrphrase.tab");
}
else {
strcpy (file_name, tabpath);
strcat (file_name, "usrphrase.tab");
}
#endif
}
BOOL InitPinyinInput (const char* tabpath, InputModule *inmd){ char file_name [MAX_PATH + 1]; strcpy (file_name, tabpath); strcat (file_name, "pinyin.map"); if (!LoadPinyinTable (inmd, file_name)) return FALSE; strcpy (file_name, tabpath); strcat (file_name, "sysphrase.tab"); if (!LoadSysPhrase (inmd, file_name)) return FALSE; get_user_phrase_file (tabpath, file_name); if (!LoadUsrPhrase (inmd, file_name)) return FALSE; ResetPinyinInput (inmd); return TRUE;}void PinyinInputCleanup (const char* tabpath, InputModule *inmd){ char file_name [MAX_PATH + 1]; AdjustPhraseFreq (inmd); // lower the freq to [0,50) strcpy (file_name, tabpath); strcat (file_name, "sysphrase.tab"); SaveSysPhrase (inmd, file_name); get_user_phrase_file (tabpath, file_name); SaveUsrPhrase (inmd, file_name);}void RefreshPYInputArea (InputModule *inmd, HDC hDC, BOOL bTwoLines){ char str[100]; strcpy(str, "【拼音】"); strcat(str, inmd->iapybuf); TextOut(hDC, 2, 2, str); strcpy(str, inmd->iahzbuf); if (bTwoLines) TextOut(hDC, 2, 18, str); else TextOut(hDC, 150, 2, str);}void Pinyin_HZFilter(InputModule* inmd, unsigned char key, LPARAM lParam){ if ( (key>='a' && key<='z') || (key=='\''&& strlen(inmd->inbuf)) || key=='\010' || key=='\177') PinyinKeyPressed(inmd,key,lParam); else if (!strlen(inmd->inbuf)) { __mg_ime_outchar(key, lParam); return; } switch(key) { case '=': FillForwardSelection(inmd,inmd->endpos+1); break; case '-': FillBackwardSelection(inmd,inmd->startpos-1); break; case '\033': //ESCAPE ResetPinyinInput(inmd); break; default: // select some keys if ( (key>='1' && key<='9') || key=='0' || key==' ') SelectKeyPressed(inmd,key,lParam); break; }}void ResetPinyinInput(InputModule *inmd){ *(inmd->inbuf)='\0'; *(inmd->inbuftmp)='\0'; *(inmd->pybuftmp)='\0'; *(inmd->iapybuf)='\0'; *(inmd->iahzbuf)='\0'; inmd->len = 0; inmd->lenpy = 0; inmd->pinyinpos=0; inmd->lenkey=0; inmd->key[0]='\0';}static void PinyinKeyPressed(InputModule *inmd,char ch,LPARAM lParam){ /* parameter strbuf is the newly inputed pinyin, inbuf the is the whole inputed pinyin, inbuftmp is the unselected pinyin */ char strbuf[2]; char *inbuf=inmd->inbuf; char *pybuftmp=inmd->pybuftmp; /* already selected Hanzi buffer */ char *inbuftmp=inmd->inbuftmp; /* inputed pinyin buffer */ char chtmp; int count; strbuf[0] = ch; strbuf[1] = '\0'; /* \010 = Ctrl+H, \177 = BackSpace */ if (ch == '\010' || ch == '\177') // BackSpace { if (!strlen(inbuf)){ __mg_ime_outchar(ch, lParam); return; } else if (!strlen(inbuftmp)) { strcpy(inbuftmp,inbuf); *pybuftmp='\0'; // clear all the selected chars, reparse } else { inbuf[strlen(inbuf)-1] = '\0'; inbuftmp[strlen(inbuftmp)-1] = '\0'; // cut one pinyin-char off if (!strlen(inbuf)) { ResetPinyinInput(inmd); return; } } } else //other than BackSpace, ch = a-z or ' { if (strlen(inbuf) + strlen(strbuf) + 1 < MAX_INPUT_BUF) { strcat(inbuf,strbuf); strcat(inbuftmp,strbuf); } else { Ping ();#ifdef _DEBUG fprintf(stderr, "buffer overrun\n");#endif } } if (!strlen(pybuftmp)) inmd->pinyinpos = 0; /* first pinyin char */ // parse the unselected pinyin(inbuftmp) input count = ParsePy(inmd,inbuftmp,inmd->pinyin + inmd->pinyinpos); inmd->lenpy = inmd->pinyinpos + count; /* exclude the last i/u/v-beginning pinyin */ if (inmd->lenpy > 0) { chtmp = inmd->pinyin[inmd->lenpy-1][0]; if (chtmp=='i' || chtmp=='u' || chtmp=='v') { inbuf[strlen(inbuf)-1] = '\0'; inbuftmp[strlen(inbuftmp)-1] = '\0'; inmd->lenpy--; return; } } /* Too many chars now */ if (EffectPyNum(inmd->pinyin,inmd->lenpy) > MAX_PHRASE_LEN) { inbuf[strlen(inbuf)-1] = '\0'; inbuftmp[strlen(inbuftmp)-1] = '\0'; inmd->lenpy--; return; } FindMatchPhrase(inmd,inmd->pinyin + inmd->pinyinpos, inmd->lenpy-inmd->pinyinpos); FillForwardSelection(inmd,0); CreatePyMsg(inmd); return;}static void SelectKeyPressed(InputModule *inmd,char ch,LPARAM lParam){ ChoiceItem *phr=inmd->sel; char *pybuftmp=inmd->pybuftmp; /* already selected Hanzi buffer */ char *inbuftmp=inmd->inbuftmp; /* inputed pinyin buffer */ int i,j; u_char *fq; char strhz[MAX_PHRASE_LEN*2+1]; int pos,idx; if (!inmd->len){ __mg_ime_outchar(ch,lParam); return; } if (ch == ' ') idx = 0; else if (ch == '0') idx = 9; else idx = (int)ch-(int)'1'; idx += inmd->startpos; if (idx > inmd->endpos) return; // out of range selection! strncpy(strhz,GetPhrase(phr+idx), MAX_PHRASE_LEN*2+1); if (strlen(pybuftmp) + strlen(strhz) + 1 < MAX_INPUT_BUF) { strcat(pybuftmp,strhz); } else { Ping ();#ifdef _DEBUG fprintf(stderr, "buffer overrun\n");#endif } inmd->key[0] |= phr[idx].head->key[0] << inmd->lenkey; for(i=1; i<=phr[idx].head->len; i++) inmd->key[(inmd->lenkey)++ +1] = phr[idx].head->key[i]; /* pybuftmp, already selected chars */ if (strlen(pybuftmp)/2 == (size_t)EffectPyNum(inmd->pinyin,inmd->lenpy)) { if (strlen(strhz) == strlen(pybuftmp) ) { fq = GetFreq(phr+idx); if (*fq < 250) (*fq)++; /* strhz is the last phrase/char, equal, existing phrase/char increase its frequency */ } else if(strlen(pybuftmp) > 2) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -