Parent: [152d47] (diff)

Child: [d392d3] (diff)

Download this file

mimetype.cpp    80 lines (66 with data), 1.7 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#ifndef lint
static char rcsid[] = "@(#$Id: mimetype.cpp,v 1.5 2005-02-09 13:34:08 dockes Exp $ (C) 2004 J.F.Dockes";
#endif
#include <ctype.h>
#include <string>
using std::string;
#include "mimetype.h"
#include "debuglog.h"
string mimetype(const string &fn, ConfTree *mtypes)
{
if (mtypes == 0)
return "";
static list<string> stoplist;
if (stoplist.empty()) {
string stp;
if (mtypes->get(string("recoll_noindex"), stp, "")) {
ConfTree::stringToStrings(stp, stoplist);
}
}
if (!stoplist.empty()) {
for (list<string>::const_iterator it = stoplist.begin();
it != stoplist.end(); it++) {
if (it->length() > fn.length())
continue;
if (!fn.compare(fn.length() - it->length(), string::npos, *it)) {
LOGDEB1(("mimetype: fn %s in stoplist (%s)\n", fn.c_str(),
it->c_str()));
return "";
}
}
}
// If the file name has a suffix and we find it in the map, we're done
string::size_type dot = fn.find_last_of(".");
if (dot != string::npos) {
string suff = fn.substr(dot);
for (unsigned int i = 0; i < suff.length(); i++)
suff[i] = tolower(suff[i]);
string mtype;
if (mtypes->get(suff, mtype, ""))
return mtype;
}
// Look at file data ? One day maybe
return "";
}
#ifdef _TEST_MIMETYPE_
#include <iostream>
const char *tvec[] = {
"/toto/tutu",
"/",
"toto.txt",
"toto.TXT",
"toto.C.txt",
"toto.C1",
"",
};
const int n = sizeof(tvec) / sizeof(char*);
using namespace std;
int main(int argc, const char **argv)
{
map<string, string>mtypes;
mtypes[".txt"] = "text/plain";
for (int i = 0; i < n; i++) {
cout << tvec[i] << " -> " << mimetype(string(tvec[i]), mtypes) << endl;
}
}
#endif