Fix tg_termpos1 for 64-bit termpos
[xapian.git] / xapian-applications / omega / mime.cc
blob114bf68abc5884d34c8d2a3dbaa86c3522997305
1 /** @file
2 * @brief Find MIME type for a file
3 */
4 /* Copyright 1999,2000,2001 BrightStation PLC
5 * Copyright 2001,2005 James Aylett
6 * Copyright 2001,2002 Ananova Ltd
7 * Copyright 2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015 Olly Betts
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of the
12 * License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
22 * USA
25 #include <config.h>
27 #include "mime.h"
29 #include <algorithm>
31 #include "keyword.h"
32 #include "mimemap.h"
33 #include "stringutils.h"
35 using namespace std;
37 // The longest string after a '.' to treat as an extension. If there's a
38 // longer entry in the mime_map, we set this to that length instead.
39 size_t max_ext_len = max(size_t(7), MAX_BUILTIN_MIMEMAP_EXTENSION_LEN);
41 const char *
42 built_in_mime_map(const string & ext)
44 int k = keyword2(tab, ext.data(), ext.size());
45 return k >= 0 ? default_mime_map[k] : NULL;
48 string
49 mimetype_from_ext(const map<string, string> & mime_map, string ext)
51 map<string, string>::const_iterator mt = mime_map.find(ext);
52 if (mt != mime_map.end())
53 return mt->second;
55 const char * r = built_in_mime_map(ext);
56 if (r) return r;
58 // The extension wasn't found, see if the lower-cased version (if
59 // different) is found.
60 bool changed = false;
61 string::iterator i;
62 for (i = ext.begin(); i != ext.end(); ++i) {
63 if (*i >= 'A' && *i <= 'Z') {
64 *i = C_tolower(*i);
65 changed = true;
69 if (changed) {
70 mt = mime_map.find(ext);
71 if (mt != mime_map.end())
72 return mt->second;
74 r = built_in_mime_map(ext);
75 if (r) return r;
78 return string();