X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/9a0c5c012a1137c4c3fb0773df8644553dcc0551..004867dbc54950a43acc7d250fa4a966f046a679:/src/common/filename.cpp diff --git a/src/common/filename.cpp b/src/common/filename.cpp index 0f206a94b5..4e21ea1449 100644 --- a/src/common/filename.cpp +++ b/src/common/filename.cpp @@ -93,7 +93,7 @@ #endif #if defined(__WXMAC__) - #include "wx/mac/private.h" // includes mac headers + #include "wx/osx/private.h" // includes mac headers #endif // utime() is POSIX so should normally be available on all Unices @@ -161,7 +161,7 @@ public: Write }; - wxFileHandle(const wxString& filename, OpenMode mode) + wxFileHandle(const wxString& filename, OpenMode mode, int flags = 0) { m_hFile = ::CreateFile ( @@ -172,7 +172,7 @@ public: FILE_SHARE_WRITE, // (allow everything) NULL, // no secutity attr OPEN_EXISTING, // creation disposition - 0, // no flags + flags, // flags NULL // no template file ); @@ -1026,55 +1026,55 @@ wxFileName::CreateTempFileName(const wxString& prefix, wxFFile *fileTemp) // directory operations // ---------------------------------------------------------------------------- -wxString wxFileName::GetTempDir() +// helper of GetTempDir(): check if the given directory exists and return it if +// it does or an empty string otherwise +namespace { - 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()) +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() ) { - // FIXME. Create \temp dir? - if (DirExists(wxT("\\temp"))) - dir = wxT("\\temp"); + dir = CheckIfDirExists(wxGetenv("TMP")); + if ( dir.empty() ) + dir = CheckIfDirExists(wxGetenv("TEMP")); } -#elif defined(__WINDOWS__) && !defined(__WXMICROWIN__) + // 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")); } - - if ( dir.empty() ) - { - // GetTempFileName() fails if we pass it an empty string - dir = _T('.'); - } +#elif defined(__WXMAC__) && wxOSX_USE_CARBON + dir = wxMacFindFolder(short(kOnSystemDisk), kTemporaryFolderType, kCreateFolder); +#endif // systems with native way } -#else // !Windows + // fall back to hard coded value 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 +#ifdef __UNIX_LIKE__ + dir = CheckIfDirExists("/tmp"); + if ( dir.empty() ) +#endif // __UNIX_LIKE__ + dir = "."; } -#endif return dir; } @@ -1152,7 +1152,6 @@ bool wxFileName::Normalize(int flags, } } - // the existing path components wxArrayString dirs = GetDirs(); @@ -1302,6 +1301,49 @@ bool wxFileName::Normalize(int flags, return true; } +#ifndef __WXWINCE__ +bool wxFileName::ReplaceEnvVariable(const wxString& envname, + const wxString& replacementFmtString, + wxPathFormat format) +{ + // look into stringForm for the contents of the given environment variable + wxString val; + if (envname.empty() || + !wxGetEnv(envname, &val)) + return false; + if (val.empty()) + return false; + + wxString stringForm = GetPath(wxPATH_GET_VOLUME, format); + // do not touch the file name and the extension + + wxString replacement = wxString::Format(replacementFmtString, envname); + stringForm.Replace(val, replacement); + + // Now assign ourselves the modified path: + Assign(stringForm, GetFullName(), format); + + return true; +} +#endif + +bool wxFileName::ReplaceHomeDir(wxPathFormat format) +{ + wxString homedir = wxGetHomeDir(); + if (homedir.empty()) + return false; + + wxString stringForm = GetPath(wxPATH_GET_VOLUME, format); + // do not touch the file name and the extension + + stringForm.Replace(homedir, "~"); + + // Now assign ourselves the modified path: + Assign(stringForm, GetFullName(), format); + + return true; +} + // ---------------------------------------------------------------------------- // get the shortcut target // ---------------------------------------------------------------------------- @@ -1331,7 +1373,7 @@ bool wxFileName::GetShortcutTarget(const wxString& shortcutPath, wxString* arguments) { wxString path, file, ext; - wxSplitPath(shortcutPath, & path, & file, & ext); + wxFileName::SplitPath(shortcutPath, & path, & file, & ext); HRESULT hres; IShellLink* psl; @@ -1896,19 +1938,21 @@ wxString wxFileName::GetLongPath() const size_t count = dirs.GetCount(); for ( size_t i = 0; i < count; i++ ) { + const wxString& dir = dirs[i]; + // We're using pathOut to collect the long-name path, but using a // temporary for appending the last path component which may be // short-name - tmpPath = pathOut + dirs[i]; - - if ( tmpPath.empty() ) - continue; - - // can't see this being necessary? MF - if ( tmpPath.Last() == GetVolumeSeparator(wxPATH_DOS) ) + tmpPath = pathOut + dir; + + // We must not process "." or ".." here as they would be (unexpectedly) + // replaced by the corresponding directory names so just leave them + // alone + // + // And we can't pass a drive and root dir to FindFirstFile (VZ: why?) + if ( tmpPath.empty() || dir == '.' || dir == ".." || + tmpPath.Last() == GetVolumeSeparator(wxPATH_DOS) ) { - // Can't pass a drive and root dir to FindFirstFile, - // so continue to next dir tmpPath += wxFILE_SEP_PATH; pathOut = tmpPath; continue; @@ -1953,6 +1997,23 @@ wxPathFormat wxFileName::GetFormat( wxPathFormat format ) return format; } +#ifdef wxHAS_FILESYSTEM_VOLUMES + +/* static */ +wxString wxFileName::GetVolumeString(char drive, int flags) +{ + wxASSERT_MSG( !(flags & ~wxPATH_GET_SEPARATOR), "invalid flag specified" ); + + wxString vol(drive); + vol += wxFILE_SEP_DSK; + if ( flags & wxPATH_GET_SEPARATOR ) + vol += wxFILE_SEP_PATH; + + return vol; +} + +#endif // wxHAS_FILESYSTEM_VOLUMES + // ---------------------------------------------------------------------------- // path splitting function // ---------------------------------------------------------------------------- @@ -2148,32 +2209,44 @@ bool wxFileName::SetTimes(const wxDateTime *dtAccess, const wxDateTime *dtCreate) { #if defined(__WIN32__) + FILETIME ftAccess, ftCreate, ftWrite; + + if ( dtCreate ) + ConvertWxToFileTime(&ftCreate, *dtCreate); + if ( dtAccess ) + ConvertWxToFileTime(&ftAccess, *dtAccess); + if ( dtMod ) + ConvertWxToFileTime(&ftWrite, *dtMod); + + wxString path; + int flags; if ( IsDir() ) { - // VZ: please let me know how to do this if you can - wxFAIL_MSG( _T("SetTimes() not implemented for the directories") ); + if ( wxGetOsVersion() == wxOS_WINDOWS_9X ) + { + wxLogError(_("Setting directory access times is not supported " + "under this OS version")); + return false; + } + + path = GetPath(); + flags = FILE_FLAG_BACKUP_SEMANTICS; } else // file { - wxFileHandle fh(GetFullPath(), wxFileHandle::Write); - if ( fh.IsOk() ) - { - FILETIME ftAccess, ftCreate, ftWrite; - - if ( dtCreate ) - ConvertWxToFileTime(&ftCreate, *dtCreate); - if ( dtAccess ) - ConvertWxToFileTime(&ftAccess, *dtAccess); - if ( dtMod ) - ConvertWxToFileTime(&ftWrite, *dtMod); + path = GetFullPath(); + flags = 0; + } - if ( ::SetFileTime(fh, - dtCreate ? &ftCreate : NULL, - dtAccess ? &ftAccess : NULL, - dtMod ? &ftWrite : NULL) ) - { - return true; - } + wxFileHandle fh(path, wxFileHandle::Write, flags); + if ( fh.IsOk() ) + { + if ( ::SetFileTime(fh, + dtCreate ? &ftCreate : NULL, + dtAccess ? &ftAccess : NULL, + dtMod ? &ftWrite : NULL) ) + { + return true; } } #elif defined(__UNIX_LIKE__) || (defined(__DOS__) && defined(__WATCOMC__)) @@ -2383,54 +2456,53 @@ wxString wxFileName::GetHumanReadableSize(const wxString &failmsg, int precision // Mac-specific functions // ---------------------------------------------------------------------------- -#ifdef __WXMAC__ +#if defined( __WXOSX_MAC__ ) && wxOSX_USE_CARBON + +namespace +{ -const short kMacExtensionMaxLength = 16 ; class MacDefaultExtensionRecord { -public : - MacDefaultExtensionRecord() - { - m_ext[0] = 0 ; - m_type = m_creator = 0 ; - } - MacDefaultExtensionRecord( const MacDefaultExtensionRecord& from ) - { - wxStrcpy( m_ext , from.m_ext ) ; - m_type = from.m_type ; - m_creator = from.m_creator ; - } - MacDefaultExtensionRecord( const wxChar * extension , OSType type , OSType creator ) - { - wxStrncpy( m_ext , extension , kMacExtensionMaxLength ) ; - m_ext[kMacExtensionMaxLength] = 0 ; - m_type = type ; - m_creator = creator ; - } - wxChar m_ext[kMacExtensionMaxLength] ; - OSType m_type ; - OSType m_creator ; -} ; +public: + MacDefaultExtensionRecord() + { + m_type = + m_creator = 0 ; + } -WX_DECLARE_OBJARRAY(MacDefaultExtensionRecord, MacDefaultExtensionArray) ; + // default copy ctor, assignment operator and dtor are ok + + MacDefaultExtensionRecord(const wxString& ext, OSType type, OSType creator) + : m_ext(ext) + { + m_type = type; + m_creator = creator; + } + + wxString m_ext; + OSType m_type; + OSType m_creator; +}; -bool gMacDefaultExtensionsInited = false ; +WX_DECLARE_OBJARRAY(MacDefaultExtensionRecord, MacDefaultExtensionArray); + +bool gMacDefaultExtensionsInited = false; #include "wx/arrimpl.cpp" -WX_DEFINE_EXPORTED_OBJARRAY(MacDefaultExtensionArray) ; +WX_DEFINE_EXPORTED_OBJARRAY(MacDefaultExtensionArray); -MacDefaultExtensionArray gMacDefaultExtensions ; +MacDefaultExtensionArray gMacDefaultExtensions; // load the default extensions -MacDefaultExtensionRecord gDefaults[] = +const MacDefaultExtensionRecord gDefaults[] = { - MacDefaultExtensionRecord( wxT("txt") , 'TEXT' , 'ttxt' ) , - MacDefaultExtensionRecord( wxT("tif") , 'TIFF' , '****' ) , - MacDefaultExtensionRecord( wxT("jpg") , 'JPEG' , '****' ) , -} ; + MacDefaultExtensionRecord( "txt", 'TEXT', 'ttxt' ), + MacDefaultExtensionRecord( "tif", 'TIFF', '****' ), + MacDefaultExtensionRecord( "jpg", 'JPEG', '****' ), +}; -static void MacEnsureDefaultExtensionsLoaded() +void MacEnsureDefaultExtensionsLoaded() { if ( !gMacDefaultExtensionsInited ) { @@ -2439,10 +2511,12 @@ static void MacEnsureDefaultExtensionsLoaded() { gMacDefaultExtensions.Add( gDefaults[i] ) ; } - gMacDefaultExtensionsInited = true ; + gMacDefaultExtensionsInited = true; } } +} // anonymous namespace + bool wxFileName::MacSetTypeAndCreator( wxUint32 type , wxUint32 creator ) { FSRef fsRef ; @@ -2511,11 +2585,9 @@ bool wxFileName::MacFindDefaultTypeAndCreator( const wxString& ext , wxUint32 *t void wxFileName::MacRegisterDefaultTypeAndCreator( const wxString& ext , wxUint32 type , wxUint32 creator ) { - MacEnsureDefaultExtensionsLoaded() ; - MacDefaultExtensionRecord rec ; - rec.m_type = type ; - rec.m_creator = creator ; - wxStrncpy( rec.m_ext , ext.Lower().c_str() , kMacExtensionMaxLength ) ; - gMacDefaultExtensions.Add( rec ) ; + MacEnsureDefaultExtensionsLoaded(); + MacDefaultExtensionRecord rec(ext.Lower(), type, creator); + gMacDefaultExtensions.Add( rec ); } -#endif + +#endif // defined( __WXOSX_MAC__ ) && wxOSX_USE_CARBON