⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pinyin.c

📁 使用miniGUI模拟器的简单开发例子(值得参考)
💻 C
📖 第 1 页 / 共 3 页
字号:
          SaveUsrPhraseToMem(inmd,                    pybuftmp,inmd->key,strlen(pybuftmp)/2,1);              // not equal and pybuftmp, save the new phrase, 0 is user phrase      }      __mg_ime_writemsg(pybuftmp, strlen(pybuftmp), lParam, 1); //1 means as the format of WORD       ResetPinyinInput(inmd);      return;//strlen(strbuf);          /* All the pinyin are translated to char/phrases!  */  }  else  // not yet, some unselected pinyin exist  {      // forward the pinyinpos pointer      for(pos = strlen(strhz)/2;  pos > 0 ; inmd->pinyinpos++)      {         ch = inmd->pinyin[inmd->pinyinpos][0];         if (ch=='i' || ch=='u' || ch=='v' || ch<'a' || ch>'z') continue;         pos--;      }      FindMatchPhrase(inmd, inmd->pinyin + inmd->pinyinpos,               inmd->lenpy - inmd->pinyinpos);      FillForwardSelection(inmd,0);      *inbuftmp = '\0';  // put the rest of the pinyin into inbuftmp      for(j = inmd->pinyinpos; j < inmd->lenpy; j++) {          if (strlen(inbuftmp) + strlen(inmd->pinyin[j]) + 1 < MAX_INPUT_BUF) {              strcat(inbuftmp, inmd->pinyin[j]);          }           else {              Ping ();#ifdef _DEBUG              fprintf(stderr, "buffer overrun\n");#endif          }      }      CreatePyMsg(inmd);      return;   }}static void CreatePyMsg(InputModule *inmd){  int i;  strncpy(inmd->iapybuf,inmd->pybuftmp, MAX_INPUT_BUF-1);  for(i=inmd->pinyinpos; i<inmd->lenpy; i++)  {      if (strlen(inmd->iapybuf)+strlen(inmd->pinyin[i])+2 >= MAX_INPUT_BUF-1) {#ifdef _DEBUG          fprintf(stderr, "buffer overrun\n");#endif      }      strcat(inmd->iapybuf,inmd->pinyin[i]);   // MAX_PY_LEN = 7      if (inmd->pinyin[i+1][0] == '\'' || inmd->pinyin[i][0] == '\'')          continue;      else          strcat(inmd->iapybuf," ");  }}static BOOL LoadPinyinTable (InputModule *inmd, char* pathname){    FILE *stream;    char str[250],strpy[15],strhz[241];    int i=0, j=0, lastpy=0, curpy;       if ( (stream = fopen (pathname, "r" )) == NULL ) {        fprintf(stderr, "IME (Pinyin): Can not open Pinyin Table to read.\n");        return FALSE;    }    while ( !feof( stream )) {        if ( fgets(str,250,stream) != NULL) {            sscanf(str,"%14s %240s",strpy,strhz);            curpy = strpy[0]-'a';            if (curpy != lastpy) j = 0;             strncpy( inmd->pytab[curpy][j].py,strpy, MAX_PY_LEN);            inmd->pytab[curpy][j].key = i+1;            lastpy = curpy;            i++,j++;        }    }    fclose(stream);      return TRUE;}
static BOOL LoadUsrPhrase (InputModule *inmd, char *pathname){    FILE *stream;    UsrPhrase *kph,*tmp;    int i,j,ahead,fsize;    u_short count;    u_char len, size;    if (access (pathname, F_OK)) {        for (i = 0; i < MAX_PY_NUM; i++)            inmd->usrph[i] = NULL;        return TRUE;    }    if ((stream = fopen (pathname, "r")) == NULL ) {        fprintf (stderr, "IME (Pinyin): %s file can't be opened.\n", pathname);        return FALSE;    }    if (fseek (stream, -4, SEEK_END) == -1 ||        fread (&fsize, sizeof(int), 1, stream) != 1 ||#if MGUI_BYTEORDER == MGUI_BIG_ENDIAN        (fsize = ArchSwap32 (fsize)) == 0 ||#endif        fsize != ftell (stream) - 4)    {       fprintf (stderr, "IME (Pinyin): %s is not a valid pinyin phrase file.\n", pathname);       fclose (stream);       return FALSE;    }    fseek (stream, 0, SEEK_SET);    for(i = 1; i < MAX_PY_NUM; i++) {       inmd->usrph[i] = NULL;       fread (&count, sizeof(count), 1, stream);#if MGUI_BYTEORDER == MGUI_BIG_ENDIAN       count = ArchSwap16 (count);#endif       if (count == 0) continue;       for(j = 0; j < count; j++)       {          fread(&len,sizeof(len),1,stream);          fread(&size,sizeof(size),1,stream);                    if ((kph = (UsrPhrase *)malloc(6+len+1+(2*len+1)*size)) == NULL) {                fprintf (stderr, "IME (Pinyin): No enough memory.\n");                fclose (stream);                return FALSE;          }          kph->len = len;          kph->count = size;          kph->next = NULL;          fread (kph->key, sizeof(char), len+1, stream);          fread (kph->key + len + 1, 2*len+1, size, stream);          ahead = kph->key[1];          ahead |= (kph->key[0] & 0x01) << 8;         if (inmd->usrph[ahead] == NULL)             inmd->usrph[ahead] = kph;         else         {            tmp = inmd->usrph[ahead];            while (tmp->next != NULL)                 tmp = tmp->next;            tmp->next = kph;         }      }   }   fclose(stream);   return TRUE;}// Load the system and user phrase library// the phrase file can be combined just by cat a >> bstatic BOOL LoadSysPhrase (InputModule *inmd, char *pathname){    FILE *stream;    Phrase *kph;    SysPhrase *sysph;    char *p;    int i,j;
//houhh 20061128...
#if 1
    if ( (stream = fopen(pathname , "rb" )) == NULL ) {
#else
	if ( (stream = fopen(pathname , "r" )) == NULL ) {
#endif//	if ( (stream = fopen(pathname , "r" )) == NULL ) {
        fprintf (stderr, "IME (Pinyin): %s file can't open\n", pathname);
        return FALSE;
    }
    if (fseek (stream, -4, SEEK_END) == -1 ||      !(inmd->sys_size = MGUI_ReadLE32FP (stream)) ||      inmd->sys_size != ftell (stream)-4)   // error!!    {        fprintf (stderr, "IME (Pinyin): %s is not a valid pinyin phrase file.\n",                 pathname);        fclose (stream);        return FALSE;    }     fseek (stream, 0, SEEK_SET);    if ((p = malloc(inmd->sys_size)) == NULL) {        fprintf (stderr, "IME (Pinyin): No enough memory.\n");        fclose (stream);        return FALSE;    }    if (fread (p, inmd->sys_size, 1, stream) != 1) {        fprintf (stderr, "IME (Pinyin): Loading system phrase error.\n");        fclose (stream);        free (p);        return FALSE;    }       for(i = 1; i < MAX_PY_NUM; i++) {      inmd->sysph[i] = sysph = (SysPhrase*)p;#if MGUI_BYTEORDER == MGUI_BIG_ENDIAN      sysph->count = ArchSwap16 (sysph->count);#endif      p = (char*)sysph->phrase;      for(j = 0; j < sysph->count; j++) {         kph = (Phrase*)p;         p += SizeOfPhrase (kph->len,kph->count);  // skip the string       }   }   fclose (stream);   return TRUE;}static void AdjustPhraseFreq (InputModule *inmd){  UsrPhrase *uph;  SysPhrase *sysph;  Phrase *sph;  int i,j,k,index;  char *p;  for(i = 1; i < MAX_PY_NUM; i++)  {     // user phrases     for(uph = inmd->usrph[i]; uph != NULL; uph = uph->next)     {         for(k =0; k < uph->count; k++)         {            index = uph->len+1 + (2*uph->len+1)*k + 2*uph->len;            if (uph->key[index] > 25)                uph->key[index] = 25 + (uph->key[index]-25)/10;         }     }        // system phrases     sysph = inmd->sysph[i];     p = (char*)sysph->phrase;   // count = total pinyin number     for(j = 0; j < sysph->count; j++)     {        sph = (Phrase *)p;        for(k = 0; k < sph->count; k++)        {            index = sph->len+1 + (2*sph->len+1)*k + 2*sph->len;            if (sph->key[index] > 25)                 sph->key[index] = 25 + (sph->key[index] - 25)/10;        }           p += SizeOfPhrase(sph->len,sph->count);     }  }  return;}static BOOL SaveSysPhrase (InputModule *inmd, char *pathname){    FILE *out;    char *p;    if ( (out = fopen( pathname, "wb")) == NULL) {        fprintf (stderr, "IME (Pinyin): %s cant open for saving.\n", pathname);        return FALSE;    }    p = (char*)(inmd->sysph[1]);#if MGUI_BYTEORDER == MGUI_LIL_ENDIAN    fwrite (p, inmd->sys_size, 1, out);    fwrite (&(inmd->sys_size), sizeof(int), 1, out);#else  {    int i, j;    for (i = 1; i < MAX_PY_NUM; i++) {        SysPhrase *sysph = (SysPhrase*)p;        int phrase_size;        MGUI_WriteLE16FP (out, sysph->count);        p = (char*)sysph->phrase;        for (j = 0; j < sysph->count; j++) {            Phrase* kph = (Phrase*)p;            phrase_size = SizeOfPhrase (kph->len, kph->count);            fwrite (p, phrase_size, 1, out);            p += phrase_size;        }    }    MGUI_WriteLE32FP (out, inmd->sys_size);    p = (char*)(inmd->sysph[1]);  }#endif    free (p);    fclose (out);    return TRUE;}static BOOL SaveUsrPhrase (InputModule *inmd, char *pathname){    int i,fsize = 0, tmpcount;    FILE *out;    UsrPhrase *p0,*q0;    u_short total;    u_char len,count;    if ((out = fopen (pathname,"wb")) == NULL) {        fprintf (stderr, "IME (Pinyin): %s cant open for saving.\n", pathname);        return FALSE;    }    for (i=1; i<MAX_PY_NUM; i++) {      total = 0;      fsize += sizeof(u_short); // for total      p0 =  inmd->usrph[i];      if (p0 == NULL) {        MGUI_WriteLE16FP (out, total);        continue;      }      // first, calculate the different pinyin key phrases      total++;      for (p0 = p0->next; p0 != NULL; p0 = p0->next) {         len = p0->len;         for (q0 = inmd->usrph[i]; q0 != p0; q0 = q0->next)            if (q0->len == len && !memcmp(p0->key, q0->key, len+1)) break;         if (p0 == q0) total++;        }      MGUI_WriteLE16FP (out, total);      for (p0 = inmd->usrph[i]; p0 != NULL; p0 = p0->next) {          if (p0->count == 0) continue;          tmpcount = p0->count;          len = p0->len;          for(q0 = p0->next; q0 != NULL; q0 = q0->next)             if (q0->len == len && !memcmp(q0->key,p0->key,len+1))                 tmpcount += q0->count;          if (tmpcount > 255)              tmpcount = 255;          else count = (u_char)tmpcount;           fsize += SizeOfPhrase(len,count);          // u_char count, count must less than 256 phrases                 fwrite(&len,sizeof(len),1,out);          fwrite(&count,sizeof(count),1,out);          fwrite(p0->key,len+1,1,out);          for(q0 = p0; q0 != NULL; q0 = q0->next)             if (q0->len == len && !memcmp(q0->key,p0->key,len+1))             {                 fwrite(q0->key+len+1,q0->count*(2*len+1),1,out);                 q0->count = 0;              }       }    }    MGUI_WriteLE32FP (out, fsize);    fclose (out);    for (i = 1; i < MAX_PY_NUM; i++)       free (inmd->usrph[i]);    return TRUE;}static void SaveUsrPhraseToMem(InputModule *inmd,u_char *str,u_char *key,int len,int freq){

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -