From 8d7d6deac6086ebdd1a985518e3c4ab27ece7530 Mon Sep 17 00:00:00 2001 From: Julian Smart Date: Mon, 23 Oct 2006 17:47:02 +0000 Subject: [PATCH] Added GetTempDir git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@42290 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- docs/latex/wx/filename.tex | 7 +++ include/wx/filename.h | 3 ++ src/common/filename.cpp | 102 ++++++++++++++++++++----------------- 3 files changed, 64 insertions(+), 48 deletions(-) diff --git a/docs/latex/wx/filename.tex b/docs/latex/wx/filename.tex index ba2c91213b..b7c8e658f0 100644 --- a/docs/latex/wx/filename.tex +++ b/docs/latex/wx/filename.tex @@ -556,6 +556,13 @@ In case of success, the returned string is a floating-point number with {\tt pre followed by the size unit (B, kB, MB, GB, TB: respectively bytes, kilobytes, megabytes, gigabytes, terabytes). +\membersection{wxFileName::GetTempDir}\label{wxfilenamegettempdir} + +\func{static wxString}{GetTempDir}{\void} + +Returns the directory used for temporary files. + + \membersection{wxFileName::GetTimes}\label{wxfilenamegettimes} \constfunc{bool}{GetTimes}{\param{wxDateTime* }{dtAccess}, \param{wxDateTime* }{dtMod}, \param{wxDateTime* }{dtCreate}} diff --git a/include/wx/filename.h b/include/wx/filename.h index 762237b5ae..f78164d73e 100644 --- a/include/wx/filename.h +++ b/include/wx/filename.h @@ -270,6 +270,9 @@ public: void AssignHomeDir(); static wxString GetHomeDir(); + // get the system temporary directory + static wxString GetTempDir(); + #if wxUSE_FILE || wxUSE_FFILE // get a temp file name starting with the specified prefix void AssignTempFileName(const wxString& prefix); diff --git a/src/common/filename.cpp b/src/common/filename.cpp index c6e42ca232..98af00ab43 100644 --- a/src/common/filename.cpp +++ b/src/common/filename.cpp @@ -697,24 +697,10 @@ static wxString wxCreateTempImpl( if (dir.empty()) { - dir = wxGetenv(_T("TMPDIR")); - if (dir.empty()) - { - dir = wxGetenv(_T("TMP")); - if (dir.empty()) - { - dir = wxGetenv(_T("TEMP")); - } - } + dir = wxFileName::GetTempDir(); } #if defined(__WXWINCE__) - if (dir.empty()) - { - // FIXME. Create \temp dir? - if (wxFileName::DirExists(wxT("\\temp"))) - dir = wxT("\\temp"); - } path = dir + wxT("\\") + name; int i = 1; while (wxFileName::FileExists(path)) @@ -725,27 +711,6 @@ static wxString wxCreateTempImpl( } #elif defined(__WINDOWS__) && !defined(__WXMICROWIN__) - - 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('.'); - } - } - else // we have a dir to create the file in - { - // ensure we use only the back slashes as GetTempFileName(), unlike all - // the other APIs, is picky and doesn't accept the forward ones - dir.Replace(_T("/"), _T("\\")); - } - if ( !::GetTempFileName(dir, name, 0, wxStringBuffer(path, MAX_PATH + 1)) ) { wxLogLastError(_T("GetTempFileName")); @@ -754,18 +719,6 @@ static wxString wxCreateTempImpl( } #else // !Windows - if ( dir.empty() ) - { - // default -#if defined(__DOS__) || defined(__OS2__) - dir = _T("."); -#elif defined(__WXMAC__) - dir = wxMacFindFolder(short(kOnSystemDisk), kTemporaryFolderType, kCreateFolder); -#else - dir = _T("/tmp"); -#endif - } - path = dir; if ( !wxEndsWithPathSeparator(dir) && @@ -1036,6 +989,59 @@ wxFileName::CreateTempFileName(const wxString& prefix, wxFFile *fileTemp) // directory operations // ---------------------------------------------------------------------------- +wxString wxFileName::GetTempDir() +{ + wxString dir; + dir = wxGetenv(_T("TMPDIR")); + if (dir.empty()) + { + dir = wxGetenv(_T("TMP")); + if (dir.empty()) + { + dir = wxGetenv(_T("TEMP")); + } + } + +#if defined(__WXWINCE__) + if (dir.empty()) + { + // FIXME. Create \temp dir? + if (DirExists(wxT("\\temp"))) + dir = wxT("\\temp"); + } +#elif defined(__WINDOWS__) && !defined(__WXMICROWIN__) + + 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('.'); + } + } +#else // !Windows + + if ( dir.empty() ) + { + // default +#if defined(__DOS__) || defined(__OS2__) + dir = _T("."); +#elif defined(__WXMAC__) + dir = wxMacFindFolder(short(kOnSystemDisk), kTemporaryFolderType, kCreateFolder); +#else + dir = _T("/tmp"); +#endif + } +#endif + + return dir; +} + bool wxFileName::Mkdir( int perm, int flags ) { return wxFileName::Mkdir(GetPath(), perm, flags); -- 2.45.2