Back to index

im-sdk  12.3.91
Defines | Functions | Variables
CodeTable.c File Reference
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <dirent.h>
#include <sys/types.h>
#include <unistd.h>
#include "CodeTable.h"
#include "UDEngine.h"

Go to the source code of this file.

Defines

#define _POSIX_PTHREAD_SEMANTICS

Functions

char ** get_list_of_codetable (char *pathname)
int is_preedit_char (int imid, char c)
int read_code_table (int imid)
 find_candidate (int imid, unsigned char *str, int *low, int *high)
 match (int imid, char *keystr, int idx)
 make_selections (int imid, char **selections, int *low, int high, int dir, int len, char *encode, int max_number)
 keylen (int k, char fc, char *cstr, char *buf)

Variables

engine_info ** im

Define Documentation

Definition at line 42 of file CodeTable.c.


Function Documentation

find_candidate ( int  imid,
unsigned char *  str,
int *  low,
int *  high 
)

Definition at line 196 of file CodeTable.c.

{
       int     d, i, h, l;

       for (l = im[imid]->tab_idx[idx(str[0]) - 1].edge,
           h = im[imid]->tab_idx[idx(str[0])].edge - 1; l < h; ) {
              i = (l + h) / 2;
              if ((d = match(imid, (char *)&str[1], i)) == 0)
                     break;
              if (d > 0)
                     l = i + 1;
              else h = i - 1;
       }

       if (l < h) {
              for (l = i; l > im[imid]->tab_idx[idx(str[0]) - 1].edge; l--) {
                     if (match(imid, (char *)&str[1], l-1) != 0)
                            break;
              }
              for (h = i; h < im[imid]->tab_idx[idx(str[0])].edge - 1; h++) {
                     if (match(imid, (char *)&str[1], h+1) != 0)
                            break;
              }
              *low = l;
              *high = h;
              return;
       }

       if (l == h && match(imid, (char *)&str[1], l) == 0) {
              *low = *high = l;
              return;
       }

       *low = 1;
       *high = 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

char** get_list_of_codetable ( char *  pathname)

Definition at line 66 of file CodeTable.c.

                                              {
       DIR *dir;
       struct dirent *result;
       struct dirent *entry;
       int ret_val;
       char *ext;
       int i;
       char **ct_list;

       ct_list = (char **) malloc(sizeof(char *) * 100);
       dir = opendir(pathname);
       if (!dir) {
              printf("can not open dir %s\n", pathname);
              free(ct_list);
              return NULL;
       }
       entry = (struct dirent *) malloc(sizeof(struct dirent) + FILENAME_MAX);
       i = 0;
       while ((ret_val = readdir_r(dir, entry, &result)) == 0) {
              if (!result)
                     break;
              ext = strrchr(result->d_name, (int) '.');
              if (ext) {
                     if ( (i < 99) && (!strcmp(".mb", ext)) )   {
                            printf("file : %s\n", result->d_name);
                            ct_list[i] = (char *) malloc(strlen(pathname) + strlen(result->d_name) + 1);
                            strcpy(ct_list[i], pathname);
                            strcat(ct_list[i], "/");
                            strcat(ct_list[i], result->d_name);
                            i ++;
                     }
              }
       }
       ct_list[i] = NULL;
       if( i == 0 ) {
              free(ct_list);
              ct_list = NULL;
       }
       free(entry);
       closedir(dir);
       return (char **)ct_list;
}

Here is the caller graph for this function:

int is_preedit_char ( int  imid,
char  c 
)

Definition at line 159 of file CodeTable.c.

                                      {
       if(im[imid]->tab_idx[idx(c)].used) return 1;
       else return 0;
}

Here is the caller graph for this function:

keylen ( int  k,
char  fc,
char *  cstr,
char *  buf 
)

Definition at line 335 of file CodeTable.c.

{
       int     i;
       char    c, *s;

       sprintf(buf, "%s %c", cstr, fc);
       s = &buf[strlen(buf)];
       for (i = 1; i < 5; i++) {
              if ((c = ((k >> (4 - i) * 7) & 0x7f)) == 0)
                     break;
              *s++ = c;
       }
       *s = '\0';
       return i;
}

Here is the caller graph for this function:

make_selections ( int  imid,
char **  selections,
int *  low,
int  high,
int  dir,
int  len,
char*  encode,
int  max_number 
)

Definition at line 261 of file CodeTable.c.

{
       int     i, j, k;
       char    *p, buf[64], t[64], t1[64];
       int     number;
       int    direction;
       int    flag;

#ifdef DEBUG
printf("high = %d, low = %d, dir = %d, encode = %s\n", high, *low, dir, encode);
#endif

       if(dir == 1) {
              number = high - *low + 1;
              direction = 1;
       }
       else {
              number = *low - high + 1;
              direction = -1;
       }
       for (i = 0, j = 0; (i * direction) < number; i += direction) {
              flag = 0;
              p = (char *)(im[imid]->data +
                  im[imid]->c_tab[*low + i].code);
              if (len == 0 || keylen(im[imid]->c_tab[*low + i].key, *encode, p, buf) >= len) {
                     if (len != 0) p = buf;
                     sscanf(p, "%s%s", t, t1);
                     if((len != 0) && (len < strlen(encode))) {
                            for(k = len; k < strlen(encode); k ++) {
                                   if(!t1[k]) { flag = 1; break; }
                                   else if((encode[k] != encode[len - 1]) && (t1[k] != encode[k])) { 
                                          flag = 1; break; 
                                   }
                            }
                     }
                     if(!flag) {
                            strcpy(selections[j], p);
#ifdef DEBUG
printf("%s \n", selections[j]);
#endif
                            selections[j][20] = '\0';
                            j++;
                     }
              }
              if(j == max_number) {
                     i += direction;
                     break;
              }
       }
#ifdef DEBUG
printf("\n j = %d\n", j);
#endif
       *low += i;
#ifdef DEBUG
printf("\n low = %d\n", *low);
#endif
       if (j == 0) return 0;
       if(direction == -1) {
              for(i = 0; i < j / 2; i ++) {
                     strcpy(t, selections[i]);
                     strcpy(selections[i], selections[j - i - 1]);
                     strcpy(selections[j - i - 1], t);
              }
       }
       return j;
}

Here is the call graph for this function:

Here is the caller graph for this function:

match ( int  imid,
char *  keystr,
int  idx 
)

Definition at line 237 of file CodeTable.c.

{
       code_table      *ct_item = (code_table *) &(im[imid]->c_tab[idx]);
       char            *data = im[imid]->data;
       int     i;
       int     len = strlen(keystr);
       char    *tab_str, buf[8];

       if (ct_item->key & KEYSTR_MASK) {
              tab_str = (char *)(ct_item->key - KEYSTR_MASK + data);
       } else {
              for (i = 0; i < 4; i++) {
                     buf[i] = (ct_item->key >> (7 * (3 - i))) & 0x7f;
              }
              buf[4] = '\0';
              tab_str = buf;
       }

       return strncmp(keystr, tab_str,len);
}

Here is the caller graph for this function:

int read_code_table ( int  imid)

Definition at line 164 of file CodeTable.c.

{
       int     fd, i;
       int     tablen, data_len, max_len;
       char    header[80];
       char *ct_file_name;
       char    *buf, *tmp;

       ct_file_name = (char*) im[imid]->ct_file;

       if ((fd = open(ct_file_name, O_RDONLY)) == -1) {
              return -1;
       }

       read(fd, header, strlen(CODE_TABLE_HEAD));
       if (strncmp(header, CODE_TABLE_HEAD, strlen(CODE_TABLE_HEAD)) != 0) {
              close(fd);
              if( make_code_table(im[imid], ct_file_name) == -1) return -1;
              return 0;
       }
       read(fd, &tablen, sizeof(int));
       read(fd, &data_len, sizeof(int));
       read(fd, &max_len, sizeof(int));
       read(fd, im[imid]->tab_idx, sizeof(table_index) * MAXIDX);
       im[imid]->c_tab = (code_table *) malloc(sizeof(code_table) * tablen);
       im[imid]->data = (char *) malloc(data_len);
       read(fd, im[imid]->c_tab, sizeof(code_table) * tablen);
       read(fd, im[imid]->data, data_len);
       close(fd);
       return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

Definition at line 50 of file IMHandler.c.