YAZ  5.34.0
mime.c
Go to the documentation of this file.
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) Index Data
3  * See the file LICENSE for details.
4  */
5 
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 
21  char *suffix;
22  char *mime_type;
24 };
25 
26 struct yaz_mime_info {
28 };
29 
31 {
32  yaz_mime_types p = (yaz_mime_types) xmalloc(sizeof(*p));
33  p->table = 0;
34  return p;
35 }
36 
38  const char *mime_type)
39 {
40  struct yaz_mime_entry *e = (struct yaz_mime_entry *) xmalloc(sizeof(*e));
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 
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 
yaz_mime_types yaz_mime_types_create()
Definition: mime.c:30
const char * yaz_mime_lookup_suffix(yaz_mime_types t, const char *suffix)
Definition: mime.c:47
const char * yaz_mime_lookup_fname(yaz_mime_types t, const char *fname)
Definition: mime.c:58
void yaz_mime_types_destroy(yaz_mime_types t)
Definition: mime.c:66
void yaz_mime_types_add(yaz_mime_types t, const char *suffix, const char *mime_type)
Definition: mime.c:37
Small utility to manage MIME types.
struct yaz_mime_info * yaz_mime_types
Definition: mime.h:35
Definition: mime.c:20
char * mime_type
Definition: mime.c:22
struct yaz_mime_entry * next
Definition: mime.c:23
char * suffix
Definition: mime.c:21
struct yaz_mime_entry * table
Definition: mime.c:27
Header for memory handling functions.
#define xstrdup(s)
utility macro which calls xstrdup_f
Definition: xmalloc.h:55
#define xfree(x)
utility macro which calls xfree_f
Definition: xmalloc.h:53
#define xmalloc(x)
utility macro which calls malloc_f
Definition: xmalloc.h:49