+// ----------------------------------------------------------------------------
+// wxFileName
+// ----------------------------------------------------------------------------
+
+#ifdef TEST_FILENAME
+
+#include "wx/filename.h"
+
+static void DumpFileName(const wxFileName& fn)
+{
+ wxString full = fn.GetFullPath();
+
+ wxString vol, path, name, ext;
+ wxFileName::SplitPath(full, &vol, &path, &name, &ext);
+
+ wxPrintf(_T("Filename '%s' -> vol '%s', path '%s', name '%s', ext '%s'\n"),
+ full.c_str(), vol.c_str(), path.c_str(), name.c_str(), ext.c_str());
+}
+
+static struct FileNameInfo
+{
+ const wxChar *fullname;
+ const wxChar *volume;
+ const wxChar *path;
+ const wxChar *name;
+ const wxChar *ext;
+ bool isAbsolute;
+ wxPathFormat format;
+} filenames[] =
+{
+ // Unix file names
+ { _T("/usr/bin/ls"), _T(""), _T("/usr/bin"), _T("ls"), _T(""), TRUE, wxPATH_UNIX },
+ { _T("/usr/bin/"), _T(""), _T("/usr/bin"), _T(""), _T(""), TRUE, wxPATH_UNIX },
+ { _T("~/.zshrc"), _T(""), _T("~"), _T(".zshrc"), _T(""), TRUE, wxPATH_UNIX },
+ { _T("../../foo"), _T(""), _T("../.."), _T("foo"), _T(""), FALSE, wxPATH_UNIX },
+ { _T("foo.bar"), _T(""), _T(""), _T("foo"), _T("bar"), FALSE, wxPATH_UNIX },
+ { _T("~/foo.bar"), _T(""), _T("~"), _T("foo"), _T("bar"), TRUE, wxPATH_UNIX },
+ { _T("/foo"), _T(""), _T("/"), _T("foo"), _T(""), TRUE, wxPATH_UNIX },
+ { _T("Mahogany-0.60/foo.bar"), _T(""), _T("Mahogany-0.60"), _T("foo"), _T("bar"), FALSE, wxPATH_UNIX },
+ { _T("/tmp/wxwin.tar.bz"), _T(""), _T("/tmp"), _T("wxwin.tar"), _T("bz"), TRUE, wxPATH_UNIX },
+
+ // Windows file names
+ { _T("foo.bar"), _T(""), _T(""), _T("foo"), _T("bar"), FALSE, wxPATH_DOS },
+ { _T("\\foo.bar"), _T(""), _T("\\"), _T("foo"), _T("bar"), FALSE, wxPATH_DOS },
+ { _T("c:foo.bar"), _T("c"), _T(""), _T("foo"), _T("bar"), FALSE, wxPATH_DOS },
+ { _T("c:\\foo.bar"), _T("c"), _T("\\"), _T("foo"), _T("bar"), TRUE, wxPATH_DOS },
+ { _T("c:\\Windows\\command.com"), _T("c"), _T("\\Windows"), _T("command"), _T("com"), TRUE, wxPATH_DOS },
+ { _T("\\\\server\\foo.bar"), _T("server"), _T("\\"), _T("foo"), _T("bar"), TRUE, wxPATH_DOS },
+
+ // Mac file names
+ { _T("Volume:Dir:File"), _T("Volume"), _T("Dir"), _T("File"), _T(""), TRUE, wxPATH_MAC },
+ { _T("Volume:Dir:Subdir:File"), _T("Volume"), _T("Dir:Subdir"), _T("File"), _T(""), TRUE, wxPATH_MAC },
+ { _T("Volume:"), _T("Volume"), _T(""), _T(""), _T(""), TRUE, wxPATH_MAC },
+ { _T(":Dir:File"), _T(""), _T("Dir"), _T("File"), _T(""), FALSE, wxPATH_MAC },
+ { _T(":File.Ext"), _T(""), _T(""), _T("File"), _T(".Ext"), FALSE, wxPATH_MAC },
+ { _T("File.Ext"), _T(""), _T(""), _T("File"), _T(".Ext"), FALSE, wxPATH_MAC },
+
+ // VMS file names
+ { _T("device:[dir1.dir2.dir3]file.txt"), _T("device"), _T("dir1.dir2.dir3"), _T("file"), _T("txt"), TRUE, wxPATH_VMS },
+ { _T("file.txt"), _T(""), _T(""), _T("file"), _T("txt"), FALSE, wxPATH_VMS },
+};
+
+static void TestFileNameConstruction()
+{
+ puts("*** testing wxFileName construction ***");
+
+ for ( size_t n = 0; n < WXSIZEOF(filenames); n++ )
+ {
+ const FileNameInfo& fni = filenames[n];
+
+ wxFileName fn(fni.fullname, fni.format);
+
+ wxString fullname = fn.GetFullPath(fni.format);
+ if ( fullname != fni.fullname )
+ {
+ printf("ERROR: fullname should be '%s'\n", fni.fullname);
+ }
+
+ bool isAbsolute = fn.IsAbsolute();
+ printf("'%s' is %s (%s)\n\t",
+ fullname.c_str(),
+ isAbsolute ? "absolute" : "relative",
+ isAbsolute == fni.isAbsolute ? "ok" : "ERROR");
+
+ if ( !fn.Normalize(wxPATH_NORM_ALL, _T(""), fni.format) )
+ {
+ puts("ERROR (couldn't be normalized)");
+ }
+ else
+ {
+ printf("normalized: '%s'\n", fn.GetFullPath(fni.format).c_str());
+ }
+ }
+
+ puts("");
+}
+
+static void TestFileNameSplit()
+{
+ puts("*** testing wxFileName splitting ***");
+
+ for ( size_t n = 0; n < WXSIZEOF(filenames); n++ )
+ {
+ const FileNameInfo& fni = filenames[n];
+ wxString volume, path, name, ext;
+ wxFileName::SplitPath(fni.fullname,
+ &volume, &path, &name, &ext, fni.format);
+
+ printf("%s -> volume = '%s', path = '%s', name = '%s', ext = '%s'",
+ fni.fullname,
+ volume.c_str(), path.c_str(), name.c_str(), ext.c_str());
+
+ if ( volume != fni.volume )
+ printf(" (ERROR: volume = '%s')", fni.volume);
+ if ( path != fni.path )
+ printf(" (ERROR: path = '%s')", fni.path);
+ if ( name != fni.name )
+ printf(" (ERROR: name = '%s')", fni.name);
+ if ( ext != fni.ext )
+ printf(" (ERROR: ext = '%s')", fni.ext);
+
+ puts("");
+ }
+}
+
+static void TestFileNameTemp()
+{
+ puts("*** testing wxFileName temp file creation ***");
+
+ static const char *tmpprefixes[] =
+ {
+ "foo",
+ "/tmp/foo",
+ "..",
+ "../bar",
+ "/tmp/foo/bar", // this one must be an error
+ };
+
+ for ( size_t n = 0; n < WXSIZEOF(tmpprefixes); n++ )
+ {
+ wxString path = wxFileName::CreateTempFileName(tmpprefixes[n]);
+ if ( !path.empty() )
+ {
+ printf("Prefix '%s'\t-> temp file '%s'\n",
+ tmpprefixes[n], path.c_str());
+
+ if ( !wxRemoveFile(path) )
+ {
+ wxLogWarning("Failed to remove temp file '%s'", path.c_str());
+ }
+ }
+ }
+}
+
+static void TestFileNameMakeRelative()
+{
+ puts("*** testing wxFileName::MakeRelativeTo() ***");
+
+ for ( size_t n = 0; n < WXSIZEOF(filenames); n++ )
+ {
+ const FileNameInfo& fni = filenames[n];
+
+ wxFileName fn(fni.fullname, fni.format);
+
+ // choose the base dir of the same format
+ wxString base;
+ switch ( fni.format )
+ {
+ case wxPATH_UNIX:
+ base = "/usr/bin/";
+ break;
+
+ case wxPATH_DOS:
+ base = "c:\\";
+ break;
+
+ case wxPATH_MAC:
+ case wxPATH_VMS:
+ // TODO: I don't know how this is supposed to work there
+ continue;
+
+ case wxPATH_NATIVE: // make gcc happy
+ default:
+ wxFAIL_MSG( "unexpected path format" );
+ }
+
+ printf("'%s' relative to '%s': ",
+ fn.GetFullPath(fni.format).c_str(), base.c_str());
+
+ if ( !fn.MakeRelativeTo(base, fni.format) )
+ {
+ puts("unchanged");
+ }
+ else
+ {
+ printf("'%s'\n", fn.GetFullPath(fni.format).c_str());
+ }
+ }
+}
+
+static void TestFileNameComparison()
+{
+ // TODO!
+}
+
+static void TestFileNameOperations()
+{
+ // TODO!
+}
+
+static void TestFileNameCwd()
+{
+ // TODO!
+}
+
+#endif // TEST_FILENAME
+
+// ----------------------------------------------------------------------------
+// wxFileName time functions
+// ----------------------------------------------------------------------------
+
+#ifdef TEST_FILETIME
+
+#include <wx/filename.h>
+#include <wx/datetime.h>
+
+static void TestFileGetTimes()
+{
+ wxFileName fn(_T("testdata.fc"));
+
+ wxDateTime dtAccess, dtMod, dtChange;
+ if ( !fn.GetTimes(&dtAccess, &dtMod, &dtChange) )
+ {
+ wxPrintf(_T("ERROR: GetTimes() failed.\n"));
+ }
+ else
+ {
+ static const wxChar *fmt = _T("%Y-%b-%d %H:%M:%S");
+
+ wxPrintf(_T("File times for '%s':\n"), fn.GetFullPath().c_str());
+ wxPrintf(_T("Access: \t%s\n"), dtAccess.Format(fmt).c_str());
+ wxPrintf(_T("Mod/creation:\t%s\n"), dtMod.Format(fmt).c_str());
+ wxPrintf(_T("Change: \t%s\n"), dtChange.Format(fmt).c_str());
+ }
+}
+
+static void TestFileSetTimes()
+{
+ wxFileName fn(_T("testdata.fc"));
+
+ if ( !fn.Touch() )
+ {
+ wxPrintf(_T("ERROR: Touch() failed.\n"));
+ }
+}
+
+#endif // TEST_FILETIME
+