small fixes to MIME mailcap test command handling, more MIME tests in the sample

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/branches/WX_2_2_BRANCH@7540 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2000-06-07 12:54:32 +00:00
parent fd094b9d81
commit f880e232cc
3 changed files with 214 additions and 42 deletions

View File

@@ -37,16 +37,17 @@
//#define TEST_ARRAYS //#define TEST_ARRAYS
//#define TEST_CMDLINE //#define TEST_CMDLINE
#define TEST_DATETIME //#define TEST_DATETIME
//#define TEST_DIR //#define TEST_DIR
//#define TEST_DLLLOADER //#define TEST_DLLLOADER
//#define TEST_EXECUTE //#define TEST_EXECUTE
//#define TEST_FILE //#define TEST_FILE
//#define TEST_FILECONF //#define TEST_FILECONF
//#define TEST_HASH //#define TEST_HASH
//#define TEST_LIST
//#define TEST_LOG //#define TEST_LOG
//#define TEST_LONGLONG //#define TEST_LONGLONG
//#define TEST_MIME #define TEST_MIME
//#define TEST_INFO_FUNCTIONS //#define TEST_INFO_FUNCTIONS
//#define TEST_SOCKETS //#define TEST_SOCKETS
//#define TEST_STRINGS //#define TEST_STRINGS
@@ -55,6 +56,32 @@
//#define TEST_VCARD //#define TEST_VCARD
//#define TEST_WCHAR //#define TEST_WCHAR
// ----------------------------------------------------------------------------
// test class for container objects
// ----------------------------------------------------------------------------
#if defined(TEST_ARRAYS) || defined(TEST_LIST)
class Bar // Foo is already taken in the hash test
{
public:
Bar(const wxString& name) : m_name(name) { ms_bars++; }
~Bar() { ms_bars--; }
static size_t GetNumber() { return ms_bars; }
const char *GetName() const { return m_name; }
private:
wxString m_name;
static size_t ms_bars;
};
size_t Bar::ms_bars = 0;
#endif // defined(TEST_ARRAYS) || defined(TEST_LIST)
// ============================================================================ // ============================================================================
// implementation // implementation
// ============================================================================ // ============================================================================
@@ -533,6 +560,44 @@ static void TestHash()
#endif // TEST_HASH #endif // TEST_HASH
// ----------------------------------------------------------------------------
// wxList
// ----------------------------------------------------------------------------
#ifdef TEST_LIST
#include <wx/list.h>
WX_DECLARE_LIST(Bar, wxListBars);
#include <wx/listimpl.cpp>
WX_DEFINE_LIST(wxListBars);
static void TestListCtor()
{
puts("*** Testing wxList construction ***\n");
{
wxListBars list1;
list1.Append(new Bar(_T("first")));
list1.Append(new Bar(_T("second")));
printf("After 1st list creation: %u objects in the list, %u objects total.\n",
list1.GetCount(), Bar::GetNumber());
wxListBars list2;
list2 = list1;
printf("After 2nd list creation: %u and %u objects in the lists, %u objects total.\n",
list1.GetCount(), list2.GetCount(), Bar::GetNumber());
list1.DeleteContents(TRUE);
}
printf("After list destruction: %u objects left.\n", Bar::GetNumber());
}
#endif // TEST_LIST
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// MIME types // MIME types
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@@ -541,12 +606,13 @@ static void TestHash()
#include <wx/mimetype.h> #include <wx/mimetype.h>
static wxMimeTypesManager g_mimeManager;
static void TestMimeEnum() static void TestMimeEnum()
{ {
wxMimeTypesManager mimeTM;
wxArrayString mimetypes; wxArrayString mimetypes;
size_t count = mimeTM.EnumAllFileTypes(mimetypes); size_t count = g_mimeManager.EnumAllFileTypes(mimetypes);
printf("*** All %u known filetypes: ***\n", count); printf("*** All %u known filetypes: ***\n", count);
@@ -555,7 +621,7 @@ static void TestMimeEnum()
for ( size_t n = 0; n < count; n++ ) for ( size_t n = 0; n < count; n++ )
{ {
wxFileType *filetype = mimeTM.GetFileTypeFromMimeType(mimetypes[n]); wxFileType *filetype = g_mimeManager.GetFileTypeFromMimeType(mimetypes[n]);
if ( !filetype ) if ( !filetype )
{ {
printf("nothing known about the filetype '%s'!\n", printf("nothing known about the filetype '%s'!\n",
@@ -581,6 +647,60 @@ static void TestMimeEnum()
} }
} }
static void TestMimeOverride()
{
wxPuts(_T("*** Testing wxMimeTypesManager additional files loading ***\n"));
wxString mailcap = _T("/tmp/mailcap"),
mimetypes = _T("/tmp/mime.types");
wxPrintf(_T("Loading mailcap from '%s': %s\n"),
mailcap.c_str(),
g_mimeManager.ReadMailcap(mailcap) ? _T("ok") : _T("ERROR"));
wxPrintf(_T("Loading mime.types from '%s': %s\n"),
mimetypes.c_str(),
g_mimeManager.ReadMimeTypes(mimetypes) ? _T("ok") : _T("ERROR"));
}
static void TestMimeFilename()
{
wxPuts(_T("*** Testing MIME type from filename query ***\n"));
static const wxChar *filenames[] =
{
_T("readme.txt"),
_T("document.pdf"),
_T("image.gif"),
};
for ( size_t n = 0; n < WXSIZEOF(filenames); n++ )
{
const wxString fname = filenames[n];
wxString ext = fname.AfterLast(_T('.'));
wxFileType *ft = g_mimeManager.GetFileTypeFromExtension(ext);
if ( !ft )
{
wxPrintf(_T("WARNING: extension '%s' is unknown.\n"), ext.c_str());
}
else
{
wxString desc;
if ( !ft->GetDescription(&desc) )
desc = _T("<no description>");
wxString cmd;
if ( !ft->GetOpenCommand(&cmd,
wxFileType::MessageParameters(fname, _T(""))) )
cmd = _T("<no command available>");
wxPrintf(_T("To open %s (%s) do '%s'.\n"),
fname.c_str(), desc.c_str(), cmd.c_str());
delete ft;
}
}
}
#endif // TEST_MIME #endif // TEST_MIME
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@@ -2690,22 +2810,6 @@ static int StringLenCompare(const wxString& first, const wxString& second)
#include "wx/dynarray.h" #include "wx/dynarray.h"
class Bar // Foo is already taken in the hash test
{
public:
Bar(const wxString& name) : m_name(name) { ms_bars++; }
~Bar() { ms_bars--; }
static size_t GetNumber() { return ms_bars; }
private:
wxString m_name;
static size_t ms_bars;
};
size_t Bar::ms_bars = 0;
WX_DECLARE_OBJARRAY(Bar, ArrayBars); WX_DECLARE_OBJARRAY(Bar, ArrayBars);
#include "wx/arrimpl.cpp" #include "wx/arrimpl.cpp"
WX_DEFINE_OBJARRAY(ArrayBars); WX_DEFINE_OBJARRAY(ArrayBars);
@@ -3220,6 +3324,10 @@ int main(int argc, char **argv)
TestFileConfRead(); TestFileConfRead();
#endif // TEST_FILECONF #endif // TEST_FILECONF
#ifdef TEST_LIST
TestListCtor();
#endif // TEST_LIST
#ifdef TEST_LOG #ifdef TEST_LOG
wxString s; wxString s;
for ( size_t n = 0; n < 8000; n++ ) for ( size_t n = 0; n < 8000; n++ )
@@ -3287,7 +3395,11 @@ int main(int argc, char **argv)
#endif // TEST_HASH #endif // TEST_HASH
#ifdef TEST_MIME #ifdef TEST_MIME
TestMimeEnum(); wxLog::AddTraceMask(_T("mime"));
if ( 0 )
TestMimeEnum();
TestMimeOverride();
TestMimeFilename();
#endif // TEST_MIME #endif // TEST_MIME
#ifdef TEST_INFO_FUNCTIONS #ifdef TEST_INFO_FUNCTIONS

View File

@@ -189,8 +189,16 @@ wxString wxFileType::ExpandCommand(const wxString& command,
} }
// metamail(1) man page states that if the mailcap entry doesn't have '%s' // metamail(1) man page states that if the mailcap entry doesn't have '%s'
// the program will accept the data on stdin: so give it to it! // the program will accept the data on stdin so normally we should append
if ( !hasFilename && !str.IsEmpty() ) { // "< %s" to the end of the command in such case, but not all commands
// behave like this, in particular a common test is 'test -n "$DISPLAY"'
// and appending "< %s" to this command makes the test fail... I don't
// know of the correct solution, try to guess what we have to do.
if ( !hasFilename && !str.IsEmpty()
#ifdef __UNIX__
&& !str.StartsWith(_T("test "))
#endif // Unix
) {
str << wxT(" < '") << params.GetFileName() << wxT('\''); str << wxT(" < '") << params.GetFileName() << wxT('\'');
} }

View File

@@ -9,8 +9,16 @@
// Licence: wxWindows license (part of wxExtra library) // Licence: wxWindows license (part of wxExtra library)
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////
#ifdef __GNUG__ // ============================================================================
#pragma implementation "mimetype.h" // declarations
// ============================================================================
// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------
#ifdef __GNUG__
#pragma implementation "mimetype.h"
#endif #endif
// for compilers that support precompilation, includes "wx.h". // for compilers that support precompilation, includes "wx.h".
@@ -24,7 +32,7 @@
#include "wx/defs.h" #include "wx/defs.h"
#endif #endif
#if (wxUSE_FILE && wxUSE_TEXTFILE) || defined(__WXMSW__) #if wxUSE_FILE && wxUSE_TEXTFILE
#ifndef WX_PRECOMP #ifndef WX_PRECOMP
#include "wx/string.h" #include "wx/string.h"
@@ -54,6 +62,22 @@
// in case we're compiling in non-GUI mode // in case we're compiling in non-GUI mode
class WXDLLEXPORT wxIcon; class WXDLLEXPORT wxIcon;
// ----------------------------------------------------------------------------
// constants
// ----------------------------------------------------------------------------
// MIME code tracing mask
#define TRACE_MIME _T("mime")
// ----------------------------------------------------------------------------
// private functions
// ----------------------------------------------------------------------------
// there are some fields which we don't understand but for which we don't give
// warnings as we know that they're not important - this function is used to
// test for them
static bool IsKnownUnimportantField(const wxString& field);
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// private classes // private classes
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@@ -876,15 +900,21 @@ wxFileTypeImpl::GetEntry(const wxFileType::MessageParameters& params) const
// notice that an empty command would always succeed (it's ok) // notice that an empty command would always succeed (it's ok)
command = wxFileType::ExpandCommand(entry->GetTestCmd(), params); command = wxFileType::ExpandCommand(entry->GetTestCmd(), params);
if ( command.IsEmpty() || (wxSystem(command) == 0) ) { // suppress the command output
// ok, passed if ( !command.IsEmpty() )
wxLogTrace(wxT("Test '%s' for mime type '%s' succeeded."), {
command.c_str(), params.GetMimeType().c_str()); if ( wxSystem(command) == 0 ) {
break; // ok, passed
} wxLogTrace(TRACE_MIME,
else { wxT("Test '%s' for mime type '%s' succeeded."),
wxLogTrace(wxT("Test '%s' for mime type '%s' failed."), command.c_str(), params.GetMimeType().c_str());
command.c_str(), params.GetMimeType().c_str()); break;
}
else {
wxLogTrace(TRACE_MIME,
wxT("Test '%s' for mime type '%s' failed."),
command.c_str(), params.GetMimeType().c_str());
}
} }
entry = entry->GetNext(); entry = entry->GetNext();
@@ -1189,7 +1219,8 @@ void wxMimeTypesManagerImpl::AddMailcapInfo(const wxString& strType,
bool wxMimeTypesManagerImpl::ReadMimeTypes(const wxString& strFileName) bool wxMimeTypesManagerImpl::ReadMimeTypes(const wxString& strFileName)
{ {
wxLogTrace(wxT("--- Parsing mime.types file '%s' ---"), strFileName.c_str()); wxLogTrace(TRACE_MIME, wxT("--- Parsing mime.types file '%s' ---"),
strFileName.c_str());
wxTextFile file(strFileName); wxTextFile file(strFileName);
if ( !file.Open() ) if ( !file.Open() )
@@ -1345,7 +1376,8 @@ bool wxMimeTypesManagerImpl::ReadMimeTypes(const wxString& strFileName)
bool wxMimeTypesManagerImpl::ReadMailcap(const wxString& strFileName, bool wxMimeTypesManagerImpl::ReadMailcap(const wxString& strFileName,
bool fallback) bool fallback)
{ {
wxLogTrace(wxT("--- Parsing mailcap file '%s' ---"), strFileName.c_str()); wxLogTrace(TRACE_MIME, wxT("--- Parsing mailcap file '%s' ---"),
strFileName.c_str());
wxTextFile file(strFileName); wxTextFile file(strFileName);
if ( !file.Open() ) if ( !file.Open() )
@@ -1495,10 +1527,7 @@ bool wxMimeTypesManagerImpl::ReadMailcap(const wxString& strFileName,
if ( !ok ) if ( !ok )
{ {
// we don't understand this field, but if ( !IsKnownUnimportantField(curField) )
// Netscape stores info in it, so don't warn
// about it
if ( curField.Left(16u) != "x-mozilla-flags=" )
{ {
// don't flood the user with error // don't flood the user with error
// messages if we don't understand // messages if we don't understand
@@ -1638,6 +1667,29 @@ size_t wxMimeTypesManagerImpl::EnumAllFileTypes(wxArrayString& mimetypes)
return mimetypes.GetCount(); return mimetypes.GetCount();
} }
// ----------------------------------------------------------------------------
// private functions
// ----------------------------------------------------------------------------
static bool IsKnownUnimportantField(const wxString& fieldAll)
{
static const wxChar *knownFields[] =
{
_T("x-mozilla-flags"),
_T("nametemplate"),
_T("textualnewlines"),
};
wxString field = fieldAll.BeforeFirst(_T('='));
for ( size_t n = 0; n < WXSIZEOF(knownFields); n++ )
{
if ( field.CmpNoCase(knownFields[n]) == 0 )
return TRUE;
}
return FALSE;
}
#endif #endif
// wxUSE_FILE && wxUSE_TEXTFILE // wxUSE_FILE && wxUSE_TEXTFILE