Main Page   Data Structures   File List   Data Fields   Globals   Related Pages  

mime.c

Go to the documentation of this file.
00001 /***************************************************************************
00002                                    mime.c
00003                              -------------------
00004     begin                : Sun Dec 09 2001
00005     copyright            : (C) 2001-2002 by Christian Hoenig & Gunter Ohrner
00006     email                : pdepp@CustomCDROM.de
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
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     // empty line?
00064     if (getNextValue(&tok_start, &tok_end, tok_end+1, line) < 0)
00065       cont = false;
00066     // comment?
00067     if (line[tok_start] == '#')
00068       cont = false; // found comment
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; // found comment
00084       // file extension found
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; // getNV checks for end-of-string
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 }

Generated on Fri Jan 25 22:40:31 2002 for PDepp Webserver by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001