Back to index

im-sdk  12.3.91
lookupbuf.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <assert.h>
00004 #include "lookupbuf.h"
00005 #include "utfchar.h"
00006 
00007 static LookupBufferPage *hangul_lookupbuf_page_new (int , UTFCHAR **);
00008 static void hangul_lookupbuf_append_page (LookupBufferStruct *, LookupBufferPage *);
00009 static UTFCHAR *hangul_lookupbuf_page_get_candidate_string (LookupBufferPage *, int);
00010 static void hangul_lookupbuf_page_free (LookupBufferPage *);
00011 static void
00012 hangul_lookupbuf_page_next_candidate (LookupBufferPage *page,
00013                                   Bool *goto_next_page_return);
00014 static void hangul_lookupbuf_page_reset (LookupBufferPage *);
00015 static int hangul_lookupbuf_page_get_current_selection_index (LookupBufferPage *);
00016 static void hangul_lookupbuf_page_set_focus (LookupBufferPage *page);
00017 static void hangul_lookupbuf_page_unset_focus (LookupBufferPage *page);
00018 
00019 LookupBufferStruct *
00020 hangul_lookupbuf_new (int n_candidates, int count_per_page, UTFCHAR **string_list)
00021 {
00022   LookupBufferStruct *lub = NULL;
00023   LookupBufferPage *lup = NULL;
00024   
00025   int i;
00026   int n_pages;
00027   int n_last_page_size = 0;
00028   
00029   assert (n_candidates > 0);
00030   assert (string_list != NULL);
00031 
00032   if ((n_candidates <= 0) || (string_list == NULL)){
00033     fprintf (stderr, "hangul_lookupbuf_new error: "
00034             "n_candidates or string_list is not valid\n");
00035     return NULL;
00036   }
00037 
00038   lub = (LookupBufferStruct *) calloc (1, sizeof (LookupBufferStruct));
00039 
00040   lub->n_candidates = n_candidates;
00041   lub->n_items_per_page = count_per_page;
00042   lub->p_current_page = NULL;
00043   lub->page_list = NULL;
00044 
00045   n_last_page_size = n_candidates % lub->n_items_per_page;
00046   if (!n_last_page_size)
00047     n_pages = n_candidates / lub->n_items_per_page;
00048   else
00049     n_pages = n_candidates / lub->n_items_per_page +1;
00050 
00051   for (i = 0 ; i < n_pages; i++){
00052     if ( i < n_pages - 1){
00053        lup =
00054          hangul_lookupbuf_page_new (lub->n_items_per_page,
00055                                  string_list + (i * count_per_page));
00056     } else {
00057       if (n_last_page_size){
00058        lup =
00059          hangul_lookupbuf_page_new ( n_last_page_size,
00060                                   string_list + (i * count_per_page));
00061       } else {
00062        lup =
00063          hangul_lookupbuf_page_new (lub->n_items_per_page,
00064                                  string_list + (i *count_per_page));
00065       }
00066     }
00067     hangul_lookupbuf_append_page (lub, lup);
00068     lup = NULL;
00069   }
00070   lub->p_current_page = lub->page_list;
00071   hangul_lookupbuf_page_set_focus (lub->p_current_page);
00072   return lub;
00073 }
00074 
00075 Bool
00076 hangul_lookupbuf_free (LookupBufferStruct *lub)
00077 {
00078   LookupBufferPage *p, *pnext;
00079   
00080   if (lub == NULL){
00081     fprintf (stderr, "hangul_lookupbuf_free error: lub is already NULL\n");
00082     return True;
00083   }
00084   p = lub->page_list;
00085   while (p){
00086     pnext = p->next;
00087     hangul_lookupbuf_page_free (p);
00088     p = pnext;
00089   }
00090   return True;
00091 }
00092 
00093 LookupBufferPage *
00094 hangul_lookupbuf_get_current_page (LookupBufferStruct *lub)
00095 {
00096   assert (lub != NULL);
00097   if (lub == NULL){
00098     fprintf (stderr, "hangul_lookupbuf_get_current_page error :"
00099             "lub is null\n");
00100     return NULL;
00101   }
00102   if (!lub->p_current_page){
00103     if (lub->page_list)
00104       lub->p_current_page = lub->page_list;
00105     else
00106       return NULL;
00107   }
00108   return lub->p_current_page;
00109 }
00110 
00111 int
00112 hangul_lookupbuf_get_current_selection_index (LookupBufferStruct *lub)
00113 {
00114   int i;
00115   assert (lub != NULL);
00116   if (lub == NULL){
00117     fprintf (stderr, "hangul_lookupbuf_get_current_selection_index error :"
00118             "lub is null\n");
00119     return -1;
00120   }
00121   if (!lub->p_current_page)
00122     return -1;
00123 
00124   i = hangul_lookupbuf_page_get_current_selection_index (lub->p_current_page);
00125   return i;
00126 }
00127 
00128 void
00129 hangul_lookupbuf_get_current_page_candidates (LookupBufferStruct *lub,
00130                                int *n_candidates_return,
00131                                int *n_current_candidate,
00132                                UTFCHAR ***candidates_list_return)
00133 {
00134   int i;
00135   LookupBufferPage *p_page;
00136   
00137   assert (lub != NULL);
00138   if (lub == NULL){
00139     fprintf (stderr, "hangul_lookupbuf_get_current_page error :"
00140             "lub is null\n");
00141     return;
00142   }
00143   if (!lub->p_current_page){
00144     if (lub->page_list)
00145       lub->p_current_page = lub->page_list;
00146     else {
00147       *n_candidates_return = 0;
00148       *n_current_candidate = -1;
00149       *candidates_list_return = NULL;
00150       return ;
00151     }
00152   }
00153   p_page = lub->p_current_page;
00154 
00155   *n_candidates_return = p_page->n_strings;
00156   *n_current_candidate = p_page->i_candidate;
00157   *candidates_list_return = (UTFCHAR **) calloc (*n_candidates_return,
00158                                            sizeof (UTFCHAR *));
00159   for (i = 0 ; i < *n_candidates_return; i++)
00160     (*candidates_list_return)[i] = _utfchar_dup (p_page->string_list[i]);
00161   return;
00162 }
00163 
00164 
00165 static void
00166 hangul_lookupbuf_append_page (LookupBufferStruct *lub, LookupBufferPage *lup)
00167 {
00168   LookupBufferPage *p, *tail;
00169   p = lub->page_list;
00170   if (!p){
00171     lub->page_list = lup;
00172     lup->next = NULL;
00173     lup->prev = NULL;
00174   } else {
00175     while (p){
00176       tail = p;
00177       p = p->next;
00178     }
00179     p = tail;
00180     p->next = lup;
00181     lup->next = NULL;
00182     lup->prev = p;
00183   }
00184 }
00185 UTFCHAR *
00186 hangul_lookupbuf_get_candidate_string (LookupBufferStruct *lub, int i)
00187 {
00188   UTFCHAR *str_return;
00189   LookupBufferPage *page;
00190   assert (lub != NULL);
00191   if (lub == NULL){
00192     fprintf (stderr, "hangul_lookupbuf_get_candidate_string error :"
00193             "lub is null \n");
00194     return NULL;
00195   }
00196   page = lub->p_current_page;
00197   if (!page){
00198     fprintf (stderr, "hangul_lookupbuf_get_candidate_string error :"
00199             "lub->p_current_page is null \n");
00200     return NULL;
00201   }
00202   str_return = hangul_lookupbuf_page_get_candidate_string (page, i);
00203   return str_return;
00204 } 
00205 
00206 void
00207 hangul_lookupbuf_next_page (LookupBufferStruct *lub)
00208 {
00209   assert (lub != NULL);
00210   if (lub == NULL){
00211     fprintf (stderr, "hangul_lookupbuf_next_page error: "
00212             "lub is NULL\n");
00213     return;
00214   }
00215   
00216   if (!lub->p_current_page){
00217     lub->p_current_page = lub->page_list;
00218     hangul_lookupbuf_page_set_focus (lub->p_current_page);
00219   } else {
00220     if (lub->p_current_page->next){
00221       hangul_lookupbuf_page_unset_focus (lub->p_current_page);
00222       lub->p_current_page = lub->p_current_page->next;
00223       hangul_lookupbuf_page_set_focus (lub->p_current_page);
00224     }
00225     else {
00226       hangul_lookupbuf_page_unset_focus (lub->p_current_page);
00227       lub->p_current_page = lub->page_list;
00228       hangul_lookupbuf_page_set_focus (lub->p_current_page);
00229     }
00230   }
00231 }
00232 
00233 
00234 void
00235 hangul_lookupbuf_previous_page (LookupBufferStruct *lub)
00236 {
00237   assert (lub != NULL);
00238   if (lub == NULL){
00239     fprintf (stderr, "hangul_lookupbuf_next_page error: "
00240             "lub is NULL\n");
00241     return;
00242   }
00243   
00244   if (!lub->p_current_page){
00245     lub->p_current_page = lub->page_list;
00246     hangul_lookupbuf_page_set_focus (lub->p_current_page);
00247   } else {
00248     if (lub->p_current_page->prev){
00249       hangul_lookupbuf_page_unset_focus (lub->p_current_page);
00250       lub->p_current_page = lub->p_current_page->prev;
00251       hangul_lookupbuf_page_set_focus (lub->p_current_page);
00252     } else {
00253       /* This is not efficient... */
00254       LookupBufferPage *p = lub->page_list;
00255       LookupBufferPage *tail = NULL;
00256       hangul_lookupbuf_page_unset_focus (lub->p_current_page);
00257       while (p){
00258        tail = p;
00259        p = p->next;
00260       }
00261       p = tail;
00262       /* p is the last page */
00263       lub->p_current_page = p;
00264       hangul_lookupbuf_page_set_focus (lub->p_current_page);
00265     }
00266   }
00267 }
00268 
00269 int
00270 hangul_lookupbuf_get_count_of_candidates_in_current_page (LookupBufferStruct *lub)
00271 {
00272   LookupBufferPage *page;
00273   assert (lub != NULL);
00274   if (lub == NULL){
00275     fprintf (stderr,
00276             "hangul_lookupbuf_get_count_of_candidates_in_current_page error :"
00277             "lub is NULL\n");
00278     return -1;
00279   }
00280   page = lub->p_current_page;
00281   if (page == NULL){
00282     fprintf (stderr, 
00283             "hangul_lookupbuf_get_count_of_candidates_in_current_page error :"
00284             "lub->p_current_page is NULL\n");
00285     return -1;    
00286   }
00287   return page->n_strings;
00288 }
00289 static LookupBufferPage *
00290 hangul_lookupbuf_page_new (int n_candidates, UTFCHAR **string_list)
00291 {
00292   LookupBufferPage *page;
00293   int i;
00294   assert (n_candidates > 0);
00295   assert (string_list != NULL);
00296 
00297   if ((n_candidates <= 0) || (string_list == NULL)){
00298     fprintf (stderr, "hangul_lookupbuf_page_new error :"
00299             "n_candidates  or string_list is NULL\n");
00300     return NULL;
00301   }
00302 
00303   if (n_candidates == 0)
00304     return NULL;
00305 
00306   page = (LookupBufferPage *) calloc (1, sizeof (LookupBufferPage));
00307   
00308   if (!page){
00309     fprintf (stderr, "hangul_lookupbuf_page_new error :"
00310             "memory allocation failure\n");
00311     return NULL;
00312   }
00313   page->next = NULL;
00314   page->prev = NULL;
00315   page->n_strings = n_candidates;
00316   page->i_candidate = -1;
00317   page->string_list = (UTFCHAR **) calloc (n_candidates + 1, sizeof (UTFCHAR *));
00318 
00319   for (i = 0 ; i < n_candidates; i++){
00320     page->string_list[i] = _utfchar_dup (string_list[i]);
00321   }
00322   page->string_list[i] = NULL;
00323   return page;
00324 }
00325 
00326 void
00327 hangul_lookupbuf_next_candidate (LookupBufferStruct *lub)
00328 {
00329   Bool move_to_next_page = False;
00330   LookupBufferPage *p;
00331 
00332   assert (lub != NULL);
00333   if (lub == NULL){
00334     fprintf (stderr, "hangul_lookupbuf_next_candidate error :"
00335             "lub is NULL\n");
00336     return;
00337   }
00338 
00339   if (!lub->p_current_page){
00340     if (!lub->page_list){
00341       fprintf (stderr, "hangul_lookupbuf_next_candidate error :"
00342               "fatal error, not lookup page\n");
00343       return;
00344     }
00345     lub->p_current_page = lub->page_list;
00346   } 
00347 
00348   p = lub->p_current_page;
00349   
00350   hangul_lookupbuf_page_next_candidate (p, &move_to_next_page);
00351   if (move_to_next_page){
00352     hangul_lookupbuf_page_unset_focus (lub->p_current_page);
00353     if (p->next)
00354       lub->p_current_page = p->next;
00355     else
00356       lub->p_current_page = lub->page_list;
00357     hangul_lookupbuf_page_set_focus (lub->p_current_page);
00358   }
00359 }
00360 
00361 
00362 
00363 static void
00364 hangul_lookupbuf_page_free (LookupBufferPage *page)
00365 {
00366   int i, n_candidates;
00367   if (!page){
00368     fprintf (stderr, "hangul_lookupbuf_page_free error :"
00369             "page is already NULL, can't free\n");
00370     return;
00371   }
00372   n_candidates = page->n_strings;
00373   for (i = 0 ; i < n_candidates; i++){
00374     if (page->string_list[i])
00375       free (page->string_list[i]);
00376   }
00377   
00378   free (page->string_list);
00379   free (page);
00380   return;
00381 }
00382 
00383 static void
00384 hangul_lookupbuf_page_next_candidate (LookupBufferPage *page,
00385                                Bool *goto_next_page_return)
00386 {
00387   assert (page != NULL);
00388   if (page == NULL){
00389     fprintf (stderr, "hangul_lookup_page_next_candidate error :"
00390             "page is NULL\n");
00391     /* is this reasonable correctional way to do? */
00392     page->i_candidate = -1;
00393     if (goto_next_page_return)
00394       *goto_next_page_return = False;
00395   }
00396 
00397   page->i_candidate++;
00398   if (page->string_list[page->i_candidate] == NULL){
00399     if (goto_next_page_return)
00400       *goto_next_page_return = True;
00401     page->i_candidate = -1;
00402   } else {
00403     if (goto_next_page_return)
00404       *goto_next_page_return = False;
00405   }
00406   
00407 }
00408 
00409 static UTFCHAR *
00410 hangul_lookupbuf_page_get_candidate_string (LookupBufferPage *page, int i)
00411 {
00412   UTFCHAR *str_candidate;
00413   
00414   assert (page != NULL);
00415   if (page == NULL){
00416     fprintf (stderr, "hangul_lookupbuf_page_get_candidate_string error :"
00417             "page is NULL\n");
00418     return NULL;
00419   }
00420   
00421   if ( (i < 0) || (i >= page->n_strings)){
00422     fprintf (stderr, "hangul_lookupbuf_page_get_candidate_string error :"
00423             "index is outof boundary\n");
00424     return NULL;
00425   }
00426   
00427   str_candidate = _utfchar_dup (page->string_list [i]);
00428   return str_candidate;
00429 }
00430 
00431 static void
00432 hangul_lookupbuf_page_reset (LookupBufferPage *page)
00433 {
00434   assert (page != NULL);
00435   if (page == NULL){
00436     fprintf (stderr, "hangul_lookupbuf_page_reset error :"
00437             "page is NULL\n");
00438     return;
00439   }
00440   page->i_candidate = -1;
00441 
00442 }
00443 
00444 static int
00445 hangul_lookupbuf_page_get_current_selection_index (LookupBufferPage *page)
00446 {
00447   assert (page != NULL);
00448   if (page == NULL){
00449     fprintf (stderr, "hangul_lookupbuf_page_get_current_selection_index error: "
00450             "page is null\n");
00451     return -1;
00452   }
00453   return page->i_candidate;
00454 }
00455       
00456 
00457 static void
00458 hangul_lookupbuf_page_set_focus (LookupBufferPage *page)
00459 {
00460   assert (page != NULL);
00461   if (page == NULL){
00462     fprintf (stderr, "hangul_lookupbuf_page_set_focus error :"
00463             "page is NULL\n");
00464     return;
00465   }
00466   page->i_candidate = 0;
00467 
00468 }
00469 
00470 static void
00471 hangul_lookupbuf_page_unset_focus (LookupBufferPage *page)
00472 {
00473   assert (page != NULL);
00474   if (page == NULL){
00475     fprintf (stderr, "hangul_lookupbuf_page_unset_focus error :"
00476             "page is NULL\n");
00477     return;
00478   }
00479   page->i_candidate = -1;
00480 
00481 }
00482 
00483 #ifdef TESTRUN
00484 void
00485 hangul_lookupbuf_page_print (LookupBufferPage *lup, Bool got_focus, FILE *fp)
00486 {
00487   int i;
00488   if (!fp)
00489     fp = stdout;
00490   
00491   fprintf (fp, "Page (%d candidates, candidate index %d)\n",
00492          lup->n_strings, lup->i_candidate);
00493   fprintf (fp, "\t");
00494   for (i = 0 ; i < lup->n_strings; i++){
00495     if (got_focus &&(i == lup->i_candidate)){
00496       fprintf (fp, ">>%s<< ", _utfchar_convert_u16_to_u8 (lup->string_list[i]));
00497     } else
00498       fprintf (fp, "%s ", _utfchar_convert_u16_to_u8 (lup->string_list[i]));
00499   }
00500   fprintf (fp, "\n");
00501 }
00502 
00503 void
00504 hangul_lookupbuf_print (LookupBufferStruct *lub, FILE *fp)
00505 {
00506   LookupBufferPage *p;
00507   if (!fp)
00508     fp = stdout;
00509   p = lub->page_list;
00510   fprintf (fp, "Buffer info\n");
00511   fprintf (fp, "total : %d candidates\n", lub->n_candidates);
00512   while (p){
00513     if (p == lub->p_current_page)
00514       hangul_lookupbuf_page_print (p, True, fp);
00515     else
00516       hangul_lookupbuf_page_print (p, False, fp);
00517 
00518     p = p->next;
00519   }
00520 }
00521 
00522 
00523 
00524 
00525 #include "hhdict.h"
00526 Bool
00527 construct_binary_tree_from_file
00528 (char *dic_path, Tree **trees_return, int *n_trees_return );
00529 
00530 int
00531 main (int argc, char **argv)
00532 {
00533   Tree *trees;
00534   int n_trees;
00535   Bool mthd_return = False;
00536   UTFCHAR ga[] = { 0xac00 };
00537   UTFCHAR **cand = NULL;
00538   int n_candidates;
00539   int i;
00540   int ch;
00541   LookupBufferStruct *lub;
00542   FILE *fp;
00543 
00544   if (argc != 2){
00545     fprintf (stdout, "usage\n%s output_file\n", argv[1]);
00546     exit (-1);
00547   }
00548   fp = fopen (argv[1], "w");
00549   
00550   construct_binary_tree_from_file ("/usr/lib/iiim/le/kole/input_methods/data/hhdict",
00551                                &trees, &n_trees);
00552   mthd_return = dictionary_search_hanja_candidates_in_utf16
00553     (ga, &n_candidates, &cand );
00554   if (!mthd_return){
00555     fprintf (stderr, "fail to search\n");
00556     exit (-1);
00557   }
00558 #if 0  
00559   for (i = 0 ; i < n_candidates; i++){
00560     printf ("%s", _utfchar_convert_u16_to_u8 (cand[i]));
00561     printf ("\n");
00562   }
00563 #endif
00564   lub = hangul_lookupbuf_new (n_candidates, 10, cand);
00565   hangul_lookupbuf_print (lub, fp);
00566 
00567   while (ch = fgetc (stdin)){
00568     if (ch == ' '){
00569       hangul_lookupbuf_next_candidate (lub);
00570       hangul_lookupbuf_print (lub, fp);
00571     } else if (ch == 'q')
00572       break;
00573   }
00574   fclose (fp);
00575   return 0;
00576                                
00577   
00578 }
00579 #endif