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