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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
#include "Glob.h"
/* MIME type/ext map */
ContentTypeDB MIME = NULL;
/* Default Mime type when recognition fails */
TCHAR DefaultMime[] = _T("application/octet-stream");
int bInitMimeHandling() {
FILE *mimeDB;
TCHAR line[LINE_MAX_SIZE];
TCHAR *tok = NULL;
ContentType *pDBCell = NULL;
ContentTypeDB pDB = NULL;
ExtensionList extListCur = NULL;
ExtensionListCell *pExtCell = NULL;
TCHAR szBuf[10000];
_tcscpy(szBuf, szPluginPath);
_tcscat(szBuf, szMimeTypeConfigFile);
mimeDB = _tfopen(szBuf, _T("r"));
if (mimeDB != NULL) {
while (_fgetts(line, LINE_MAX_SIZE, mimeDB)) {
/*filter junk lines assuming Mime type start with letter
(convention ?) */
if ((line[0] <= 'z' && line[0] >= 'a')
|| (line[0] <= 'Z' && line[0] >= 'A')) {
/*remove comments trailing comments*/
tok = _tcsrchr(line, '#');
if (tok != NULL) {
*tok = '\0';
}
/* remove trailing \n */
int lenght = (int)_tcslen(line);
if (lenght > 0 && line[lenght - 1] == '\n')
line[lenght - 1] = '\0';
/* first token = mime type */
tok = (TCHAR *)_tcstok(line, _T(" \t"));
/*create and fill a cell*/
pDBCell = (ContentType*)malloc(sizeof(ContentType));
pDBCell->mimeType = (TCHAR *)malloc((_tcslen(tok) + 1) * sizeof(TCHAR));
_tcscpy(pDBCell->mimeType, tok);
pDBCell->extList = NULL;
pDBCell->next = NULL;
/* looking for extensions */
tok = (TCHAR *)_tcstok(NULL, _T(" \t"));
while (tok != NULL) {
/*create and fill a cell*/
pExtCell = (ExtensionListCell*)malloc(sizeof(ExtensionListCell));
pExtCell->ext = (TCHAR *)malloc((_tcslen(tok) + 1) * sizeof(TCHAR));
_tcscpy(pExtCell->ext, tok);
pExtCell->next = NULL;
/*link*/
if (pDBCell->extList == NULL) {
pDBCell->extList = pExtCell;
} else {
extListCur->next = pExtCell;
}
extListCur = pExtCell;
tok = (TCHAR *)_tcstok(NULL, _T(" \t"));
}
/* link */
if (pDBCell->extList != NULL) { /*extension(s) found*/
if (MIME == NULL) {
MIME = pDBCell;
} else {
pDB->next = pDBCell;
}
pDB = pDBCell;
} else { /*no extension found, freeing memory*/
free(pDBCell->mimeType);
free(pDBCell);
}
}
}
fclose(mimeDB);
}
if (MIME == NULL) {
return 0;
}
return 1;
}
const TCHAR *pszGetMimeType(const TCHAR *pszFileName)
{
ContentTypeDB courMIME;
ExtensionList courEXT;
const TCHAR *ext;
ext = _tcsrchr(pszFileName, '.');
if (ext != NULL) {
if (ext[1] == '\0') {
/*empty extension */
return DefaultMime;
} else {
/*remove the "."*/
ext = ext + 1;
}
courMIME = MIME;
while (courMIME != NULL) {
courEXT = courMIME->extList;
while (courEXT != NULL) {
if (!_tcsicmp(courEXT->ext, ext)) {
return courMIME->mimeType;
}
courEXT = courEXT->next;
}
courMIME = courMIME->next;
}
/*extension unknown*/
return DefaultMime;
} else {
/*no extension*/
return DefaultMime;
}
}
#ifdef TEST
void printDB() {
ContentTypeDB courMIME;
ExtensionList courEXT;
courMIME = MIME;
while (courMIME != NULL) {
printf("%s", courMIME->mimeType);
courEXT = courMIME->extList;
while (courEXT != NULL) {
printf(" %s", courEXT->ext);
courEXT = courEXT->next;
}
courMIME = courMIME->next;
printf("\n");
}
}
int main(int argc, char* argv[]) {
bInitMimeHandling();
printDB();
printf("%s\n", pszGetMimeType(argv[1]));
return 0;
}
#endif
|