00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00025 #include "common.h"
00026
00027 #include <errno.h>
00028 #include <string.h>
00029
00030 #include "mime.h"
00031
00032 #include "char_xhash.h"
00033 #include "conftable.h"
00034 #include "logger.h"
00035 #include "misc.h"
00036 #include "xmalloc.h"
00037
00039 CharHashTab mime_tab;
00040
00041 int mimeInit(const char* mime_types_file)
00042 {
00043 FILE* mime_file;
00044
00045 mime_tab = cxhInit(26);
00046
00047 mime_file = fopen(mime_types_file, "r");
00048 if (mime_file == NULL)
00049 {
00050 logerr(MIME_TABLE, ERROR, errno, "mime type file could not be opened");
00051 return -1;
00052 }
00053
00054 while(!feof(mime_file))
00055 {
00056 int cont = true;
00057 int tok_start, tok_end = -1;
00058 char* line = getLine(mime_file);
00059 char* type;
00060 size_t type_len;
00061 if (line == NULL) continue;
00062
00063
00064 if (getNextValue(&tok_start, &tok_end, tok_end+1, line) < 0)
00065 cont = false;
00066
00067 if (line[tok_start] == '#')
00068 cont = false;
00069 if (cont != true)
00070 {
00071 pd_free(line);
00072 continue;
00073 }
00074 type_len = tok_end - tok_start + 1;
00075 type = (char*) pd_malloc(type_len + 1);
00076 strncpy(type, line + tok_start, type_len);
00077 type[type_len] = '\0';
00078
00079 while(getNextValue(&tok_start, &tok_end, tok_end+2, line) >= 0)
00080 {
00081 char old_sep;
00082 if (line[tok_start] == '#')
00083 break;
00084
00085 old_sep = line[tok_end+1];
00086 line[tok_end+1] = '\0';
00087 cxhInsert(mime_tab, line+tok_start, type);
00088 line[tok_end+1] = old_sep;
00089 }
00090 pd_free(type);
00091 pd_free(line);
00092 }
00093
00094 fclose(mime_file);
00095 return 0;
00096 }
00097
00098 char* mimeLookup(const char* file_ext)
00099 {
00100 return cxhGet(mime_tab, file_ext);
00101 }
00102
00103 int mimeTerminate()
00104 {
00105 cxhTerminate(mime_tab);
00106 return 0;
00107 }