+#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 += wxT("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, wxT("r+b"));
+ close(fdTemp);
+ #endif
+ }
+ else
+ #endif
+
+ {
+ close(fdTemp);
+ }
+ }
+#else // !HAVE_MKSTEMP
+
+#ifdef HAVE_MKTEMP
+ // same as above
+ path += wxT("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(wxT("%.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(wxT("GetTempPath"));
+ }
+#elif defined(__WXMAC__) && wxOSX_USE_CARBON
+ dir = wxMacFindFolderNoSeparator(short(kOnSystemDisk), kTemporaryFolderType, kCreateFolder);
+#endif // systems with native way
+ }
+ else // we got directory from an environment variable
+ {
+ // remove any trailing path separators, we don't want to ever return
+ // them from this function for consistency
+ const size_t lastNonSep = dir.find_last_not_of(GetPathSeparators());
+ if ( lastNonSep == wxString::npos )
+ {
+ // the string consists entirely of separators, leave only one
+ dir = GetPathSeparator();
+ }
+ else
+ {
+ dir.erase(lastNonSep + 1);
+ }
+ }
+
+ // 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 ) const
+{
+ 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(int flags) const
+{
+ return wxFileName::Rmdir( GetPath(), flags );
+}
+
+bool wxFileName::Rmdir(const wxString& dir, int flags)
+{
+#ifdef __WXMSW__
+ if ( flags & wxPATH_RMDIR_RECURSIVE )
+ {
+ // SHFileOperation needs double null termination string
+ // but without separator at the end of the path
+ wxString path(dir);
+ if ( path.Last() == wxFILE_SEP_PATH )
+ path.RemoveLast();
+ path += wxT('\0');
+
+ SHFILEOPSTRUCT fileop;
+ wxZeroMemory(fileop);
+ fileop.wFunc = FO_DELETE;
+ #if defined(__CYGWIN__) && defined(wxUSE_UNICODE)
+ fileop.pFrom = path.wc_str();
+ #else
+ fileop.pFrom = path.fn_str();
+ #endif
+ fileop.fFlags = FOF_SILENT | FOF_NOCONFIRMATION;
+ #ifndef __WXWINCE__
+ // FOF_NOERRORUI is not defined in WinCE
+ fileop.fFlags |= FOF_NOERRORUI;
+ #endif
+
+ int ret = SHFileOperation(&fileop);
+ if ( ret != 0 )
+ {
+ // SHFileOperation may return non-Win32 error codes, so the error
+ // message can be incorrect
+ wxLogApiError(wxT("SHFileOperation"), ret);
+ return false;
+ }
+
+ return true;
+ }
+ else if ( flags & wxPATH_RMDIR_FULL )
+#else // !__WXMSW__
+ if ( flags != 0 ) // wxPATH_RMDIR_FULL or wxPATH_RMDIR_RECURSIVE
+#endif // !__WXMSW__
+ {
+ wxString path(dir);
+ if ( path.Last() != wxFILE_SEP_PATH )
+ path += wxFILE_SEP_PATH;
+
+ wxDir d(path);
+
+ if ( !d.IsOpened() )
+ return false;
+
+ wxString filename;
+
+ // first delete all subdirectories
+ bool cont = d.GetFirst(&filename, "", wxDIR_DIRS | wxDIR_HIDDEN);
+ while ( cont )
+ {
+ wxFileName::Rmdir(path + filename, flags);
+ cont = d.GetNext(&filename);
+ }
+
+#ifndef __WXMSW__
+ if ( flags & wxPATH_RMDIR_RECURSIVE )
+ {
+ // delete all files too
+ cont = d.GetFirst(&filename, "", wxDIR_FILES | wxDIR_HIDDEN);
+ while ( cont )
+ {
+ ::wxRemoveFile(path + filename);
+ cont = d.GetNext(&filename);
+ }
+ }
+#endif // !__WXMSW__
+ }
+
+ 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) && m_relative )
+ {
+ if ( !dirs.IsEmpty() )
+ {
+ wxString dir = dirs[0u];
+ if ( !dir.empty() && dir[0u] == wxT('~') )
+ {
+ // to make the path absolute use the home directory
+ curDir.AssignDir(wxGetUserHome(dir.c_str() + 1));
+ 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.m_relative )
+ {
+ // 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