5a515c3a1b3a8bf9c9470e037f215b14da4bd6da
[yaz-moved-to-github.git] / src / mime.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2008 Index Data
3  * See the file LICENSE for details.
4  */
5
6 /** \file mime.c
7     \brief Small utility to manage MIME types
8 */
9
10 #if HAVE_CONFIG_H
11 #include <config.h>
12 #endif
13
14 #include <stdio.h>
15 #include <string.h>
16 #include <ctype.h>
17 #include <yaz/xmalloc.h>
18
19 #include "mime.h"
20
21 struct yaz_mime_entry {
22     char *suffix;
23     char *mime_type;
24     struct yaz_mime_entry *next;
25 };
26
27 struct yaz_mime_info {
28     struct yaz_mime_entry *table;
29 };
30
31 yaz_mime_types yaz_mime_types_create()
32 {
33     yaz_mime_types p = (yaz_mime_types) xmalloc(sizeof(*p));
34     p->table = 0;
35     return p;
36 }
37
38 void yaz_mime_types_add(yaz_mime_types t, const char *suffix,
39                         const char *mime_type)
40 {
41     struct yaz_mime_entry *e = (struct yaz_mime_entry *) xmalloc(sizeof(*e));
42     e->mime_type  = xstrdup(mime_type);
43     e->suffix = xstrdup(suffix);
44     e->next = t->table;
45     t->table = e;
46 }
47
48 const char *yaz_mime_lookup_suffix(yaz_mime_types t, const char *suffix)
49 {
50     struct yaz_mime_entry *e = t->table;
51     for (; e; e = e->next)
52     {
53         if (!strcmp(e->suffix, suffix))
54             return e->mime_type;
55     }
56     return 0;
57 }
58
59 const char *yaz_mime_lookup_fname(yaz_mime_types t, const char *fname)
60 {
61     const char *cp = strrchr(fname, '.');
62     if (!cp) /* if no . return now */
63         return 0;
64     return yaz_mime_lookup_suffix(t, cp+1);  /* skip . */
65 }
66
67 void yaz_mime_types_destroy(yaz_mime_types t)
68 {
69     struct yaz_mime_entry *e = t->table;
70     while (e)
71     {
72         struct yaz_mime_entry *e_next = e->next;
73         xfree(e->suffix);
74         xfree(e->mime_type);
75         xfree(e);
76         e = e_next;
77     }
78     xfree(t);
79 }
80
81 /*
82  * Local variables:
83  * c-basic-offset: 4
84  * indent-tabs-mode: nil
85  * End:
86  * vim: shiftwidth=4 tabstop=8 expandtab
87  */
88