Back to index

im-sdk  12.3.91
comp-char-with-feedback.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_char_with_feedback *
00012 iiimp_char_with_feedback_new(
00013     IIIMP_data_s *          data_s,
00014     IIIMP_card16            code,
00015     IIIMP_feedback_attr_list *     feedback_attr)
00016 {
00017     IIIMP_char_with_feedback *     cwf;
00018 
00019     cwf = (IIIMP_char_with_feedback *)malloc(sizeof (IIIMP_char_with_feedback));
00020     if (NULL == cwf) {
00021        data_s->status = IIIMP_DATA_MALLOC_ERROR;
00022        return NULL;
00023     }
00024 
00025     cwf->nbyte = (2 + 2 + feedback_attr->nbyte);
00026     cwf->code = code;
00027     cwf->feedback_attr = feedback_attr;
00028     cwf->next = NULL;
00029 
00030     return cwf;
00031 }
00032 
00033 
00034 void
00035 iiimp_char_with_feedback_delete(
00036     IIIMP_data_s *          data_s,
00037     IIIMP_char_with_feedback *     cwf)
00038 {
00039     if (NULL == cwf) return;
00040     iiimp_feedback_attr_list_delete(data_s, cwf->feedback_attr);
00041     free(cwf);
00042     return;
00043 }
00044 
00045 
00046 void
00047 iiimp_char_with_feedback_list_delete(
00048     IIIMP_data_s *          data_s,
00049     IIIMP_char_with_feedback *     cwf)
00050 {
00051     IIIMP_char_with_feedback *     cwf_next;
00052     for (; NULL != cwf; cwf = cwf_next) {
00053        cwf_next = cwf->next;
00054        iiimp_char_with_feedback_delete(data_s, cwf);
00055     }
00056     return;
00057 }
00058 
00059 
00060 void
00061 iiimp_char_with_feedback_pack(
00062     IIIMP_data_s *          data_s,
00063     IIIMP_char_with_feedback *     m,
00064     size_t *                nbyte,
00065     uchar_t **                     ptr)
00066 {
00067     size_t    rest;
00068     uchar_t * p;
00069 
00070     rest = *nbyte;
00071     p = *ptr;
00072 
00073     PUTU16(m->code, rest, p, data_s->byte_swap);
00074     PUTU16(m->feedback_attr->nbyte, rest, p, data_s->byte_swap);
00075     iiimp_feedback_attr_list_pack(data_s, m->feedback_attr, &rest, &p);
00076 
00077     *nbyte = rest;
00078     *ptr = p;
00079 
00080     return;
00081 }
00082 
00083 
00084 void
00085 iiimp_char_with_feedback_list_pack(
00086     IIIMP_data_s *          data_s,
00087     IIIMP_char_with_feedback *     m,
00088     size_t *                nbyte,
00089     uchar_t **                     ptr)
00090 {
00091     size_t    rest;
00092     uchar_t * p;
00093 
00094     rest = *nbyte;
00095     p = *ptr;
00096 
00097     for (; NULL != m; m = m->next) {
00098        iiimp_char_with_feedback_pack(data_s, m, &rest, &p);
00099     }
00100 
00101     *nbyte = rest;
00102     *ptr = p;
00103 
00104     return;
00105 }
00106 
00107 
00108 IIIMP_char_with_feedback *
00109 iiimp_char_with_feedback_unpack(
00110     IIIMP_data_s *   data_s,
00111     size_t *         nbyte,
00112     const uchar_t ** ptr,
00113     size_t           nbyte_max)
00114 {
00115     IIIMP_char_with_feedback *     cwf;
00116     size_t                  rest;
00117     const uchar_t *         p;
00118     int                            len;
00119 
00120     rest = nbyte_max;
00121     p = *ptr;
00122 
00123     if ((*nbyte < rest) || (rest < 4)) {
00124        data_s->status = IIIMP_DATA_INVALID;
00125        return NULL;
00126     }
00127 
00128     cwf = (IIIMP_char_with_feedback *)malloc(sizeof (IIIMP_char_with_feedback));
00129     if (NULL == cwf) {
00130        data_s->status = IIIMP_DATA_MALLOC_ERROR;
00131        return NULL;
00132     }
00133 
00134     GETU16(cwf->code, rest, p, data_s->byte_swap);
00135     GET16(len, rest, p, data_s->byte_swap);
00136     if ((len < 0) || (rest < len) || (0 != (len & 0x07))) {
00137        free(cwf);
00138        data_s->status = IIIMP_DATA_INVALID;
00139        return NULL;
00140     }
00141 
00142     cwf->next = NULL;
00143     cwf->feedback_attr =
00144        iiimp_feedback_attr_list_unpack(data_s, &rest, &p, len);
00145     if (NULL == cwf->feedback_attr) {
00146        free(cwf);
00147        return NULL;
00148     }
00149 
00150     *nbyte -= (nbyte_max - rest);
00151     *ptr = p;
00152 
00153     return cwf;
00154 }
00155 
00156 
00157 IIIMP_char_with_feedback *
00158 iiimp_char_with_feedback_list_unpack(
00159     IIIMP_data_s *   data_s,
00160     size_t *         nbyte,
00161     const uchar_t ** ptr,
00162     size_t           nbyte_max)
00163 {
00164     IIIMP_char_with_feedback *     cwf;
00165     size_t                  rest;
00166     const uchar_t *         p;
00167     IIIMP_char_with_feedback *     cwf_first;
00168     IIIMP_char_with_feedback *     cwf_last;
00169 
00170     rest = nbyte_max;
00171     p = *ptr;
00172     cwf = NULL;
00173     cwf_first = NULL;
00174     cwf_last = NULL;
00175 
00176     if (((*nbyte) < nbyte_max) || (0 != (rest & 0x01))) {
00177        data_s->status = IIIMP_DATA_INVALID;
00178        return NULL;
00179     }
00180 
00181     while (0 < rest) {
00182        cwf = iiimp_char_with_feedback_unpack(data_s, &rest, &p, rest);
00183        if (NULL == cwf) {
00184            iiimp_char_with_feedback_list_delete(data_s, cwf_first);
00185            return NULL;
00186        } else {
00187            if (NULL == cwf_first) {
00188               cwf_first = cwf;
00189            } else {
00190               cwf_last->next = cwf;
00191            }
00192            cwf_last = cwf;
00193        }
00194     }
00195 
00196     *nbyte -= (nbyte_max - rest);
00197     *ptr = p;
00198 
00199     return cwf_first;
00200 }
00201 
00202 
00203 void
00204 iiimp_char_with_feedback_print(
00205     IIIMP_data_s *          data_s,
00206     IIIMP_char_with_feedback *     m)
00207 {
00208     if (NULL == m) return;
00209     (void)fprintf(data_s->print_fp, "U+%04x", m->code);
00210     iiimp_feedback_attr_list_print(data_s, m->feedback_attr);
00211 }
00212 
00213 
00214 void
00215 iiimp_char_with_feedback_list_print(
00216     IIIMP_data_s *          data_s,
00217     IIIMP_char_with_feedback *     m)
00218 {
00219     for (; NULL != m; m = m->next) {
00220        (void)fputc('\t', data_s->print_fp);
00221        iiimp_char_with_feedback_print(data_s, m);
00222        (void)fputc('\n', data_s->print_fp);
00223     }
00224 }
00225 
00226 
00227 /* Local Variables: */
00228 /* c-file-style: "iiim-project" */
00229 /* End: */