#include <string.h> #include "mime.h" static STR_MIME_MAP mime_map[]= { MIME_MAP(MIME_STR_GEN) }; void get_mime_type(const char* filename, EN_MIME_TYPE* filetype) { int i =0; for(i=0; i<= MIME_ELSE; i++) { if(strstr(filename, mime_map[i].extension)) { *filetype = (EN_MIME_TYPE)i; break; } if( i == MIME_ELSE) *filetype = MIME_ELSE; } } char* get_resp_type(EN_MIME_TYPE filetype) { if(filetype > MIME_ELSE || filetype < 0) return NULL; return mime_map[filetype].mimetype; }
#ifndef __MIME__H #define __MIME__H #define MIME_MAP(xx) xx(HTML, ".html", "text/html ") xx(HTM, ".htm", "text/html ") xx(XML, ".xml", "text/xml ") xx(IMAGE_GIF, ".gif", "image/gif ") xx(IMAGE_JPG, ".jpg", "image/jpeg ") xx(IMAGE_PNG, ".png", "image/png ") xx(TEXT, ".txt", "text/html ") xx(MP3, ".mp3", "audio/x-mpeg ") xx(ELSE, "*", "text/plain ") #define MIME_ENUM_GEN(n, s, t) MIME_##n, #define MIME_STR_GEN(n, s, t) {MIME_##n, s, t}, typedef enum en_mine_type { MIME_MAP(MIME_ENUM_GEN) }EN_MIME_TYPE; typedef struct str_mime_map { EN_MIME_TYPE entype; char* extension; char* mimetype; }STR_MIME_MAP; void get_mime_type(const char* filename, EN_MIME_TYPE* filetype); char* get_resp_type(EN_MIME_TYPE filetype); //typedef struct str_mime_map STR_MIME_MAP; #endif