From e4ffdb7a9487618b72ffed0ddb3f046fd9288754 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Axel=20D=C3=B6rfler?= Date: Sat, 1 Mar 2008 11:56:30 +0000 Subject: [PATCH] * Added a more generic subtype for the source code files for automatic MIME type guessing. * Now, if the specific MIME type exists, it will be chosen, if it doesn't, the generic MIME type is chosen. If that does not exist either, the most generic is chosen (a.k.a. "text/plain"). git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@24187 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- .../translators/stxt/STXTTranslator.cpp | 45 +++++++++++++++---- src/servers/registrar/TextSnifferAddon.cpp | 28 +++++++++--- src/servers/registrar/names.h | 39 ++++++++-------- 3 files changed, 77 insertions(+), 35 deletions(-) diff --git a/src/add-ons/translators/stxt/STXTTranslator.cpp b/src/add-ons/translators/stxt/STXTTranslator.cpp index 597307d02d..a3dd9693b7 100644 --- a/src/add-ons/translators/stxt/STXTTranslator.cpp +++ b/src/add-ons/translators/stxt/STXTTranslator.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2002-2007, Haiku, Inc. All Rights Reserved. + * Copyright 2002-2008, Haiku, Inc. All Rights Reserved. * Distributed under the terms of the MIT License. * * Authors: @@ -143,7 +143,20 @@ make_nth_translator(int32 n, image_id you, uint32 flags, ...) /* * ASCII magic -- file types that we know based on keywords * that can appear anywhere in the file. - * + * bool found = false; + if (subtypeMimeSpecific != NULL) { + mimeType->SetTo(subtypeMimeSpecific); + if (mimeType->IsInstalled()) + found = true; + } + if (!found && subtypeMimeGeneric != NULL) { + mimeType->SetTo(subtypeMimeGeneric); + if (mimeType->IsInstalled()) + found = true; + } + if (!found) + mimeType->SetTo("text/plain"); + * Extensively modified by Eric Fischer in July, 2000, * to handle character codes other than ASCII on a unified basis. * @@ -189,7 +202,8 @@ file_ascmagic(const unsigned char *buf, size_t nbytes, BMimeType* mimeType, encoding = NULL; const char *type = NULL; const char *subtype = NULL; - const char *subtype_mime = NULL; + const char *subtypeMimeGeneric = NULL; + const char *subtypeMimeSpecific = NULL; int has_escapes = 0; int has_backspace = 0; @@ -291,14 +305,16 @@ file_ascmagic(const unsigned char *buf, size_t nbytes, BMimeType* mimeType, isascii((unsigned char)tp[1]) && isalnum((unsigned char)tp[1]) && ISSPC(tp[2]))) { - subtype_mime = "text/troff"; + subtypeMimeGeneric = "text/x-source-code"; + subtypeMimeSpecific = "text/troff"; subtype = "troff or preprocessor input"; goto subtype_identified; } } if ((*buf == 'c' || *buf == 'C') && ISSPC(buf[1])) { - subtype_mime = "text/fortran"; + subtypeMimeGeneric = "text/x-source-code"; + subtypeMimeSpecific = "text/fortran"; subtype = "fortran program"; goto subtype_identified; } @@ -331,7 +347,8 @@ file_ascmagic(const unsigned char *buf, size_t nbytes, BMimeType* mimeType, if (ascmatch((const unsigned char *)p->name, ubuf + i, end - i)) { subtype = types[p->type].human; - subtype_mime = types[p->type].mime; + subtypeMimeGeneric = types[p->type].generic_mime; + subtypeMimeSpecific = types[p->type].specific_mime; goto subtype_identified; } } @@ -383,9 +400,19 @@ done: if (rv) { // If we have identified the subtype, return it, otherwise just // text/plain. - if (subtype_mime) - mimeType->SetTo(subtype_mime); - else + + bool found = false; + if (subtypeMimeSpecific != NULL) { + mimeType->SetTo(subtypeMimeSpecific); + if (mimeType->IsInstalled()) + found = true; + } + if (!found && subtypeMimeGeneric != NULL) { + mimeType->SetTo(subtypeMimeGeneric); + if (mimeType->IsInstalled()) + found = true; + } + if (!found) mimeType->SetTo("text/plain"); } diff --git a/src/servers/registrar/TextSnifferAddon.cpp b/src/servers/registrar/TextSnifferAddon.cpp index c19ffe6a2f..1773eab161 100644 --- a/src/servers/registrar/TextSnifferAddon.cpp +++ b/src/servers/registrar/TextSnifferAddon.cpp @@ -135,7 +135,8 @@ file_ascmagic(const unsigned char *buf, size_t nbytes, BMimeType* mimeType) const char *code_mime = NULL; const char *type = NULL; const char *subtype = NULL; - const char *subtype_mime = NULL; + const char *subtypeMimeGeneric = NULL; + const char *subtypeMimeSpecific = NULL; int has_escapes = 0; int has_backspace = 0; @@ -225,14 +226,16 @@ file_ascmagic(const unsigned char *buf, size_t nbytes, BMimeType* mimeType) isascii((unsigned char)tp[1]) && isalnum((unsigned char)tp[1]) && ISSPC(tp[2]))) { - subtype_mime = "text/troff"; + subtypeMimeGeneric = "text/x-source-code"; + subtypeMimeSpecific = "text/troff"; subtype = "troff or preprocessor input"; goto subtype_identified; } } if ((*buf == 'c' || *buf == 'C') && ISSPC(buf[1])) { - subtype_mime = "text/fortran"; + subtypeMimeGeneric = "text/x-source-code"; + subtypeMimeSpecific = "text/fortran"; subtype = "fortran program"; goto subtype_identified; } @@ -265,7 +268,8 @@ file_ascmagic(const unsigned char *buf, size_t nbytes, BMimeType* mimeType) if (ascmatch((const unsigned char *)p->name, ubuf + i, end - i)) { subtype = types[p->type].human; - subtype_mime = types[p->type].mime; + subtypeMimeGeneric = types[p->type].generic_mime; + subtypeMimeSpecific = types[p->type].specific_mime; goto subtype_identified; } } @@ -317,9 +321,19 @@ done: if (rv) { // If we have identified the subtype, return it, otherwise just // text/plain. - if (subtype_mime) - mimeType->SetTo(subtype_mime); - else + + bool found = false; + if (subtypeMimeSpecific != NULL) { + mimeType->SetTo(subtypeMimeSpecific); + if (mimeType->IsInstalled()) + found = true; + } + if (!found && subtypeMimeGeneric != NULL) { + mimeType->SetTo(subtypeMimeGeneric); + if (mimeType->IsInstalled()) + found = true; + } + if (!found) mimeType->SetTo("text/plain"); } diff --git a/src/servers/registrar/names.h b/src/servers/registrar/names.h index e71f6aae7c..f38f6ef81b 100644 --- a/src/servers/registrar/names.h +++ b/src/servers/registrar/names.h @@ -41,7 +41,7 @@ */ /* these types are used to index the table 'types': keep em in sync! */ -#define L_C 0 /* first and foremost on UNIX */ +#define L_C 0 /* first and foremost on UNIX */ #define L_CC 1 /* Bjarne's postincrement */ #define L_FORT 2 /* the oldest one */ #define L_MAKE 3 /* Makefiles */ @@ -59,25 +59,26 @@ static const struct { const char *human; - const char *mime; + const char *generic_mime; + const char *specific_mime; } types[] = { - { "C program", "text/x-c", }, - { "C++ program", "text/x-c++" }, - { "FORTRAN program", "text/x-fortran" }, - { "make commands", "text/x-makefile" }, - { "PL/1 program", "text/x-pl1" }, - { "assembler program", "text/x-asm" }, - { "English", "text/plain" }, - { "Pascal program", "text/x-pascal" }, - { "mail", "text/x-mail" }, - { "news", "text/x-news" }, - { "Java program", "text/x-java" }, - { "HTML document", "text/html", }, - { "BCPL program", "text/x-bcpl" }, - { "M4 macro language pre-processor", "text/x-m4" }, - { "PO (gettext message catalogue)", "text/x-po" }, - { "cannot happen error on names.h/types", "error/x-error" }, - { 0, 0} + { "C program", "text/x-source-code", "text/x-c", }, + { "C++ program", "text/x-source-code", "text/x-c++" }, + { "FORTRAN program", "text/x-source-code", "text/x-fortran" }, + { "make commands", "text/x-source-code", "text/x-makefile" }, + { "PL/1 program", "text/x-source-code", "text/x-pl1" }, + { "assembler program", "text/x-source-code", "text/x-asm" }, + { "English", "text/plain", NULL }, + { "Pascal program", "text/x-source-code", "text/x-pascal" }, + { "mail", "text/plain", "text/x-mail" }, + { "news", "text/plain", "text/x-news" }, + { "Java program", "text/x-source-code", "text/x-java" }, + { "HTML document", "text/x-source-code", "text/html", }, + { "BCPL program", "text/x-source-code", "text/x-bcpl" }, + { "M4 macro language pre-processor", "text/x-source-code", "text/x-m4" }, + { "PO (gettext message catalogue)", "text/plain", "text/x-po" }, + { "cannot happen error on names.h/types", "text/plain", "error/x-error" }, + { NULL, NULL, NULL } }; /*