File: mime.c

package info (click to toggle)
yaz 5.27.1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 16,184 kB
  • sloc: xml: 123,414; ansic: 72,530; sh: 5,007; tcl: 2,169; makefile: 1,321; yacc: 382
file content (88 lines) | stat: -rw-r--r-- 1,817 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/* This file is part of the YAZ toolkit.
 * Copyright (C) Index Data
 * See the file LICENSE for details.
 */

/** \file mime.c
    \brief Small utility to manage MIME types
*/

#if HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <string.h>
#include <yaz/xmalloc.h>

#include "mime.h"

struct yaz_mime_entry {
    char *suffix;
    char *mime_type;
    struct yaz_mime_entry *next;
};

struct yaz_mime_info {
    struct yaz_mime_entry *table;
};

yaz_mime_types yaz_mime_types_create()
{
    yaz_mime_types p = (yaz_mime_types) xmalloc(sizeof(*p));
    p->table = 0;
    return p;
}

void yaz_mime_types_add(yaz_mime_types t, const char *suffix,
                        const char *mime_type)
{
    struct yaz_mime_entry *e = (struct yaz_mime_entry *) xmalloc(sizeof(*e));
    e->mime_type  = xstrdup(mime_type);
    e->suffix = xstrdup(suffix);
    e->next = t->table;
    t->table = e;
}

const char *yaz_mime_lookup_suffix(yaz_mime_types t, const char *suffix)
{
    struct yaz_mime_entry *e = t->table;
    for (; e; e = e->next)
    {
        if (!strcmp(e->suffix, suffix))
            return e->mime_type;
    }
    return 0;
}

const char *yaz_mime_lookup_fname(yaz_mime_types t, const char *fname)
{
    const char *cp = strrchr(fname, '.');
    if (!cp) /* if no . return now */
        return 0;
    return yaz_mime_lookup_suffix(t, cp+1);  /* skip . */
}

void yaz_mime_types_destroy(yaz_mime_types t)
{
    struct yaz_mime_entry *e = t->table;
    while (e)
    {
        struct yaz_mime_entry *e_next = e->next;
        xfree(e->suffix);
        xfree(e->mime_type);
        xfree(e);
        e = e_next;
    }
    xfree(t);
}

/*
 * Local variables:
 * c-basic-offset: 4
 * c-file-style: "Stroustrup"
 * indent-tabs-mode: nil
 * End:
 * vim: shiftwidth=4 tabstop=8 expandtab
 */