blob: 17c29e66215909358a733b842f37d898f2b7e105 (
plain)
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
|
/*
* mimetypes.c: Web video plugin for the Video Disk Recorder
*
* See the README file for copyright information and how to reach the author.
*
* $Id$
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <vdr/tools.h>
#include "mimetypes.h"
#include "common.h"
// --- cMimeListObject -----------------------------------------------------
cMimeListObject::cMimeListObject(const char *mimetype, const char *extension) {
type = strdup(mimetype);
ext = strdup(extension);
}
cMimeListObject::~cMimeListObject() {
free(type);
free(ext);
}
// --- cMimeTypes ----------------------------------------------------------
cMimeTypes::cMimeTypes(const char **mimetypefiles) {
for (const char **filename=mimetypefiles; *filename; filename++) {
FILE *f = fopen(*filename, "r");
if (!f) {
LOG_ERROR_STR((const char *)cString::sprintf("failed to open mime type file %s", *filename));
continue;
}
cReadLine rl;
char *line = rl.Read(f);
while (line) {
// Comment lines starting with '#' and empty lines are skipped
// Expected format for the lines:
// mime/type ext
if (*line && (*line != '#')) {
char *ptr = line;
while ((*ptr != '\0') && (!isspace(*ptr)))
ptr++;
if (ptr == line) {
// empty line, ignore
line = rl.Read(f);
continue;
}
char *mimetype = (char *)malloc(ptr-line+1);
strncpy(mimetype, line, ptr-line);
mimetype[ptr-line] = '\0';
while (*ptr && isspace(*ptr))
ptr++;
char *eptr = ptr;
while (*ptr && !isspace(*ptr))
ptr++;
if (ptr == eptr) {
// no extension, ignore
free(mimetype);
line = rl.Read(f);
continue;
}
char *extension = (char *)malloc(ptr-eptr+1);
strncpy(extension, eptr, ptr-eptr);
extension[ptr-eptr] = '\0';
types.Add(new cMimeListObject(mimetype, extension));
free(extension);
free(mimetype);
}
line = rl.Read(f);
}
fclose(f);
}
}
char *cMimeTypes::ExtensionFromMimeType(const char *mimetype) {
if (!mimetype)
return NULL;
for (cMimeListObject *m = types.First(); m; m = types.Next(m))
if (strcmp(m->GetType(), mimetype) == 0) {
return strdup(m->GetExtension());
}
return NULL;
}
|