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