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