Add to git.
[rws.git] / mime_types.c
1 /* MIME types.
2  * - by Richard W.M. Jones <rich@annexia.org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the Free
16  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  *
18  * $Id: mime_types.c,v 1.3 2002/10/06 11:57:22 rich Exp $
19  */
20
21 #include "config.h"
22
23 #include <stdio.h>
24
25 #include <pool.h>
26 #include <hash.h>
27 #include <pstring.h>
28 #include <pre.h>
29
30 #include "re.h"
31 #include "mime_types.h"
32
33 static pool mt_pool = 0;
34 static sash mt_map = 0;
35
36 void
37 mime_types_reread_config (const char *path)
38 {
39   FILE *fp;
40   char *line = 0;
41   pool tmp;
42   vector v;
43   int i;
44   char *mt, *ext;
45
46   if (mt_pool) delete_pool (mt_pool);
47   mt_pool = new_subpool (global_pool);
48
49   mt_map = new_sash (mt_pool);
50
51   tmp = new_subpool (mt_pool);
52
53   /* Read the /etc/mime.types file. */
54   fp = fopen (path, "r");
55   if (fp == 0) { perror (path); exit (1); }
56
57   while ((line = pgetlinec (tmp, fp, line)) != 0)
58     {
59       v = pstrresplit (tmp, line, re_ws);
60
61       switch (vector_size (v))
62         {
63         case 0:
64           abort ();
65
66         case 1:
67           break;
68
69         default:
70           vector_get (v, 0, mt);
71           for (i = 1; i < vector_size (v); ++i)
72             {
73               vector_get (v, i, ext);
74               sash_insert (mt_map, ext, mt);
75             }
76           break;
77         }
78     }
79
80   fclose (fp);
81
82   delete_pool (tmp);
83 }
84
85 const char *
86 mime_types_get_type (const char *ext)
87 {
88   const char *mt = 0;
89
90   sash_get (mt_map, ext, mt);
91   return mt;
92 }