if ( m_hFile == INVALID_HANDLE_VALUE )
{
- wxLogSysError(_("Failed to open '%s' for %s"),
- filename.c_str(),
- mode == Read ? _("reading") : _("writing"));
+ if ( mode == Read )
+ wxLogSysError(_("Failed to open '%s' for reading"),
+ filename.c_str());
+ else
+ wxLogSysError(_("Failed to open '%s' for writing"),
+ filename.c_str());
}
}
return path;
}
+// return true if the format used is the DOS/Windows one and the string looks
+// like a UNC path
+static bool IsUNCPath(const wxString& path, wxPathFormat format)
+{
+ return format == wxPATH_DOS &&
+ path.length() >= 4 && // "\\a" can't be a UNC path
+ path[0u] == wxFILE_SEP_PATH_DOS &&
+ path[1u] == wxFILE_SEP_PATH_DOS &&
+ path[2u] != wxFILE_SEP_PATH_DOS;
+}
+
// ============================================================================
// implementation
// ============================================================================
const wxString& name,
const wxString& ext,
bool hasExt,
- wxPathFormat format )
+ wxPathFormat format)
{
- SetPath( path, format );
+ // we should ignore paths which look like UNC shares because we already
+ // have the volume here and the UNC notation (\\server\path) is only valid
+ // for paths which don't start with a volume, so prevent SetPath() from
+ // recognizing "\\foo\bar" in "c:\\foo\bar" as an UNC path
+ //
+ // note also that this is a rather ugly way to do what we want (passing
+ // some kind of flag telling to ignore UNC paths to SetPath() would be
+ // better) but this is the safest thing to do to avoid breaking backwards
+ // compatibility in 2.8
+ if ( IsUNCPath(path, format) )
+ {
+ // remove one of the 2 leading backslashes to ensure that it's not
+ // recognized as an UNC path by SetPath()
+ wxString pathNonUNC(path, 1, wxString::npos);
+ SetPath(pathNonUNC, format);
+ }
+ else // no UNC complications
+ {
+ SetPath(path, format);
+ }
m_volume = volume;
m_ext = ext;
// always recognize fullpath as directory, even if it doesn't end with a
// slash
wxString fullpath = fullpathOrig;
- if ( !wxEndsWithPathSeparator(fullpath) )
+ if ( !fullpath.empty() && !wxEndsWithPathSeparator(fullpath) )
{
fullpath += GetPathSeparator(format);
}
HANDLE h = ::CreateFile(filename, access, 0, NULL,
disposition, attributes, NULL);
- return wxOpenOSFHandle(h, 0);
+ return wxOpenOSFHandle(h, wxO_BINARY);
}
#endif // wxOpenOSFHandle
return file->Open(path, _T("w+b"));
#else // wx_fdopen
int fd = wxTempOpen(path, deleteOnClose);
- if (fd != -1)
+ if (fd == -1)
return false;
file->Attach(wx_fdopen(fd, "w+b"));
return file->IsOpened();
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))
}
#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"));
}
#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) &&
// 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);
format = GetFormat(format);
- // make the path absolute
+ // set up the directory to use for making the path absolute later
if ( (flags & wxPATH_NORM_ABSOLUTE) && !IsAbsolute(format) )
{
if ( cwd.empty() )
{
curDir.AssignDir(cwd);
}
-
- // the path may be not absolute because it doesn't have the volume name
- // but in this case we shouldn't modify the directory components of it
- // 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();
- }
- }
}
// handle ~ stuff under Unix only
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() )
{
- wxArrayString dirsNew = curDir.GetDirs();
- size_t count = dirs.GetCount();
- for ( size_t n = 0; n < count; n++ )
+ // 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 )
{
- dirsNew.Add(dirs[n]);
+ // yes, it was the case - we don't need curDir then
+ curDir.Clear();
+ }
}
- dirs = dirsNew;
+ // 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_ext.MakeLower();
}
- // we do have the path now
- //
- // NB: need to do this before (maybe) calling Assign() below
- m_relative = false;
-
#if defined(__WIN32__)
if ( (flags & wxPATH_NORM_LONG) && (format == wxPATH_DOS) )
{
wxString pathOut,
path = GetFullPath();
-#if defined(__WIN32__) && !defined(__WXMICROWIN__)
+#if defined(__WIN32__) && !defined(__WXWINCE__) && !defined(__WXMICROWIN__)
#if wxUSE_DYNAMIC_LOADER
typedef DWORD (WINAPI *GET_LONG_PATH_NAME)(const wxChar *, wxChar *, DWORD);
wxString fullpath = fullpathWithVolume;
// special Windows UNC paths hack: transform \\share\path into share:path
- if ( format == wxPATH_DOS )
+ if ( IsUNCPath(fullpath, format) )
{
- if ( fullpath.length() >= 4 &&
- fullpath[0u] == wxFILE_SEP_PATH_DOS &&
- fullpath[1u] == wxFILE_SEP_PATH_DOS )
- {
- fullpath.erase(0, 2);
+ fullpath.erase(0, 2);
- size_t posFirstSlash =
- fullpath.find_first_of(GetPathTerminators(format));
- if ( posFirstSlash != wxString::npos )
- {
- fullpath[posFirstSlash] = wxFILE_SEP_DSK;
+ size_t posFirstSlash =
+ fullpath.find_first_of(GetPathTerminators(format));
+ if ( posFirstSlash != wxString::npos )
+ {
+ fullpath[posFirstSlash] = wxFILE_SEP_DSK;
- // UNC paths are always absolute, right? (FIXME)
- fullpath.insert(posFirstSlash + 1, 1, wxFILE_SEP_PATH_DOS);
- }
+ // UNC paths are always absolute, right? (FIXME)
+ fullpath.insert(posFirstSlash + 1, 1, wxFILE_SEP_PATH_DOS);
}
}
// not 9x
bool ok;
FILETIME ftAccess, ftCreate, ftWrite;
- if ( IsDir() )
+ if ( IsDir() )
{
// implemented in msw/dir.cpp
extern bool wxGetDirectoryTimes(const wxString& dirname,
return true;
}
#elif defined(__UNIX_LIKE__) || defined(__WXMAC__) || defined(__OS2__) || (defined(__DOS__) && defined(__WATCOMC__))
+ // no need to test for IsDir() here
wxStructStat stBuf;
if ( wxStat( GetFullPath().c_str(), &stBuf) == 0 )
{
DWORD lpFileSizeHigh;
DWORD ret = GetFileSize(f, &lpFileSizeHigh);
- if (ret == INVALID_FILE_SIZE)
+ if ( ret == INVALID_FILE_SIZE && ::GetLastError() != NO_ERROR )
return wxInvalidSize;
- // compose the low-order and high-order byte sizes
- return wxULongLong(ret | (lpFileSizeHigh << sizeof(WORD)*2));
-
-#else // ! __WIN32__
-
+ return wxULongLong(lpFileSizeHigh, ret);
+#else // ! __WIN32__
wxStructStat st;
#ifndef wxNEED_WX_UNISTD_H
if (wxStat( filename.fn_str() , &st) != 0)