diff options
-rw-r--r-- | riscos/filetype.c | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/riscos/filetype.c b/riscos/filetype.c index 9ff9b0df2..b881bd3a2 100644 --- a/riscos/filetype.c +++ b/riscos/filetype.c @@ -17,9 +17,32 @@ #include "netsurf/utils/log.h" #include "netsurf/utils/utils.h" +/* type_map must be in sorted order by file_type */ +struct type_entry { + bits file_type; + char mime_type[40]; +}; +static const struct type_entry type_map[] = { + {0x188, "application/x-shockwave-flash"}, + {0x695, "image/gif"}, + {0xaff, "image/x-drawfile"}, + {0xb60, "image/png"}, + {0xc85, "image/jpeg"}, + {0xf78, "image/jng"}, + {0xf79, "text/css"}, + {0xf83, "image/mng"}, + {0xfaf, "text/html"}, + {0xff9, "image/x-riscos-sprite"}, + {0xfff, "text/plain"}, +}; +#define TYPE_MAP_COUNT (sizeof(type_map) / sizeof(type_map[0])) + #define BUF_SIZE (256) static char type_buf[BUF_SIZE]; + +static int cmp_type(const void *x, const void *y); + /** * Determine the MIME type of a local file. * @@ -29,6 +52,7 @@ static char type_buf[BUF_SIZE]; */ const char *fetch_filetype(const char *unix_path) { + struct type_entry *t; unsigned int len = strlen(unix_path) + 100; char *path = calloc(len, 1); char *r, *slash; @@ -75,6 +99,16 @@ const char *fetch_filetype(const char *unix_path) } } + /* search for MIME type in our internal table */ + t = bsearch(&file_type, type_map, TYPE_MAP_COUNT, + sizeof(type_map[0]), cmp_type); + if (t) { + /* found, so return it */ + free(path); + return t->mime_type; + } + + /* not in internal table, so ask MimeMap */ error = xmimemaptranslate_filetype_to_mime_type(file_type, type_buf); if (error) { LOG(("0x%x %s", error->errnum, error->errmess)); @@ -156,6 +190,16 @@ char *fetch_mimetype(const char *ro_path) } /** + * Comparison function for bsearch + */ +int cmp_type(const void *x, const void *y) +{ + const bits *p = x; + const struct type_entry *q = y; + return *p < q->file_type ? -1 : (*p == q->file_type ? 0 : +1); +} + +/** * Determine the RISC OS filetype for a content. * * \param content The content to examine. |