Back to index

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