Back to index

im-sdk  12.3.91
IIIMPEIMIL.c
Go to the documentation of this file.
00001 /*
00002   IIIMPEIMIL.c
00003    utilities to interexchange between EIMIL and IIIMP.
00004 */
00005 
00006 #include <stdio.h>
00007 #include <string.h>
00008 #include <stdlib.h>
00009 #include <EIMIL.h>
00010 #include <iiimp.h>
00011 
00012 #define EIMIL_IIIMP_INIT_BUFFER_SIZE 32
00013 
00014 static int
00015 set_feedback_prop(
00016     EIMIL_mtext *pm,
00017     int val,
00018     int st,
00019     int end
00020 )
00021 {
00022     EIMIL_value *pv_idx, *pv_val, *pv_prop;
00023 
00024     pv_idx = EIMIL_construct_number(0);
00025     if (!pv_idx) return 0;
00026     pv_val = EIMIL_construct_number(val);
00027     if (!pv_val) {
00028        EIMIL_RMREF(*pv_idx);
00029        return 0;
00030     }
00031     pv_prop = EIMIL_construct_prop(pEIMIL_feedback_sym);
00032     if (!pv_prop) {
00033        EIMIL_RMREF(*pv_idx);
00034        EIMIL_RMREF(*pv_val);
00035        return 0;
00036     }
00037     if ((!EIMIL_add_prop(&pv_prop->v.prop, pv_idx))
00038        || (!EIMIL_add_prop(&pv_prop->v.prop, pv_val))
00039        || (!EIMIL_set_prop_on_mtext(pm, pv_prop, st, end))) {
00040        EIMIL_RMREF(*pv_idx);
00041        EIMIL_RMREF(*pv_val);
00042        EIMIL_RMREF(*pv_prop);
00043        return 0;
00044     }
00045 
00046     return 1;
00047 }
00048 
00049 EIMIL_value*
00050 EIMIL_construct_mtext_from_iiimp_text(
00051     IIIMP_text *pimt
00052 )
00053 {
00054     int i, idx;
00055     EIMIL_value *pv_mtext;
00056     EIMIL_mtext *pm;
00057     {
00058        int cur_fid0_val, cur_fid0_st;
00059        int len;
00060        int buflen = EIMIL_IIIMP_INIT_BUFFER_SIZE;
00061        UTF16 *pstrh, *pstr;
00062        IIIMP_char_with_feedback *pcwf;
00063        IIIMP_feedback_attr_list *pfal;
00064        IIIMP_feedback_attr *pfa;
00065 
00066        len = 0;
00067        pstrh = pstr = (UTF16*) malloc(buflen * sizeof(UTF16));
00068        if (!pstrh) return NULL;
00069 
00070        /* string */
00071        for (pcwf = pimt->char_with_feedback;
00072             pcwf; pcwf = pcwf->next) {
00073            if (len >= buflen) {
00074               buflen *= 2;
00075               pstr = (UTF16*) realloc(pstrh, buflen * sizeof(UTF16));
00076               if (!pstr) {
00077                   free(pstrh);
00078                   return NULL;
00079               }
00080               pstrh = pstr;
00081               pstr += len;
00082            }
00083            *pstr++ = pcwf->code;
00084            len++;
00085        }
00086        pv_mtext = EIMIL_construct_mtext_from_UTF16(len, pstrh);
00087        if (!pv_mtext) return NULL;
00088        free(pstrh);
00089        pm = &pv_mtext->v.mtext;
00090 
00091        /* feedback */
00092        cur_fid0_val = 0;
00093        cur_fid0_st = 0;
00094        idx = 0;
00095        for (pcwf = pimt->char_with_feedback;
00096             pcwf; pcwf = pcwf->next, idx++) {
00097            pfal = pcwf->feedback_attr;
00098            pfa = pfal->ptr;
00099            for (i = 0; i < pfal->count; i++) {
00100               if (pfa->id == 0) {
00101                   if (cur_fid0_val != pfa->feedback) {
00102                      if (cur_fid0_val != 0) {
00103                          if (!set_feedback_prop(pm, cur_fid0_val, cur_fid0_st, idx))
00104                             return NULL;
00105                      }
00106                      cur_fid0_st = idx;
00107                      cur_fid0_val = pfa->feedback;
00108                   }
00109                   break;
00110               }
00111            }
00112        }
00113        if (cur_fid0_val != 0) {
00114            if (!set_feedback_prop(pm, cur_fid0_val, cur_fid0_st, idx))
00115               return NULL;
00116        }
00117     }
00118     /* TOOD: annotation!!! */
00119 
00120     return pv_mtext;
00121 }
00122 
00123 EIMIL_value*
00124 EIMIL_construct_mtext_from_iiimp_string(
00125     IIIMP_string *pstr
00126 )
00127 {
00128     return EIMIL_construct_mtext_from_UTF16(pstr->len,
00129                                        pstr->ptr);
00130 }
00131 
00132 EIMIL_value*
00133 EIMIL_construct_mtext_from_iiimp_contents(
00134     IIIMP_contents *pimc
00135 )
00136 {
00137     if (pimc->type == IIIMP_CONTENTS_TEXT) {
00138        return EIMIL_construct_mtext_from_iiimp_text(pimc->value.text);
00139     } else if (pimc->type == IIIMP_CONTENTS_STRING) {
00140        return EIMIL_construct_mtext_from_iiimp_string(pimc->value.string);
00141     }
00142 
00143     return NULL;
00144 }
00145 
00146 int
00147 EIMIL_update_mtext_by_preedit_draw_message(
00148     EIMIL_value *pbase,
00149     IIIMP_message *pmes,
00150     EIMIL_value **ppv_result
00151 )
00152 {
00153     EIMIL_mtext *pm;
00154     ASSERT(pbase->type == EIMIL_TYPE_MTEXT);
00155     pm = &pbase->v.mtext;
00156 
00157     if (pmes->opcode == IM_PREEDIT_DRAW) {
00158        int i, n = 0;
00159        EIMIL_value *pvs[3];
00160        IIIMP_preedit_draw_v *pimd = &pmes->v.preedit_draw;
00161        int st, end;
00162 
00163        st = pimd->change_first;
00164        end = st + pimd->change_length;
00165 
00166        if (st > 0) {
00167            pvs[n++] = EIMIL_mtext_substr(pbase, 0, st);
00168        }
00169        if (pimd->preedit) {
00170            pvs[n++] = EIMIL_construct_mtext_from_iiimp_contents(pimd->preedit);
00171        }
00172        if (end < pm->len) {
00173            pvs[n++] = EIMIL_mtext_substr(pbase, end, pm->len);
00174        }
00175 
00176        if (n > 0) {
00177            *ppv_result = EIMIL_mtext_concat(n, pvs);
00178        } else {
00179            *ppv_result = NULL;
00180        }
00181        for (i = 0; i < n; i++) {
00182            EIMIL_RMREF(*pvs[i]);
00183        }
00184     } else {
00185        return 0;
00186     }
00187 
00188     return 1;
00189 }
00190 
00191 /* Local Variables: */
00192 /* c-file-style: "iiim-project" */
00193 /* End: */