Back to index

im-sdk  12.3.91
comp-annotation.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 #include "print-misc.h"
00010 
00011 
00012 IIIMP_annotation *
00013 iiimp_annotation_new(
00014     IIIMP_data_s *          data_s,
00015     IIIMP_card32            id,
00016     IIIMP_annotation_value *       value)
00017 {
00018     IIIMP_annotation *             anno;
00019     IIIMP_annotation_value *       anno_v;
00020 
00021     anno = (IIIMP_annotation *)malloc(sizeof (IIIMP_annotation));
00022     if (NULL == anno) {
00023        data_s->status = IIIMP_DATA_MALLOC_ERROR;
00024        return NULL;
00025     }
00026 
00027     anno->nbyte = (4 + 4);
00028     anno->id = id;
00029     anno->value = value;
00030     anno->next = NULL;
00031     anno->value_nbyte = 0;
00032 
00033     for (anno_v = value; NULL != anno_v; anno_v = anno_v->next) {
00034        anno->value_nbyte += anno_v->nbyte;
00035        anno->nbyte += anno_v->nbyte;
00036     }
00037 
00038     return anno;
00039 }
00040 
00041 
00042 void
00043 iiimp_annotation_delete(IIIMP_data_s * data_s, IIIMP_annotation * anno)
00044 {
00045     if (NULL == anno) return;
00046     switch (anno->id) {
00047     case IIIMP_ANNOTATION_TEXT_INPUT_BEFORE_CONVERSION:
00048     case IIIMP_ANNOTATION_READING_OF_THE_TEXT:
00049        iiimp_annotation_text_delete(data_s, anno->value);
00050        break;
00051     case IIIMP_ANNOTATION_TYPE_OF_SPEECH:
00052     case IIIMP_ANNOTATION_TYPE_OF_CLAUSE:
00053        iiimp_annotation_string_delete(data_s, anno->value);
00054        break;
00055     default:
00056        break;
00057     }
00058     free(anno);
00059     return;
00060 }
00061 
00062 
00063 void
00064 iiimp_annotation_pack(
00065     IIIMP_data_s *   data_s,
00066     IIIMP_annotation *      m,
00067     size_t *         nbyte,
00068     uchar_t **              ptr)
00069 {
00070     size_t           rest;
00071     uchar_t *        p;
00072 
00073     rest = *nbyte;
00074     p = *ptr;
00075 
00076     PUTU32(m->id, rest, p, data_s->byte_swap);
00077 
00078     switch (m->id) {
00079     case IIIMP_ANNOTATION_TEXT_INPUT_BEFORE_CONVERSION:
00080     case IIIMP_ANNOTATION_READING_OF_THE_TEXT:
00081        iiimp_annotation_text_pack(data_s, m->value, &rest, &p);
00082        break;
00083     case IIIMP_ANNOTATION_TYPE_OF_SPEECH:
00084     case IIIMP_ANNOTATION_TYPE_OF_CLAUSE:
00085        iiimp_annotation_string_pack(data_s, m->value, &rest, &p);
00086        break;
00087     default:
00088        break;
00089     }
00090 
00091     *nbyte = rest;
00092     *ptr = p;
00093 
00094     return;
00095 }
00096 
00097 
00098 void
00099 iiimp_annotation_list_pack(
00100     IIIMP_data_s *   data_s,
00101     IIIMP_annotation *      m,
00102     size_t *         nbyte,
00103     uchar_t **              ptr)
00104 {
00105     size_t           rest;
00106     uchar_t *        p;
00107 
00108     rest = *nbyte;
00109     p = *ptr;
00110 
00111     for (; NULL != m; m = m->next) {
00112        iiimp_annotation_pack(data_s, m, &rest, &p);
00113     }
00114 
00115     *nbyte = rest;
00116     *ptr = p;
00117 
00118     return;
00119 }
00120 
00121 
00122 IIIMP_annotation *
00123 iiimp_annotation_unpack(
00124     IIIMP_data_s *   data_s,
00125     size_t *         nbyte,
00126     const uchar_t ** ptr,
00127     size_t           nbyte_max)
00128 {
00129     IIIMP_annotation *      anno;
00130     size_t           rest;
00131     const uchar_t *  p;
00132     int                     len;
00133     IIIMP_card32     anno_id;
00134 
00135     rest = nbyte_max;
00136     p = *ptr;
00137 
00138     if ((*nbyte < rest) || (rest < (4 + 4))) {
00139        data_s->status = IIIMP_DATA_INVALID;
00140        return NULL;
00141     }
00142 
00143     GETU32(anno_id, rest, p, data_s->byte_swap);
00144     GETU32(len, rest, p, data_s->byte_swap);
00145 
00146     if ((rest < len) || (0 != (len & 0x03))) {
00147        data_s->status = IIIMP_DATA_INVALID;
00148        return NULL;
00149     }
00150 
00151     anno = (IIIMP_annotation *)malloc(sizeof (IIIMP_annotation));
00152     if (NULL == anno) {
00153        data_s->status = IIIMP_DATA_MALLOC_ERROR;
00154        return NULL;
00155     }
00156 
00157     anno->id = anno_id;
00158     anno->next = NULL;
00159 
00160     switch (anno->id) {
00161     case IIIMP_ANNOTATION_TEXT_INPUT_BEFORE_CONVERSION:
00162     case IIIMP_ANNOTATION_READING_OF_THE_TEXT:
00163        anno->value = iiimp_annotation_text_unpack(data_s, &rest, &p, len);
00164        break;
00165     case IIIMP_ANNOTATION_TYPE_OF_SPEECH:
00166     case IIIMP_ANNOTATION_TYPE_OF_CLAUSE:
00167        anno->value = iiimp_annotation_string_unpack(data_s, &rest, &p, len);
00168        break;
00169     default:
00170        anno->value = NULL;
00171        data_s->status = IIIMP_DATA_INVALID;
00172        break;
00173     }
00174 
00175     if (NULL == anno->value) {
00176        free(anno);
00177        return NULL;
00178     }
00179 
00180     *nbyte -= (nbyte_max - rest);
00181     *ptr = p;
00182 
00183     return anno;
00184 }
00185 
00186 
00187 void
00188 iiimp_annotation_print(
00189     IIIMP_data_s *   data_s,
00190     IIIMP_annotation *      m)
00191 {
00192     if (NULL == m) return;
00193 
00194     (void)fprintf(data_s->print_fp,
00195                 "\tannotation: %s: start_index=%d end_index=%d length=%d\n",
00196                 annotation_id_string_get(m->id),
00197                 m->value->start_index, m->value->end_index, m->value->length);
00198 
00199     switch (m->id) {
00200     case IIIMP_ANNOTATION_TEXT_INPUT_BEFORE_CONVERSION:
00201     case IIIMP_ANNOTATION_READING_OF_THE_TEXT:
00202        iiimp_text_print(data_s, m->value->v.text);
00203        break;
00204     case IIIMP_ANNOTATION_TYPE_OF_SPEECH:
00205     case IIIMP_ANNOTATION_TYPE_OF_CLAUSE:
00206        iiimp_string_print(data_s, m->value->v.string);
00207        break;
00208     }
00209 }
00210 
00211 
00212 /* Local Variables: */
00213 /* c-file-style: "iiim-project" */
00214 /* End: */