* 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
This commit is contained in:
@@ -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.
|
* Distributed under the terms of the MIT License.
|
||||||
*
|
*
|
||||||
* Authors:
|
* 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
|
* ASCII magic -- file types that we know based on keywords
|
||||||
* that can appear anywhere in the file.
|
* 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 <[email protected]> in July, 2000,
|
* Extensively modified by Eric Fischer <[email protected]> in July, 2000,
|
||||||
* to handle character codes other than ASCII on a unified basis.
|
* 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;
|
encoding = NULL;
|
||||||
const char *type = NULL;
|
const char *type = NULL;
|
||||||
const char *subtype = NULL;
|
const char *subtype = NULL;
|
||||||
const char *subtype_mime = NULL;
|
const char *subtypeMimeGeneric = NULL;
|
||||||
|
const char *subtypeMimeSpecific = NULL;
|
||||||
|
|
||||||
int has_escapes = 0;
|
int has_escapes = 0;
|
||||||
int has_backspace = 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]) &&
|
isascii((unsigned char)tp[1]) &&
|
||||||
isalnum((unsigned char)tp[1]) &&
|
isalnum((unsigned char)tp[1]) &&
|
||||||
ISSPC(tp[2]))) {
|
ISSPC(tp[2]))) {
|
||||||
subtype_mime = "text/troff";
|
subtypeMimeGeneric = "text/x-source-code";
|
||||||
|
subtypeMimeSpecific = "text/troff";
|
||||||
subtype = "troff or preprocessor input";
|
subtype = "troff or preprocessor input";
|
||||||
goto subtype_identified;
|
goto subtype_identified;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((*buf == 'c' || *buf == 'C') && ISSPC(buf[1])) {
|
if ((*buf == 'c' || *buf == 'C') && ISSPC(buf[1])) {
|
||||||
subtype_mime = "text/fortran";
|
subtypeMimeGeneric = "text/x-source-code";
|
||||||
|
subtypeMimeSpecific = "text/fortran";
|
||||||
subtype = "fortran program";
|
subtype = "fortran program";
|
||||||
goto subtype_identified;
|
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,
|
if (ascmatch((const unsigned char *)p->name, ubuf + i,
|
||||||
end - i)) {
|
end - i)) {
|
||||||
subtype = types[p->type].human;
|
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;
|
goto subtype_identified;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -383,9 +400,19 @@ done:
|
|||||||
if (rv) {
|
if (rv) {
|
||||||
// If we have identified the subtype, return it, otherwise just
|
// If we have identified the subtype, return it, otherwise just
|
||||||
// text/plain.
|
// text/plain.
|
||||||
if (subtype_mime)
|
|
||||||
mimeType->SetTo(subtype_mime);
|
bool found = false;
|
||||||
else
|
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");
|
mimeType->SetTo("text/plain");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -135,7 +135,8 @@ file_ascmagic(const unsigned char *buf, size_t nbytes, BMimeType* mimeType)
|
|||||||
const char *code_mime = NULL;
|
const char *code_mime = NULL;
|
||||||
const char *type = NULL;
|
const char *type = NULL;
|
||||||
const char *subtype = NULL;
|
const char *subtype = NULL;
|
||||||
const char *subtype_mime = NULL;
|
const char *subtypeMimeGeneric = NULL;
|
||||||
|
const char *subtypeMimeSpecific = NULL;
|
||||||
|
|
||||||
int has_escapes = 0;
|
int has_escapes = 0;
|
||||||
int has_backspace = 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]) &&
|
isascii((unsigned char)tp[1]) &&
|
||||||
isalnum((unsigned char)tp[1]) &&
|
isalnum((unsigned char)tp[1]) &&
|
||||||
ISSPC(tp[2]))) {
|
ISSPC(tp[2]))) {
|
||||||
subtype_mime = "text/troff";
|
subtypeMimeGeneric = "text/x-source-code";
|
||||||
|
subtypeMimeSpecific = "text/troff";
|
||||||
subtype = "troff or preprocessor input";
|
subtype = "troff or preprocessor input";
|
||||||
goto subtype_identified;
|
goto subtype_identified;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((*buf == 'c' || *buf == 'C') && ISSPC(buf[1])) {
|
if ((*buf == 'c' || *buf == 'C') && ISSPC(buf[1])) {
|
||||||
subtype_mime = "text/fortran";
|
subtypeMimeGeneric = "text/x-source-code";
|
||||||
|
subtypeMimeSpecific = "text/fortran";
|
||||||
subtype = "fortran program";
|
subtype = "fortran program";
|
||||||
goto subtype_identified;
|
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,
|
if (ascmatch((const unsigned char *)p->name, ubuf + i,
|
||||||
end - i)) {
|
end - i)) {
|
||||||
subtype = types[p->type].human;
|
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;
|
goto subtype_identified;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -317,9 +321,19 @@ done:
|
|||||||
if (rv) {
|
if (rv) {
|
||||||
// If we have identified the subtype, return it, otherwise just
|
// If we have identified the subtype, return it, otherwise just
|
||||||
// text/plain.
|
// text/plain.
|
||||||
if (subtype_mime)
|
|
||||||
mimeType->SetTo(subtype_mime);
|
bool found = false;
|
||||||
else
|
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");
|
mimeType->SetTo("text/plain");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -59,25 +59,26 @@
|
|||||||
|
|
||||||
static const struct {
|
static const struct {
|
||||||
const char *human;
|
const char *human;
|
||||||
const char *mime;
|
const char *generic_mime;
|
||||||
|
const char *specific_mime;
|
||||||
} types[] = {
|
} types[] = {
|
||||||
{ "C program", "text/x-c", },
|
{ "C program", "text/x-source-code", "text/x-c", },
|
||||||
{ "C++ program", "text/x-c++" },
|
{ "C++ program", "text/x-source-code", "text/x-c++" },
|
||||||
{ "FORTRAN program", "text/x-fortran" },
|
{ "FORTRAN program", "text/x-source-code", "text/x-fortran" },
|
||||||
{ "make commands", "text/x-makefile" },
|
{ "make commands", "text/x-source-code", "text/x-makefile" },
|
||||||
{ "PL/1 program", "text/x-pl1" },
|
{ "PL/1 program", "text/x-source-code", "text/x-pl1" },
|
||||||
{ "assembler program", "text/x-asm" },
|
{ "assembler program", "text/x-source-code", "text/x-asm" },
|
||||||
{ "English", "text/plain" },
|
{ "English", "text/plain", NULL },
|
||||||
{ "Pascal program", "text/x-pascal" },
|
{ "Pascal program", "text/x-source-code", "text/x-pascal" },
|
||||||
{ "mail", "text/x-mail" },
|
{ "mail", "text/plain", "text/x-mail" },
|
||||||
{ "news", "text/x-news" },
|
{ "news", "text/plain", "text/x-news" },
|
||||||
{ "Java program", "text/x-java" },
|
{ "Java program", "text/x-source-code", "text/x-java" },
|
||||||
{ "HTML document", "text/html", },
|
{ "HTML document", "text/x-source-code", "text/html", },
|
||||||
{ "BCPL program", "text/x-bcpl" },
|
{ "BCPL program", "text/x-source-code", "text/x-bcpl" },
|
||||||
{ "M4 macro language pre-processor", "text/x-m4" },
|
{ "M4 macro language pre-processor", "text/x-source-code", "text/x-m4" },
|
||||||
{ "PO (gettext message catalogue)", "text/x-po" },
|
{ "PO (gettext message catalogue)", "text/plain", "text/x-po" },
|
||||||
{ "cannot happen error on names.h/types", "error/x-error" },
|
{ "cannot happen error on names.h/types", "text/plain", "error/x-error" },
|
||||||
{ 0, 0}
|
{ NULL, NULL, NULL }
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|||||||
Reference in New Issue
Block a user