2003-06-30 16:44:03 +04:00
|
|
|
/*
|
|
|
|
* This file is part of NetSurf, http://netsurf.sourceforge.net/
|
|
|
|
* Licensed under the GNU General Public License,
|
|
|
|
* http://www.opensource.org/licenses/gpl-license
|
2004-03-11 05:19:14 +03:00
|
|
|
* Copyright 2004 James Bursa <bursa@users.sourceforge.net>
|
2003-03-15 18:53:20 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
2003-12-27 03:11:57 +03:00
|
|
|
#include <string.h>
|
2003-03-15 18:53:20 +03:00
|
|
|
#include <unixlib/local.h>
|
2004-03-11 05:19:14 +03:00
|
|
|
#include "oslib/mimemap.h"
|
2003-03-15 18:53:20 +03:00
|
|
|
#include "oslib/osfile.h"
|
2004-03-11 05:19:14 +03:00
|
|
|
#include "netsurf/content/content.h"
|
2003-03-15 18:53:20 +03:00
|
|
|
#include "netsurf/content/fetch.h"
|
2004-03-11 05:19:14 +03:00
|
|
|
#include "netsurf/riscos/gui.h"
|
2004-09-14 03:59:30 +04:00
|
|
|
#include "netsurf/utils/config.h"
|
2003-03-15 18:53:20 +03:00
|
|
|
#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;
|
2003-08-25 02:39:55 +04:00
|
|
|
char mime_type[40];
|
2003-03-15 18:53:20 +03:00
|
|
|
};
|
|
|
|
static const struct type_entry type_map[] = {
|
2003-08-25 02:39:55 +04:00
|
|
|
{0x188, "application/x-shockwave-flash"},
|
2003-06-05 17:17:55 +04:00
|
|
|
{0x695, "image/gif"},
|
2003-09-11 02:27:15 +04:00
|
|
|
{0xaff, "image/x-drawfile"},
|
2003-05-10 15:15:49 +04:00
|
|
|
{0xb60, "image/png"},
|
2003-03-15 18:53:20 +03:00
|
|
|
{0xc85, "image/jpeg"},
|
2004-07-26 00:45:16 +04:00
|
|
|
{0xf78, "image/jng"},
|
2003-04-10 01:57:09 +04:00
|
|
|
{0xf79, "text/css"},
|
2004-07-26 00:45:16 +04:00
|
|
|
{0xf83, "image/mng"},
|
2003-03-15 18:53:20 +03:00
|
|
|
{0xfaf, "text/html"},
|
2003-09-11 02:27:15 +04:00
|
|
|
{0xff9, "image/x-riscos-sprite"},
|
2003-03-15 18:53:20 +03:00
|
|
|
{0xfff, "text/plain"},
|
|
|
|
};
|
|
|
|
#define TYPE_MAP_COUNT (sizeof(type_map) / sizeof(type_map[0]))
|
|
|
|
|
|
|
|
|
|
|
|
static int cmp_type(const void *x, const void *y);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2004-03-11 05:19:14 +03:00
|
|
|
* Determine the MIME type of a local file.
|
2003-03-15 18:53:20 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
const char *fetch_filetype(const char *unix_path)
|
|
|
|
{
|
|
|
|
struct type_entry *t;
|
|
|
|
unsigned int len = strlen(unix_path) + 100;
|
|
|
|
char *path = xcalloc(len, 1);
|
|
|
|
char *r;
|
|
|
|
os_error *error;
|
|
|
|
bits file_type;
|
|
|
|
|
|
|
|
LOG(("unix_path = '%s'", unix_path));
|
|
|
|
|
|
|
|
/* convert path to RISC OS format and read file type */
|
2004-07-31 16:56:25 +04:00
|
|
|
r = __riscosify(unix_path, 0, __RISCOSIFY_NO_SUFFIX, path, len, 0);
|
2003-03-15 18:53:20 +03:00
|
|
|
if (r == 0) {
|
|
|
|
LOG(("__riscosify failed"));
|
|
|
|
return "application/riscos";
|
|
|
|
}
|
|
|
|
LOG(("riscos path '%s'", path));
|
|
|
|
|
|
|
|
error = xosfile_read_stamped_no_path(path, 0, 0, 0, 0, 0, &file_type);
|
|
|
|
if (error != 0) {
|
|
|
|
LOG(("xosfile_read_stamped_no_path failed: %s", error->errmess));
|
|
|
|
return "application/riscos";
|
|
|
|
}
|
|
|
|
|
|
|
|
/* search for MIME type */
|
|
|
|
t = bsearch(&file_type, type_map, TYPE_MAP_COUNT, sizeof(type_map[0]), cmp_type);
|
|
|
|
if (t == 0)
|
|
|
|
return "application/riscos";
|
|
|
|
LOG(("mime type '%s'", t->mime_type));
|
|
|
|
return t->mime_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-03-22 00:32:15 +03:00
|
|
|
char *fetch_mimetype(const char *ro_path) {
|
|
|
|
|
|
|
|
os_error *e;
|
2004-03-22 01:31:30 +03:00
|
|
|
bits filetype = 0, load;
|
|
|
|
int objtype;
|
2004-03-22 00:32:15 +03:00
|
|
|
char *mime = xcalloc(256, sizeof(char));
|
|
|
|
|
2004-03-22 01:31:30 +03:00
|
|
|
e = xosfile_read_no_path(ro_path, &objtype, &load, 0, 0, 0);
|
2004-03-22 00:32:15 +03:00
|
|
|
if (e) return 0;
|
|
|
|
|
2004-03-22 01:31:30 +03:00
|
|
|
if (objtype == 0x2) return 0; /* directories are pointless */
|
|
|
|
|
|
|
|
if ((load >> 20) & 0xFFF) {
|
|
|
|
filetype = (load>>8) & 0x000FFF;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return 0; /* no idea */
|
|
|
|
}
|
|
|
|
|
2004-03-22 00:32:15 +03:00
|
|
|
e = xmimemaptranslate_filetype_to_mime_type(filetype, mime);
|
|
|
|
if (e) return 0;
|
|
|
|
|
|
|
|
return mime;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-03-15 18:53:20 +03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2004-03-11 05:19:14 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine the RISC OS filetype for a content.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int ro_content_filetype(struct content *content)
|
|
|
|
{
|
|
|
|
int file_type;
|
|
|
|
os_error *error;
|
|
|
|
|
|
|
|
switch (content->type) {
|
|
|
|
case CONTENT_HTML: return 0xfaf;
|
|
|
|
case CONTENT_TEXTPLAIN: return 0xfff;
|
|
|
|
case CONTENT_CSS: return 0xf79;
|
2004-09-14 03:59:30 +04:00
|
|
|
#ifdef WITH_MNG
|
2004-07-26 00:45:16 +04:00
|
|
|
case CONTENT_JNG: return 0xf78;
|
2004-09-14 03:59:30 +04:00
|
|
|
case CONTENT_MNG: return 0xf84;
|
|
|
|
#endif
|
|
|
|
#ifdef WITH_JPEG
|
2004-03-11 05:19:14 +03:00
|
|
|
case CONTENT_JPEG: return 0xc85;
|
2004-09-14 03:59:30 +04:00
|
|
|
#endif
|
|
|
|
#ifdef WITH_PNG
|
2004-03-11 05:19:14 +03:00
|
|
|
case CONTENT_PNG: return 0xb60;
|
2004-09-14 03:59:30 +04:00
|
|
|
#endif
|
|
|
|
#ifdef WITH_GIF
|
2004-03-11 05:19:14 +03:00
|
|
|
case CONTENT_GIF: return 0x695;
|
2004-09-14 03:59:30 +04:00
|
|
|
#endif
|
|
|
|
#ifdef WITH_SPRITE
|
2004-03-11 05:19:14 +03:00
|
|
|
case CONTENT_SPRITE: return 0xff9;
|
2004-09-14 03:59:30 +04:00
|
|
|
#endif
|
|
|
|
#ifdef WITH_DRAW
|
2004-03-11 05:19:14 +03:00
|
|
|
case CONTENT_DRAW: return 0xaff;
|
2004-09-14 03:59:30 +04:00
|
|
|
#endif
|
2004-03-11 05:19:14 +03:00
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
|
|
|
|
error = xmimemaptranslate_mime_type_to_filetype(content->mime_type,
|
|
|
|
&file_type);
|
|
|
|
if (error)
|
|
|
|
return 0xffd;
|
|
|
|
return file_type;
|
|
|
|
}
|