+#else // !Windows
+ path = dir;
+
+ if ( !wxEndsWithPathSeparator(dir) &&
+ (name.empty() || !wxIsPathSeparator(name[0u])) )
+ {
+ path += wxFILE_SEP_PATH;
+ }
+
+ path += name;
+
+#if defined(HAVE_MKSTEMP)
+ // scratch space for mkstemp()
+ path += _T("XXXXXX");
+
+ // we need to copy the path to the buffer in which mkstemp() can modify it
+ wxCharBuffer buf(path.fn_str());
+
+ // cast is safe because the string length doesn't change
+ int fdTemp = mkstemp( (char*)(const char*) buf );
+ if ( fdTemp == -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 // mkstemp() succeeded
+ {
+ path = wxConvFile.cMB2WX( (const char*) buf );
+
+ #if wxUSE_FILE
+ // avoid leaking the fd
+ if ( fileTemp )
+ {
+ fileTemp->Attach(fdTemp);
+ }
+ else
+ #endif
+
+ #if wxUSE_FFILE
+ if ( ffileTemp )
+ {
+ #ifdef wx_fdopen
+ ffileTemp->Attach(wx_fdopen(fdTemp, "r+b"));
+ #else
+ ffileTemp->Open(path, _T("r+b"));
+ close(fdTemp);
+ #endif
+ }
+ else
+ #endif
+
+ {
+ close(fdTemp);
+ }
+ }
+#else // !HAVE_MKSTEMP
+
+#ifdef HAVE_MKTEMP
+ // same as above
+ path += _T("XXXXXX");
+
+ wxCharBuffer buf = wxConvFile.cWX2MB( path );
+ if ( !mktemp( (char*)(const char*) buf ) )
+ {
+ path.clear();
+ }
+ else
+ {
+ path = wxConvFile.cMB2WX( (const char*) buf );
+ }
+#else // !HAVE_MKTEMP (includes __DOS__)
+ // generate the unique file name ourselves
+ #if !defined(__DOS__) && !defined(__PALMOS__) && (!defined(__MWERKS__) || defined(__DARWIN__) )
+ path << (unsigned int)getpid();
+ #endif
+
+ 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"), (unsigned int) n);
+ if ( !wxFileName::FileExists(pathTry) )
+ {
+ break;
+ }
+
+ pathTry.clear();
+ }
+
+ path = pathTry;
+#endif // HAVE_MKTEMP/!HAVE_MKTEMP
+
+#endif // HAVE_MKSTEMP/!HAVE_MKSTEMP
+
+#endif // Windows/!Windows
+
+ if ( path.empty() )
+ {
+ wxLogSysError(_("Failed to create a temporary file name"));
+ }
+ else
+ {
+ bool ok = true;
+
+ // open the file - of course, there is a race condition here, this is
+ // why we always prefer using mkstemp()...
+ #if wxUSE_FILE
+ if ( fileTemp && !fileTemp->IsOpened() )
+ {
+ *deleteOnClose = wantDeleteOnClose;
+ int fd = wxTempOpen(path, deleteOnClose);
+ if (fd != -1)
+ fileTemp->Attach(fd);
+ else
+ ok = false;
+ }
+ #endif
+
+ #if wxUSE_FFILE
+ if ( ffileTemp && !ffileTemp->IsOpened() )
+ {
+ *deleteOnClose = wantDeleteOnClose;
+ ok = wxTempOpen(ffileTemp, path, deleteOnClose);
+ }
+ #endif
+
+ if ( !ok )
+ {
+ // 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();
+ }
+ }
+
+ return path;
+}
+
+
+static bool wxCreateTempImpl(
+ const wxString& prefix,
+ WXFILEARGS(wxFile *fileTemp, wxFFile *ffileTemp),
+ wxString *name)
+{
+ bool deleteOnClose = true;
+
+ *name = wxCreateTempImpl(prefix,
+ WXFILEARGS(fileTemp, ffileTemp),
+ &deleteOnClose);
+
+ bool ok = !name->empty();
+
+ if (deleteOnClose)
+ name->clear();
+#ifdef __UNIX__
+ else if (ok && wxRemoveFile(*name))
+ name->clear();
+#endif
+
+ return ok;
+}
+
+
+static void wxAssignTempImpl(
+ wxFileName *fn,
+ const wxString& prefix,
+ WXFILEARGS(wxFile *fileTemp, wxFFile *ffileTemp))
+{
+ wxString tempname;
+ tempname = wxCreateTempImpl(prefix, WXFILEARGS(fileTemp, ffileTemp));
+
+ if ( tempname.empty() )
+ {
+ // error, failed to get temp file name
+ fn->Clear();
+ }
+ else // ok
+ {
+ fn->Assign(tempname);
+ }
+}
+
+
+void wxFileName::AssignTempFileName(const wxString& prefix)
+{
+ wxAssignTempImpl(this, prefix, WXFILEARGS(NULL, NULL));
+}
+
+/* static */
+wxString wxFileName::CreateTempFileName(const wxString& prefix)
+{
+ return wxCreateTempImpl(prefix, WXFILEARGS(NULL, NULL));
+}
+
+#endif // wxUSE_FILE || wxUSE_FFILE
+
+
+#if wxUSE_FILE
+
+wxString wxCreateTempFileName(const wxString& prefix,
+ wxFile *fileTemp,
+ bool *deleteOnClose)
+{
+ return wxCreateTempImpl(prefix, WXFILEARGS(fileTemp, NULL), deleteOnClose);
+}
+
+bool wxCreateTempFile(const wxString& prefix,
+ wxFile *fileTemp,
+ wxString *name)
+{
+ return wxCreateTempImpl(prefix, WXFILEARGS(fileTemp, NULL), name);
+}
+
+void wxFileName::AssignTempFileName(const wxString& prefix, wxFile *fileTemp)
+{
+ wxAssignTempImpl(this, prefix, WXFILEARGS(fileTemp, NULL));
+}
+
+/* static */
+wxString
+wxFileName::CreateTempFileName(const wxString& prefix, wxFile *fileTemp)
+{
+ return wxCreateTempFileName(prefix, fileTemp);
+}
+
+#endif // wxUSE_FILE
+
+
+#if wxUSE_FFILE
+
+wxString wxCreateTempFileName(const wxString& prefix,
+ wxFFile *fileTemp,
+ bool *deleteOnClose)
+{
+ return wxCreateTempImpl(prefix, WXFILEARGS(NULL, fileTemp), deleteOnClose);
+}
+
+bool wxCreateTempFile(const wxString& prefix,
+ wxFFile *fileTemp,
+ wxString *name)
+{
+ return wxCreateTempImpl(prefix, WXFILEARGS(NULL, fileTemp), name);
+
+}
+
+void wxFileName::AssignTempFileName(const wxString& prefix, wxFFile *fileTemp)
+{
+ wxAssignTempImpl(this, prefix, WXFILEARGS(NULL, fileTemp));
+}
+
+/* static */
+wxString
+wxFileName::CreateTempFileName(const wxString& prefix, wxFFile *fileTemp)
+{
+ return wxCreateTempFileName(prefix, fileTemp);
+}
+
+#endif // wxUSE_FFILE
+
+
+// ----------------------------------------------------------------------------
+// directory operations
+// ----------------------------------------------------------------------------
+
+// helper of GetTempDir(): check if the given directory exists and return it if
+// it does or an empty string otherwise
+namespace
+{
+
+wxString CheckIfDirExists(const wxString& dir)
+{
+ return wxFileName::DirExists(dir) ? dir : wxString();
+}
+
+} // anonymous namespace
+
+wxString wxFileName::GetTempDir()
+{
+ // first try getting it from environment: this allows overriding the values
+ // used by default if the user wants to create temporary files in another
+ // directory
+ wxString dir = CheckIfDirExists(wxGetenv("TMPDIR"));
+ if ( dir.empty() )
+ {
+ dir = CheckIfDirExists(wxGetenv("TMP"));
+ if ( dir.empty() )
+ dir = CheckIfDirExists(wxGetenv("TEMP"));
+ }
+
+ // if no environment variables are set, use the system default
+ if ( dir.empty() )
+ {
+#if defined(__WXWINCE__)
+ dir = CheckIfDirExists(wxT("\\temp"));
+#elif defined(__WINDOWS__) && !defined(__WXMICROWIN__)
+ if ( !::GetTempPath(MAX_PATH, wxStringBuffer(dir, MAX_PATH + 1)) )
+ {
+ wxLogLastError(_T("GetTempPath"));
+ }
+#elif defined(__WXMAC__) && wxOSX_USE_CARBON
+ dir = wxMacFindFolder(short(kOnSystemDisk), kTemporaryFolderType, kCreateFolder);
+#endif // systems with native way
+ }
+
+ // fall back to hard coded value
+ if ( dir.empty() )
+ {
+#ifdef __UNIX_LIKE__
+ dir = CheckIfDirExists("/tmp");
+ if ( dir.empty() )
+#endif // __UNIX_LIKE__
+ dir = ".";
+ }
+
+ return dir;
+}
+
+bool wxFileName::Mkdir( int perm, int flags )
+{
+ return wxFileName::Mkdir(GetPath(), perm, flags);
+}
+
+bool wxFileName::Mkdir( const wxString& dir, int perm, int flags )
+{
+ if ( flags & wxPATH_MKDIR_FULL )
+ {
+ // split the path in components
+ wxFileName filename;
+ filename.AssignDir(dir);
+
+ wxString currPath;
+ if ( filename.HasVolume())
+ {
+ currPath << wxGetVolumeString(filename.GetVolume(), wxPATH_NATIVE);
+ }
+
+ wxArrayString dirs = filename.GetDirs();
+ size_t count = dirs.GetCount();
+ for ( size_t i = 0; i < count; i++ )
+ {
+ if ( i > 0 || filename.IsAbsolute() )
+ currPath += wxFILE_SEP_PATH;
+ currPath += dirs[i];
+
+ if (!DirExists(currPath))
+ {
+ if (!wxMkdir(currPath, perm))
+ {
+ // no need to try creating further directories
+ return false;
+ }
+ }
+ }
+
+ return true;
+
+ }
+
+ return ::wxMkdir( dir, perm );
+}
+
+bool wxFileName::Rmdir()
+{
+ return wxFileName::Rmdir( GetPath() );
+}
+
+bool wxFileName::Rmdir( const wxString &dir )
+{
+ return ::wxRmdir( dir );
+}
+
+// ----------------------------------------------------------------------------
+// path normalization
+// ----------------------------------------------------------------------------
+
+bool wxFileName::Normalize(int flags,
+ const wxString& cwd,
+ wxPathFormat format)
+{
+ // deal with env vars renaming first as this may seriously change the path
+ if ( flags & wxPATH_NORM_ENV_VARS )
+ {
+ wxString pathOrig = GetFullPath(format);
+ wxString path = wxExpandEnvVars(pathOrig);
+ if ( path != pathOrig )
+ {
+ Assign(path);
+ }
+ }
+
+
+ // the existing path components
+ wxArrayString dirs = GetDirs();
+
+ // the path to prepend in front to make the path absolute
+ wxFileName curDir;
+
+ format = GetFormat(format);
+
+ // set up the directory to use for making the path absolute later
+ if ( (flags & wxPATH_NORM_ABSOLUTE) && !IsAbsolute(format) )
+ {
+ if ( cwd.empty() )
+ {
+ curDir.AssignCwd(GetVolume());
+ }
+ else // cwd provided
+ {
+ curDir.AssignDir(cwd);
+ }
+ }
+
+ // handle ~ stuff under Unix only
+ if ( (format == wxPATH_UNIX) && (flags & wxPATH_NORM_TILDE) )
+ {
+ if ( !dirs.IsEmpty() )
+ {
+ wxString dir = dirs[0u];
+ if ( !dir.empty() && dir[0u] == _T('~') )
+ {
+ // to make the path absolute use the home directory
+ curDir.AssignDir(wxGetUserHome(dir.c_str() + 1));
+
+ // if we are expanding the tilde, then this path
+ // *should* be already relative (since we checked for
+ // the tilde only in the first char of the first dir);
+ // if m_relative==false, it's because it was initialized
+ // from a string which started with /~; in that case
+ // we reach this point but then need m_relative=true
+ // for relative->absolute expansion later
+ m_relative = true;
+
+ dirs.RemoveAt(0u);
+ }
+ }
+ }
+
+ // transform relative path into abs one
+ if ( curDir.IsOk() )
+ {
+ // this path may be relative because it doesn't have the volume name
+ // and still have m_relative=true; in this case we shouldn't modify
+ // our directory components but just set the current volume
+ if ( !HasVolume() && curDir.HasVolume() )
+ {
+ SetVolume(curDir.GetVolume());
+
+ if ( !m_relative )
+ {
+ // yes, it was the case - we don't need curDir then
+ curDir.Clear();
+ }
+ }
+
+ // finally, prepend curDir to the dirs array
+ wxArrayString dirsNew = curDir.GetDirs();
+ WX_PREPEND_ARRAY(dirs, dirsNew);
+
+ // if we used e.g. tilde expansion previously and wxGetUserHome didn't
+ // return for some reason an absolute path, then curDir maybe not be absolute!
+ if ( curDir.IsAbsolute(format) )
+ {
+ // we have prepended an absolute path and thus we are now an absolute
+ // file name too
+ m_relative = false;
+ }
+ // else if (flags & wxPATH_NORM_ABSOLUTE):
+ // should we warn the user that we didn't manage to make the path absolute?
+ }
+
+ // now deal with ".", ".." and the rest
+ m_dirs.Empty();