Back to index

im-sdk  12.3.91
Classes | Defines | Enumerations | Functions | Variables
MakeCodeTable.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include "CodeTable.h"
#include "UDEngine.h"

Go to the source code of this file.

Classes

struct  key_table

Defines

#define MAXBUF   (1024 * 1024)
#define MAXLEN   256
#define MAXTAB   0x00018000
#define OFFSET_START   1
#define CODETABLE_ID   "<codetable>"
#define KEYTABLE_ID   "<preedit_keytable>"
#define CTINFO_ID   "<codetable_info>"

Enumerations

enum  _READING { CODETABLE, KEYTABLE, CTINFO, OTHER }

Functions

int write_code_table (engine_info *im, table_index *, code_table *, int, char *, int)
int make_code_table (engine_info *im, char *in_file)
 read_file (FILE *fp)
 is_bin_code_table (int fd)
 read_bin_ct (int fd)
 read_text_ct (FILE *fp)
 tabcomp (key_table *ti1, key_table *ti2)
 generate_code_table (table_index *tab_idx, code_table *c_tab, key_table *k_tab, int *tablen, char *code_data, char **p_code)
short search_for (char *code, char *code_data, code_table *c_tab, int *tablen)
 encoding (str, char *code_data, char **p_code)
 record_char_use (table_index *tab_idx, char *str)

Variables

char * code_data = NULL
char * p_code = NULL
char name [16]
char locale [16]
char wildchar
key_tablek_tab = NULL
table_index tab_idx [MAXIDX]
int tablen = 0
int max_len = 0

Class Documentation

struct key_table

Definition at line 62 of file MakeCodeTable.c.

Class Members
char * code
char key

Define Documentation

#define CODETABLE_ID   "<codetable>"

Definition at line 54 of file MakeCodeTable.c.

#define CTINFO_ID   "<codetable_info>"

Definition at line 56 of file MakeCodeTable.c.

#define KEYTABLE_ID   "<preedit_keytable>"

Definition at line 55 of file MakeCodeTable.c.

#define MAXBUF   (1024 * 1024)

Definition at line 49 of file MakeCodeTable.c.

#define MAXLEN   256

Definition at line 50 of file MakeCodeTable.c.

#define MAXTAB   0x00018000

Definition at line 51 of file MakeCodeTable.c.

#define OFFSET_START   1

Definition at line 52 of file MakeCodeTable.c.


Enumeration Type Documentation

enum _READING
Enumerator:
CODETABLE 
KEYTABLE 
CTINFO 
OTHER 

Definition at line 58 of file MakeCodeTable.c.


Function Documentation

encoding ( str  ,
char *  code_data,
char **  p_code 
)

Definition at line 333 of file MakeCodeTable.c.

{
       int    code = 0;
       int    n;

       if (strlen(str) <= 5) {
              for (n = 1; str[n] != '\0'; n++) {
                     code += str[n] << (7 * (4 - n));
              }
       } else {
              strcpy(*p_code, (char *)&str[1]);
              code = KEYSTR_MASK + *p_code - code_data;
              *p_code += strlen(str);
       }
       return code;
}

Here is the caller graph for this function:

generate_code_table ( table_index tab_idx,
code_table c_tab,
key_table k_tab,
int *  tablen,
char *  code_data,
char **  p_code 
)

Definition at line 262 of file MakeCodeTable.c.

{
       int    i, size;
       short  *l, search_for();
       char   *p, *str;
       char   fc;           /* first char */

       size = *tablen;

       /* Make code table */
       fc = k_tab->key[0];
       tab_idx[idx(fc)].edge = 0;
       tab_idx[idx(fc)].used = 0;
       for (i = 0; i < size; i++) {
              if (fc != (k_tab + i)->key[0]) {
                     tab_idx[idx(fc)].edge = i;
                     fc = (k_tab + i)->key[0];
              }
              c_tab[i].key = encoding((k_tab + i)->key, code_data, p_code);
              c_tab[i].code = (k_tab + i)->code - code_data;
       }
       tab_idx[idx(fc)].edge = i;
}

Here is the call graph for this function:

Here is the caller graph for this function:

is_bin_code_table ( int  fd)

Definition at line 142 of file MakeCodeTable.c.

{
       char   header[32];

       read(fd, header, strlen(CODE_TABLE_HEAD));

       if (strcmp(header, CODE_TABLE_HEAD) == 0)
              return 1;
       lseek(fd, 0, 0);
       return 0;
}

Here is the call graph for this function:

int make_code_table ( engine_info im,
char *  in_file 
)

Definition at line 78 of file MakeCodeTable.c.

{
       FILE   *in_fp;
       int    fd;
       int    tabcomp();
       code_table    *c_tab = NULL;
       int    i, k;

       tablen = 0;
       max_len = 0;

       if( (in_fp = (FILE*) fopen(in_file, "r")) == NULL)  return -1;

       for (i = 0; i < MAXIDX; i++) {
              tab_idx[i].edge = tab_idx[i].used = 0;
              tab_idx[i].pe_text[0] = i + 0x20;
              for (k = 1; k < MAX_CHAR_PE; k++)
                     tab_idx[i].pe_text[k] = '\0';
       }

       if(k_tab == NULL) {
              printf("k_tab is NULL\n");
              k_tab = (key_table*) calloc(MAXTAB, sizeof(key_table));
       }

       if(code_data == NULL) {
              printf("code_data is NULL\n");
              code_data = (char *) calloc(MAXBUF, sizeof(char));
       }

       p_code = code_data + OFFSET_START;

       if(read_file(in_fp) == -1) return -1;

       qsort(k_tab, tablen - 1, sizeof(key_table), tabcomp);

       if(c_tab == NULL) {
              printf("c_tab is NULL\n");
              c_tab = (code_table *) calloc(tablen, sizeof(code_table));
       }
       generate_code_table(tab_idx, c_tab, k_tab, &tablen, code_data, &p_code);
       for (i = 0; i < tablen; i++)
              record_char_use(tab_idx, (k_tab + i)->key);

       for (i = 1; i < MAXIDX; i++) {
              if (tab_idx[i].edge < tab_idx[i-1].edge)
                     tab_idx[i].edge = tab_idx[i-1].edge;
       }

       write_code_table(im, tab_idx, c_tab, tablen, code_data, p_code - code_data);

       free(k_tab); k_tab = NULL;
       free(c_tab); c_tab = NULL;
       free(code_data); code_data = NULL;
}

Here is the call graph for this function:

Here is the caller graph for this function:

read_bin_ct ( int  fd)

Definition at line 155 of file MakeCodeTable.c.

{
       return -1;
}
read_file ( FILE *  fp)

Definition at line 134 of file MakeCodeTable.c.

{
       read_text_ct(fp);
       fclose(fp);
       return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

read_text_ct ( FILE *  fp)

Definition at line 161 of file MakeCodeTable.c.

{
       char   c, str[MAXLEN];
       unsigned char check;
       short  reading = OTHER;
       int    lineno = 0;
       int    i, k;
       char    arg[20], value[20];

       while (!feof(fp)) {
              fgets(str, MAXLEN, fp);
              if (feof(fp) || (str == NULL) || (strlen(str) < 1)) break;
              lineno++;
              if (str[0] == '#') {
                     continue;
              }
              if (strncmp(str, CTINFO_ID, 16) == 0) {
                     reading = CTINFO;
                     strcpy(name, "뷨");
                     strcpy(locale, "zh_CN");
                     wildchar = 'z';
                     continue;
              }
              if (strncmp(str, KEYTABLE_ID, 18) == 0) {
                     reading = KEYTABLE;
                     continue;
              }
              if (strncmp(str, CODETABLE_ID, 11) == 0) {
                     reading = CODETABLE;
                     continue;
              }
              switch (reading) {
              case CTINFO:
                     value[0] = '\0';
                     sscanf(str,"%s%s", arg, value);
                     if(!strcmp(arg, "name")) {
                            if(strlen(value) > 0) strcpy(name, value);
                            printf("name is %s\n", name);
                     }
                     else if(!strcmp(arg, "locale"))  {
                            if(strlen(value) > 0) strcpy(locale, value);
                            printf("locale is %s\n", locale);
                     }
                     else if(!strcmp(arg, "wildchar")) {
                            if(strlen(value) > 0) wildchar = value[0];
                            printf("wildchar is %c\n", wildchar);
                     }
                     break;

              case KEYTABLE:
                     if (strlen(str) != 1) {
                            fprintf(stderr, "Error: line %d\n", lineno);
                            exit(1);
                     }
                     i = idx(str[0]);
                     k = 0;
                     while ((c = getc(fp)) != EOF && c != '\n') {
                            if (isspace(c))
                                   continue;
                            if (k >= MAX_CHAR_PE) {
                                   fprintf(stderr, 
                                       "line %d: mapping string too long\n", 
                                       lineno);
                                   break;
                            }
                            tab_idx[i].pe_text[k++] = c;
                     }
                     break;
              case CODETABLE:
                     if( tablen >= MAXTAB ) break;
                     sscanf(str,"%s%s", p_code, (k_tab + tablen)->key);
                     check = p_code[0];
                     if( (check < 0x80) || (strlen((k_tab + tablen)->key) < 1) ) {
                            fprintf(stderr, "Codetable error, line %d , ignored.\n", lineno);
                            break;
                     }
                     (k_tab + tablen)->code = p_code;
                     p_code += strlen(p_code) + 1;
                     if (strlen((k_tab + tablen)->key) > max_len)
                            max_len = strlen((k_tab + tablen)->key);
                     tablen++;
                     break;
              default:
                     fprintf(stderr, "File format error, line %d .\n", lineno);
                     break;
              }
       }
}

Here is the call graph for this function:

Here is the caller graph for this function:

record_char_use ( table_index tab_idx,
char *  str 
)

Definition at line 354 of file MakeCodeTable.c.

{
       while (*str != '\0') {
              tab_idx[idx(*str)].used = 1;
              str++;
       }
}

Here is the caller graph for this function:

short search_for ( char *  code,
char *  code_data,
code_table c_tab,
int *  tablen 
)

Definition at line 294 of file MakeCodeTable.c.

{
       short  i;

       for (i = 0; i < *tablen; i++) {
              if (strcmp((char *) (c_tab[i].code + code_data), code) == 0) {
                     return i;
              }
       }
       c_tab[i].code = c_tab[i-1].code + 
           strlen((char *) (c_tab[i - 1].code + code_data)) + 1;
       strcpy((char *)(c_tab[i].code + code_data), code);
       *tablen += strlen(code) + 1;
       return i;
}

Here is the caller graph for this function:

tabcomp ( key_table ti1,
key_table ti2 
)

Definition at line 251 of file MakeCodeTable.c.

{
       int    r;

       if (r = strcmp(ti1->key, ti2->key))
              return r;
       return strcmp(ti1->code, ti2->code);
}

Here is the caller graph for this function:

int write_code_table ( engine_info im,
table_index tab_idx,
code_table c_tab,
int  tablen,
char *  code_data,
int  data_len 
)

Definition at line 314 of file MakeCodeTable.c.

{
       strcpy(im->name, name);
       strcpy(im->locale, locale);
       im->wild_char = wildchar;
       memcpy(im->tab_idx, tab_idx, sizeof(table_index) * MAXIDX);
       im->c_tab = (code_table *) malloc(sizeof(code_table) * tablen);
       memcpy(im->c_tab, c_tab, sizeof(code_table) * tablen);
       im->data = (char *) malloc(data_len);
       memcpy(im->data, code_data, data_len);
}

Here is the caller graph for this function:


Variable Documentation

char* code_data = NULL

Definition at line 67 of file MakeCodeTable.c.

Definition at line 71 of file MakeCodeTable.c.

char locale[16]

Definition at line 69 of file MakeCodeTable.c.

int max_len = 0

Definition at line 74 of file MakeCodeTable.c.

char name[16]

Definition at line 69 of file MakeCodeTable.c.

char * p_code = NULL

Definition at line 67 of file MakeCodeTable.c.

Definition at line 72 of file MakeCodeTable.c.

int tablen = 0

Definition at line 73 of file MakeCodeTable.c.

char wildchar

Definition at line 69 of file MakeCodeTable.c.