YAZ
5.34.0
src
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
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
config.h
yaz_mime_types_create
yaz_mime_types yaz_mime_types_create()
Definition:
mime.c:30
yaz_mime_lookup_suffix
const char * yaz_mime_lookup_suffix(yaz_mime_types t, const char *suffix)
Definition:
mime.c:47
yaz_mime_lookup_fname
const char * yaz_mime_lookup_fname(yaz_mime_types t, const char *fname)
Definition:
mime.c:58
yaz_mime_types_destroy
void yaz_mime_types_destroy(yaz_mime_types t)
Definition:
mime.c:66
yaz_mime_types_add
void yaz_mime_types_add(yaz_mime_types t, const char *suffix, const char *mime_type)
Definition:
mime.c:37
mime.h
Small utility to manage MIME types.
yaz_mime_types
struct yaz_mime_info * yaz_mime_types
Definition:
mime.h:35
yaz_mime_entry
Definition:
mime.c:20
yaz_mime_entry::mime_type
char * mime_type
Definition:
mime.c:22
yaz_mime_entry::next
struct yaz_mime_entry * next
Definition:
mime.c:23
yaz_mime_entry::suffix
char * suffix
Definition:
mime.c:21
yaz_mime_info
Definition:
mime.c:26
yaz_mime_info::table
struct yaz_mime_entry * table
Definition:
mime.c:27
xmalloc.h
Header for memory handling functions.
xstrdup
#define xstrdup(s)
utility macro which calls xstrdup_f
Definition:
xmalloc.h:55
xfree
#define xfree(x)
utility macro which calls xfree_f
Definition:
xmalloc.h:53
xmalloc
#define xmalloc(x)
utility macro which calls malloc_f
Definition:
xmalloc.h:49
Generated by
1.9.1