📄 cjk_ime.c
字号:
#include <stdio.h>#include <unistd.h>#include <fcntl.h>#include <sys/mman.h>#include <sys/types.h>#include <sys/types.h>#include <sys/socket.h>#include <sys/ioctl.h>/* #include <arpa/ftp.h> <--- unused? */#include <signal.h>#include <unistd.h>#include <string.h>#include <stdlib.h>#include <stdio.h>#include <errno.h>#include <ctype.h>#include <netdb.h>#include <pwd.h>#include <linux/ppw.h>#include "cjk_ime.h"#define ICON_SIZE 28static int fd_ime; // the file handle of current IME data filestatic Ime_Header_Info ime_header_info; // to record the file headerstatic Kb_Info kb_info; // information of the key boardestatic Key_Info *key_info=0; // information of the keystatic UInt8 icon[ICON_SIZE]; // to record the iconstatic char *letter_list; // the string tablestatic UInt16 *t_offset=0; // data offsetstatic int start_addr_of_contents; // start address in DAT file of contentsstatic unsigned short result_string[256]; // store the result CJK string/*************************************************** * * Initial the CJK ime engine,return -1 if failed * ***************************************************/int init_cjk_ime(void){ int s_size; // Open the ime data file fd_ime = open(IME_DATA_FILE_GB_PY,O_RDONLY); if (fd_ime < 0) return -1; // read the data file header if (read(fd_ime,&ime_header_info,sizeof(Ime_Header_Info)) != sizeof(Ime_Header_Info)) return -1; ime_header_info.max_keys = htonl(ime_header_info.max_keys); ime_header_info.nr_codes = htonl(ime_header_info.nr_codes); //printf("sizeof ime_header:%d\n",sizeof(Ime_Header_Info)); // Check the magic number, if not matched that means the file is not a valid data file if (ime_header_info.magic_number != DAT_FILE_MAGIC_NUMBER) return -1; read(fd_ime,&kb_info,sizeof(Kb_Info)); kb_info.nr_keys = htons(kb_info.nr_keys); kb_info.bmp_width = htons(kb_info.bmp_width); kb_info.bmp_height = htons(kb_info.bmp_height); kb_info.bmp_bits = htons(kb_info.bmp_bits); kb_info.bmp_length = htonl(kb_info.bmp_length); /*printf("sizeof Kb_Info:%d,nr_keys:%d,width:%d,height:%d,bits:%d,length:%d\n", sizeof(Kb_Info),kb_info.nr_keys, kb_info.bmp_width,kb_info.bmp_height,kb_info.bmp_bits,kb_info.bmp_length); */ //printf("sizeof bmp:%d\n",kb_info.bmp_length); //Ime_Bmp=malloc(kb_info.bmp_length); //read(fd_ime,Ime_Bmp,kb_info.bmp_length); lseek(fd_ime,kb_info.bmp_length,SEEK_CUR); // read out the icon bmp read(fd_ime,icon,ICON_SIZE ); /* { int i; printf("icon:"); for (i = 0;i < 28;i++) printf("%02x,",icon[i]); printf("\n"); } */ // read out the key information s_size=kb_info.nr_keys*sizeof(Key_Info); key_info=malloc(s_size); read(fd_ime,key_info,s_size); // gets the original string table s_size=ime_header_info.max_keys*ime_header_info.nr_codes; letter_list = malloc(s_size); read(fd_ime,letter_list,s_size); /* { int i; printf("letter_list:"); for (i = 0;i < 16;i++) printf("%c",letter_list[i]); printf("\n"); } */ s_size= ime_header_info.nr_codes * sizeof(UInt16); t_offset=malloc(s_size); read(fd_ime,t_offset,s_size); start_addr_of_contents = lseek(fd_ime,0,SEEK_CUR);// begin addres of codes }// Close the IME data file & reset all of the recourcevoid close_cjk_ime(void){ close(fd_ime); free(letter_list); free(key_info); free(t_offset);}// return the data offset in file, return -1 if false unsigned short* get_data_offset(char *key_str){ int s_len,i,flag=0; char *t; int s_offset; s_len = strlen(key_str); if (s_len <= 0) return 0; t = letter_list; for (i=0;i<ime_header_info.nr_codes;i++) { if (strncmp(key_str,t,s_len) == 0) // find the matched string { s_offset = *(t_offset+i); s_offset = (htons(s_offset)-1)*2; lseek(fd_ime,start_addr_of_contents+s_offset,0); if (read(fd_ime,result_string,sizeof(result_string)) < 0) return 0;#if 0 printf("Matched:%s,data_offset:%x,%x,\n", t,s_offset,start_addr_of_contents); { int k; for (k = 0;k < 10;k++) printf("%04x,",*(result_string+k)); printf("\n"); }#endif return result_string; } t += ime_header_info.max_keys; } return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -