Back to index

im-sdk  12.3.91
comp-inputmethod-descriptor.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <string.h>
00004 #include <sys/types.h>
00005 
00006 #include <iiimp-data.h>
00007 
00008 #include "iiimp-dataP.h"
00009 
00010 
00011 IIIMP_inputmethod_descriptor *
00012 iiimp_inputmethod_descriptor_new(
00013     IIIMP_data_s *   data_s,
00014     IIIMP_card16     id,
00015     IIIMP_string *   idname,
00016     IIIMP_string *   hrn,
00017     IIIMP_language * language,
00018     IIIMP_string *   rdun)
00019 {
00020     return iiimp_inputmethod_descriptor2_new(data_s, id, idname, hrn,
00021                                         language, rdun, NULL);
00022 }
00023 
00024 
00025 IIIMP_inputmethod_descriptor *
00026 iiimp_inputmethod_descriptor2_new(
00027     IIIMP_data_s *   data_s,
00028     IIIMP_card16     id,
00029     IIIMP_string *   idname,
00030     IIIMP_string *   hrn,
00031     IIIMP_language * language,
00032     IIIMP_string *   rdun,
00033     IIIMP_imeinfo *  imeinfo)
00034 {
00035     IIIMP_inputmethod_descriptor * imd;
00036 
00037     REQUIRE_PROTOCOL_VERSION(data_s, 3);
00038 
00039     imd = (IIIMP_inputmethod_descriptor *)
00040        malloc(sizeof (IIIMP_inputmethod_descriptor));
00041     if (NULL == imd) {
00042        data_s->status = IIIMP_DATA_MALLOC_ERROR;
00043        return NULL;
00044     }
00045 
00046     imd->nbyte = (2 + 2);
00047     imd->id = id;
00048     imd->next = NULL;
00049 
00050     imd->idname = idname;
00051     if (NULL == idname) {
00052        imd->nbyte += 4;
00053     } else {
00054        imd->nbyte += idname->nbyte;
00055     }
00056 
00057     imd->hrn = hrn;
00058     if (NULL == hrn) {
00059        imd->nbyte += 4;
00060     } else {
00061        imd->nbyte += hrn->nbyte;
00062     }
00063 
00064     imd->nbyte += 4;
00065     imd->language_nbyte = 0;
00066     imd->language = language;
00067     for (; NULL != language; language = language->next) {
00068        imd->language_nbyte += language->nbyte;
00069     }
00070     imd->nbyte += imd->language_nbyte;
00071 
00072     imd->rdun = rdun;
00073     if (NULL == rdun) {
00074        imd->nbyte += 4;
00075     } else {
00076        imd->nbyte += rdun->nbyte;
00077     }
00078 
00079     imd->nbyte += 4;
00080     imd->imeinfo_nbyte = 0;
00081     imd->imeinfo = imeinfo;
00082     for (; NULL != imeinfo; imeinfo = imeinfo->next) {
00083         imd->imeinfo_nbyte += imeinfo->nbyte;
00084     }
00085     imd->nbyte += imd->imeinfo_nbyte;
00086 
00087     return imd;
00088 }
00089 
00090 
00091 void
00092 iiimp_inputmethod_descriptor_delete(
00093     IIIMP_data_s *                 data_s,
00094     IIIMP_inputmethod_descriptor * im_desc)
00095 {
00096     if (NULL == im_desc) return;
00097     iiimp_string_delete(data_s, im_desc->idname);
00098     iiimp_string_delete(data_s, im_desc->hrn);
00099     iiimp_language_list_delete(data_s, im_desc->language);
00100     iiimp_string_delete(data_s, im_desc->rdun);
00101     iiimp_imeinfo_list_delete(data_s, im_desc->imeinfo);
00102     free(im_desc);
00103     return;
00104 }
00105 
00106 
00107 void
00108 iiimp_inputmethod_descriptor_list_delete(
00109     IIIMP_data_s *                 data_s,
00110     IIIMP_inputmethod_descriptor * im_desc)
00111 {
00112     IIIMP_inputmethod_descriptor * im_desc_next;
00113     for (; NULL != im_desc; im_desc = im_desc_next) {
00114        im_desc_next = im_desc->next;
00115        iiimp_inputmethod_descriptor_delete(data_s, im_desc);
00116     }
00117     return;
00118 }
00119 
00120 
00121 void
00122 iiimp_inputmethod_descriptor_pack(
00123     IIIMP_data_s *                 data_s,
00124     IIIMP_inputmethod_descriptor * m,
00125     size_t *                       nbyte,
00126     uchar_t **                            ptr)
00127 {
00128     size_t    rest;
00129     uchar_t * p;
00130 
00131     rest = *nbyte;
00132     p = *ptr;
00133 
00134     PUTU16(m->id, rest, p, data_s->byte_swap);
00135     PUTU16(0, rest, p, data_s->byte_swap);
00136     iiimp_string_pack(data_s, m->idname, &rest, &p);
00137     iiimp_string_pack(data_s, m->hrn, &rest, &p);
00138     PUT32(m->language_nbyte, rest, p, data_s->byte_swap);
00139     iiimp_language_list_pack(data_s, m->language, &rest, &p);
00140     iiimp_string_pack(data_s, m->rdun, &rest, &p);
00141     PUT32(m->imeinfo_nbyte, rest, p, data_s->byte_swap);
00142     if (m->imeinfo) iiimp_imeinfo_list_pack (data_s, m->imeinfo, &rest, &p);
00143 
00144     *nbyte = rest;
00145     *ptr = p;
00146 
00147     return;
00148 }
00149 
00150 
00151 void
00152 iiimp_inputmethod_descriptor_list_pack(
00153     IIIMP_data_s *                 data_s,
00154     IIIMP_inputmethod_descriptor * m,
00155     size_t *                       nbyte,
00156     uchar_t **                            ptr)
00157 {
00158     size_t    rest;
00159     uchar_t * p;
00160 
00161     rest = *nbyte;
00162     p = *ptr;
00163 
00164     for (; NULL != m; m = m->next) {
00165        iiimp_inputmethod_descriptor_pack(data_s, m, &rest, &p);
00166     }
00167 
00168     *nbyte = rest;
00169     *ptr = p;
00170 
00171     return;
00172 }
00173 
00174 
00175 IIIMP_inputmethod_descriptor *
00176 iiimp_inputmethod_descriptor_unpack(
00177     IIIMP_data_s *   data_s,
00178     size_t *         nbyte,
00179     const uchar_t ** ptr,
00180     size_t           nbyte_max)
00181 {
00182     const uchar_t *                p;
00183     IIIMP_inputmethod_descriptor * r;
00184     size_t                         rest;
00185     int                                   len;
00186 
00187     rest = nbyte_max;
00188     p = *ptr;
00189 
00190     if ((*nbyte < rest) || (rest < 4)) {
00191        data_s->status = IIIMP_DATA_INVALID;
00192        return NULL;
00193     }
00194 
00195     r = (IIIMP_inputmethod_descriptor *)
00196        malloc(sizeof (IIIMP_inputmethod_descriptor));
00197     if (NULL == r) {
00198        data_s->status = IIIMP_DATA_MALLOC_ERROR;
00199        return NULL;
00200     }
00201 
00202     GETU16(r->id, rest, p, data_s->byte_swap);
00203     SKIP16(rest, p);
00204     r->next = NULL;
00205 
00206     r->idname = iiimp_string_unpack(data_s, &rest, &p, rest);
00207     if (NULL == r->idname) {
00208        iiimp_inputmethod_descriptor_delete(data_s, r);
00209        return NULL;
00210     }
00211 
00212     r->hrn = iiimp_string_unpack(data_s, &rest, &p, rest);
00213     if (NULL == r->hrn) {
00214        iiimp_inputmethod_descriptor_delete(data_s, r);
00215        return NULL;
00216     }
00217 
00218     GET32(len, rest, p, data_s->byte_swap);
00219     if ((len < 0) || (rest < len)) {
00220        iiimp_inputmethod_descriptor_delete(data_s, r);
00221        data_s->status = IIIMP_DATA_INVALID;
00222        return NULL;
00223     }
00224 
00225     r->language = iiimp_language_list_unpack(data_s, &rest, &p, len);
00226     if (NULL == r->language) {
00227        iiimp_inputmethod_descriptor_delete(data_s, r);
00228        return NULL;
00229     }
00230 
00231     r->rdun = iiimp_string_unpack(data_s, &rest, &p, rest);
00232     if (NULL == r->rdun) {
00233        iiimp_inputmethod_descriptor_delete(data_s, r);
00234        return NULL;
00235     }
00236 
00237     GET32(len, rest, p, data_s->byte_swap);
00238     r->imeinfo = iiimp_imeinfo_list_unpack (data_s, &rest, &p, len);
00239 
00240     *nbyte = rest;
00241     *ptr = p;
00242 
00243     return r;
00244 }
00245 
00246 
00247 IIIMP_inputmethod_descriptor *
00248 iiimp_inputmethod_descriptor_list_unpack(
00249     IIIMP_data_s *   data_s,
00250     size_t *         nbyte,
00251     const uchar_t ** ptr,
00252     size_t           nbyte_max)
00253 {
00254     IIIMP_inputmethod_descriptor * im_desc;
00255     size_t                         rest;
00256     const uchar_t *                p;
00257     IIIMP_inputmethod_descriptor * im_desc_first;
00258     IIIMP_inputmethod_descriptor * im_desc_last;
00259 
00260     rest = nbyte_max;
00261     p = *ptr;
00262     im_desc_first = NULL;
00263     im_desc_last = NULL;
00264 
00265     if (((*nbyte) < nbyte_max) || (0 != (rest & 0x01))) {
00266        data_s->status = IIIMP_DATA_INVALID;
00267        return NULL;
00268     }
00269 
00270     while (0 < rest) {
00271        im_desc = iiimp_inputmethod_descriptor_unpack(data_s, &rest, &p, rest);
00272        if (NULL == im_desc) {
00273            iiimp_inputmethod_descriptor_list_delete(data_s, im_desc_first);
00274            return NULL;
00275        } else {
00276            if (NULL == im_desc_first) {
00277               im_desc_first = im_desc;
00278            } else {
00279               im_desc_last->next = im_desc;
00280            }
00281            im_desc_last = im_desc;
00282        }
00283     }
00284 
00285     *nbyte -= (nbyte_max - rest);
00286     *ptr = p;
00287 
00288     return im_desc_first;
00289 }
00290 
00291 
00292 void
00293 iiimp_inputmethod_descriptor_print(
00294     IIIMP_data_s *                 data_s,
00295     IIIMP_inputmethod_descriptor * m)
00296 {
00297     IIIMP_language * l;
00298 
00299     if (NULL == m) return;
00300 
00301     (void)fprintf(data_s->print_fp, "\t\tinput method identifier = %d\n",
00302                 m->id);
00303 
00304     (void)fprintf(data_s->print_fp, "\t\tid name = ");
00305     iiimp_string_print(data_s, m->idname);
00306     (void)fputc('\n', data_s->print_fp);
00307 
00308     (void)fprintf(data_s->print_fp, "\t\tname (hrn) = ");
00309     iiimp_string_print(data_s, m->hrn);
00310     (void)fputc('\n', data_s->print_fp);
00311 
00312     for (l = m->language; NULL != l; l = l->next) {
00313        (void)fprintf(data_s->print_fp, "\t\tsupported language=(");
00314        iiimp_language_print(data_s, l);
00315        (void)fprintf(data_s->print_fp, ")\n");
00316     }
00317 
00318     (void)fprintf(data_s->print_fp, "\t\treverse domain name = ");
00319     iiimp_string_print(data_s, m->rdun);
00320     (void)fputc('\n', data_s->print_fp);
00321 }
00322 
00323 
00324 void
00325 iiimp_inputmethod_descriptor_list_print(
00326     IIIMP_data_s *                 data_s,
00327     IIIMP_inputmethod_descriptor * m)
00328 {
00329     for (; NULL != m; m = m->next) {
00330        iiimp_inputmethod_descriptor_print(data_s, m);
00331     }
00332 }
00333 
00334 
00335 /* Local Variables: */
00336 /* c-file-style: "iiim-project" */
00337 /* End: */