src/libmetha/mime.c
M_CODE
lm_mimetb_add(mimetb_t *t, const char *mime, FT_ID id)
{
    mime_t m = lm_mime_hash(mime);
    int cat = (m & ~MT_HASH_MASK) >> MT_HASH_CAT_BIT_OFFS;
    int offs = m & MT_HASH_MASK;

    if (t->listsz[cat] == MT_HASH_LIST_LIMIT) {
        /* type is hash table */
        mimetb_nh_t *this;
        h_add:
        this = malloc(sizeof(mimetb_nh_t));
        if (!this)
            return M_OUT_OF_MEM;
        this->id   = id;
        this->str  = mime;
        this->next = 0;

        if (t->tbl[cat][offs]) {
            mimetb_nh_t *n=t->tbl[cat][offs];
            while (n->next)
                n=n->next;
            /* add this mime type to the list */
            n->next = this;
        } else
            t->tbl[cat][offs] = this;
    } else {
        mimetb_nl_t *this;
        /* type is list */
        if (t->listsz[cat]+1 == MT_HASH_LIST_LIMIT) {
            /* convert to hash table */
            int x;
            this = (mimetb_nl_t*)t->tbl[cat];
            if (!(t->tbl[cat] = calloc(1,sizeof(mimetb_nh_t*)*MT_HASH_COUNT)))
                return M_OUT_OF_MEM;

            t->listsz[cat]++; /* add one for listsz to exceed MT_HASH_LIST_LIMIT */

            for (x=0; x<t->listsz[cat]-1; x++)
                lm_mimetb_add(t, this[x].str, this[x].id);

            free(this);

            goto h_add;
        }
        if (!t->tbl[cat]) {
            if (!(this = (mimetb_nl_t*)(t->tbl[cat] = malloc(sizeof(mimetb_nl_t)))))
                return M_OUT_OF_MEM;
        } else {
            this = (mimetb_nl_t*)(t->tbl[cat] = realloc(t->tbl[cat], (t->listsz[cat]+1)*sizeof(mimetb_nl_t)));
            if (!this)
                return M_OUT_OF_MEM;
            this += t->listsz[cat];
        }
        t->listsz[cat]++;
        this->str = mime;
        this->id = id;
        this->mime = m;
    }

    return M_OK;
}
/** 
 * Add a mime type to the table, 
 * id is the value to associate with the
 * mime type
 **/
Add a mime type to the table,
id is the value to associate with the
mime type
Add a mime type to the table id is the value to associate with the mime type
M_CODE
lm_mimetb_add(mimetb_t *t, const char *mime, FT_ID id)
{
    mime_t m = lm_mime_hash(mime);
    int cat = (m & ~MT_HASH_MASK) >> MT_HASH_CAT_BIT_OFFS;
    int offs = m & MT_HASH_MASK;
    if (t->listsz[cat] == MT_HASH_LIST_LIMIT) {
        mimetb_nh_t *this;
        h_add:
        this = malloc(sizeof(mimetb_nh_t));
        if (!this)
            return M_OUT_OF_MEM;
        this->id   = id;
        this->str  = mime;
        this->next = 0;
        if (t->tbl[cat][offs]) {
            mimetb_nh_t *n=t->tbl[cat][offs];
            while (n->next)
                n=n->next;
            n->next = this;
        } else
            t->tbl[cat][offs] = this;
    } else {
        mimetb_nl_t *this;
        if (t->listsz[cat]+1 == MT_HASH_LIST_LIMIT) {
            int x;
            this = (mimetb_nl_t*)t->tbl[cat];
            if (!(t->tbl[cat] = calloc(1,sizeof(mimetb_nh_t*)*MT_HASH_COUNT)))
                return M_OUT_OF_MEM;
            t->listsz[cat]++; 
            for (x=0; x<t->listsz[cat]-1; x++)
                lm_mimetb_add(t, this[x].str, this[x].id);
            free(this);
            goto h_add;
        }
        if (!t->tbl[cat]) {
            if (!(this = (mimetb_nl_t*)(t->tbl[cat] = malloc(sizeof(mimetb_nl_t)))))
                return M_OUT_OF_MEM;
        } else {
            this = (mimetb_nl_t*)(t->tbl[cat] = realloc(t->tbl[cat], (t->listsz[cat]+1)*sizeof(mimetb_nl_t)));
            if (!this)
                return M_OUT_OF_MEM;
            this += t->listsz[cat];
        }
        t->listsz[cat]++;
        this->str = mime;
        this->id = id;
        this->mime = m;
    }
    return M_OK;
}
M_CODE lm_mimetb_add ( mimetb_t * t , const char * mime , FT_ID id ) { mime_t m = lm_mime_hash ( mime ) ; int cat = ( m & ~ MT_HASH_MASK ) >> MT_HASH_CAT_BIT_OFFS ; int offs = m & MT_HASH_MASK ; if ( t -> listsz [ cat ] == MT_HASH_LIST_LIMIT ) { mimetb_nh_t * this ; h_add : this = malloc ( sizeof ( mimetb_nh_t ) ) ; if ( ! this ) return M_OUT_OF_MEM ; this -> id = id ; this -> str = mime ; this -> next = 0 ; if ( t -> tbl [ cat ] [ offs ] ) { mimetb_nh_t * n = t -> tbl [ cat ] [ offs ] ; while ( n -> next ) n = n -> next ; n -> next = this ; } else t -> tbl [ cat ] [ offs ] = this ; } else { mimetb_nl_t * this ; if ( t -> listsz [ cat ] + 1 == MT_HASH_LIST_LIMIT ) { int x ; this = ( mimetb_nl_t * ) t -> tbl [ cat ] ; if ( ! ( t -> tbl [ cat ] = calloc ( 1 , sizeof ( mimetb_nh_t * ) * MT_HASH_COUNT ) ) ) return M_OUT_OF_MEM ; t -> listsz [ cat ] ++ ; for ( x = 0 ; x < t -> listsz [ cat ] - 1 ; x ++ ) lm_mimetb_add ( t , this [ x ] . str , this [ x ] . id ) ; free ( this ) ; goto h_add ; } if ( ! t -> tbl [ cat ] ) { if ( ! ( this = ( mimetb_nl_t * ) ( t -> tbl [ cat ] = malloc ( sizeof ( mimetb_nl_t ) ) ) ) ) return M_OUT_OF_MEM ; } else { this = ( mimetb_nl_t * ) ( t -> tbl [ cat ] = realloc ( t -> tbl [ cat ] , ( t -> listsz [ cat ] + 1 ) * sizeof ( mimetb_nl_t ) ) ) ; if ( ! this ) return M_OUT_OF_MEM ; this += t -> listsz [ cat ] ; } t -> listsz [ cat ] ++ ; this -> str = mime ; this -> id = id ; this -> mime = m ; } return M_OK ; }
Add a mime type to the table,
 id is the value to associate with the
 mime type
Add a mime type to the table id is the value to associate with the mime type
/* type is hash table */ /* add this mime type to the list */ /* type is list */ /* convert to hash table */ /* add one for listsz to exceed MT_HASH_LIST_LIMIT */