Back to index

im-sdk  12.3.91
attr.c
Go to the documentation of this file.
00001 /*
00002   attr.c
00003 */
00004 #ifdef HAVE_CONFIG_H
00005 #include <config.h>
00006 #endif
00007 #ifdef HAVE_STDLIB_H
00008 #include <stdlib.h>
00009 #endif
00010 #ifdef HAVE_STRING_H
00011 #include <string.h>
00012 #endif
00013 #ifdef HAVE_MEMORY_H
00014 #include <memory.h>
00015 #endif
00016 
00017 #include "iiimcfint.h"
00018 
00019 IIIMF_status
00020 iiimcf_create_attr(
00021     IIIMCF_attr *pattr
00022 )
00023 {
00024     IIIMCF_attr_rec *pa;
00025 
00026     pa = (IIIMCF_attr_rec*) malloc(sizeof(*pa));
00027     if (!pa) return IIIMF_STATUS_MALLOC;
00028     memset(pa, 0, sizeof(*pa));
00029 
00030     *pattr = pa;
00031 
00032     return IIIMF_STATUS_SUCCESS;
00033 }
00034 
00035 IIIMF_status
00036 iiimcf_destroy_attr(
00037     IIIMCF_attr attr
00038 )
00039 {
00040     IIIMCF_attr_rec *pa = (IIIMCF_attr_rec*) attr;
00041 
00042     if (pa->pslots) free(pa->pslots);
00043     free(pa);
00044     return IIIMF_STATUS_SUCCESS;
00045 }
00046 
00047 /* Find the slots that correcpoinds to "property".
00048    If it fails, return NULL.  */
00049 static IIIMCF_attr_slot*
00050 find_slot(
00051     IIIMCF_attr_rec *pa,
00052     int property
00053 )
00054 {
00055     int i, size;
00056     IIIMCF_attr_slot *pst;
00057 
00058     if (!pa) return NULL;
00059     size = pa->nprops;
00060     pst = pa->pslots;
00061     for (i = 0; i < size; i++, pst++) {
00062        if (pst->idx == property) {
00063            return pst;
00064        }
00065     }
00066 
00067     return NULL;
00068 }
00069 
00070 /* allocate a new slot.  If it can do so, return a pointer of the slot;
00071    otherwise, return NULL */
00072 static IIIMCF_attr_slot*
00073 alloc_slot(
00074     IIIMCF_attr_rec *pa,
00075     int property
00076 )
00077 {
00078     int i, size;
00079     IIIMCF_attr_slot *pst;
00080 
00081     size = pa->nprops;
00082     pst = pa->pslots;
00083     for (i = 0; i < size; i++, pst++) {
00084        if (pst->idx == property) {
00085            return pst;
00086        }
00087     }
00088 
00089     pst = (IIIMCF_attr_slot*) realloc(pa->pslots, sizeof(*pst) * (size + 1));
00090     if (!pst) return NULL;
00091     pa->pslots = pst;
00092     pst += size;
00093     memset(pst, 0, sizeof(*pst));
00094     pa->nprops++;
00095     pst->idx = property;
00096 
00097     return pst;
00098 }
00099    
00100 IIIMF_status
00101 iiimcf_attr_put_string_value(
00102     IIIMCF_attr attr,
00103     int property,
00104     const char *str
00105 )
00106 {
00107     IIIMCF_attr_slot *ps;
00108     IIIMCF_attr_rec *pa = (IIIMCF_attr_rec*) attr;
00109 
00110     ps = alloc_slot(pa, property);
00111     if (!ps) return IIIMF_STATUS_MALLOC;
00112     if ((ps->type != IIIMCF_ATTR_TYPE_NONE) 
00113        && (ps->type != IIIMCF_ATTR_TYPE_STRING))
00114        return IIIMF_STATUS_ARGUMENT;
00115     ps->type = IIIMCF_ATTR_TYPE_STRING;
00116     ps->v.str = str;
00117 
00118     return IIIMF_STATUS_SUCCESS;
00119 }
00120 
00121 IIIMF_status
00122 iiimcf_attr_get_string_value(
00123     IIIMCF_attr attr,
00124     int property,
00125     const char **pstr
00126 )
00127 {
00128     IIIMCF_attr_slot *ps;
00129     IIIMCF_attr_rec *pa = (IIIMCF_attr_rec*) attr;
00130 
00131     ps = find_slot(pa, property);
00132     if (!ps) return IIIMF_STATUS_NO_ATTR_VALUE;
00133     if (ps->type != IIIMCF_ATTR_TYPE_STRING)
00134        return IIIMF_STATUS_ARGUMENT;
00135 
00136     *pstr = ps->v.str;
00137 
00138     return IIIMF_STATUS_SUCCESS;
00139 }
00140 
00141 IIIMF_status
00142 iiimcf_attr_put_integer_value(
00143     IIIMCF_attr attr,
00144     int property,
00145     int number
00146 )
00147 {
00148     IIIMCF_attr_slot *ps;
00149     IIIMCF_attr_rec *pa = (IIIMCF_attr_rec*) attr;
00150 
00151     ps = alloc_slot(pa, property);
00152     if (!ps) return IIIMF_STATUS_MALLOC;
00153     if ((ps->type != IIIMCF_ATTR_TYPE_NONE) 
00154        && (ps->type != IIIMCF_ATTR_TYPE_INTEGER))
00155        return IIIMF_STATUS_ARGUMENT;
00156     ps->type = IIIMCF_ATTR_TYPE_INTEGER;
00157     ps->v.number = number;
00158 
00159     return IIIMF_STATUS_SUCCESS;
00160 }
00161 
00162 IIIMF_status
00163 iiimcf_attr_get_integer_value(
00164     IIIMCF_attr attr,
00165     int property,
00166     int *pnumber
00167 )
00168 {
00169     IIIMCF_attr_slot *ps;
00170     IIIMCF_attr_rec *pa = (IIIMCF_attr_rec*) attr;
00171 
00172     ps = find_slot(pa, property);
00173     if (!ps) return IIIMF_STATUS_NO_ATTR_VALUE;
00174     if (ps->type != IIIMCF_ATTR_TYPE_INTEGER)
00175        return IIIMF_STATUS_ARGUMENT;
00176 
00177     *pnumber = ps->v.number;
00178 
00179     return IIIMF_STATUS_SUCCESS;
00180 }
00181 
00182 IIIMF_status
00183 iiimcf_attr_put_ptr_value(
00184     IIIMCF_attr attr,
00185     int property,
00186     void *ptr
00187 )
00188 {
00189     IIIMCF_attr_slot *ps;
00190     IIIMCF_attr_rec *pa = (IIIMCF_attr_rec*) attr;
00191 
00192     ps = alloc_slot(pa, property);
00193     if (!ps) return IIIMF_STATUS_MALLOC;
00194     if ((ps->type != IIIMCF_ATTR_TYPE_NONE) 
00195        && (ps->type != IIIMCF_ATTR_TYPE_POINTER))
00196        return IIIMF_STATUS_ARGUMENT;
00197     ps->type = IIIMCF_ATTR_TYPE_POINTER;
00198     ps->v.ptr = ptr;
00199 
00200     return IIIMF_STATUS_SUCCESS;
00201 }
00202 
00203 IIIMF_status
00204 iiimcf_attr_get_ptr_value(
00205     IIIMCF_attr attr,
00206     int property,
00207     void **pptr
00208 )
00209 {
00210     IIIMCF_attr_slot *ps;
00211     IIIMCF_attr_rec *pa = (IIIMCF_attr_rec*) attr;
00212 
00213     ps = find_slot(pa, property);
00214     if (!ps) return IIIMF_STATUS_NO_ATTR_VALUE;
00215     if (ps->type != IIIMCF_ATTR_TYPE_POINTER)
00216        return IIIMF_STATUS_ARGUMENT;
00217 
00218     *pptr = ps->v.ptr;
00219 
00220     return IIIMF_STATUS_SUCCESS;
00221 }
00222 
00223 /* Local Variables: */
00224 /* c-file-style: "iiim-project" */
00225 /* End: */
00226