Back to index

im-sdk  12.3.91
comp-contents.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 static IIIMP_contents *
00012 iiimp_contents_new(
00013     IIIMP_data_s *   data_s,
00014     IIIMP_card32     type,
00015     void *           value)
00016 {
00017     IIIMP_contents * c;
00018 
00019     c = (IIIMP_contents *)malloc(sizeof (IIIMP_contents));
00020     if (NULL == c) {
00021        data_s->status = IIIMP_DATA_MALLOC_ERROR;
00022        return NULL;
00023     }
00024 
00025     c->nbyte = 4;
00026 
00027     c->type = type;
00028 
00029     c->value.string = NULL;
00030     c->value.text = NULL;
00031     c->value.keyevent_list = NULL;
00032 
00033     c->value.any = value;
00034 
00035     switch (type) {
00036     case IIIMP_CONTENTS_STRING:    /* string */
00037        c->nbyte += c->value.string->nbyte;
00038        c->value_nbyte = c->value.string->nbyte;
00039        break;
00040     case IIIMP_CONTENTS_TEXT: /* text */
00041        c->nbyte += c->value.text->nbyte;
00042        c->value_nbyte = c->value.text->nbyte;
00043        break;
00044     case IIIMP_CONTENTS_KEYEVENT: /* keyevent */
00045        c->nbyte += 4;
00046        c->nbyte += c->value.keyevent_list->nbyte;
00047        c->value_nbyte = c->value.keyevent_list->nbyte;
00048        break;
00049     default: /* error */
00050        free(c);
00051        data_s->status = IIIMP_DATA_INVALID;
00052        return NULL;
00053     }
00054 
00055     return c;
00056 }
00057 
00058 
00059 void
00060 iiimp_contents_delete(IIIMP_data_s * data_s, IIIMP_contents * c)
00061 {
00062     if (NULL == c) return;
00063 
00064     switch (c->type) {
00065     case IIIMP_CONTENTS_STRING:    /* string */
00066        iiimp_string_delete(data_s, c->value.string);
00067        break;
00068     case IIIMP_CONTENTS_TEXT: /* text */
00069        iiimp_text_delete(data_s, c->value.text);
00070        break;
00071     case IIIMP_CONTENTS_KEYEVENT: /* keyevent */
00072        iiimp_keyevent_list_delete(data_s, c->value.keyevent_list);
00073        break;
00074     }
00075 
00076     free(c);
00077 }
00078 
00079 
00080 IIIMP_contents *
00081 iiimp_contents_string_new(
00082     IIIMP_data_s *   data_s,
00083     IIIMP_string *   value)
00084 {
00085     return iiimp_contents_new(data_s, IIIMP_CONTENTS_STRING, (void *)value);
00086 }
00087 
00088 
00089 IIIMP_contents *
00090 iiimp_contents_text_new(
00091     IIIMP_data_s *   data_s,
00092     IIIMP_text *     value)
00093 {
00094     return iiimp_contents_new(data_s, IIIMP_CONTENTS_TEXT, (void *)value);
00095 }
00096 
00097 
00098 IIIMP_contents *
00099 iiimp_contents_keyevent_list_new(
00100     IIIMP_data_s *          data_s,
00101     IIIMP_keyevent_list *   value)
00102 {
00103     return iiimp_contents_new(data_s, IIIMP_CONTENTS_KEYEVENT, (void *)value);
00104 }
00105 
00106 
00107 void
00108 iiimp_contents_pack(
00109     IIIMP_data_s *   data_s,
00110     IIIMP_contents * m,
00111     size_t *         nbyte,
00112     uchar_t **              ptr)
00113 {
00114     size_t    rest;
00115     uchar_t * p;
00116 
00117     rest = *nbyte;
00118     p = *ptr;
00119 
00120     PUTU32(m->type, rest, p, data_s->byte_swap);
00121     switch (m->type) {
00122     case IIIMP_CONTENTS_STRING:    /* string */
00123        iiimp_string_pack(data_s, m->value.string, &rest, &p);
00124        break;
00125     case IIIMP_CONTENTS_TEXT: /* text */
00126        iiimp_text_pack(data_s, m->value.text, &rest, &p);
00127        break;
00128     case IIIMP_CONTENTS_KEYEVENT: /* keyevent */
00129        PUTU32(m->value.keyevent_list->nbyte, rest, p, data_s->byte_swap);
00130        iiimp_keyevent_list_pack(data_s, m->value.keyevent_list, &rest, &p);
00131        break;
00132     }
00133 
00134     *nbyte = rest;
00135     *ptr = p;
00136 
00137     return;
00138 }
00139 
00140 
00141 IIIMP_contents *
00142 iiimp_contents_unpack(
00143     IIIMP_data_s *   data_s,
00144     size_t *         nbyte,
00145     const uchar_t ** ptr,
00146     size_t           nbyte_max)
00147 {
00148     IIIMP_contents * c;
00149     size_t           rest;
00150     const uchar_t *  p;
00151     size_t           n;
00152 
00153     rest = nbyte_max;
00154     p = *ptr;
00155 
00156     if ((*nbyte < rest) || (rest < (4))) {
00157        data_s->status = IIIMP_DATA_INVALID;
00158        return NULL;
00159     }
00160 
00161     c = (IIIMP_contents *)malloc(sizeof (IIIMP_contents));
00162     if (NULL == c) {
00163        data_s->status = IIIMP_DATA_MALLOC_ERROR;
00164        return NULL;
00165     }
00166 
00167     GETU32(c->type, rest, p, data_s->byte_swap);
00168     c->value.string = NULL;
00169     c->value.text = NULL;
00170     c->value.keyevent_list = NULL;
00171 
00172     switch (c->type) {
00173     case IIIMP_CONTENTS_STRING:    /* string */
00174        c->value.string = iiimp_string_unpack(data_s, &rest, &p, rest);
00175        if (NULL == c->value.string) {
00176            free(c);
00177            return NULL;
00178        }
00179        break;
00180     case IIIMP_CONTENTS_TEXT: /* text */
00181        c->value.text = iiimp_text_unpack(data_s, &rest, &p, rest);
00182        if (NULL == c->value.text) {
00183            free(c);
00184            return NULL;
00185        }
00186        break;
00187     case IIIMP_CONTENTS_KEYEVENT: /* keyevent */
00188        GETU32(n, rest, p, data_s->byte_swap);
00189        c->value.keyevent_list =
00190            iiimp_keyevent_list_unpack(data_s, &rest, &p, n);
00191        if (NULL == c->value.keyevent_list) {
00192            free(c);
00193            return NULL;
00194        }
00195        break;
00196     default: /* error */
00197        free(c);
00198        data_s->status = IIIMP_DATA_INVALID;
00199        return NULL;
00200     }
00201 
00202     *nbyte = rest;
00203     *ptr = p;
00204 
00205     return c;
00206 }
00207 
00208 
00209 void
00210 iiimp_contents_print(
00211     IIIMP_data_s *   data_s,
00212     IIIMP_contents * m)
00213 {
00214     if (NULL == m) return;
00215 
00216     switch (m->type) {
00217     case IIIMP_CONTENTS_STRING: /* string */
00218        (void)fprintf(data_s->print_fp, "\t*");
00219        iiimp_string_print(data_s, m->value.string);
00220        (void)fputc('\n', data_s->print_fp);
00221        break;
00222     case IIIMP_CONTENTS_TEXT: /* text */
00223        iiimp_text_print(data_s, m->value.text);
00224        break;
00225     case IIIMP_CONTENTS_KEYEVENT: /* keyevent */
00226        iiimp_keyevent_list_print(data_s, m->value.keyevent_list);
00227        break;
00228     }
00229 }
00230 
00231 
00232 /* Local Variables: */
00233 /* c-file-style: "iiim-project" */
00234 /* End: */