/*
Here are brief descriptions of the filename formats supported by this class:
- wxPATH_UNIX: standard Unix format, absolute file names have the form
+ wxPATH_UNIX: standard Unix format, used under Darwin as well, absolute file
+ names have the form:
/dir1/dir2/.../dirN/filename, "." and ".." stand for the
current and parent directory respectively, "~" is parsed as the
user HOME and "~username" as the HOME of that user
- wxPATH_DOS: DOS/Windows format, absolute file names have the form
+ wxPATH_DOS: DOS/Windows format, absolute file names have the form:
drive:\dir1\dir2\...\dirN\filename.ext where drive is a single
letter. "." and ".." as for Unix but no "~".
There are also UNC names of the form \\share\fullpath
- wxPATH_MAC: Mac OS 8/9 format, absolute file names have the form
+ wxPATH_MAC: Mac OS 8/9 and Mac OS X under CodeWarrior 7 format, absolute file
+ names have the form
volume:dir1:...:dirN:filename
and the relative file names are either
:dir1:...:dirN:filename
or just
filename
(although :filename works as well).
+ :::filename.ext is not yet supported. TODO.
+ Since the volume is just part of the file path, it is not
+ treated like a separate entity as it is done under DOS.
wxPATH_VMS: VMS native format, absolute file names have the form
<device>:[dir1.dir2.dir3]file.txt
#include <unix.h>
#endif
+#ifdef __WATCOMC__
+ #include <io.h>
+ #include <sys/utime.h>
+ #include <sys/stat.h>
+#endif
+
// ----------------------------------------------------------------------------
// private classes
// ----------------------------------------------------------------------------
Assign(volume, path, name, ext, format);
}
-void wxFileName::Assign(const wxString& fullpath,
+void wxFileName::Assign(const wxString& fullpathOrig,
const wxString& fullname,
wxPathFormat format)
{
+ // always recognize fullpath as directory, even if it doesn't end with a
+ // slash
+ wxString fullpath = fullpathOrig;
+ if ( !wxEndsWithPathSeparator(fullpath) )
+ {
+ fullpath += GetPathSeparators(format)[0u];
+ }
+
wxString volume, path, name, ext;
+
+ // do some consistency checks in debug mode: the name should be really just
+ // the filename and the path should be realyl just a path
+#ifdef __WXDEBUG__
+ wxString pathDummy, nameDummy, extDummy;
+
+ SplitPath(fullname, &pathDummy, &name, &ext, format);
+
+ wxASSERT_MSG( pathDummy.empty(),
+ _T("the file name shouldn't contain the path") );
+
+ SplitPath(fullpath, &volume, &path, &nameDummy, &extDummy, format);
+
+ wxASSERT_MSG( nameDummy.empty() && extDummy.empty(),
+ _T("the path shouldn't contain file name nor extension") );
+
+#else // !__WXDEBUG__
SplitPath(fullname, NULL /* no path */, &name, &ext, format);
SplitPath(fullpath, &volume, &path, NULL, NULL, format);
+#endif // __WXDEBUG__/!__WXDEBUG__
Assign(volume, path, name, ext, format);
}
return ::wxGetHomeDir();
}
-void wxFileName::AssignTempFileName( const wxString &prefix )
+void wxFileName::AssignTempFileName( const wxString& prefix )
{
- wxString fullname;
- if ( wxGetTempFileName(prefix, fullname) )
+ wxString tempname = CreateTempFileName(prefix);
+ if ( tempname.empty() )
{
- Assign(fullname);
+ // error, failed to get temp file name
+ Clear();
}
- else // error
+ else // ok
{
- Clear();
+ Assign(tempname);
+ }
+}
+
+/* static */
+wxString wxFileName::CreateTempFileName(const wxString& prefix)
+{
+ wxString path, dir, name;
+
+ // use the directory specified by the prefix
+ SplitPath(prefix, &dir, &name, NULL /* extension */);
+
+#if defined(__WINDOWS__) && !defined(__WXMICROWIN__)
+
+#ifdef __WIN32__
+ if ( dir.empty() )
+ {
+ if ( !::GetTempPath(MAX_PATH, wxStringBuffer(dir, MAX_PATH + 1)) )
+ {
+ wxLogLastError(_T("GetTempPath"));
+ }
+
+ if ( dir.empty() )
+ {
+ // GetTempFileName() fails if we pass it an empty string
+ dir = _T('.');
+ }
+ }
+
+ if ( !::GetTempFileName(dir, name, 0, wxStringBuffer(path, MAX_PATH + 1)) )
+ {
+ wxLogLastError(_T("GetTempFileName"));
+
+ path.clear();
+ }
+#else // Win16
+ if ( !::GetTempFileName(NULL, prefix, 0, wxStringBuffer(path, 1025)) )
+ {
+ path.clear();
+ }
+#endif // Win32/16
+
+#elif defined(__WXPM__)
+ // for now just create a file
+ //
+ // future enhancements can be to set some extended attributes for file
+ // systems OS/2 supports that have them (HPFS, FAT32) and security
+ // (HPFS386)
+ static const wxChar *szMktempSuffix = wxT("XXX");
+ path << dir << _T('/') << name << szMktempSuffix;
+
+ // Temporarily remove - MN
+ #ifndef __WATCOMC__
+ ::DosCreateDir(wxStringBuffer(MAX_PATH), NULL);
+ #endif
+
+#else // !Windows, !OS/2, !DOS
+ if ( dir.empty() )
+ {
+ dir = wxGetenv(_T("TMP"));
+ if ( path.empty() )
+ {
+ dir = wxGetenv(_T("TEMP"));
+ }
+
+ if ( dir.empty() )
+ {
+ // default
+ #ifdef __DOS__
+ dir = _T(".");
+ #else
+ dir = _T("/tmp");
+ #endif
+ }
+ }
+
+ path = dir;
+
+ if ( !wxEndsWithPathSeparator(dir) &&
+ (name.empty() || !wxIsPathSeparator(name[0u])) )
+ {
+ path += wxFILE_SEP_PATH;
+ }
+
+ path += name;
+
+#if defined(__DOS__) && defined(__WATCOMC__)
+ // scratch space for mkstemp()
+ path += _T("XXXXXX");
+
+ // can use the cast here because the length doesn't change and the string
+ // is not shared
+ if ( !_mktemp((char *)path.mb_str()) )
+ {
+ // this might be not necessary as mkstemp() on most systems should have
+ // already done it but it doesn't hurt neither...
+ path.clear();
+ }
+#elif defined(HAVE_MKSTEMP)
+ // scratch space for mkstemp()
+ path += _T("XXXXXX");
+
+ // can use the cast here because the length doesn't change and the string
+ // is not shared
+ if ( mkstemp((char *)path.mb_str()) == -1 )
+ {
+ // this might be not necessary as mkstemp() on most systems should have
+ // already done it but it doesn't hurt neither...
+ path.clear();
+ }
+ //else: file already created
+#else // !HAVE_MKSTEMP
+
+#ifdef HAVE_MKTEMP
+ // same as above
+ path += _T("XXXXXX");
+
+ if ( !mktemp((char *)path.mb_str()) )
+ {
+ path.clear();
+ }
+#else // !HAVE_MKTEMP
+ // generate the unique file name ourselves
+ path << (unsigned int)getpid();
+
+ wxString pathTry;
+
+ static const size_t numTries = 1000;
+ for ( size_t n = 0; n < numTries; n++ )
+ {
+ // 3 hex digits is enough for numTries == 1000 < 4096
+ pathTry = path + wxString::Format(_T("%.03x"), n);
+ if ( !wxFile::Exists(pathTry) )
+ {
+ break;
+ }
+
+ pathTry.clear();
+ }
+
+ path = pathTry;
+#endif // HAVE_MKTEMP/!HAVE_MKTEMP
+
+ if ( !path.empty() )
+ {
+ // create the file - of course, there is a race condition here, this is
+ // why we always prefer using mkstemp()...
+ wxFile file;
+ if ( !file.Open(path, wxFile::write_excl, wxS_IRUSR | wxS_IWUSR) )
+ {
+ // FIXME: If !ok here should we loop and try again with another
+ // file name? That is the standard recourse if open(O_EXCL)
+ // fails, though of course it should be protected against
+ // possible infinite looping too.
+
+ wxLogError(_("Failed to open temporary file."));
+
+ path.clear();
+ }
}
+#endif // HAVE_MKSTEMP/!HAVE_MKSTEMP
+
+#endif // Windows/!Windows
+
+ if ( path.empty() )
+ {
+ wxLogSysError(_("Failed to create a temporary file name"));
+ }
+
+ return path;
}
// ----------------------------------------------------------------------------
{
wxString dir = dirs[n];
- if ( flags && wxPATH_NORM_DOTS )
+ if ( flags & wxPATH_NORM_DOTS )
{
if ( dir == wxT(".") )
{
return FALSE;
}
- m_dirs.Remove(m_dirs.GetCount() - 1);
+ m_dirs.RemoveAt(m_dirs.GetCount() - 1);
continue;
}
}
return TRUE;
}
+bool wxFileName::MakeRelativeTo(const wxString& pathBase, wxPathFormat format)
+{
+ wxFileName fnBase(pathBase, format);
+
+ // get cwd only once - small time saving
+ wxString cwd = wxGetCwd();
+ Normalize(wxPATH_NORM_ALL, cwd, format);
+ fnBase.Normalize(wxPATH_NORM_ALL, cwd, format);
+
+ bool withCase = IsCaseSensitive(format);
+
+ // we can't do anything if the files live on different volumes
+ if ( !GetVolume().IsSameAs(fnBase.GetVolume(), withCase) )
+ {
+ // nothing done
+ return FALSE;
+ }
+
+ // same drive, so we don't need our volume
+ m_volume.clear();
+
+ // remove common directories starting at the top
+ while ( !m_dirs.IsEmpty() && !fnBase.m_dirs.IsEmpty() &&
+ m_dirs[0u].IsSameAs(fnBase.m_dirs[0u], withCase) )
+ {
+ m_dirs.RemoveAt(0);
+ fnBase.m_dirs.RemoveAt(0);
+ }
+
+ // add as many ".." as needed
+ size_t count = fnBase.m_dirs.GetCount();
+ for ( size_t i = 0; i < count; i++ )
+ {
+ m_dirs.Insert(wxT(".."), 0u);
+ }
+
+ // we were modified
+ return TRUE;
+}
+
// ----------------------------------------------------------------------------
// filename kind tests
// ----------------------------------------------------------------------------
-bool wxFileName::SameAs( const wxFileName &filepath, wxPathFormat format)
+bool wxFileName::SameAs(const wxFileName &filepath, wxPathFormat format)
{
wxFileName fn1 = *this,
fn2 = filepath;
{
wxString sepVol;
- if ( GetFormat(format) != wxPATH_UNIX )
+ if ( (GetFormat(format) == wxPATH_DOS) ||
+ (GetFormat(format) == wxPATH_VMS) )
{
- // so far it is the same for all systems which have it
sepVol = wxFILE_SEP_DSK;
}
- //else: leave empty, no volume separators under Unix
+ //else: leave empty
return sepVol;
}
// first put the volume
if ( !m_volume.empty() )
{
- // special Windows UNC paths hack, part 2: undo what we did in
- // SplitPath() and make an UNC path if we have a drive which is not a
- // single letter (hopefully the network shares can't be one letter only
- // although I didn't find any authoritative docs on this)
- if ( format == wxPATH_DOS && m_volume.length() > 1 )
- {
- fullpath << wxFILE_SEP_PATH_DOS << wxFILE_SEP_PATH_DOS << m_volume;
- }
- else // !UNC
- {
- fullpath << m_volume << GetVolumeSeparator(format);
+ {
+ // Special Windows UNC paths hack, part 2: undo what we did in
+ // SplitPath() and make an UNC path if we have a drive which is not a
+ // single letter (hopefully the network shares can't be one letter only
+ // although I didn't find any authoritative docs on this)
+ if ( format == wxPATH_DOS && m_volume.length() > 1 )
+ {
+ fullpath << wxFILE_SEP_PATH_DOS << wxFILE_SEP_PATH_DOS << m_volume;
+ }
+ else // !UNC
+ {
+ fullpath << m_volume << GetVolumeSeparator(format);
+ }
}
}
{
// under Mac, we must have a path separator in the beginning of the
// relative path - otherwise it would be parsed as an absolute one
- if ( format == wxPATH_MAC && m_volume.empty() && !m_dirs[0].empty() )
+ if ( format == wxPATH_MAC && m_dirs[0].empty() )
{
fullpath += wxFILE_SEP_PATH_MAC;
}
{
if (format == wxPATH_NATIVE)
{
-#if defined(__WXMSW__) || defined(__WXPM__)
+#if defined(__WXMSW__) || defined(__WXPM__) || defined(__DOS__)
format = wxPATH_DOS;
#elif defined(__WXMAC__) && !defined(__DARWIN__)
format = wxPATH_MAC;
}
}
- // do we have the volume name in the beginning?
- wxString sepVol = GetVolumeSeparator(format);
- if ( !sepVol.empty() )
+ // We separate the volume here
+ if ( format == wxPATH_DOS || format == wxPATH_VMS )
{
+ wxString sepVol = GetVolumeSeparator(format);
+
size_t posFirstColon = fullpath.find_first_of(sepVol);
if ( posFirstColon != wxString::npos )
{
const wxDateTime *dtAccess,
const wxDateTime *dtMod)
{
-#if defined(__UNIX_LIKE__)
+#if defined(__UNIX_LIKE__) || (defined(__DOS__) && defined(__WATCOMC__))
if ( !dtAccess && !dtMod )
{
// can't modify the creation time anyhow, don't try
wxDateTime *dtMod,
wxDateTime *dtChange) const
{
-#if defined(__UNIX_LIKE__)
- wxStructStat stBuf;
- if ( wxStat(GetFullPath(), &stBuf) == 0 )
- {
- if ( dtAccess )
- dtAccess->Set(stBuf.st_atime);
- if ( dtMod )
- dtMod->Set(stBuf.st_mtime);
- if ( dtChange )
- dtChange->Set(stBuf.st_ctime);
-
- return TRUE;
- }
-#elif defined(__WXMAC__)
+#if defined(__UNIX_LIKE__) || defined(__WXMAC__) || (defined(__DOS__) && defined(__WATCOMC__))
wxStructStat stBuf;
if ( wxStat(GetFullPath(), &stBuf) == 0 )
{