Back to index

im-sdk  12.3.91
array_filter.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include "ctim.h"
00003 #include "array_data.h"
00004 
00005 #define LOOKUP_CHOICE_NUM   6
00006 
00007 #define IME_Status          ime_buffer->return_status
00008 #define Input_Buf           ime_buffer->input_buf
00009 #define Input_Len           ime_buffer->input_len
00010 #define Preedit_Buf         ime_buffer->preedit_buf
00011 #define Preedit_Len         ime_buffer->preedit_len
00012 #define Preedit_CaretPos    ime_buffer->preedit_caretpos
00013 #define Commit_Buf          ime_buffer->commit_buf
00014 #define Commit_Len          ime_buffer->commit_len
00015 #define Status_Buf          ime_buffer->status_buf
00016 #define Status_Len          ime_buffer->status_len
00017 #define Lookups_Num         ime_buffer->num_candidates
00018 #define Lookup_Pos          ime_buffer->cur_lookup_pos
00019 #define Candidates          ime_buffer->candidates
00020 #define Comments            ime_buffer->comments
00021 #define Lookups                    ime_buffer->lookups
00022 #define Page_State          ime_buffer->page_state
00023 
00024 int Is_UsedCodes_Key(CodeTableStruct *hztbl, int key)
00025 {
00026        if (index(hztbl->UsedCodes, key))
00027               return(1);
00028        else
00029               return(0);
00030 }
00031 
00032 int get_select_num(CodeTableStruct *hztbl, int key)
00033 {
00034        int ret = -1;
00035        switch(hztbl->nSelectKeyMode) {
00036               case NUMBER0_MODE:
00037                      if (key >= '0' && key <= '9')
00038                             ret = (key - '0') % 10;
00039                      break;
00040 
00041               case NUMBER_MODE:
00042                      if (key >= '0' && key <= '9')
00043                             ret = (key - '0' + 9) % 10;
00044                      break;
00045 
00046               case LOWER_MODE:
00047                      if (key >= 'a' && key <= 'a' + 10)
00048                             ret = key - 'a';
00049                      break;
00050 
00051               case UPPER_MODE:
00052                      if (key >= 'A' && key <= 'A' + 10)
00053                             ret = key - 'A';
00054                      break;
00055        }
00056        return(ret);
00057 }
00058 
00059 int Is_Select_Key(CodeTableStruct *hztbl, int key)
00060 {
00061        int ret = get_select_num(hztbl, key);
00062        if (ret == -1)
00063               return(0);
00064        else
00065               return(1);
00066 }
00067 
00068 int Is_Space_Key(CodeTableStruct *hztbl, int key)
00069 {
00070        if (key == IME_FILTERED_KEY_SPACEBAR)
00071               return(1);
00072        else
00073               return(0);
00074 }
00075 
00076 int Is_Return_Key(CodeTableStruct *hztbl, int key)
00077 {
00078        if (key == IME_FILTERED_KEY_ENTER)
00079               return(1);
00080        else
00081               return(0);
00082 }
00083 
00084 int Is_BackSpace_Key(CodeTableStruct *hztbl, int key)
00085 {
00086        int i;
00087        unsigned char *keylist = hztbl->functionkey[BACKSPACE_KEY_ID].keylist;
00088 
00089        if (key == IME_FILTERED_KEY_BACKSPACE || key == IME_FILTERED_KEY_DELETE)
00090               return(1);
00091 
00092        if (keylist[0]) {
00093               for (i=0; i<MAX_FUNCTIONKEY_LEN; i++)
00094                      if (keylist[i] == key)
00095                             return(1);
00096        }
00097 
00098        return(0);
00099 }
00100 
00101 int Is_ClearAll_Key(CodeTableStruct *hztbl, int key)
00102 {
00103        int i;
00104        unsigned char *keylist = hztbl->functionkey[CLEARALL_KEY_ID].keylist;
00105 
00106        if (key == IME_FILTERED_KEY_ESCAPE)
00107               return(1);
00108 
00109        if (keylist[0]) {
00110               for (i=0; i<MAX_FUNCTIONKEY_LEN; i++)
00111                      if (keylist[i] == key)
00112                             return(1);
00113        }
00114        return(0);
00115 }
00116 
00117 int Is_NextPage_Key(CodeTableStruct *hztbl, int key)
00118 {
00119        int i;
00120        unsigned char *keylist = hztbl->functionkey[PAGEDOWN_KEY_ID].keylist;
00121 
00122        if (key == IME_FILTERED_KEY_PAGEDOWN)
00123               return(1);
00124 
00125        if (keylist[0]) {
00126               for (i=0; i<MAX_FUNCTIONKEY_LEN; i++)
00127                      if (keylist[i] == key)
00128                             return(1);
00129        }
00130        return(0);
00131 }
00132 
00133 int Is_PrevPage_Key(CodeTableStruct *hztbl, int key)
00134 {
00135        int i;
00136        unsigned char *keylist = hztbl->functionkey[PAGEUP_KEY_ID].keylist;
00137 
00138        if (key == IME_FILTERED_KEY_PAGEUP)
00139               return(1);
00140        
00141        if (keylist[0]) {
00142               for (i=0; i<MAX_FUNCTIONKEY_LEN; i++)
00143                      if (keylist[i] == key)
00144                             return(1);
00145        }
00146        return(0);
00147 }
00148 
00149 int get_lookup_result(CodeTableStruct *hztbl, ImeBufferRec *ime_buffer, 
00150 int nHelpInfoMode, int pos, int max_lookup_num)
00151 {
00152        int i, lookup_num;
00153 
00154        lookup_num = codetable_search(hztbl, Input_Buf, Candidates, Comments, pos, max_lookup_num + 1); 
00155        if (lookup_num > 0) {
00156               Page_State = 0;
00157               if (pos == 0)
00158                      Page_State |= ImeCandidatesFirstPage;
00159               if (lookup_num < max_lookup_num + 1)
00160                      Page_State |= ImeCandidatesLastPage;
00161               if (lookup_num == max_lookup_num + 1)
00162                      lookup_num = max_lookup_num;
00163 
00164               for (i=0; i<lookup_num; i++) {
00165                      DEBUG_printf("%d: %s %s\n", i, Candidates[i], Comments[i]);
00166                      if (nHelpInfoMode)
00167                             snprintf((char *)Lookups[i], MAX_CANDIDATE_CHAR_NUM, "%s %s",
00168                                     Candidates[i], Comments[i]);
00169                      else
00170                             strcpy(Lookups[i], Candidates[i]);
00171               }
00172        }
00173 
00174        return(lookup_num);
00175 
00176 }
00177 
00178 int Commit_And_Clean_All_Area(ImeBufferRec *ime_buffer)
00179 {
00180        Input_Len = 0;
00181        Preedit_Len = 0;
00182        Lookups_Num = 0;
00183        Preedit_CaretPos = 0;
00184        IME_Status = IME_COMMIT | IME_PREEDIT_AREA | IME_LOOKUP_AREA;
00185 }
00186 
00187 int Clean_All_Area(ImeBufferRec *ime_buffer)
00188 {
00189        Input_Len = 0;
00190        Preedit_Len = 0;
00191        Lookups_Num = 0;
00192        Preedit_CaretPos = 0;
00193        IME_Status = IME_PREEDIT_AREA | IME_LOOKUP_AREA;
00194 }
00195 
00196 int commit_candidate(ImeBufferRec *ime_buffer, int id)
00197 {
00198        if (id >= Lookups_Num)
00199               return(-1);
00200 
00201        strcpy((char *)Commit_Buf, (char *)Candidates[id]);
00202        Commit_Len = strlen((char *)Commit_Buf);
00203        Commit_And_Clean_All_Area(ime_buffer);
00204 
00205        return(0);
00206 }
00207 
00208 void warning_bell(ImeBufferRec *ime_buffer)
00209 {
00210        IME_Status |= IME_BEEP;
00211 }
00212 
00213 int If_Has_WildChar(char *input_str)
00214 {
00215        char *p = input_str;
00216 
00217        while(*p) {
00218               if (*p == '?' || *p == '*')
00219                      return(1);
00220               p++;
00221        }
00222        return(0);
00223 }
00224 
00225 int get_high_frequence_characters(CodeTableStruct *hztbl, ImeBufferRec *ime_buffer)
00226 {
00227        int i;
00228        int high_char_pos = 0;
00229        int found = 0;
00230 
00231        Page_State = ImeCandidatesFirstPage | ImeCandidatesLastPage;
00232 
00233        while(high_char_pos <= 930) {
00234               if (!strcasecmp(highword[high_char_pos][0], Input_Buf))  {
00235                      found = 1;
00236                      break;
00237               }
00238               high_char_pos ++;
00239        }
00240        if (found) {
00241               for (i=0; i<10; i++) {
00242                      char *hw_str = highword[high_char_pos][i+1];
00243                      if (hztbl->Encode == ENCODE_UTF8) {
00244                             strcpy(Candidates[i], hw_str);
00245                             strcpy(Lookups[i], hw_str);
00246                      } else {
00247                             char *ip, *op, buffer[128];
00248                             int ileft, oleft;
00249 
00250                             ip = (char *)hw_str;
00251                             ileft = strlen(hw_str);
00252                             op = buffer;
00253                             oleft = 128;
00254                             memset(buffer, 0, 128);
00255                             Convert_UTF8_To_Native(hztbl->Encode, ip, ileft, &op, &oleft);
00256                             strcpy(Candidates[i], buffer);
00257                             strcpy(Lookups[i], buffer);
00258                      }
00259               }
00260               return(10);
00261        }
00262 
00263        return(0);
00264 }
00265 
00266 int get_symbol_characters(CodeTableStruct *hztbl, ImeBufferRec *ime_buffer, int symbol_area_id, int pos, int max_lookup_num)
00267 {
00268        int k;
00269        int num_matched;
00270        char *symbol_str;
00271 
00272        num_matched = 0;
00273        for(k=0;k<no_arraytbl[symbol_area_id];k++) {
00274               if (k<pos) continue;
00275 
00276               symbol_str = (char *)arraytbl10[symbol_area_id][k];
00277               if (!is_valid_candidate(symbol_str, strlen(symbol_str), ENCODE_UTF8, hztbl->Output_Encode))
00278                      continue;
00279 
00280               if (hztbl->Encode == ENCODE_UTF8) {
00281                      strcpy(Candidates[num_matched], symbol_str);
00282                      strcpy(Lookups[num_matched], symbol_str);
00283               } else {
00284                      char *ip, *op, buffer[128];
00285                      int ileft, oleft;
00286 
00287                      ip = (char *)symbol_str;
00288                      ileft = strlen(symbol_str);
00289                      op = buffer;
00290                      oleft = 128;
00291                      memset(buffer, 0, 128);
00292                      Convert_UTF8_To_Native(hztbl->Encode, ip, ileft, &op, &oleft);
00293                      strcpy(Candidates[num_matched], buffer);
00294                      strcpy(Lookups[num_matched], buffer);
00295               }
00296               if (num_matched == max_lookup_num)
00297                      break;
00298 
00299               num_matched++;
00300        }
00301 
00302        Page_State = ImeCandidatesFirstPage | ImeCandidatesLastPage;
00303        return(num_matched);
00304 }
00305 
00306 /* return value:  IME_UNUSED_KEY:  if IME not use this key, return this key to systerm directly */
00307 /*                IME_OK:      if IME has used this key */
00308 int ctim_filter(CodeTableStruct *hztbl, unsigned char key, ImeBufferRec *ime_buffer)
00309 {
00310        char    ch;
00311        int     i, k, hznum;
00312        int     pos, select_num;
00313        int    bHasWildChar, symbol_area_id;
00314 
00315        int    Max_Lookups_Num = LOOKUP_CHOICE_NUM;
00316        int     Max_Symbols_Lookups_Num = 10;
00317        int    nHelpInfoMode = hztbl->nHelpInfoMode;
00318        int    nAutoSelectMode = hztbl->nAutoSelectMode;
00319        int    nKeyPromptMode = hztbl->nKeyPromptMode;
00320 
00321        ime_buffer->encoding = hztbl->Encode;
00322 
00323        IME_Status = 0;
00324 
00325 /*
00326        ime_buffer->lookup_label_type = NUMERIC_LABEL;
00327 */
00328        bHasWildChar = If_Has_WildChar(Input_Buf);
00329 
00330        if ((key >= '0' && key <= '9') && (Input_Len == 1) && (toupper(Input_Buf[0]) == 'W')) {
00331               /* "W1": Process Symbol Input */
00332               symbol_area_id = key - '0';
00333               Lookups_Num = get_symbol_characters(hztbl, ime_buffer, symbol_area_id, 0, Max_Symbols_Lookups_Num); 
00334 
00335               Input_Buf[1] = key;
00336               Input_Len = 0;
00337               Preedit_Len = 0;
00338               Preedit_CaretPos = 0;
00339               IME_Status = IME_PREEDIT_AREA | IME_LOOKUP_AREA;
00340 
00341               return(IME_OK);
00342        }
00343 
00344        if(Is_Select_Key(hztbl, key)) {
00345               if (Lookups_Num > 0) {
00346                      select_num = get_select_num(hztbl, key);
00347                      if (select_num >= 0 && select_num < Lookups_Num) {
00348                             commit_candidate(ime_buffer, select_num);
00349                      } else {
00350                             warning_bell(ime_buffer);
00351                      }
00352                      return(IME_OK);
00353               }
00354        }
00355 
00356        if((Is_UsedCodes_Key(hztbl, key)) || (Is_Wildchar_Key(hztbl, key))) { 
00357               /* if inputted keys length has over the limit */
00358               if (Input_Len >= hztbl->MaxCodes) {
00359                      return(IME_OK);
00360               }
00361 
00362               if (Lookups_Num > 0 && Lookups_Num < 10 && Input_Len <= 2) {
00363                      strcpy((char *)Commit_Buf, (char *)Candidates[0]);
00364                         Commit_Len = strlen((char *)Commit_Buf);
00365                         Commit_And_Clean_All_Area(ime_buffer);
00366               }
00367 
00368               Input_Buf[Input_Len] = key;
00369               Input_Len ++;
00370               Input_Buf[Input_Len] = '\0';
00371               
00372               if (nKeyPromptMode) {
00373                      strcpy(Preedit_Buf+Preedit_Len, hztbl->keyprompt[key].prompt);
00374                      Preedit_Len += strlen(hztbl->keyprompt[key].prompt);
00375                      Preedit_Buf[Preedit_Len] = '\0';
00376               } else {
00377                      Preedit_Buf[Preedit_Len] = key;
00378                      Preedit_Len += 1;
00379                      Preedit_Buf[Preedit_Len] = '\0';
00380               }
00381               Preedit_CaretPos = Preedit_Len;
00382               IME_Status |= IME_PREEDIT_AREA;
00383 
00384               Lookups_Num = 0;
00385               if (!bHasWildChar && (Input_Len == 1 || Input_Len == 2)) {
00386                      Lookups_Num = get_high_frequence_characters(hztbl, ime_buffer);
00387               } else {
00388                      Lookup_Pos = 0;
00389                      Lookups_Num = get_lookup_result(hztbl, ime_buffer, nHelpInfoMode, Lookup_Pos, Max_Lookups_Num); 
00390               }
00391               IME_Status |= IME_LOOKUP_AREA;
00392               return(IME_OK);
00393        }
00394 
00395        if(Is_ClearAll_Key(hztbl, key)) { 
00396               /* Esc , clear preedit area, lookup choice area */
00397 
00398               /*  if no inputted key, directly return this key */
00399               if (Lookups_Num == 0 && Input_Len==0) return(IME_UNUSED_KEY);
00400 
00401               Clean_All_Area(ime_buffer);
00402               return (IME_OK);
00403        }
00404 
00405        if (Is_BackSpace_Key(hztbl, key)) { /* Back Space & Delete */
00406               /*  if no inputted key, directly return this key */
00407               if (Input_Len==0) return(IME_UNUSED_KEY);
00408 
00409               /* Delete characters in pre-edit region */
00410               Input_Len --;
00411               if (nKeyPromptMode) {
00412                      ch = Input_Buf[Input_Len];
00413                      Preedit_Len -= strlen(hztbl->keyprompt[ch].prompt);
00414                      Preedit_Buf[Preedit_Len] = '\0';
00415               } else {
00416                      Preedit_Len -= 1;
00417                      Preedit_Buf[Preedit_Len] = '\0';
00418               }
00419 
00420               Input_Buf[Input_Len] = '\0';
00421               Preedit_CaretPos = Preedit_Len;
00422               IME_Status = IME_PREEDIT_AREA;
00423 
00424               Lookups_Num = 0;
00425               if (Input_Len > 0) {
00426                      if (!bHasWildChar && (Input_Len == 1 || Input_Len == 2)) {
00427                             Lookups_Num = get_high_frequence_characters(hztbl, ime_buffer);
00428                      } else {
00429                             Lookup_Pos = 0;
00430                             Lookups_Num = get_lookup_result(hztbl, ime_buffer, nHelpInfoMode, Lookup_Pos, Max_Lookups_Num); 
00431                      }
00432               }
00433               IME_Status |= IME_LOOKUP_AREA;
00434               return(IME_OK);
00435 
00436        }
00437 
00438        if(Is_NextPage_Key(hztbl, key)) {
00439               if ((toupper(Input_Buf[0]) == 'W') && (Input_Buf[1] >= '0' && Input_Buf[1] <= '9')) {
00440                      int symbol_area_id = Input_Buf[1] - '0';
00441 
00442                      pos = Lookup_Pos + Max_Symbols_Lookups_Num;
00443                      hznum = get_symbol_characters(hztbl, ime_buffer, symbol_area_id, pos, Max_Symbols_Lookups_Num); 
00444                      if (hznum > 0) {
00445                             Lookups_Num = hznum;
00446                             Lookup_Pos = pos;
00447                             IME_Status = IME_LOOKUP_AREA;
00448                      } else {
00449                                    warning_bell(ime_buffer);
00450                      }
00451 
00452                      return(IME_OK);
00453               }
00454 
00455               /*  if no inputted key, directly return this key */
00456               if (Input_Len==0) return(IME_UNUSED_KEY);
00457 
00458               /* if any preedit key and not in Lookup status */
00459               if (Lookups_Num==0) return(IME_OK);
00460 
00461               if (!bHasWildChar && (Input_Len == 1 || Input_Len == 2)) {
00462                      return(IME_OK);
00463               }
00464 
00465               pos = Lookup_Pos + Max_Lookups_Num;
00466               hznum = get_lookup_result(hztbl, ime_buffer, nHelpInfoMode, pos, Max_Lookups_Num); 
00467               if (hznum > 0) {
00468                      Lookups_Num = hznum;
00469                      Lookup_Pos = pos;
00470                      IME_Status = IME_LOOKUP_AREA;
00471               } else {
00472                             warning_bell(ime_buffer);
00473               }
00474 
00475               return(IME_OK);
00476        }
00477 
00478        if(Is_PrevPage_Key(hztbl, key)) {
00479               if ((toupper(Input_Buf[0]) == 'W') && (Input_Buf[1] >= '0' && Input_Buf[1] <= '9')) {
00480                      int symbol_area_id = Input_Buf[1] - '0';
00481 
00482                      /* if in beginning pos */
00483                      if (Lookup_Pos<=0) {
00484                                    warning_bell(ime_buffer);
00485                             return(IME_OK);
00486                      }
00487 
00488                      pos = Lookup_Pos - Max_Symbols_Lookups_Num;
00489                      hznum = get_symbol_characters(hztbl, ime_buffer, symbol_area_id, pos, Max_Symbols_Lookups_Num); 
00490                      if (hznum > 0) {
00491                             Lookups_Num = hznum;
00492                             Lookup_Pos = pos;
00493                             IME_Status = IME_LOOKUP_AREA;
00494                      } else {
00495                                    warning_bell(ime_buffer);
00496                      }
00497        
00498                      return(IME_OK);
00499               }
00500 
00501               /*  if no inputted key, directly return this key */
00502               if (Input_Len==0) return(IME_UNUSED_KEY);
00503 
00504               /* if any preedit key and not in Lookup status */
00505               if (Lookups_Num==0) return(IME_OK);
00506 
00507               if (!bHasWildChar && (Input_Len == 1 || Input_Len == 2)) {
00508                      return(IME_OK);
00509               }
00510 
00511               /* if in beginning pos */
00512               if (Lookup_Pos<=0) {
00513                             warning_bell(ime_buffer);
00514                      return(IME_OK);
00515               }
00516 
00517               pos = Lookup_Pos - Max_Lookups_Num;
00518               hznum = get_lookup_result(hztbl, ime_buffer, nHelpInfoMode, pos, Max_Lookups_Num); 
00519               if (hznum > 0) {
00520                      Lookups_Num = hznum;
00521                      Lookup_Pos = pos;
00522                      IME_Status = IME_LOOKUP_AREA;
00523               } else {
00524                             warning_bell(ime_buffer);
00525               }
00526 
00527               return(IME_OK);
00528        }
00529 
00530        if(Is_Return_Key(hztbl, key)) { 
00531               /* Esc , clear preedit area, lookup choice area */
00532 
00533               /*  if no inputted key, directly return this key */
00534               if (Lookups_Num == 0 && Input_Len==0) return(IME_UNUSED_KEY);
00535 
00536               if ((toupper(Input_Buf[0]) == 'W') && (Input_Buf[1] >= '0' && Input_Buf[1] <= '9')) {
00537                      return(IME_UNUSED_KEY);
00538               }
00539 
00540               Clean_All_Area(ime_buffer);
00541               return (IME_OK);
00542        }
00543 
00544        if(Is_Space_Key(hztbl, key)) {
00545               /* if no input keys and no candidates */
00546               if (Input_Len == 0 && Lookups_Num == 0) 
00547                      return(IME_UNUSED_KEY);
00548 
00549               if ((toupper(Input_Buf[0]) == 'W') && (Input_Buf[1] >= '0' && Input_Buf[1] <= '9')) {
00550                      int symbol_area_id = Input_Buf[1] - '0';
00551 
00552                      pos = Lookup_Pos + Max_Symbols_Lookups_Num;
00553                      hznum = get_symbol_characters(hztbl, ime_buffer, symbol_area_id, pos, Max_Symbols_Lookups_Num); 
00554                      if (hznum > 0) {
00555                             Lookups_Num = hznum;
00556                             Lookup_Pos = pos;
00557                      } else {
00558                             Lookups_Num = get_symbol_characters(hztbl, ime_buffer, symbol_area_id, 0, Max_Symbols_Lookups_Num); 
00559                             Lookup_Pos = 0;
00560                      }
00561                      IME_Status = IME_LOOKUP_AREA;
00562 
00563                      return(IME_OK);
00564               }
00565 
00566               /* if any candidates, then commit the first candidate */
00567               if (Lookups_Num > 0 && Lookups_Num < 10) {
00568                      commit_candidate(ime_buffer, 0);
00569                      return(IME_OK);
00570               }
00571 
00572               /* if any keys and key is Space key and mode is not keybykey */
00573               /* then search and display the candidates */
00574               Lookup_Pos = 0;
00575               Lookups_Num = get_lookup_result(hztbl, ime_buffer, nHelpInfoMode, Lookup_Pos, Max_Lookups_Num); 
00576               if (Lookups_Num == 1) {
00577                      commit_candidate(ime_buffer, 0);
00578                      return(IME_OK);
00579               } else if (Lookups_Num == 0) {
00580                      warning_bell(ime_buffer);
00581               }
00582 
00583               IME_Status = IME_LOOKUP_AREA;
00584               return(IME_OK);
00585        }
00586 
00587        /* for unnormal keys */
00588        if (Lookups_Num > 0) {
00589               /* need commit the first candidate */
00590               strcpy((char *)Commit_Buf, (char *)Candidates[0]);
00591               Commit_Len = strlen((char *)Commit_Buf);
00592               Commit_And_Clean_All_Area(ime_buffer);
00593        }
00594 
00595         return(IME_UNUSED_KEY);
00596 }