Back to index

im-sdk  12.3.91
text.c
Go to the documentation of this file.
00001 /*
00002   text.c
00003 */
00004 #include <stdlib.h>
00005 #include <string.h>
00006 #include "iiimcfint.h"
00007 
00008 static IIIMF_status
00009 update_text(
00010     IIIMCF_text_rec *pt
00011 )
00012 {
00013     EIMIL_mtext *pm;
00014     if (pt->pu16) free(pt->pu16);
00015     if (pt->pv_mtext) {
00016        pm = &pt->pv_mtext->v.mtext;
00017        if (!EIMIL_convert_UTF32_to_UTF16(pm->ustr, pm->len, &pt->pu16, &pt->len)) {
00018            return IIIMF_STATUS_MALLOC;
00019        }
00020     } else {
00021        pt->pu16 = NULL;
00022        pt->len = 0;
00023     }
00024     pt->updated = 1;
00025 
00026     return IIIMF_STATUS_SUCCESS;
00027 }
00028 #define UPDATE_TEXT(pt) (((pt)->updated) ? IIIMF_STATUS_SUCCESS : update_text(pt))
00029 
00030 IIIMF_status
00031 iiimcf_get_text_utf16string(
00032     IIIMCF_text text,
00033     const IIIMP_card16** putf16str
00034 )
00035 {
00036     IIIMF_status st;
00037     IIIMCF_text_rec *pt = (IIIMCF_text_rec*) text;
00038 
00039     st = UPDATE_TEXT(pt);
00040     if (st != IIIMF_STATUS_SUCCESS) return st;
00041 
00042     *putf16str = pt->pu16;
00043 
00044     return IIIMF_STATUS_SUCCESS;
00045 }
00046 
00047 IIIMF_status
00048 iiimcf_get_text_length(
00049     IIIMCF_text text,
00050     int *plen
00051 )
00052 {
00053     IIIMF_status st;
00054     IIIMCF_text_rec *pt = (IIIMCF_text_rec*) text;
00055 
00056     st = UPDATE_TEXT(pt);
00057     if (st != IIIMF_STATUS_SUCCESS) return st;
00058 
00059     *plen = pt->len;
00060 
00061     return IIIMF_STATUS_SUCCESS;
00062 }
00063 
00064 IIIMF_status
00065 iiimcf_get_char_with_feedback(
00066     IIIMCF_text text,
00067     int pos,
00068     IIIMP_card16 *pch,
00069     int *pnum_feedbacks,
00070     const IIIMP_card32** pfeedback_id,
00071     const IIIMP_card32** pfeedback
00072 )
00073 {
00074     int npos;
00075     IIIMCF_text_rec *pt = (IIIMCF_text_rec*) text;
00076     EIMIL_mtext *pm = &pt->pv_mtext->v.mtext;
00077     EIMIL_value *pv;
00078 
00079     UPDATE_TEXT(pt);
00080     if ((pos < 0) || (pos >= pt->len)) return IIIMF_STATUS_FAIL;
00081     *pch = pt->pu16[pos];
00082 
00083     npos = EIMIL_adjust_UTF16_pos_to_UTF32(pos, pm->ustr,
00084                                       pm->ustr + pm->len);
00085     if (npos < 0) return IIIMF_STATUS_FAIL;
00086 
00087     *pfeedback_id = pt->pfeedback_ids;
00088     *pfeedback = pt->pfeedbacks;
00089     pv = EIMIL_get_prop_from_mtext(pm, pEIMIL_feedback_sym, npos);
00090     if (pv && (pv->v.prop.size > 0)) {
00091        int i, j, n;
00092        EIMIL_value **ppv = pv->v.prop.pvals;
00093 
00094        ASSERT(pv->type == EIMIL_TYPE_PROP);
00095        n = pv->v.prop.size / 2;
00096        if (n > IIIMCF_MAX_FEEDBACK_NUM) n = IIIMCF_MAX_FEEDBACK_NUM;
00097        for (i = 0, j = 0; i < n; ) {
00098            pv = ppv[j++];
00099            if (pv->type != EIMIL_TYPE_NUMBER) return IIIMF_STATUS_FAIL;
00100            pt->pfeedback_ids[i] = pv->v.number;
00101            pv = ppv[j++];
00102            if (pv->type != EIMIL_TYPE_NUMBER) return IIIMF_STATUS_FAIL;
00103            pt->pfeedbacks[i] = pv->v.number;
00104            i++;
00105        }
00106        *pnum_feedbacks = n;
00107     } else {
00108        *pnum_feedbacks = 0;
00109     }
00110 
00111     return IIIMF_STATUS_SUCCESS;
00112 }
00113 
00114 IIIMF_status
00115 iiimcf_destruct_text(
00116     IIIMCF_text_rec *pt
00117 )
00118 {
00119     if (pt->pu16) free(pt->pu16);
00120     if (pt->pv_mtext) EIMIL_destruct_value(pt->pv_mtext);
00121     memset(pt, 0, sizeof(*pt));
00122 
00123     return IIIMF_STATUS_SUCCESS;
00124 }
00125 
00126 IIIMF_status
00127 iiimcf_duplicate_text(
00128     IIIMCF_text src,
00129     IIIMCF_text *pduplicated
00130 )
00131 {
00132     IIIMCF_text_rec *ptdup;
00133     IIIMCF_text_rec *pt = (IIIMCF_text_rec*) src;
00134 
00135     ptdup = (IIIMCF_text_rec*) malloc(sizeof(*ptdup));
00136     if (!ptdup) return IIIMF_STATUS_MALLOC;
00137     memset(ptdup, 0, sizeof(*ptdup));
00138     ptdup->pv_mtext = EIMIL_copy_value(pt->pv_mtext);
00139     if (!ptdup->pv_mtext) {
00140        free(ptdup);
00141        return IIIMF_STATUS_MALLOC;
00142     }
00143 
00144     *pduplicated = ptdup;
00145     return IIIMF_STATUS_SUCCESS;
00146 }
00147 
00148 IIIMF_status
00149 iiimcf_destroy_text(
00150     IIIMCF_text text
00151 )
00152 {
00153     IIIMF_status st;
00154     IIIMCF_text_rec *pt = (IIIMCF_text_rec*) text;
00155 
00156     st = iiimcf_destruct_text(pt);
00157     free(pt);
00158     return st;
00159 }
00160 
00161 /********************************************************************************
00162          convert between IIIMP_string/text/contents and IIIMCF_text
00163  ********************************************************************************/
00164 
00165 IIIMF_status
00166 iiimcf_construct_text_from_UTF8(
00167     const unsigned char *pstr,
00168     IIIMCF_text_rec *pt
00169 )
00170 {
00171     EIMIL_value *pv_mtext;
00172 
00173     if (pt->pv_mtext) EIMIL_destruct_value(pt->pv_mtext);
00174     pt->pv_mtext = NULL;
00175     pt->updated = 0;
00176     if (!pstr) {
00177        pt->pv_mtext = NULL;
00178        return IIIMF_STATUS_SUCCESS;
00179     }
00180     pv_mtext = EIMIL_construct_mtext_from_UTF8(pstr);
00181     if (!pv_mtext) {
00182        return IIIMF_STATUS_MALLOC;
00183     }
00184     pt->pv_mtext = pv_mtext;
00185 
00186     return IIIMF_STATUS_SUCCESS;
00187 }
00188 
00189 IIIMF_status
00190 iiimcf_convert_iiimp_string_to_text(
00191     IIIMP_string *pstr,
00192     IIIMCF_text_rec *pt
00193 )
00194 {
00195     EIMIL_value *pv_mtext;
00196 
00197     if (pt->pv_mtext) EIMIL_destruct_value(pt->pv_mtext);
00198     pt->pv_mtext = NULL;
00199     pt->updated = 0;
00200     if (pstr->len == 0) {
00201        pt->pv_mtext = NULL;
00202        return IIIMF_STATUS_SUCCESS;
00203     }
00204     pv_mtext = EIMIL_construct_mtext_from_iiimp_string(pstr);
00205     if (!pv_mtext) {
00206        return IIIMF_STATUS_MALLOC;
00207     }
00208     pt->pv_mtext = pv_mtext;
00209 
00210     return IIIMF_STATUS_SUCCESS;
00211 }
00212 
00213 IIIMF_status
00214 iiimcf_convert_iiimp_text_to_text(
00215     IIIMP_text *ptext,
00216     IIIMCF_text_rec *pt
00217 )
00218 {
00219     EIMIL_value *pv_mtext;
00220 
00221     if (ptext == NULL)
00222       return IIIMF_STATUS_FAIL;
00223     if (pt->pv_mtext) EIMIL_destruct_value(pt->pv_mtext);
00224     pt->pv_mtext = NULL;
00225     pt->updated = 0;
00226     if (!ptext->char_with_feedback) {
00227        pt->pv_mtext = NULL;
00228        return IIIMF_STATUS_SUCCESS;
00229     }
00230     pv_mtext = EIMIL_construct_mtext_from_iiimp_text(ptext);
00231     if (!pv_mtext) {
00232        return IIIMF_STATUS_MALLOC;
00233     }
00234     pt->pv_mtext = pv_mtext;
00235 
00236     return IIIMF_STATUS_SUCCESS;
00237 }
00238 
00239 
00240 IIIMF_status
00241 iiimcf_convert_iiimp_contents_to_text(
00242     IIIMP_contents *pcon,
00243     IIIMCF_text_rec *pt
00244 )
00245 {
00246     if (pcon->type == IIIMP_CONTENTS_TEXT) {
00247        return iiimcf_convert_iiimp_text_to_text(pcon->value.text, pt);
00248     } else if (pcon->type == IIIMP_CONTENTS_STRING) {
00249        return iiimcf_convert_iiimp_string_to_text(pcon->value.string, pt);
00250     } else {
00251        return IIIMF_STATUS_ARGUMENT;
00252     }
00253 
00254     return IIIMF_STATUS_SUCCESS;
00255 }
00256 
00257 IIIMF_status
00258 iiimcf_update_text_by_preedit_draw(
00259     IIIMCF_text_rec *pbase,
00260     IIIMP_message *pmes
00261 )
00262 {
00263     if (!pbase->pv_mtext) {
00264        return iiimcf_convert_iiimp_contents_to_text(pmes->v.preedit_draw.preedit, pbase);
00265     } else {
00266        EIMIL_value *pv;
00267 
00268        if (!EIMIL_update_mtext_by_preedit_draw_message(pbase->pv_mtext, pmes, &pv)) {
00269            iiimcf_destruct_text(pbase);
00270            return IIIMF_STATUS_MALLOC;
00271        }
00272        EIMIL_destruct_value(pbase->pv_mtext);
00273        pbase->pv_mtext = pv;
00274        pbase->updated = 0;
00275     }
00276     return IIIMF_STATUS_SUCCESS;
00277 }
00278 
00279 IIIMF_status
00280 iiimcf_append_contents_to_text(
00281     IIIMCF_text_rec *pbase,
00282     IIIMP_contents *pcon
00283 )
00284 {
00285     if (!pbase->pv_mtext) {
00286        return iiimcf_convert_iiimp_contents_to_text(pcon, pbase);
00287     } else {
00288        EIMIL_value *pvs[2];
00289        EIMIL_value *pv;
00290 
00291        pvs[0] = pbase->pv_mtext;
00292        pvs[1] = EIMIL_construct_mtext_from_iiimp_contents(pcon);
00293        if (!pvs[1]) return IIIMF_STATUS_MALLOC;
00294        pv = EIMIL_mtext_concat(2, pvs);
00295        if (!pv) {
00296            EIMIL_destruct_value(pvs[1]);
00297            return IIIMF_STATUS_MALLOC;
00298        }
00299        EIMIL_destruct_value(pbase->pv_mtext);
00300        pbase->pv_mtext = pv;
00301        pbase->updated = 0;
00302     }
00303 
00304     return IIIMF_STATUS_SUCCESS;
00305 }
00306 
00307 /* Local Variables: */
00308 /* c-file-style: "iiim-project" */
00309 /* End: */
00310