Author: rjek
Date: Wed May 30 00:53:35 2007
New Revision: 3305
URL:
http://svn.semichrome.net?rev=3D3305&view=3Drev
Log:
Add UNIX-specific fetch_filetype() support. Uses /etc/mime.types by defaul=
t, but can build minimal mappings if it doesn't exist. New code allows fil=
e:// directory listings to be generated, although these are not sorted unli=
ke under RISC OS, as POSIX dirent() doesn't specify sorting.
Added:
trunk/netsurf/gtk/gtk_filetype.c
trunk/netsurf/gtk/gtk_filetype.h
Modified:
trunk/netsurf/gtk/gtk_gui.c
trunk/netsurf/makefile
Added: trunk/netsurf/gtk/gtk_filetype.c
URL:
http://svn.semichrome.net/trunk/netsurf/gtk/gtk_filetype.c?rev=3D3305&=
view=3Dauto
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D
--- trunk/netsurf/gtk/gtk_filetype.c (added)
+++ trunk/netsurf/gtk/gtk_filetype.c Wed May 30 00:53:35 2007
@@ -1,0 +1,199 @@
+/*
+ * This file is part of NetSurf,
http://netsurf-browser.org/
+ * Licensed under the GNU General Public License,
+ *
http://www.opensource.org/licenses/gpl-license
+ * Copyright 2007 Rob Kendrick <rjek(a)netsurf-browser.org>
+ * Copyright 2007 Vincent Sanders <vince(a)debian.org>
+ */
+ =
+#include <stdio.h>
+#include <stdbool.h>
+#include <string.h>
+#include <stdlib.h>
+#include <ctype.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#include "netsurf/gtk/gtk_filetype.h"
+#include "netsurf/content/fetch.h"
+#include "netsurf/utils/log.h"
+#include "netsurf/utils/hashtable.h"
+
+static struct hash_table *mime_hash =3D NULL;
+
+void gtk_fetch_filetype_init(const char *mimefile)
+{
+ struct stat statbuf;
+ FILE *fh =3D NULL;
+
+ mime_hash =3D hash_create(117);
+ =
+ /* first, check to see if /etc/mime.types in preference */
+ =
+ if ((stat("/etc/mime.types", &statbuf) =3D=3D 0) &&
+ S_ISREG(statbuf.st_mode)) {
+ mimefile =3D "/etc/mime.types"; =
+ =
+ }
+ =
+ fh =3D fopen(mimefile, "r");
+ =
+ if (fh =3D=3D NULL) {
+ LOG(("Unable to open a mime.types file, so building a minimal one for yo=
u."));
+ hash_add(mime_hash, "css", "text/css");
+ hash_add(mime_hash, "jpg", "image/jpeg");
+ hash_add(mime_hash, "jpeg", "image/jpeg");
+ hash_add(mime_hash, "gif", "image/gif");
+ hash_add(mime_hash, "png", "image/png");
+ hash_add(mime_hash, "jng", "image/jng");
+ =
+ return;
+ }
+ =
+ while (!feof(fh)) {
+ char line[256], *ptr, *type, *ext;
+ fgets(line, 256, fh);
+ if (!feof(fh) && line[0] !=3D '#') {
+ ptr =3D line;
+ =
+ /* search for the first non-whitespace character */
+ while (isspace(*ptr))
+ ptr++;
+ =
+ /* is this line empty other than leading whitespace? */
+ if (*ptr =3D=3D '\n' || *ptr =3D=3D '\0')
+ continue;
+ =
+ type =3D ptr;
+ =
+ /* search for the first non-whitespace char or NUL or
+ * NL */ =
+ while (*ptr && (!isspace(*ptr)) && *ptr !=3D '\n')
+ ptr++;
+ =
+ if (*ptr =3D=3D '\0' || *ptr =3D=3D '\n') {
+ /* this mimetype has no extensions - read next
+ * line.
+ */
+ continue;
+ }
+ =
+ *ptr++ =3D '\0';
+ =
+ /* search for the first non-whitespace character which
+ * will be the first filename extenion */
+ while (isspace(*ptr))
+ ptr++;
+ =
+ while(true) {
+ ext =3D ptr;
+ =
+ /* search for the first whitespace char or
+ * NUL or NL which is the end of the ext.
+ */ =
+ while (*ptr && (!isspace(*ptr)) &&
+ *ptr !=3D '\n')
+ ptr++;
+ =
+ if (*ptr =3D=3D '\0' || *ptr =3D=3D '\n') {
+ /* special case for last extension on
+ * the line
+ */
+ *ptr =3D '\0';
+ hash_add(mime_hash, ext, type);
+ break;
+ }
+ =
+ *ptr++ =3D '\0';
+ hash_add(mime_hash, ext, type);
+ =
+ /* search for the first non-whitespace char or
+ * NUL or NL, to find start of next ext.
+ */ =
+ while (*ptr && (isspace(*ptr)) && *ptr !=3D '\n')
+ ptr++; =
+ }
+ }
+ }
+ =
+ fclose(fh);
+}
+
+void gtk_fetch_filetype_fin(void)
+{
+ hash_destroy(mime_hash);
+}
+
+const char *fetch_filetype(const char *unix_path)
+{
+ struct stat statbuf;
+ char *ext;
+ const char *ptr;
+ char *lowerchar;
+ const char *type;
+ =
+ stat(unix_path, &statbuf);
+ if (S_ISDIR(statbuf.st_mode))
+ return "application/x-netsurf-directory";
+ =
+ if (strchr(unix_path, '.') =3D=3D NULL) {
+ /* no extension anywhere! */
+ return "text/plain";
+ }
+ =
+ ptr =3D unix_path + strlen(unix_path);
+ while (*ptr !=3D '.' && *ptr !=3D '/')
+ ptr--;
+ =
+ if (*ptr !=3D '.')
+ return "text/plain";
+ =
+ ext =3D strdup(ptr + 1); /* skip the . */
+ =
+ /* the hash table only contains lower-case versions - make sure this
+ * copy is lower case too.
+ */
+ lowerchar =3D ext;
+ while(*lowerchar) {
+ *lowerchar =3D tolower(*lowerchar);
+ lowerchar++;
+ }
+ =
+ type =3D hash_get(mime_hash, ext);
+ free(ext);
+ =
+ return type !=3D NULL ? type : "text/plain"; =
+}
+
+char *fetch_mimetype(const char *unix_path)
+{
+ return strdup(fetch_filetype(unix_path));
+}
+
+#ifdef TEST_RIG
+
+int main(int argc, char *argv[])
+{
+ unsigned int c1, *c2;
+ const char *key;
+ =
+ gtk_fetch_filetype_init("./mime.types");
+ =
+ c1 =3D 0; c2 =3D 0;
+ =
+ while ( (key =3D hash_iterate(mime_hash, &c1, &c2)) !=3D NULL) {
+ printf("%s ", key);
+ }
+ =
+ printf("\n");
+ =
+ if (argc > 1) {
+ printf("%s maps to %s\n", argv[1], fetch_filetype(argv[1]));
+ }
+ =
+ gtk_fetch_filetype_fin();
+}
+
+#endif
+
Added: trunk/netsurf/gtk/gtk_filetype.h
URL:
http://svn.semichrome.net/trunk/netsurf/gtk/gtk_filetype.h?rev=3D3305&=
view=3Dauto
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D
--- trunk/netsurf/gtk/gtk_filetype.h (added)
+++ trunk/netsurf/gtk/gtk_filetype.h Wed May 30 00:53:35 2007
@@ -1,0 +1,12 @@
+/*
+ * This file is part of NetSurf,
http://netsurf-browser.org/
+ * Licensed under the GNU General Public License,
+ *
http://www.opensource.org/licenses/gpl-license
+ * Copyright 2007 Rob Kendrick <rjek(a)netsurf-browser.org>
+ * Copyright 2007 Vincent Sanders <vince(a)debian.org>
+ */
+
+void gtk_fetch_filetype_init(const char *mimefile);
+void gtk_fetch_filetype_fin(void);
+
+
Modified: trunk/netsurf/gtk/gtk_gui.c
URL:
http://svn.semichrome.net/trunk/netsurf/gtk/gtk_gui.c?rev=3D3305&r1=3D=
3304&r2=3D3305&view=3Ddiff
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D
--- trunk/netsurf/gtk/gtk_gui.c (original)
+++ trunk/netsurf/gtk/gtk_gui.c Wed May 30 00:53:35 2007
@@ -35,6 +35,7 @@
#include "netsurf/gtk/options.h"
#include "netsurf/gtk/gtk_throbber.h"
#include "netsurf/gtk/gtk_history.h"
+#include "netsurf/gtk/gtk_filetype.h"
#include "netsurf/render/box.h"
#include "netsurf/render/form.h"
#include "netsurf/render/html.h"
@@ -211,6 +212,9 @@
find_resource(buf, "messages", "./gtk/res/messages");
LOG(("Using '%s' as Messages file", buf));
messages_load(buf);
+
+ find_resource(buf, "mime.types", "/etc/mime.types");
+ gtk_fetch_filetype_init(buf);
=
/* set up stylesheet urls */
find_resource(buf, "default.css", "./gtk/res/default.css");
@@ -321,6 +325,7 @@
free(adblock_stylesheet_url);
free(option_cookie_file);
free(option_cookie_jar);
+ gtk_fetch_filetype_fin();
}
=
=
Modified: trunk/netsurf/makefile
URL:
http://svn.semichrome.net/trunk/netsurf/makefile?rev=3D3305&r1=3D3304...
r2=3D3305&view=3Ddiff
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D
--- trunk/netsurf/makefile (original)
+++ trunk/netsurf/makefile Wed May 30 00:53:35 2007
@@ -65,14 +65,13 @@
sprite.o # riscos/
=
OBJECTS_GTK =3D $(OBJECTS_COMMON) $(OBJECTS_IMAGE)
-OBJECTS_GTK +=3D filetyped.o # debug/
OBJECTS_GTK +=3D browser.o frames.o history_core.o netsurf.o \
selection.o textinput.o gesture_core.o # desktop/
OBJECTS_GTK +=3D font_pango.o gtk_bitmap.o gtk_gui.o \
gtk_schedule.o gtk_thumbnail.o gtk_options.o \
gtk_plotters.o gtk_treeview.o gtk_scaffolding.o \
gtk_completion.o gtk_login.o gtk_throbber.o \
- gtk_history.o gtk_window.o # gtk/
+ gtk_history.o gtk_window.o gtk_filetype.o # gtk/
=
=
OBJDIR_RISCOS =3D arm-riscos-aof
@@ -132,7 +131,7 @@
-D_BSD_SOURCE \
-DGTK_DISABLE_DEPRECATED \
-D_POSIX_C_SOURCE \
- $(WARNFLAGS) -I.. -g -O0 -Wformat=3D2 \
+ $(WARNFLAGS) -I.. -g -O2 -Wformat=3D2 \
`pkg-config --cflags libglade-2.0 gtk+-2.0` `xml2-config --cflags`
=
# Stop GCC under Cygwin throwing a fit