Updated mime functions

This commit is contained in:
2014-08-04 13:16:42 +01:00
parent 34ef45478e
commit cb16d83961
3 changed files with 35 additions and 19 deletions

View File

@@ -38,7 +38,7 @@ static void signal_int(int signum) {
int main(int argc, char** argv) { int main(int argc, char** argv) {
mime_load(NULL); mime_init(NULL);
config_server *config = config_server_new(); config_server *config = config_server_new();
if (config_read_ini("khttpd.ini", config) < 0) { if (config_read_ini("khttpd.ini", config) < 0) {
fatal("Could not read config"); fatal("Could not read config");
@@ -152,7 +152,7 @@ int main(int argc, char** argv) {
} }
} }
mime_free(); mime_destroy();
config_server_delete(config); config_server_delete(config);
svr_release(serverfd); svr_release(serverfd);
serverfd = 0; serverfd = 0;

View File

@@ -10,7 +10,18 @@
mime_type *mime_list = NULL; mime_type *mime_list = NULL;
int mime_load(const char* file) { int mime_init(const char* file) {
mime_destroy();
return mime_load(&mime_list, NULL);
}
void mime_destroy() {
if (mime_list != NULL) {
mime_free(mime_list);
}
mime_list = NULL;
}
int mime_load(mime_type **list, const char* file) {
FILE *mimetypes = fopen(file == NULL ? MIME_DEFAULT_FILE : file, "r"); FILE *mimetypes = fopen(file == NULL ? MIME_DEFAULT_FILE : file, "r");
if (mimetypes == NULL) { if (mimetypes == NULL) {
return -1; return -1;
@@ -45,38 +56,40 @@ int mime_load(const char* file) {
free(buffer); free(buffer);
fclose(mimetypes); fclose(mimetypes);
if (mime_list != NULL) { *list = new_list;
mime_free();
}
mime_list = new_list;
count = 0; count = 0;
mime_type *elem; mime_type *elem;
LL_COUNT(mime_list, elem, count); LL_COUNT(new_list, elem, count);
return count; return count;
} }
void mime_free() { void mime_free(mime_type *list) {
if (list == NULL) {
list = mime_list;
}
mime_type *elem, *tmp; mime_type *elem, *tmp;
LL_FOREACH_SAFE(mime_list, elem, tmp) { LL_FOREACH_SAFE(list, elem, tmp) {
free(elem->extension); free(elem->extension);
free(elem->mime); free(elem->mime);
free(elem); free(elem);
} }
mime_list = NULL;
} }
void mime_print_all() { void mime_print_all(mime_type *list) {
mime_type *elem; mime_type *elem;
LL_FOREACH(mime_list, elem) { LL_FOREACH(list, elem) {
printf("%s\t\t%s\n", elem->mime, elem->extension); printf("%s\t\t%s\n", elem->mime, elem->extension);
} }
} }
const char* mime_get_type(const char* filename, const char* fallback) { const char* mime_get_type(const char* filename, const char* fallback) {
return mime_list_get_type(mime_list, filename, fallback);
}
const char* mime_list_get_type(mime_type *list, const char* filename, const char* fallback) {
char *ext = strrchr(filename, '.'); char *ext = strrchr(filename, '.');
if (ext != NULL && strlen(ext) > 1) { if (ext != NULL && strlen(ext) > 1) {
ext++; //Skip . ext++; //Skip .
mime_type *elem; mime_type *elem;
LL_FOREACH(mime_list, elem) { LL_FOREACH(list, elem) {
if (strcmp(ext, elem->extension) == 0) { if (strcmp(ext, elem->extension) == 0) {
return elem->mime; return elem->mime;
} }
@@ -84,7 +97,6 @@ const char* mime_get_type(const char* filename, const char* fallback) {
} }
return mime_get_type_magic(filename, fallback); return mime_get_type_magic(filename, fallback);
} }
const char* mime_get_type_magic(const char* filename, const char* fallback) { const char* mime_get_type_magic(const char* filename, const char* fallback) {
magic_t magic; magic_t magic;

View File

@@ -24,11 +24,15 @@ extern "C" {
extern mime_type *mime_list; extern mime_type *mime_list;
int mime_load(const char* file); int mime_init(const char* file);
void mime_free(); void mime_destroy();
void mime_print_all();
int mime_load(mime_type **list, const char* file);
void mime_free(mime_type *list);
void mime_print_all(mime_type *list);
const char* mime_get_type(const char* filename, const char* fallback); const char* mime_get_type(const char* filename, const char* fallback);
const char* mime_list_get_type(mime_type *list, const char* filename, const char* fallback);
const char* mime_get_type_magic(const char* filename, const char* fallback); const char* mime_get_type_magic(const char* filename, const char* fallback);
#ifdef __cplusplus #ifdef __cplusplus