| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: src/common/filename.cpp |
| 3 | // Purpose: wxFileName - encapsulates a file path |
| 4 | // Author: Robert Roebling, Vadim Zeitlin |
| 5 | // Modified by: |
| 6 | // Created: 28.12.2000 |
| 7 | // Copyright: (c) 2000 Robert Roebling |
| 8 | // Licence: wxWindows licence |
| 9 | ///////////////////////////////////////////////////////////////////////////// |
| 10 | |
| 11 | /* |
| 12 | Here are brief descriptions of the filename formats supported by this class: |
| 13 | |
| 14 | wxPATH_UNIX: standard Unix format, used under Darwin as well, absolute file |
| 15 | names have the form: |
| 16 | /dir1/dir2/.../dirN/filename, "." and ".." stand for the |
| 17 | current and parent directory respectively, "~" is parsed as the |
| 18 | user HOME and "~username" as the HOME of that user |
| 19 | |
| 20 | wxPATH_DOS: DOS/Windows format, absolute file names have the form: |
| 21 | drive:\dir1\dir2\...\dirN\filename.ext where drive is a single |
| 22 | letter. "." and ".." as for Unix but no "~". |
| 23 | |
| 24 | There are also UNC names of the form \\share\fullpath and |
| 25 | MSW unique volume names of the form \\?\Volume{GUID}\fullpath. |
| 26 | |
| 27 | The latter provide a uniform way to access a volume regardless of |
| 28 | its current mount point, i.e. you can change a volume's mount |
| 29 | point from D: to E:, or even remove it, and still be able to |
| 30 | access it through its unique volume name. More on the subject can |
| 31 | be found in MSDN's article "Naming a Volume" that is currently at |
| 32 | http://msdn.microsoft.com/en-us/library/aa365248(VS.85).aspx. |
| 33 | |
| 34 | |
| 35 | wxPATH_MAC: Mac OS 8/9 only, not used any longer, absolute file |
| 36 | names have the form |
| 37 | volume:dir1:...:dirN:filename |
| 38 | and the relative file names are either |
| 39 | :dir1:...:dirN:filename |
| 40 | or just |
| 41 | filename |
| 42 | (although :filename works as well). |
| 43 | Since the volume is just part of the file path, it is not |
| 44 | treated like a separate entity as it is done under DOS and |
| 45 | VMS, it is just treated as another dir. |
| 46 | |
| 47 | wxPATH_VMS: VMS native format, absolute file names have the form |
| 48 | <device>:[dir1.dir2.dir3]file.txt |
| 49 | or |
| 50 | <device>:[000000.dir1.dir2.dir3]file.txt |
| 51 | |
| 52 | the <device> is the physical device (i.e. disk). 000000 is the |
| 53 | root directory on the device which can be omitted. |
| 54 | |
| 55 | Note that VMS uses different separators unlike Unix: |
| 56 | : always after the device. If the path does not contain : than |
| 57 | the default (the device of the current directory) is assumed. |
| 58 | [ start of directory specification |
| 59 | . separator between directory and subdirectory |
| 60 | ] between directory and file |
| 61 | */ |
| 62 | |
| 63 | // ============================================================================ |
| 64 | // declarations |
| 65 | // ============================================================================ |
| 66 | |
| 67 | // ---------------------------------------------------------------------------- |
| 68 | // headers |
| 69 | // ---------------------------------------------------------------------------- |
| 70 | |
| 71 | // For compilers that support precompilation, includes "wx.h". |
| 72 | #include "wx/wxprec.h" |
| 73 | |
| 74 | #ifdef __BORLANDC__ |
| 75 | #pragma hdrstop |
| 76 | #endif |
| 77 | |
| 78 | #ifndef WX_PRECOMP |
| 79 | #ifdef __WINDOWS__ |
| 80 | #include "wx/msw/wrapwin.h" // For GetShort/LongPathName |
| 81 | #endif |
| 82 | #include "wx/dynarray.h" |
| 83 | #include "wx/intl.h" |
| 84 | #include "wx/log.h" |
| 85 | #include "wx/utils.h" |
| 86 | #include "wx/crt.h" |
| 87 | #endif |
| 88 | |
| 89 | #include "wx/filename.h" |
| 90 | #include "wx/private/filename.h" |
| 91 | #include "wx/tokenzr.h" |
| 92 | #include "wx/config.h" // for wxExpandEnvVars |
| 93 | #include "wx/dynlib.h" |
| 94 | #include "wx/dir.h" |
| 95 | #include "wx/longlong.h" |
| 96 | |
| 97 | #if defined(__WIN32__) && defined(__MINGW32__) |
| 98 | #include "wx/msw/gccpriv.h" |
| 99 | #endif |
| 100 | |
| 101 | #ifdef __WINDOWS__ |
| 102 | #include "wx/msw/private.h" |
| 103 | #include <shlobj.h> // for CLSID_ShellLink |
| 104 | #include "wx/msw/missing.h" |
| 105 | #endif |
| 106 | |
| 107 | #if defined(__WXMAC__) |
| 108 | #include "wx/osx/private.h" // includes mac headers |
| 109 | #endif |
| 110 | |
| 111 | // utime() is POSIX so should normally be available on all Unices |
| 112 | #ifdef __UNIX_LIKE__ |
| 113 | #include <sys/types.h> |
| 114 | #include <utime.h> |
| 115 | #include <sys/stat.h> |
| 116 | #include <unistd.h> |
| 117 | #endif |
| 118 | |
| 119 | #ifdef __DJGPP__ |
| 120 | #include <unistd.h> |
| 121 | #endif |
| 122 | |
| 123 | #ifdef __WATCOMC__ |
| 124 | #include <io.h> |
| 125 | #include <sys/utime.h> |
| 126 | #include <sys/stat.h> |
| 127 | #endif |
| 128 | |
| 129 | #ifdef __VISAGECPP__ |
| 130 | #ifndef MAX_PATH |
| 131 | #define MAX_PATH 256 |
| 132 | #endif |
| 133 | #endif |
| 134 | |
| 135 | #ifdef __EMX__ |
| 136 | #include <os2.h> |
| 137 | #define MAX_PATH _MAX_PATH |
| 138 | #endif |
| 139 | |
| 140 | #ifndef S_ISREG |
| 141 | #define S_ISREG(mode) ((mode) & S_IFREG) |
| 142 | #endif |
| 143 | #ifndef S_ISDIR |
| 144 | #define S_ISDIR(mode) ((mode) & S_IFDIR) |
| 145 | #endif |
| 146 | |
| 147 | #if wxUSE_LONGLONG |
| 148 | extern const wxULongLong wxInvalidSize = (unsigned)-1; |
| 149 | #endif // wxUSE_LONGLONG |
| 150 | |
| 151 | namespace |
| 152 | { |
| 153 | |
| 154 | // ---------------------------------------------------------------------------- |
| 155 | // private classes |
| 156 | // ---------------------------------------------------------------------------- |
| 157 | |
| 158 | // small helper class which opens and closes the file - we use it just to get |
| 159 | // a file handle for the given file name to pass it to some Win32 API function |
| 160 | #if defined(__WIN32__) && !defined(__WXMICROWIN__) |
| 161 | |
| 162 | class wxFileHandle |
| 163 | { |
| 164 | public: |
| 165 | enum OpenMode |
| 166 | { |
| 167 | ReadAttr, |
| 168 | WriteAttr |
| 169 | }; |
| 170 | |
| 171 | wxFileHandle(const wxString& filename, OpenMode mode, int flags = 0) |
| 172 | { |
| 173 | // be careful and use FILE_{READ,WRITE}_ATTRIBUTES here instead of the |
| 174 | // usual GENERIC_{READ,WRITE} as we don't want the file access time to |
| 175 | // be changed when we open it because this class is used for setting |
| 176 | // access time (see #10567) |
| 177 | m_hFile = ::CreateFile |
| 178 | ( |
| 179 | filename.t_str(), // name |
| 180 | mode == ReadAttr ? FILE_READ_ATTRIBUTES // access mask |
| 181 | : FILE_WRITE_ATTRIBUTES, |
| 182 | FILE_SHARE_READ | // sharing mode |
| 183 | FILE_SHARE_WRITE, // (allow everything) |
| 184 | NULL, // no secutity attr |
| 185 | OPEN_EXISTING, // creation disposition |
| 186 | flags, // flags |
| 187 | NULL // no template file |
| 188 | ); |
| 189 | |
| 190 | if ( m_hFile == INVALID_HANDLE_VALUE ) |
| 191 | { |
| 192 | if ( mode == ReadAttr ) |
| 193 | { |
| 194 | wxLogSysError(_("Failed to open '%s' for reading"), |
| 195 | filename.c_str()); |
| 196 | } |
| 197 | else |
| 198 | { |
| 199 | wxLogSysError(_("Failed to open '%s' for writing"), |
| 200 | filename.c_str()); |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | ~wxFileHandle() |
| 206 | { |
| 207 | if ( m_hFile != INVALID_HANDLE_VALUE ) |
| 208 | { |
| 209 | if ( !::CloseHandle(m_hFile) ) |
| 210 | { |
| 211 | wxLogSysError(_("Failed to close file handle")); |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | // return true only if the file could be opened successfully |
| 217 | bool IsOk() const { return m_hFile != INVALID_HANDLE_VALUE; } |
| 218 | |
| 219 | // get the handle |
| 220 | operator HANDLE() const { return m_hFile; } |
| 221 | |
| 222 | private: |
| 223 | HANDLE m_hFile; |
| 224 | }; |
| 225 | |
| 226 | #endif // __WIN32__ |
| 227 | |
| 228 | // ---------------------------------------------------------------------------- |
| 229 | // private functions |
| 230 | // ---------------------------------------------------------------------------- |
| 231 | |
| 232 | #if wxUSE_DATETIME && defined(__WIN32__) && !defined(__WXMICROWIN__) |
| 233 | |
| 234 | // Convert between wxDateTime and FILETIME which is a 64-bit value representing |
| 235 | // the number of 100-nanosecond intervals since January 1, 1601 UTC. |
| 236 | // |
| 237 | // This is the offset between FILETIME epoch and the Unix/wxDateTime Epoch. |
| 238 | static wxInt64 EPOCH_OFFSET_IN_MSEC = wxLL(11644473600000); |
| 239 | |
| 240 | static void ConvertFileTimeToWx(wxDateTime *dt, const FILETIME &ft) |
| 241 | { |
| 242 | wxLongLong t(ft.dwHighDateTime, ft.dwLowDateTime); |
| 243 | t /= 10000; // Convert hundreds of nanoseconds to milliseconds. |
| 244 | t -= EPOCH_OFFSET_IN_MSEC; |
| 245 | |
| 246 | *dt = wxDateTime(t); |
| 247 | } |
| 248 | |
| 249 | static void ConvertWxToFileTime(FILETIME *ft, const wxDateTime& dt) |
| 250 | { |
| 251 | // Undo the conversions above. |
| 252 | wxLongLong t(dt.GetValue()); |
| 253 | t += EPOCH_OFFSET_IN_MSEC; |
| 254 | t *= 10000; |
| 255 | |
| 256 | ft->dwHighDateTime = t.GetHi(); |
| 257 | ft->dwLowDateTime = t.GetLo(); |
| 258 | } |
| 259 | |
| 260 | #endif // wxUSE_DATETIME && __WIN32__ |
| 261 | |
| 262 | // return a string with the volume par |
| 263 | static wxString wxGetVolumeString(const wxString& volume, wxPathFormat format) |
| 264 | { |
| 265 | wxString path; |
| 266 | |
| 267 | if ( !volume.empty() ) |
| 268 | { |
| 269 | format = wxFileName::GetFormat(format); |
| 270 | |
| 271 | // Special Windows UNC paths hack, part 2: undo what we did in |
| 272 | // SplitPath() and make an UNC path if we have a drive which is not a |
| 273 | // single letter (hopefully the network shares can't be one letter only |
| 274 | // although I didn't find any authoritative docs on this) |
| 275 | if ( format == wxPATH_DOS && volume.length() > 1 ) |
| 276 | { |
| 277 | // We also have to check for Windows unique volume names here and |
| 278 | // return it with '\\?\' prepended to it |
| 279 | if ( wxFileName::IsMSWUniqueVolumeNamePath("\\\\?\\" + volume + "\\", |
| 280 | format) ) |
| 281 | { |
| 282 | path << "\\\\?\\" << volume; |
| 283 | } |
| 284 | else |
| 285 | { |
| 286 | // it must be a UNC path |
| 287 | path << wxFILE_SEP_PATH_DOS << wxFILE_SEP_PATH_DOS << volume; |
| 288 | } |
| 289 | } |
| 290 | else if ( format == wxPATH_DOS || format == wxPATH_VMS ) |
| 291 | { |
| 292 | path << volume << wxFileName::GetVolumeSeparator(format); |
| 293 | } |
| 294 | // else ignore |
| 295 | } |
| 296 | |
| 297 | return path; |
| 298 | } |
| 299 | |
| 300 | // return true if the character is a DOS path separator i.e. either a slash or |
| 301 | // a backslash |
| 302 | inline bool IsDOSPathSep(wxUniChar ch) |
| 303 | { |
| 304 | return ch == wxFILE_SEP_PATH_DOS || ch == wxFILE_SEP_PATH_UNIX; |
| 305 | } |
| 306 | |
| 307 | // return true if the format used is the DOS/Windows one and the string looks |
| 308 | // like a UNC path |
| 309 | static bool IsUNCPath(const wxString& path, wxPathFormat format) |
| 310 | { |
| 311 | return format == wxPATH_DOS && |
| 312 | path.length() >= 4 && // "\\a" can't be a UNC path |
| 313 | IsDOSPathSep(path[0u]) && |
| 314 | IsDOSPathSep(path[1u]) && |
| 315 | !IsDOSPathSep(path[2u]); |
| 316 | } |
| 317 | |
| 318 | #ifndef __WIN32__ |
| 319 | |
| 320 | // Under Unix-ish systems (basically everything except Windows) we may work |
| 321 | // either with the file itself or its target if it's a symbolic link and we |
| 322 | // should dereference it, as determined by wxFileName::ShouldFollowLink() and |
| 323 | // the absence of the wxFILE_EXISTS_NO_FOLLOW flag. StatAny() can be used to |
| 324 | // stat the appropriate file with an extra twist that it also works when there |
| 325 | // is no wxFileName object at all, as is the case in static methods. |
| 326 | |
| 327 | // Private implementation, don't call directly, use one of the overloads below. |
| 328 | bool DoStatAny(wxStructStat& st, wxString path, bool dereference) |
| 329 | { |
| 330 | // We need to remove any trailing slashes from the path because they could |
| 331 | // interfere with the symlink following decision: even if we use lstat(), |
| 332 | // it would still follow the symlink if we pass it a path with a slash at |
| 333 | // the end because the symlink resolution would happen while following the |
| 334 | // path and not for the last path element itself. |
| 335 | |
| 336 | while ( wxEndsWithPathSeparator(path) ) |
| 337 | { |
| 338 | const size_t posLast = path.length() - 1; |
| 339 | if ( !posLast ) |
| 340 | { |
| 341 | // Don't turn "/" into empty string. |
| 342 | break; |
| 343 | } |
| 344 | |
| 345 | path.erase(posLast); |
| 346 | } |
| 347 | |
| 348 | int ret = dereference ? wxStat(path, &st) : wxLstat(path, &st); |
| 349 | return ret == 0; |
| 350 | } |
| 351 | |
| 352 | // Overloads to use for a case when we don't have wxFileName object and when we |
| 353 | // do have one. |
| 354 | inline |
| 355 | bool StatAny(wxStructStat& st, const wxString& path, int flags) |
| 356 | { |
| 357 | return DoStatAny(st, path, !(flags & wxFILE_EXISTS_NO_FOLLOW)); |
| 358 | } |
| 359 | |
| 360 | inline |
| 361 | bool StatAny(wxStructStat& st, const wxFileName& fn) |
| 362 | { |
| 363 | return DoStatAny(st, fn.GetFullPath(), fn.ShouldFollowLink()); |
| 364 | } |
| 365 | |
| 366 | #endif // !__WIN32__ |
| 367 | |
| 368 | // ---------------------------------------------------------------------------- |
| 369 | // private constants |
| 370 | // ---------------------------------------------------------------------------- |
| 371 | |
| 372 | // length of \\?\Volume{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}\ string |
| 373 | static const size_t wxMSWUniqueVolumePrefixLength = 49; |
| 374 | |
| 375 | } // anonymous namespace |
| 376 | |
| 377 | // ============================================================================ |
| 378 | // implementation |
| 379 | // ============================================================================ |
| 380 | |
| 381 | // ---------------------------------------------------------------------------- |
| 382 | // wxFileName construction |
| 383 | // ---------------------------------------------------------------------------- |
| 384 | |
| 385 | void wxFileName::Assign( const wxFileName &filepath ) |
| 386 | { |
| 387 | m_volume = filepath.GetVolume(); |
| 388 | m_dirs = filepath.GetDirs(); |
| 389 | m_name = filepath.GetName(); |
| 390 | m_ext = filepath.GetExt(); |
| 391 | m_relative = filepath.m_relative; |
| 392 | m_hasExt = filepath.m_hasExt; |
| 393 | m_dontFollowLinks = filepath.m_dontFollowLinks; |
| 394 | } |
| 395 | |
| 396 | void wxFileName::Assign(const wxString& volume, |
| 397 | const wxString& path, |
| 398 | const wxString& name, |
| 399 | const wxString& ext, |
| 400 | bool hasExt, |
| 401 | wxPathFormat format) |
| 402 | { |
| 403 | // we should ignore paths which look like UNC shares because we already |
| 404 | // have the volume here and the UNC notation (\\server\path) is only valid |
| 405 | // for paths which don't start with a volume, so prevent SetPath() from |
| 406 | // recognizing "\\foo\bar" in "c:\\foo\bar" as an UNC path |
| 407 | // |
| 408 | // note also that this is a rather ugly way to do what we want (passing |
| 409 | // some kind of flag telling to ignore UNC paths to SetPath() would be |
| 410 | // better) but this is the safest thing to do to avoid breaking backwards |
| 411 | // compatibility in 2.8 |
| 412 | if ( IsUNCPath(path, format) ) |
| 413 | { |
| 414 | // remove one of the 2 leading backslashes to ensure that it's not |
| 415 | // recognized as an UNC path by SetPath() |
| 416 | wxString pathNonUNC(path, 1, wxString::npos); |
| 417 | SetPath(pathNonUNC, format); |
| 418 | } |
| 419 | else // no UNC complications |
| 420 | { |
| 421 | SetPath(path, format); |
| 422 | } |
| 423 | |
| 424 | m_volume = volume; |
| 425 | m_ext = ext; |
| 426 | m_name = name; |
| 427 | |
| 428 | m_hasExt = hasExt; |
| 429 | } |
| 430 | |
| 431 | void wxFileName::SetPath( const wxString& pathOrig, wxPathFormat format ) |
| 432 | { |
| 433 | m_dirs.Clear(); |
| 434 | |
| 435 | if ( pathOrig.empty() ) |
| 436 | { |
| 437 | // no path at all |
| 438 | m_relative = true; |
| 439 | |
| 440 | return; |
| 441 | } |
| 442 | |
| 443 | format = GetFormat( format ); |
| 444 | |
| 445 | // 0) deal with possible volume part first |
| 446 | wxString volume, |
| 447 | path; |
| 448 | SplitVolume(pathOrig, &volume, &path, format); |
| 449 | if ( !volume.empty() ) |
| 450 | { |
| 451 | m_relative = false; |
| 452 | |
| 453 | SetVolume(volume); |
| 454 | } |
| 455 | |
| 456 | // 1) Determine if the path is relative or absolute. |
| 457 | |
| 458 | if ( path.empty() ) |
| 459 | { |
| 460 | // we had only the volume |
| 461 | return; |
| 462 | } |
| 463 | |
| 464 | wxChar leadingChar = path[0u]; |
| 465 | |
| 466 | switch (format) |
| 467 | { |
| 468 | case wxPATH_MAC: |
| 469 | m_relative = leadingChar == wxT(':'); |
| 470 | |
| 471 | // We then remove a leading ":". The reason is in our |
| 472 | // storage form for relative paths: |
| 473 | // ":dir:file.txt" actually means "./dir/file.txt" in |
| 474 | // DOS notation and should get stored as |
| 475 | // (relative) (dir) (file.txt) |
| 476 | // "::dir:file.txt" actually means "../dir/file.txt" |
| 477 | // stored as (relative) (..) (dir) (file.txt) |
| 478 | // This is important only for the Mac as an empty dir |
| 479 | // actually means <UP>, whereas under DOS, double |
| 480 | // slashes can be ignored: "\\\\" is the same as "\\". |
| 481 | if (m_relative) |
| 482 | path.erase( 0, 1 ); |
| 483 | break; |
| 484 | |
| 485 | case wxPATH_VMS: |
| 486 | // TODO: what is the relative path format here? |
| 487 | m_relative = false; |
| 488 | break; |
| 489 | |
| 490 | default: |
| 491 | wxFAIL_MSG( wxT("Unknown path format") ); |
| 492 | // !! Fall through !! |
| 493 | |
| 494 | case wxPATH_UNIX: |
| 495 | m_relative = leadingChar != wxT('/'); |
| 496 | break; |
| 497 | |
| 498 | case wxPATH_DOS: |
| 499 | m_relative = !IsPathSeparator(leadingChar, format); |
| 500 | break; |
| 501 | |
| 502 | } |
| 503 | |
| 504 | // 2) Break up the path into its members. If the original path |
| 505 | // was just "/" or "\\", m_dirs will be empty. We know from |
| 506 | // the m_relative field, if this means "nothing" or "root dir". |
| 507 | |
| 508 | wxStringTokenizer tn( path, GetPathSeparators(format) ); |
| 509 | |
| 510 | while ( tn.HasMoreTokens() ) |
| 511 | { |
| 512 | wxString token = tn.GetNextToken(); |
| 513 | |
| 514 | // Remove empty token under DOS and Unix, interpret them |
| 515 | // as .. under Mac. |
| 516 | if (token.empty()) |
| 517 | { |
| 518 | if (format == wxPATH_MAC) |
| 519 | m_dirs.Add( wxT("..") ); |
| 520 | // else ignore |
| 521 | } |
| 522 | else |
| 523 | { |
| 524 | m_dirs.Add( token ); |
| 525 | } |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | void wxFileName::Assign(const wxString& fullpath, |
| 530 | wxPathFormat format) |
| 531 | { |
| 532 | wxString volume, path, name, ext; |
| 533 | bool hasExt; |
| 534 | SplitPath(fullpath, &volume, &path, &name, &ext, &hasExt, format); |
| 535 | |
| 536 | Assign(volume, path, name, ext, hasExt, format); |
| 537 | } |
| 538 | |
| 539 | void wxFileName::Assign(const wxString& fullpathOrig, |
| 540 | const wxString& fullname, |
| 541 | wxPathFormat format) |
| 542 | { |
| 543 | // always recognize fullpath as directory, even if it doesn't end with a |
| 544 | // slash |
| 545 | wxString fullpath = fullpathOrig; |
| 546 | if ( !fullpath.empty() && !wxEndsWithPathSeparator(fullpath) ) |
| 547 | { |
| 548 | fullpath += GetPathSeparator(format); |
| 549 | } |
| 550 | |
| 551 | wxString volume, path, name, ext; |
| 552 | bool hasExt; |
| 553 | |
| 554 | // do some consistency checks: the name should be really just the filename |
| 555 | // and the path should be really just a path |
| 556 | wxString volDummy, pathDummy, nameDummy, extDummy; |
| 557 | |
| 558 | SplitPath(fullname, &volDummy, &pathDummy, &name, &ext, &hasExt, format); |
| 559 | |
| 560 | wxASSERT_MSG( volDummy.empty() && pathDummy.empty(), |
| 561 | wxT("the file name shouldn't contain the path") ); |
| 562 | |
| 563 | SplitPath(fullpath, &volume, &path, &nameDummy, &extDummy, format); |
| 564 | |
| 565 | #ifndef __VMS |
| 566 | // This test makes no sense on an OpenVMS system. |
| 567 | wxASSERT_MSG( nameDummy.empty() && extDummy.empty(), |
| 568 | wxT("the path shouldn't contain file name nor extension") ); |
| 569 | #endif |
| 570 | Assign(volume, path, name, ext, hasExt, format); |
| 571 | } |
| 572 | |
| 573 | void wxFileName::Assign(const wxString& pathOrig, |
| 574 | const wxString& name, |
| 575 | const wxString& ext, |
| 576 | wxPathFormat format) |
| 577 | { |
| 578 | wxString volume, |
| 579 | path; |
| 580 | SplitVolume(pathOrig, &volume, &path, format); |
| 581 | |
| 582 | Assign(volume, path, name, ext, format); |
| 583 | } |
| 584 | |
| 585 | void wxFileName::AssignDir(const wxString& dir, wxPathFormat format) |
| 586 | { |
| 587 | Assign(dir, wxEmptyString, format); |
| 588 | } |
| 589 | |
| 590 | void wxFileName::Clear() |
| 591 | { |
| 592 | m_dirs.clear(); |
| 593 | m_volume.clear(); |
| 594 | m_name.clear(); |
| 595 | m_ext.clear(); |
| 596 | |
| 597 | // we don't have any absolute path for now |
| 598 | m_relative = true; |
| 599 | |
| 600 | // nor any extension |
| 601 | m_hasExt = false; |
| 602 | |
| 603 | // follow symlinks by default |
| 604 | m_dontFollowLinks = false; |
| 605 | } |
| 606 | |
| 607 | /* static */ |
| 608 | wxFileName wxFileName::FileName(const wxString& file, wxPathFormat format) |
| 609 | { |
| 610 | return wxFileName(file, format); |
| 611 | } |
| 612 | |
| 613 | /* static */ |
| 614 | wxFileName wxFileName::DirName(const wxString& dir, wxPathFormat format) |
| 615 | { |
| 616 | wxFileName fn; |
| 617 | fn.AssignDir(dir, format); |
| 618 | return fn; |
| 619 | } |
| 620 | |
| 621 | // ---------------------------------------------------------------------------- |
| 622 | // existence tests |
| 623 | // ---------------------------------------------------------------------------- |
| 624 | |
| 625 | namespace |
| 626 | { |
| 627 | |
| 628 | #if defined(__WINDOWS__) && !defined(__WXMICROWIN__) |
| 629 | |
| 630 | void RemoveTrailingSeparatorsFromPath(wxString& strPath) |
| 631 | { |
| 632 | // Windows fails to find directory named "c:\dir\" even if "c:\dir" exists, |
| 633 | // so remove all trailing backslashes from the path - but don't do this for |
| 634 | // the paths "d:\" (which are different from "d:"), for just "\" or for |
| 635 | // windows unique volume names ("\\?\Volume{GUID}\") |
| 636 | while ( wxEndsWithPathSeparator( strPath ) ) |
| 637 | { |
| 638 | size_t len = strPath.length(); |
| 639 | if ( len == 1 || (len == 3 && strPath[len - 2] == wxT(':')) || |
| 640 | (len == wxMSWUniqueVolumePrefixLength && |
| 641 | wxFileName::IsMSWUniqueVolumeNamePath(strPath))) |
| 642 | { |
| 643 | break; |
| 644 | } |
| 645 | |
| 646 | strPath.Truncate(len - 1); |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | #endif // __WINDOWS__ || __OS2__ |
| 651 | |
| 652 | bool |
| 653 | wxFileSystemObjectExists(const wxString& path, int flags) |
| 654 | { |
| 655 | |
| 656 | // Should the existence of file/directory with this name be accepted, i.e. |
| 657 | // result in the true return value from this function? |
| 658 | const bool acceptFile = (flags & wxFILE_EXISTS_REGULAR) != 0; |
| 659 | const bool acceptDir = (flags & wxFILE_EXISTS_DIR) != 0; |
| 660 | |
| 661 | wxString strPath(path); |
| 662 | |
| 663 | #if defined(__WINDOWS__) && !defined(__WXMICROWIN__) |
| 664 | if ( acceptDir ) |
| 665 | { |
| 666 | // Ensure that the path doesn't have any trailing separators when |
| 667 | // checking for directories. |
| 668 | RemoveTrailingSeparatorsFromPath(strPath); |
| 669 | } |
| 670 | |
| 671 | // we must use GetFileAttributes() instead of the ANSI C functions because |
| 672 | // it can cope with network (UNC) paths unlike them |
| 673 | DWORD ret = ::GetFileAttributes(strPath.t_str()); |
| 674 | |
| 675 | if ( ret == INVALID_FILE_ATTRIBUTES ) |
| 676 | return false; |
| 677 | |
| 678 | if ( ret & FILE_ATTRIBUTE_DIRECTORY ) |
| 679 | return acceptDir; |
| 680 | |
| 681 | // Anything else must be a file (perhaps we should check for |
| 682 | // FILE_ATTRIBUTE_REPARSE_POINT?) |
| 683 | return acceptFile; |
| 684 | #elif defined(__OS2__) |
| 685 | if ( acceptDir ) |
| 686 | { |
| 687 | // OS/2 can't handle "d:", it wants either "d:\" or "d:." |
| 688 | if (strPath.length() == 2 && strPath[1u] == wxT(':')) |
| 689 | strPath << wxT('.'); |
| 690 | } |
| 691 | |
| 692 | FILESTATUS3 Info = {{0}}; |
| 693 | APIRET rc = ::DosQueryPathInfo((PSZ)(WXSTRINGCAST strPath), FIL_STANDARD, |
| 694 | (void*) &Info, sizeof(FILESTATUS3)); |
| 695 | |
| 696 | if ( rc == NO_ERROR ) |
| 697 | { |
| 698 | if ( Info.attrFile & FILE_DIRECTORY ) |
| 699 | return acceptDir; |
| 700 | else |
| 701 | return acceptFile; |
| 702 | } |
| 703 | |
| 704 | // We consider that the path must exist if we get a sharing violation for |
| 705 | // it but we don't know what is it in this case. |
| 706 | if ( rc == ERROR_SHARING_VIOLATION ) |
| 707 | return flags & wxFILE_EXISTS_ANY; |
| 708 | |
| 709 | // Any other error (usually ERROR_PATH_NOT_FOUND), means there is nothing |
| 710 | // there. |
| 711 | return false; |
| 712 | #else // Non-MSW, non-OS/2 |
| 713 | wxStructStat st; |
| 714 | if ( !StatAny(st, strPath, flags) ) |
| 715 | return false; |
| 716 | |
| 717 | if ( S_ISREG(st.st_mode) ) |
| 718 | return acceptFile; |
| 719 | if ( S_ISDIR(st.st_mode) ) |
| 720 | return acceptDir; |
| 721 | if ( S_ISLNK(st.st_mode) ) |
| 722 | { |
| 723 | // Take care to not test for "!= 0" here as this would erroneously |
| 724 | // return true if only wxFILE_EXISTS_NO_FOLLOW, which is part of |
| 725 | // wxFILE_EXISTS_SYMLINK, is set too. |
| 726 | return (flags & wxFILE_EXISTS_SYMLINK) == wxFILE_EXISTS_SYMLINK; |
| 727 | } |
| 728 | if ( S_ISBLK(st.st_mode) || S_ISCHR(st.st_mode) ) |
| 729 | return (flags & wxFILE_EXISTS_DEVICE) != 0; |
| 730 | if ( S_ISFIFO(st.st_mode) ) |
| 731 | return (flags & wxFILE_EXISTS_FIFO) != 0; |
| 732 | if ( S_ISSOCK(st.st_mode) ) |
| 733 | return (flags & wxFILE_EXISTS_SOCKET) != 0; |
| 734 | |
| 735 | return flags & wxFILE_EXISTS_ANY; |
| 736 | #endif // Platforms |
| 737 | } |
| 738 | |
| 739 | } // anonymous namespace |
| 740 | |
| 741 | bool wxFileName::FileExists() const |
| 742 | { |
| 743 | int flags = wxFILE_EXISTS_REGULAR; |
| 744 | if ( !ShouldFollowLink() ) |
| 745 | flags |= wxFILE_EXISTS_NO_FOLLOW; |
| 746 | |
| 747 | return wxFileSystemObjectExists(GetFullPath(), flags); |
| 748 | } |
| 749 | |
| 750 | /* static */ |
| 751 | bool wxFileName::FileExists( const wxString &filePath ) |
| 752 | { |
| 753 | return wxFileSystemObjectExists(filePath, wxFILE_EXISTS_REGULAR); |
| 754 | } |
| 755 | |
| 756 | bool wxFileName::DirExists() const |
| 757 | { |
| 758 | int flags = wxFILE_EXISTS_DIR; |
| 759 | if ( !ShouldFollowLink() ) |
| 760 | flags |= wxFILE_EXISTS_NO_FOLLOW; |
| 761 | |
| 762 | return Exists(GetPath(), flags); |
| 763 | } |
| 764 | |
| 765 | /* static */ |
| 766 | bool wxFileName::DirExists( const wxString &dirPath ) |
| 767 | { |
| 768 | return wxFileSystemObjectExists(dirPath, wxFILE_EXISTS_DIR); |
| 769 | } |
| 770 | |
| 771 | bool wxFileName::Exists(int flags) const |
| 772 | { |
| 773 | // Notice that wxFILE_EXISTS_NO_FOLLOW may be specified in the flags even |
| 774 | // if our DontFollowLink() hadn't been called and we do honour it then. But |
| 775 | // if the user took the care of calling DontFollowLink(), it is always |
| 776 | // taken into account. |
| 777 | if ( !ShouldFollowLink() ) |
| 778 | flags |= wxFILE_EXISTS_NO_FOLLOW; |
| 779 | |
| 780 | return wxFileSystemObjectExists(GetFullPath(), flags); |
| 781 | } |
| 782 | |
| 783 | /* static */ |
| 784 | bool wxFileName::Exists(const wxString& path, int flags) |
| 785 | { |
| 786 | return wxFileSystemObjectExists(path, flags); |
| 787 | } |
| 788 | |
| 789 | // ---------------------------------------------------------------------------- |
| 790 | // CWD and HOME stuff |
| 791 | // ---------------------------------------------------------------------------- |
| 792 | |
| 793 | void wxFileName::AssignCwd(const wxString& volume) |
| 794 | { |
| 795 | AssignDir(wxFileName::GetCwd(volume)); |
| 796 | } |
| 797 | |
| 798 | /* static */ |
| 799 | wxString wxFileName::GetCwd(const wxString& volume) |
| 800 | { |
| 801 | // if we have the volume, we must get the current directory on this drive |
| 802 | // and to do this we have to chdir to this volume - at least under Windows, |
| 803 | // I don't know how to get the current drive on another volume elsewhere |
| 804 | // (TODO) |
| 805 | wxString cwdOld; |
| 806 | if ( !volume.empty() ) |
| 807 | { |
| 808 | cwdOld = wxGetCwd(); |
| 809 | SetCwd(volume + GetVolumeSeparator()); |
| 810 | } |
| 811 | |
| 812 | wxString cwd = ::wxGetCwd(); |
| 813 | |
| 814 | if ( !volume.empty() ) |
| 815 | { |
| 816 | SetCwd(cwdOld); |
| 817 | } |
| 818 | |
| 819 | return cwd; |
| 820 | } |
| 821 | |
| 822 | bool wxFileName::SetCwd() const |
| 823 | { |
| 824 | return wxFileName::SetCwd( GetPath() ); |
| 825 | } |
| 826 | |
| 827 | bool wxFileName::SetCwd( const wxString &cwd ) |
| 828 | { |
| 829 | return ::wxSetWorkingDirectory( cwd ); |
| 830 | } |
| 831 | |
| 832 | void wxFileName::AssignHomeDir() |
| 833 | { |
| 834 | AssignDir(wxFileName::GetHomeDir()); |
| 835 | } |
| 836 | |
| 837 | wxString wxFileName::GetHomeDir() |
| 838 | { |
| 839 | return ::wxGetHomeDir(); |
| 840 | } |
| 841 | |
| 842 | |
| 843 | // ---------------------------------------------------------------------------- |
| 844 | // CreateTempFileName |
| 845 | // ---------------------------------------------------------------------------- |
| 846 | |
| 847 | #if wxUSE_FILE || wxUSE_FFILE |
| 848 | |
| 849 | |
| 850 | #if !defined wx_fdopen && defined HAVE_FDOPEN |
| 851 | #define wx_fdopen fdopen |
| 852 | #endif |
| 853 | |
| 854 | // NB: GetTempFileName() under Windows creates the file, so using |
| 855 | // O_EXCL there would fail |
| 856 | #ifdef __WINDOWS__ |
| 857 | #define wxOPEN_EXCL 0 |
| 858 | #else |
| 859 | #define wxOPEN_EXCL O_EXCL |
| 860 | #endif |
| 861 | |
| 862 | |
| 863 | #ifdef wxOpenOSFHandle |
| 864 | #define WX_HAVE_DELETE_ON_CLOSE |
| 865 | // On Windows create a file with the FILE_FLAGS_DELETE_ON_CLOSE flags. |
| 866 | // |
| 867 | static int wxOpenWithDeleteOnClose(const wxString& filename) |
| 868 | { |
| 869 | DWORD access = GENERIC_READ | GENERIC_WRITE; |
| 870 | |
| 871 | DWORD disposition = OPEN_ALWAYS; |
| 872 | |
| 873 | DWORD attributes = FILE_ATTRIBUTE_TEMPORARY | |
| 874 | FILE_FLAG_DELETE_ON_CLOSE; |
| 875 | |
| 876 | HANDLE h = ::CreateFile(filename.t_str(), access, 0, NULL, |
| 877 | disposition, attributes, NULL); |
| 878 | |
| 879 | return wxOpenOSFHandle(h, wxO_BINARY); |
| 880 | } |
| 881 | #endif // wxOpenOSFHandle |
| 882 | |
| 883 | |
| 884 | // Helper to open the file |
| 885 | // |
| 886 | static int wxTempOpen(const wxString& path, bool *deleteOnClose) |
| 887 | { |
| 888 | #ifdef WX_HAVE_DELETE_ON_CLOSE |
| 889 | if (*deleteOnClose) |
| 890 | return wxOpenWithDeleteOnClose(path); |
| 891 | #endif |
| 892 | |
| 893 | *deleteOnClose = false; |
| 894 | |
| 895 | return wxOpen(path, wxO_BINARY | O_RDWR | O_CREAT | wxOPEN_EXCL, 0600); |
| 896 | } |
| 897 | |
| 898 | |
| 899 | #if wxUSE_FFILE |
| 900 | // Helper to open the file and attach it to the wxFFile |
| 901 | // |
| 902 | static bool wxTempOpen(wxFFile *file, const wxString& path, bool *deleteOnClose) |
| 903 | { |
| 904 | #ifndef wx_fdopen |
| 905 | *deleteOnClose = false; |
| 906 | return file->Open(path, wxT("w+b")); |
| 907 | #else // wx_fdopen |
| 908 | int fd = wxTempOpen(path, deleteOnClose); |
| 909 | if (fd == -1) |
| 910 | return false; |
| 911 | file->Attach(wx_fdopen(fd, "w+b"), path); |
| 912 | return file->IsOpened(); |
| 913 | #endif // wx_fdopen |
| 914 | } |
| 915 | #endif // wxUSE_FFILE |
| 916 | |
| 917 | |
| 918 | #if !wxUSE_FILE |
| 919 | #define WXFILEARGS(x, y) y |
| 920 | #elif !wxUSE_FFILE |
| 921 | #define WXFILEARGS(x, y) x |
| 922 | #else |
| 923 | #define WXFILEARGS(x, y) x, y |
| 924 | #endif |
| 925 | |
| 926 | |
| 927 | // Implementation of wxFileName::CreateTempFileName(). |
| 928 | // |
| 929 | static wxString wxCreateTempImpl( |
| 930 | const wxString& prefix, |
| 931 | WXFILEARGS(wxFile *fileTemp, wxFFile *ffileTemp), |
| 932 | bool *deleteOnClose = NULL) |
| 933 | { |
| 934 | #if wxUSE_FILE && wxUSE_FFILE |
| 935 | wxASSERT(fileTemp == NULL || ffileTemp == NULL); |
| 936 | #endif |
| 937 | wxString path, dir, name; |
| 938 | bool wantDeleteOnClose = false; |
| 939 | |
| 940 | if (deleteOnClose) |
| 941 | { |
| 942 | // set the result to false initially |
| 943 | wantDeleteOnClose = *deleteOnClose; |
| 944 | *deleteOnClose = false; |
| 945 | } |
| 946 | else |
| 947 | { |
| 948 | // easier if it alwasys points to something |
| 949 | deleteOnClose = &wantDeleteOnClose; |
| 950 | } |
| 951 | |
| 952 | // use the directory specified by the prefix |
| 953 | wxFileName::SplitPath(prefix, &dir, &name, NULL /* extension */); |
| 954 | |
| 955 | if (dir.empty()) |
| 956 | { |
| 957 | dir = wxFileName::GetTempDir(); |
| 958 | } |
| 959 | |
| 960 | #if defined(__WXWINCE__) |
| 961 | path = dir + wxT("\\") + name; |
| 962 | int i = 1; |
| 963 | while (wxFileName::FileExists(path)) |
| 964 | { |
| 965 | path = dir + wxT("\\") + name ; |
| 966 | path << i; |
| 967 | i ++; |
| 968 | } |
| 969 | |
| 970 | #elif defined(__WINDOWS__) && !defined(__WXMICROWIN__) |
| 971 | if (!::GetTempFileName(dir.t_str(), name.t_str(), 0, |
| 972 | wxStringBuffer(path, MAX_PATH + 1))) |
| 973 | { |
| 974 | wxLogLastError(wxT("GetTempFileName")); |
| 975 | |
| 976 | path.clear(); |
| 977 | } |
| 978 | |
| 979 | #else // !Windows |
| 980 | path = dir; |
| 981 | |
| 982 | if ( !wxEndsWithPathSeparator(dir) && |
| 983 | (name.empty() || !wxIsPathSeparator(name[0u])) ) |
| 984 | { |
| 985 | path += wxFILE_SEP_PATH; |
| 986 | } |
| 987 | |
| 988 | path += name; |
| 989 | |
| 990 | #if defined(HAVE_MKSTEMP) |
| 991 | // scratch space for mkstemp() |
| 992 | path += wxT("XXXXXX"); |
| 993 | |
| 994 | // we need to copy the path to the buffer in which mkstemp() can modify it |
| 995 | wxCharBuffer buf(path.fn_str()); |
| 996 | |
| 997 | // cast is safe because the string length doesn't change |
| 998 | int fdTemp = mkstemp( (char*)(const char*) buf ); |
| 999 | if ( fdTemp == -1 ) |
| 1000 | { |
| 1001 | // this might be not necessary as mkstemp() on most systems should have |
| 1002 | // already done it but it doesn't hurt neither... |
| 1003 | path.clear(); |
| 1004 | } |
| 1005 | else // mkstemp() succeeded |
| 1006 | { |
| 1007 | path = wxConvFile.cMB2WX( (const char*) buf ); |
| 1008 | |
| 1009 | #if wxUSE_FILE |
| 1010 | // avoid leaking the fd |
| 1011 | if ( fileTemp ) |
| 1012 | { |
| 1013 | fileTemp->Attach(fdTemp); |
| 1014 | } |
| 1015 | else |
| 1016 | #endif |
| 1017 | |
| 1018 | #if wxUSE_FFILE |
| 1019 | if ( ffileTemp ) |
| 1020 | { |
| 1021 | #ifdef wx_fdopen |
| 1022 | ffileTemp->Attach(wx_fdopen(fdTemp, "r+b"), path); |
| 1023 | #else |
| 1024 | ffileTemp->Open(path, wxT("r+b")); |
| 1025 | close(fdTemp); |
| 1026 | #endif |
| 1027 | } |
| 1028 | else |
| 1029 | #endif |
| 1030 | |
| 1031 | { |
| 1032 | close(fdTemp); |
| 1033 | } |
| 1034 | } |
| 1035 | #else // !HAVE_MKSTEMP |
| 1036 | |
| 1037 | #ifdef HAVE_MKTEMP |
| 1038 | // same as above |
| 1039 | path += wxT("XXXXXX"); |
| 1040 | |
| 1041 | wxCharBuffer buf = wxConvFile.cWX2MB( path ); |
| 1042 | if ( !mktemp( (char*)(const char*) buf ) ) |
| 1043 | { |
| 1044 | path.clear(); |
| 1045 | } |
| 1046 | else |
| 1047 | { |
| 1048 | path = wxConvFile.cMB2WX( (const char*) buf ); |
| 1049 | } |
| 1050 | #else // !HAVE_MKTEMP (includes __DOS__) |
| 1051 | // generate the unique file name ourselves |
| 1052 | #if !defined(__DOS__) |
| 1053 | path << (unsigned int)getpid(); |
| 1054 | #endif |
| 1055 | |
| 1056 | wxString pathTry; |
| 1057 | |
| 1058 | static const size_t numTries = 1000; |
| 1059 | for ( size_t n = 0; n < numTries; n++ ) |
| 1060 | { |
| 1061 | // 3 hex digits is enough for numTries == 1000 < 4096 |
| 1062 | pathTry = path + wxString::Format(wxT("%.03x"), (unsigned int) n); |
| 1063 | if ( !wxFileName::FileExists(pathTry) ) |
| 1064 | { |
| 1065 | break; |
| 1066 | } |
| 1067 | |
| 1068 | pathTry.clear(); |
| 1069 | } |
| 1070 | |
| 1071 | path = pathTry; |
| 1072 | #endif // HAVE_MKTEMP/!HAVE_MKTEMP |
| 1073 | |
| 1074 | #endif // HAVE_MKSTEMP/!HAVE_MKSTEMP |
| 1075 | |
| 1076 | #endif // Windows/!Windows |
| 1077 | |
| 1078 | if ( path.empty() ) |
| 1079 | { |
| 1080 | wxLogSysError(_("Failed to create a temporary file name")); |
| 1081 | } |
| 1082 | else |
| 1083 | { |
| 1084 | bool ok = true; |
| 1085 | |
| 1086 | // open the file - of course, there is a race condition here, this is |
| 1087 | // why we always prefer using mkstemp()... |
| 1088 | #if wxUSE_FILE |
| 1089 | if ( fileTemp && !fileTemp->IsOpened() ) |
| 1090 | { |
| 1091 | *deleteOnClose = wantDeleteOnClose; |
| 1092 | int fd = wxTempOpen(path, deleteOnClose); |
| 1093 | if (fd != -1) |
| 1094 | fileTemp->Attach(fd); |
| 1095 | else |
| 1096 | ok = false; |
| 1097 | } |
| 1098 | #endif |
| 1099 | |
| 1100 | #if wxUSE_FFILE |
| 1101 | if ( ffileTemp && !ffileTemp->IsOpened() ) |
| 1102 | { |
| 1103 | *deleteOnClose = wantDeleteOnClose; |
| 1104 | ok = wxTempOpen(ffileTemp, path, deleteOnClose); |
| 1105 | } |
| 1106 | #endif |
| 1107 | |
| 1108 | if ( !ok ) |
| 1109 | { |
| 1110 | // FIXME: If !ok here should we loop and try again with another |
| 1111 | // file name? That is the standard recourse if open(O_EXCL) |
| 1112 | // fails, though of course it should be protected against |
| 1113 | // possible infinite looping too. |
| 1114 | |
| 1115 | wxLogError(_("Failed to open temporary file.")); |
| 1116 | |
| 1117 | path.clear(); |
| 1118 | } |
| 1119 | } |
| 1120 | |
| 1121 | return path; |
| 1122 | } |
| 1123 | |
| 1124 | |
| 1125 | static bool wxCreateTempImpl( |
| 1126 | const wxString& prefix, |
| 1127 | WXFILEARGS(wxFile *fileTemp, wxFFile *ffileTemp), |
| 1128 | wxString *name) |
| 1129 | { |
| 1130 | bool deleteOnClose = true; |
| 1131 | |
| 1132 | *name = wxCreateTempImpl(prefix, |
| 1133 | WXFILEARGS(fileTemp, ffileTemp), |
| 1134 | &deleteOnClose); |
| 1135 | |
| 1136 | bool ok = !name->empty(); |
| 1137 | |
| 1138 | if (deleteOnClose) |
| 1139 | name->clear(); |
| 1140 | #ifdef __UNIX__ |
| 1141 | else if (ok && wxRemoveFile(*name)) |
| 1142 | name->clear(); |
| 1143 | #endif |
| 1144 | |
| 1145 | return ok; |
| 1146 | } |
| 1147 | |
| 1148 | |
| 1149 | static void wxAssignTempImpl( |
| 1150 | wxFileName *fn, |
| 1151 | const wxString& prefix, |
| 1152 | WXFILEARGS(wxFile *fileTemp, wxFFile *ffileTemp)) |
| 1153 | { |
| 1154 | wxString tempname; |
| 1155 | tempname = wxCreateTempImpl(prefix, WXFILEARGS(fileTemp, ffileTemp)); |
| 1156 | |
| 1157 | if ( tempname.empty() ) |
| 1158 | { |
| 1159 | // error, failed to get temp file name |
| 1160 | fn->Clear(); |
| 1161 | } |
| 1162 | else // ok |
| 1163 | { |
| 1164 | fn->Assign(tempname); |
| 1165 | } |
| 1166 | } |
| 1167 | |
| 1168 | |
| 1169 | void wxFileName::AssignTempFileName(const wxString& prefix) |
| 1170 | { |
| 1171 | wxAssignTempImpl(this, prefix, WXFILEARGS(NULL, NULL)); |
| 1172 | } |
| 1173 | |
| 1174 | /* static */ |
| 1175 | wxString wxFileName::CreateTempFileName(const wxString& prefix) |
| 1176 | { |
| 1177 | return wxCreateTempImpl(prefix, WXFILEARGS(NULL, NULL)); |
| 1178 | } |
| 1179 | |
| 1180 | #endif // wxUSE_FILE || wxUSE_FFILE |
| 1181 | |
| 1182 | |
| 1183 | #if wxUSE_FILE |
| 1184 | |
| 1185 | wxString wxCreateTempFileName(const wxString& prefix, |
| 1186 | wxFile *fileTemp, |
| 1187 | bool *deleteOnClose) |
| 1188 | { |
| 1189 | return wxCreateTempImpl(prefix, WXFILEARGS(fileTemp, NULL), deleteOnClose); |
| 1190 | } |
| 1191 | |
| 1192 | bool wxCreateTempFile(const wxString& prefix, |
| 1193 | wxFile *fileTemp, |
| 1194 | wxString *name) |
| 1195 | { |
| 1196 | return wxCreateTempImpl(prefix, WXFILEARGS(fileTemp, NULL), name); |
| 1197 | } |
| 1198 | |
| 1199 | void wxFileName::AssignTempFileName(const wxString& prefix, wxFile *fileTemp) |
| 1200 | { |
| 1201 | wxAssignTempImpl(this, prefix, WXFILEARGS(fileTemp, NULL)); |
| 1202 | } |
| 1203 | |
| 1204 | /* static */ |
| 1205 | wxString |
| 1206 | wxFileName::CreateTempFileName(const wxString& prefix, wxFile *fileTemp) |
| 1207 | { |
| 1208 | return wxCreateTempFileName(prefix, fileTemp); |
| 1209 | } |
| 1210 | |
| 1211 | #endif // wxUSE_FILE |
| 1212 | |
| 1213 | |
| 1214 | #if wxUSE_FFILE |
| 1215 | |
| 1216 | wxString wxCreateTempFileName(const wxString& prefix, |
| 1217 | wxFFile *fileTemp, |
| 1218 | bool *deleteOnClose) |
| 1219 | { |
| 1220 | return wxCreateTempImpl(prefix, WXFILEARGS(NULL, fileTemp), deleteOnClose); |
| 1221 | } |
| 1222 | |
| 1223 | bool wxCreateTempFile(const wxString& prefix, |
| 1224 | wxFFile *fileTemp, |
| 1225 | wxString *name) |
| 1226 | { |
| 1227 | return wxCreateTempImpl(prefix, WXFILEARGS(NULL, fileTemp), name); |
| 1228 | |
| 1229 | } |
| 1230 | |
| 1231 | void wxFileName::AssignTempFileName(const wxString& prefix, wxFFile *fileTemp) |
| 1232 | { |
| 1233 | wxAssignTempImpl(this, prefix, WXFILEARGS(NULL, fileTemp)); |
| 1234 | } |
| 1235 | |
| 1236 | /* static */ |
| 1237 | wxString |
| 1238 | wxFileName::CreateTempFileName(const wxString& prefix, wxFFile *fileTemp) |
| 1239 | { |
| 1240 | return wxCreateTempFileName(prefix, fileTemp); |
| 1241 | } |
| 1242 | |
| 1243 | #endif // wxUSE_FFILE |
| 1244 | |
| 1245 | |
| 1246 | // ---------------------------------------------------------------------------- |
| 1247 | // directory operations |
| 1248 | // ---------------------------------------------------------------------------- |
| 1249 | |
| 1250 | // helper of GetTempDir(): check if the given directory exists and return it if |
| 1251 | // it does or an empty string otherwise |
| 1252 | namespace |
| 1253 | { |
| 1254 | |
| 1255 | wxString CheckIfDirExists(const wxString& dir) |
| 1256 | { |
| 1257 | return wxFileName::DirExists(dir) ? dir : wxString(); |
| 1258 | } |
| 1259 | |
| 1260 | } // anonymous namespace |
| 1261 | |
| 1262 | wxString wxFileName::GetTempDir() |
| 1263 | { |
| 1264 | // first try getting it from environment: this allows overriding the values |
| 1265 | // used by default if the user wants to create temporary files in another |
| 1266 | // directory |
| 1267 | wxString dir = CheckIfDirExists(wxGetenv("TMPDIR")); |
| 1268 | if ( dir.empty() ) |
| 1269 | { |
| 1270 | dir = CheckIfDirExists(wxGetenv("TMP")); |
| 1271 | if ( dir.empty() ) |
| 1272 | dir = CheckIfDirExists(wxGetenv("TEMP")); |
| 1273 | } |
| 1274 | |
| 1275 | // if no environment variables are set, use the system default |
| 1276 | if ( dir.empty() ) |
| 1277 | { |
| 1278 | #if defined(__WXWINCE__) |
| 1279 | dir = CheckIfDirExists(wxT("\\temp")); |
| 1280 | #elif defined(__WINDOWS__) && !defined(__WXMICROWIN__) |
| 1281 | if ( !::GetTempPath(MAX_PATH, wxStringBuffer(dir, MAX_PATH + 1)) ) |
| 1282 | { |
| 1283 | wxLogLastError(wxT("GetTempPath")); |
| 1284 | } |
| 1285 | #elif defined(__WXMAC__) && wxOSX_USE_CARBON |
| 1286 | dir = wxMacFindFolderNoSeparator(short(kOnSystemDisk), kTemporaryFolderType, kCreateFolder); |
| 1287 | #endif // systems with native way |
| 1288 | } |
| 1289 | else // we got directory from an environment variable |
| 1290 | { |
| 1291 | // remove any trailing path separators, we don't want to ever return |
| 1292 | // them from this function for consistency |
| 1293 | const size_t lastNonSep = dir.find_last_not_of(GetPathSeparators()); |
| 1294 | if ( lastNonSep == wxString::npos ) |
| 1295 | { |
| 1296 | // the string consists entirely of separators, leave only one |
| 1297 | dir = GetPathSeparator(); |
| 1298 | } |
| 1299 | else |
| 1300 | { |
| 1301 | dir.erase(lastNonSep + 1); |
| 1302 | } |
| 1303 | } |
| 1304 | |
| 1305 | // fall back to hard coded value |
| 1306 | if ( dir.empty() ) |
| 1307 | { |
| 1308 | #ifdef __UNIX_LIKE__ |
| 1309 | dir = CheckIfDirExists("/tmp"); |
| 1310 | if ( dir.empty() ) |
| 1311 | #endif // __UNIX_LIKE__ |
| 1312 | dir = "."; |
| 1313 | } |
| 1314 | |
| 1315 | return dir; |
| 1316 | } |
| 1317 | |
| 1318 | bool wxFileName::Mkdir( int perm, int flags ) const |
| 1319 | { |
| 1320 | return wxFileName::Mkdir(GetPath(), perm, flags); |
| 1321 | } |
| 1322 | |
| 1323 | bool wxFileName::Mkdir( const wxString& dir, int perm, int flags ) |
| 1324 | { |
| 1325 | if ( flags & wxPATH_MKDIR_FULL ) |
| 1326 | { |
| 1327 | // split the path in components |
| 1328 | wxFileName filename; |
| 1329 | filename.AssignDir(dir); |
| 1330 | |
| 1331 | wxString currPath; |
| 1332 | if ( filename.HasVolume()) |
| 1333 | { |
| 1334 | currPath << wxGetVolumeString(filename.GetVolume(), wxPATH_NATIVE); |
| 1335 | } |
| 1336 | |
| 1337 | wxArrayString dirs = filename.GetDirs(); |
| 1338 | size_t count = dirs.GetCount(); |
| 1339 | for ( size_t i = 0; i < count; i++ ) |
| 1340 | { |
| 1341 | if ( i > 0 || filename.IsAbsolute() ) |
| 1342 | currPath += wxFILE_SEP_PATH; |
| 1343 | currPath += dirs[i]; |
| 1344 | |
| 1345 | if (!DirExists(currPath)) |
| 1346 | { |
| 1347 | if (!wxMkdir(currPath, perm)) |
| 1348 | { |
| 1349 | // no need to try creating further directories |
| 1350 | return false; |
| 1351 | } |
| 1352 | } |
| 1353 | } |
| 1354 | |
| 1355 | return true; |
| 1356 | |
| 1357 | } |
| 1358 | |
| 1359 | return ::wxMkdir( dir, perm ); |
| 1360 | } |
| 1361 | |
| 1362 | bool wxFileName::Rmdir(int flags) const |
| 1363 | { |
| 1364 | return wxFileName::Rmdir( GetPath(), flags ); |
| 1365 | } |
| 1366 | |
| 1367 | bool wxFileName::Rmdir(const wxString& dir, int flags) |
| 1368 | { |
| 1369 | #ifdef __WINDOWS__ |
| 1370 | if ( flags & wxPATH_RMDIR_RECURSIVE ) |
| 1371 | { |
| 1372 | // SHFileOperation needs double null termination string |
| 1373 | // but without separator at the end of the path |
| 1374 | wxString path(dir); |
| 1375 | if ( path.Last() == wxFILE_SEP_PATH ) |
| 1376 | path.RemoveLast(); |
| 1377 | path += wxT('\0'); |
| 1378 | |
| 1379 | SHFILEOPSTRUCT fileop; |
| 1380 | wxZeroMemory(fileop); |
| 1381 | fileop.wFunc = FO_DELETE; |
| 1382 | fileop.pFrom = path.t_str(); |
| 1383 | fileop.fFlags = FOF_SILENT | FOF_NOCONFIRMATION; |
| 1384 | #ifndef __WXWINCE__ |
| 1385 | // FOF_NOERRORUI is not defined in WinCE |
| 1386 | fileop.fFlags |= FOF_NOERRORUI; |
| 1387 | #endif |
| 1388 | |
| 1389 | int ret = SHFileOperation(&fileop); |
| 1390 | if ( ret != 0 ) |
| 1391 | { |
| 1392 | // SHFileOperation may return non-Win32 error codes, so the error |
| 1393 | // message can be incorrect |
| 1394 | wxLogApiError(wxT("SHFileOperation"), ret); |
| 1395 | return false; |
| 1396 | } |
| 1397 | |
| 1398 | return true; |
| 1399 | } |
| 1400 | else if ( flags & wxPATH_RMDIR_FULL ) |
| 1401 | #else // !__WINDOWS__ |
| 1402 | if ( flags != 0 ) // wxPATH_RMDIR_FULL or wxPATH_RMDIR_RECURSIVE |
| 1403 | #endif // !__WINDOWS__ |
| 1404 | { |
| 1405 | #ifndef __WINDOWS__ |
| 1406 | if ( flags & wxPATH_RMDIR_RECURSIVE ) |
| 1407 | { |
| 1408 | // When deleting the tree recursively, we are supposed to delete |
| 1409 | // this directory itself even when it is a symlink -- but without |
| 1410 | // following it. Do it here as wxRmdir() would simply follow if |
| 1411 | // called for a symlink. |
| 1412 | if ( wxFileName::Exists(dir, wxFILE_EXISTS_SYMLINK) ) |
| 1413 | { |
| 1414 | return wxRemoveFile(dir); |
| 1415 | } |
| 1416 | } |
| 1417 | #endif // !__WINDOWS__ |
| 1418 | |
| 1419 | wxString path(dir); |
| 1420 | if ( path.Last() != wxFILE_SEP_PATH ) |
| 1421 | path += wxFILE_SEP_PATH; |
| 1422 | |
| 1423 | wxDir d(path); |
| 1424 | |
| 1425 | if ( !d.IsOpened() ) |
| 1426 | return false; |
| 1427 | |
| 1428 | wxString filename; |
| 1429 | |
| 1430 | // First delete all subdirectories: notice that we don't follow |
| 1431 | // symbolic links, potentially leading outside this directory, to avoid |
| 1432 | // unpleasant surprises. |
| 1433 | bool cont = d.GetFirst(&filename, wxString(), |
| 1434 | wxDIR_DIRS | wxDIR_HIDDEN | wxDIR_NO_FOLLOW); |
| 1435 | while ( cont ) |
| 1436 | { |
| 1437 | wxFileName::Rmdir(path + filename, flags); |
| 1438 | cont = d.GetNext(&filename); |
| 1439 | } |
| 1440 | |
| 1441 | #ifndef __WINDOWS__ |
| 1442 | if ( flags & wxPATH_RMDIR_RECURSIVE ) |
| 1443 | { |
| 1444 | // Delete all files too and, for the same reasons as above, don't |
| 1445 | // follow symlinks which could refer to the files outside of this |
| 1446 | // directory and just delete the symlinks themselves. |
| 1447 | cont = d.GetFirst(&filename, wxString(), |
| 1448 | wxDIR_FILES | wxDIR_HIDDEN | wxDIR_NO_FOLLOW); |
| 1449 | while ( cont ) |
| 1450 | { |
| 1451 | ::wxRemoveFile(path + filename); |
| 1452 | cont = d.GetNext(&filename); |
| 1453 | } |
| 1454 | } |
| 1455 | #endif // !__WINDOWS__ |
| 1456 | } |
| 1457 | |
| 1458 | return ::wxRmdir(dir); |
| 1459 | } |
| 1460 | |
| 1461 | // ---------------------------------------------------------------------------- |
| 1462 | // path normalization |
| 1463 | // ---------------------------------------------------------------------------- |
| 1464 | |
| 1465 | bool wxFileName::Normalize(int flags, |
| 1466 | const wxString& cwd, |
| 1467 | wxPathFormat format) |
| 1468 | { |
| 1469 | // deal with env vars renaming first as this may seriously change the path |
| 1470 | if ( flags & wxPATH_NORM_ENV_VARS ) |
| 1471 | { |
| 1472 | wxString pathOrig = GetFullPath(format); |
| 1473 | wxString path = wxExpandEnvVars(pathOrig); |
| 1474 | if ( path != pathOrig ) |
| 1475 | { |
| 1476 | Assign(path); |
| 1477 | } |
| 1478 | } |
| 1479 | |
| 1480 | // the existing path components |
| 1481 | wxArrayString dirs = GetDirs(); |
| 1482 | |
| 1483 | // the path to prepend in front to make the path absolute |
| 1484 | wxFileName curDir; |
| 1485 | |
| 1486 | format = GetFormat(format); |
| 1487 | |
| 1488 | // set up the directory to use for making the path absolute later |
| 1489 | if ( (flags & wxPATH_NORM_ABSOLUTE) && !IsAbsolute(format) ) |
| 1490 | { |
| 1491 | if ( cwd.empty() ) |
| 1492 | { |
| 1493 | curDir.AssignCwd(GetVolume()); |
| 1494 | } |
| 1495 | else // cwd provided |
| 1496 | { |
| 1497 | curDir.AssignDir(cwd); |
| 1498 | } |
| 1499 | } |
| 1500 | |
| 1501 | // handle ~ stuff under Unix only |
| 1502 | if ( (format == wxPATH_UNIX) && (flags & wxPATH_NORM_TILDE) && m_relative ) |
| 1503 | { |
| 1504 | if ( !dirs.IsEmpty() ) |
| 1505 | { |
| 1506 | wxString dir = dirs[0u]; |
| 1507 | if ( !dir.empty() && dir[0u] == wxT('~') ) |
| 1508 | { |
| 1509 | // to make the path absolute use the home directory |
| 1510 | curDir.AssignDir(wxGetUserHome(dir.c_str() + 1)); |
| 1511 | dirs.RemoveAt(0u); |
| 1512 | } |
| 1513 | } |
| 1514 | } |
| 1515 | |
| 1516 | // transform relative path into abs one |
| 1517 | if ( curDir.IsOk() ) |
| 1518 | { |
| 1519 | // this path may be relative because it doesn't have the volume name |
| 1520 | // and still have m_relative=true; in this case we shouldn't modify |
| 1521 | // our directory components but just set the current volume |
| 1522 | if ( !HasVolume() && curDir.HasVolume() ) |
| 1523 | { |
| 1524 | SetVolume(curDir.GetVolume()); |
| 1525 | |
| 1526 | if ( !m_relative ) |
| 1527 | { |
| 1528 | // yes, it was the case - we don't need curDir then |
| 1529 | curDir.Clear(); |
| 1530 | } |
| 1531 | } |
| 1532 | |
| 1533 | // finally, prepend curDir to the dirs array |
| 1534 | wxArrayString dirsNew = curDir.GetDirs(); |
| 1535 | WX_PREPEND_ARRAY(dirs, dirsNew); |
| 1536 | |
| 1537 | // if we used e.g. tilde expansion previously and wxGetUserHome didn't |
| 1538 | // return for some reason an absolute path, then curDir maybe not be absolute! |
| 1539 | if ( !curDir.m_relative ) |
| 1540 | { |
| 1541 | // we have prepended an absolute path and thus we are now an absolute |
| 1542 | // file name too |
| 1543 | m_relative = false; |
| 1544 | } |
| 1545 | // else if (flags & wxPATH_NORM_ABSOLUTE): |
| 1546 | // should we warn the user that we didn't manage to make the path absolute? |
| 1547 | } |
| 1548 | |
| 1549 | // now deal with ".", ".." and the rest |
| 1550 | m_dirs.Empty(); |
| 1551 | size_t count = dirs.GetCount(); |
| 1552 | for ( size_t n = 0; n < count; n++ ) |
| 1553 | { |
| 1554 | wxString dir = dirs[n]; |
| 1555 | |
| 1556 | if ( flags & wxPATH_NORM_DOTS ) |
| 1557 | { |
| 1558 | if ( dir == wxT(".") ) |
| 1559 | { |
| 1560 | // just ignore |
| 1561 | continue; |
| 1562 | } |
| 1563 | |
| 1564 | if ( dir == wxT("..") ) |
| 1565 | { |
| 1566 | if ( m_dirs.empty() ) |
| 1567 | { |
| 1568 | // We have more ".." than directory components so far. |
| 1569 | // Don't treat this as an error as the path could have been |
| 1570 | // entered by user so try to handle it reasonably: if the |
| 1571 | // path is absolute, just ignore the extra ".." because |
| 1572 | // "/.." is the same as "/". Otherwise, i.e. for relative |
| 1573 | // paths, keep ".." unchanged because removing it would |
| 1574 | // modify the file a relative path refers to. |
| 1575 | if ( !m_relative ) |
| 1576 | continue; |
| 1577 | |
| 1578 | } |
| 1579 | else // Normal case, go one step up. |
| 1580 | { |
| 1581 | m_dirs.pop_back(); |
| 1582 | continue; |
| 1583 | } |
| 1584 | } |
| 1585 | } |
| 1586 | |
| 1587 | m_dirs.Add(dir); |
| 1588 | } |
| 1589 | |
| 1590 | #if defined(__WIN32__) && !defined(__WXWINCE__) && wxUSE_OLE |
| 1591 | if ( (flags & wxPATH_NORM_SHORTCUT) ) |
| 1592 | { |
| 1593 | wxString filename; |
| 1594 | if (GetShortcutTarget(GetFullPath(format), filename)) |
| 1595 | { |
| 1596 | m_relative = false; |
| 1597 | Assign(filename); |
| 1598 | } |
| 1599 | } |
| 1600 | #endif |
| 1601 | |
| 1602 | #if defined(__WIN32__) |
| 1603 | if ( (flags & wxPATH_NORM_LONG) && (format == wxPATH_DOS) ) |
| 1604 | { |
| 1605 | Assign(GetLongPath()); |
| 1606 | } |
| 1607 | #endif // Win32 |
| 1608 | |
| 1609 | // Change case (this should be kept at the end of the function, to ensure |
| 1610 | // that the path doesn't change any more after we normalize its case) |
| 1611 | if ( (flags & wxPATH_NORM_CASE) && !IsCaseSensitive(format) ) |
| 1612 | { |
| 1613 | m_volume.MakeLower(); |
| 1614 | m_name.MakeLower(); |
| 1615 | m_ext.MakeLower(); |
| 1616 | |
| 1617 | // directory entries must be made lower case as well |
| 1618 | count = m_dirs.GetCount(); |
| 1619 | for ( size_t i = 0; i < count; i++ ) |
| 1620 | { |
| 1621 | m_dirs[i].MakeLower(); |
| 1622 | } |
| 1623 | } |
| 1624 | |
| 1625 | return true; |
| 1626 | } |
| 1627 | |
| 1628 | #ifndef __WXWINCE__ |
| 1629 | bool wxFileName::ReplaceEnvVariable(const wxString& envname, |
| 1630 | const wxString& replacementFmtString, |
| 1631 | wxPathFormat format) |
| 1632 | { |
| 1633 | // look into stringForm for the contents of the given environment variable |
| 1634 | wxString val; |
| 1635 | if (envname.empty() || |
| 1636 | !wxGetEnv(envname, &val)) |
| 1637 | return false; |
| 1638 | if (val.empty()) |
| 1639 | return false; |
| 1640 | |
| 1641 | wxString stringForm = GetPath(wxPATH_GET_VOLUME, format); |
| 1642 | // do not touch the file name and the extension |
| 1643 | |
| 1644 | wxString replacement = wxString::Format(replacementFmtString, envname); |
| 1645 | stringForm.Replace(val, replacement); |
| 1646 | |
| 1647 | // Now assign ourselves the modified path: |
| 1648 | Assign(stringForm, GetFullName(), format); |
| 1649 | |
| 1650 | return true; |
| 1651 | } |
| 1652 | #endif |
| 1653 | |
| 1654 | bool wxFileName::ReplaceHomeDir(wxPathFormat format) |
| 1655 | { |
| 1656 | wxString homedir = wxGetHomeDir(); |
| 1657 | if (homedir.empty()) |
| 1658 | return false; |
| 1659 | |
| 1660 | wxString stringForm = GetPath(wxPATH_GET_VOLUME, format); |
| 1661 | // do not touch the file name and the extension |
| 1662 | |
| 1663 | stringForm.Replace(homedir, "~"); |
| 1664 | |
| 1665 | // Now assign ourselves the modified path: |
| 1666 | Assign(stringForm, GetFullName(), format); |
| 1667 | |
| 1668 | return true; |
| 1669 | } |
| 1670 | |
| 1671 | // ---------------------------------------------------------------------------- |
| 1672 | // get the shortcut target |
| 1673 | // ---------------------------------------------------------------------------- |
| 1674 | |
| 1675 | // WinCE (3) doesn't have CLSID_ShellLink, IID_IShellLink definitions. |
| 1676 | // The .lnk file is a plain text file so it should be easy to |
| 1677 | // make it work. Hint from Google Groups: |
| 1678 | // "If you open up a lnk file, you'll see a |
| 1679 | // number, followed by a pound sign (#), followed by more text. The |
| 1680 | // number is the number of characters that follows the pound sign. The |
| 1681 | // characters after the pound sign are the command line (which _can_ |
| 1682 | // include arguments) to be executed. Any path (e.g. \windows\program |
| 1683 | // files\myapp.exe) that includes spaces needs to be enclosed in |
| 1684 | // quotation marks." |
| 1685 | |
| 1686 | #if defined(__WIN32__) && !defined(__WXWINCE__) && wxUSE_OLE |
| 1687 | |
| 1688 | bool wxFileName::GetShortcutTarget(const wxString& shortcutPath, |
| 1689 | wxString& targetFilename, |
| 1690 | wxString* arguments) const |
| 1691 | { |
| 1692 | wxString path, file, ext; |
| 1693 | wxFileName::SplitPath(shortcutPath, & path, & file, & ext); |
| 1694 | |
| 1695 | HRESULT hres; |
| 1696 | IShellLink* psl; |
| 1697 | bool success = false; |
| 1698 | |
| 1699 | // Assume it's not a shortcut if it doesn't end with lnk |
| 1700 | if (ext.CmpNoCase(wxT("lnk"))!=0) |
| 1701 | return false; |
| 1702 | |
| 1703 | // create a ShellLink object |
| 1704 | hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, |
| 1705 | IID_IShellLink, (LPVOID*) &psl); |
| 1706 | |
| 1707 | if (SUCCEEDED(hres)) |
| 1708 | { |
| 1709 | IPersistFile* ppf; |
| 1710 | hres = psl->QueryInterface( IID_IPersistFile, (LPVOID *) &ppf); |
| 1711 | if (SUCCEEDED(hres)) |
| 1712 | { |
| 1713 | WCHAR wsz[MAX_PATH]; |
| 1714 | |
| 1715 | MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, shortcutPath.mb_str(), -1, wsz, |
| 1716 | MAX_PATH); |
| 1717 | |
| 1718 | hres = ppf->Load(wsz, 0); |
| 1719 | ppf->Release(); |
| 1720 | |
| 1721 | if (SUCCEEDED(hres)) |
| 1722 | { |
| 1723 | wxChar buf[2048]; |
| 1724 | // Wrong prototype in early versions |
| 1725 | #if defined(__MINGW32__) && !wxCHECK_W32API_VERSION(2, 2) |
| 1726 | psl->GetPath((CHAR*) buf, 2048, NULL, SLGP_UNCPRIORITY); |
| 1727 | #else |
| 1728 | psl->GetPath(buf, 2048, NULL, SLGP_UNCPRIORITY); |
| 1729 | #endif |
| 1730 | targetFilename = wxString(buf); |
| 1731 | success = (shortcutPath != targetFilename); |
| 1732 | |
| 1733 | psl->GetArguments(buf, 2048); |
| 1734 | wxString args(buf); |
| 1735 | if (!args.empty() && arguments) |
| 1736 | { |
| 1737 | *arguments = args; |
| 1738 | } |
| 1739 | } |
| 1740 | } |
| 1741 | |
| 1742 | psl->Release(); |
| 1743 | } |
| 1744 | return success; |
| 1745 | } |
| 1746 | |
| 1747 | #endif // __WIN32__ && !__WXWINCE__ |
| 1748 | |
| 1749 | |
| 1750 | // ---------------------------------------------------------------------------- |
| 1751 | // absolute/relative paths |
| 1752 | // ---------------------------------------------------------------------------- |
| 1753 | |
| 1754 | bool wxFileName::IsAbsolute(wxPathFormat format) const |
| 1755 | { |
| 1756 | // unix paths beginning with ~ are reported as being absolute |
| 1757 | if ( format == wxPATH_UNIX ) |
| 1758 | { |
| 1759 | if ( !m_dirs.IsEmpty() ) |
| 1760 | { |
| 1761 | wxString dir = m_dirs[0u]; |
| 1762 | |
| 1763 | if (!dir.empty() && dir[0u] == wxT('~')) |
| 1764 | return true; |
| 1765 | } |
| 1766 | } |
| 1767 | |
| 1768 | // if our path doesn't start with a path separator, it's not an absolute |
| 1769 | // path |
| 1770 | if ( m_relative ) |
| 1771 | return false; |
| 1772 | |
| 1773 | if ( !GetVolumeSeparator(format).empty() ) |
| 1774 | { |
| 1775 | // this format has volumes and an absolute path must have one, it's not |
| 1776 | // enough to have the full path to be an absolute file under Windows |
| 1777 | if ( GetVolume().empty() ) |
| 1778 | return false; |
| 1779 | } |
| 1780 | |
| 1781 | return true; |
| 1782 | } |
| 1783 | |
| 1784 | bool wxFileName::MakeRelativeTo(const wxString& pathBase, wxPathFormat format) |
| 1785 | { |
| 1786 | wxFileName fnBase = wxFileName::DirName(pathBase, format); |
| 1787 | |
| 1788 | // get cwd only once - small time saving |
| 1789 | wxString cwd = wxGetCwd(); |
| 1790 | Normalize(wxPATH_NORM_ALL & ~wxPATH_NORM_CASE, cwd, format); |
| 1791 | fnBase.Normalize(wxPATH_NORM_ALL & ~wxPATH_NORM_CASE, cwd, format); |
| 1792 | |
| 1793 | bool withCase = IsCaseSensitive(format); |
| 1794 | |
| 1795 | // we can't do anything if the files live on different volumes |
| 1796 | if ( !GetVolume().IsSameAs(fnBase.GetVolume(), withCase) ) |
| 1797 | { |
| 1798 | // nothing done |
| 1799 | return false; |
| 1800 | } |
| 1801 | |
| 1802 | // same drive, so we don't need our volume |
| 1803 | m_volume.clear(); |
| 1804 | |
| 1805 | // remove common directories starting at the top |
| 1806 | while ( !m_dirs.IsEmpty() && !fnBase.m_dirs.IsEmpty() && |
| 1807 | m_dirs[0u].IsSameAs(fnBase.m_dirs[0u], withCase) ) |
| 1808 | { |
| 1809 | m_dirs.RemoveAt(0); |
| 1810 | fnBase.m_dirs.RemoveAt(0); |
| 1811 | } |
| 1812 | |
| 1813 | // add as many ".." as needed |
| 1814 | size_t count = fnBase.m_dirs.GetCount(); |
| 1815 | for ( size_t i = 0; i < count; i++ ) |
| 1816 | { |
| 1817 | m_dirs.Insert(wxT(".."), 0u); |
| 1818 | } |
| 1819 | |
| 1820 | if ( format == wxPATH_UNIX || format == wxPATH_DOS ) |
| 1821 | { |
| 1822 | // a directory made relative with respect to itself is '.' under Unix |
| 1823 | // and DOS, by definition (but we don't have to insert "./" for the |
| 1824 | // files) |
| 1825 | if ( m_dirs.IsEmpty() && IsDir() ) |
| 1826 | { |
| 1827 | m_dirs.Add(wxT('.')); |
| 1828 | } |
| 1829 | } |
| 1830 | |
| 1831 | m_relative = true; |
| 1832 | |
| 1833 | // we were modified |
| 1834 | return true; |
| 1835 | } |
| 1836 | |
| 1837 | // ---------------------------------------------------------------------------- |
| 1838 | // filename kind tests |
| 1839 | // ---------------------------------------------------------------------------- |
| 1840 | |
| 1841 | bool wxFileName::SameAs(const wxFileName& filepath, wxPathFormat format) const |
| 1842 | { |
| 1843 | wxFileName fn1 = *this, |
| 1844 | fn2 = filepath; |
| 1845 | |
| 1846 | // get cwd only once - small time saving |
| 1847 | wxString cwd = wxGetCwd(); |
| 1848 | fn1.Normalize(wxPATH_NORM_ALL | wxPATH_NORM_CASE, cwd, format); |
| 1849 | fn2.Normalize(wxPATH_NORM_ALL | wxPATH_NORM_CASE, cwd, format); |
| 1850 | |
| 1851 | if ( fn1.GetFullPath() == fn2.GetFullPath() ) |
| 1852 | return true; |
| 1853 | |
| 1854 | #if defined(__UNIX__) |
| 1855 | wxStructStat st1, st2; |
| 1856 | if ( StatAny(st1, fn1) && StatAny(st2, fn2) ) |
| 1857 | { |
| 1858 | if ( st1.st_ino == st2.st_ino && st1.st_dev == st2.st_dev ) |
| 1859 | return true; |
| 1860 | } |
| 1861 | //else: It's not an error if one or both files don't exist. |
| 1862 | #endif // defined __UNIX__ |
| 1863 | |
| 1864 | return false; |
| 1865 | } |
| 1866 | |
| 1867 | /* static */ |
| 1868 | bool wxFileName::IsCaseSensitive( wxPathFormat format ) |
| 1869 | { |
| 1870 | // only Unix filenames are truly case-sensitive |
| 1871 | return GetFormat(format) == wxPATH_UNIX; |
| 1872 | } |
| 1873 | |
| 1874 | /* static */ |
| 1875 | wxString wxFileName::GetForbiddenChars(wxPathFormat format) |
| 1876 | { |
| 1877 | // Inits to forbidden characters that are common to (almost) all platforms. |
| 1878 | wxString strForbiddenChars = wxT("*?"); |
| 1879 | |
| 1880 | // If asserts, wxPathFormat has been changed. In case of a new path format |
| 1881 | // addition, the following code might have to be updated. |
| 1882 | wxCOMPILE_TIME_ASSERT(wxPATH_MAX == 5, wxPathFormatChanged); |
| 1883 | switch ( GetFormat(format) ) |
| 1884 | { |
| 1885 | default : |
| 1886 | wxFAIL_MSG( wxT("Unknown path format") ); |
| 1887 | // !! Fall through !! |
| 1888 | |
| 1889 | case wxPATH_UNIX: |
| 1890 | break; |
| 1891 | |
| 1892 | case wxPATH_MAC: |
| 1893 | // On a Mac even names with * and ? are allowed (Tested with OS |
| 1894 | // 9.2.1 and OS X 10.2.5) |
| 1895 | strForbiddenChars.clear(); |
| 1896 | break; |
| 1897 | |
| 1898 | case wxPATH_DOS: |
| 1899 | strForbiddenChars += wxT("\\/:\"<>|"); |
| 1900 | break; |
| 1901 | |
| 1902 | case wxPATH_VMS: |
| 1903 | break; |
| 1904 | } |
| 1905 | |
| 1906 | return strForbiddenChars; |
| 1907 | } |
| 1908 | |
| 1909 | /* static */ |
| 1910 | wxString wxFileName::GetVolumeSeparator(wxPathFormat WXUNUSED_IN_WINCE(format)) |
| 1911 | { |
| 1912 | #ifdef __WXWINCE__ |
| 1913 | return wxEmptyString; |
| 1914 | #else |
| 1915 | wxString sepVol; |
| 1916 | |
| 1917 | if ( (GetFormat(format) == wxPATH_DOS) || |
| 1918 | (GetFormat(format) == wxPATH_VMS) ) |
| 1919 | { |
| 1920 | sepVol = wxFILE_SEP_DSK; |
| 1921 | } |
| 1922 | //else: leave empty |
| 1923 | |
| 1924 | return sepVol; |
| 1925 | #endif |
| 1926 | } |
| 1927 | |
| 1928 | /* static */ |
| 1929 | wxString wxFileName::GetPathSeparators(wxPathFormat format) |
| 1930 | { |
| 1931 | wxString seps; |
| 1932 | switch ( GetFormat(format) ) |
| 1933 | { |
| 1934 | case wxPATH_DOS: |
| 1935 | // accept both as native APIs do but put the native one first as |
| 1936 | // this is the one we use in GetFullPath() |
| 1937 | seps << wxFILE_SEP_PATH_DOS << wxFILE_SEP_PATH_UNIX; |
| 1938 | break; |
| 1939 | |
| 1940 | default: |
| 1941 | wxFAIL_MSG( wxT("Unknown wxPATH_XXX style") ); |
| 1942 | // fall through |
| 1943 | |
| 1944 | case wxPATH_UNIX: |
| 1945 | seps = wxFILE_SEP_PATH_UNIX; |
| 1946 | break; |
| 1947 | |
| 1948 | case wxPATH_MAC: |
| 1949 | seps = wxFILE_SEP_PATH_MAC; |
| 1950 | break; |
| 1951 | |
| 1952 | case wxPATH_VMS: |
| 1953 | seps = wxFILE_SEP_PATH_VMS; |
| 1954 | break; |
| 1955 | } |
| 1956 | |
| 1957 | return seps; |
| 1958 | } |
| 1959 | |
| 1960 | /* static */ |
| 1961 | wxString wxFileName::GetPathTerminators(wxPathFormat format) |
| 1962 | { |
| 1963 | format = GetFormat(format); |
| 1964 | |
| 1965 | // under VMS the end of the path is ']', not the path separator used to |
| 1966 | // separate the components |
| 1967 | return format == wxPATH_VMS ? wxString(wxT(']')) : GetPathSeparators(format); |
| 1968 | } |
| 1969 | |
| 1970 | /* static */ |
| 1971 | bool wxFileName::IsPathSeparator(wxChar ch, wxPathFormat format) |
| 1972 | { |
| 1973 | // wxString::Find() doesn't work as expected with NUL - it will always find |
| 1974 | // it, so test for it separately |
| 1975 | return ch != wxT('\0') && GetPathSeparators(format).Find(ch) != wxNOT_FOUND; |
| 1976 | } |
| 1977 | |
| 1978 | /* static */ |
| 1979 | bool |
| 1980 | wxFileName::IsMSWUniqueVolumeNamePath(const wxString& path, wxPathFormat format) |
| 1981 | { |
| 1982 | // return true if the format used is the DOS/Windows one and the string begins |
| 1983 | // with a Windows unique volume name ("\\?\Volume{guid}\") |
| 1984 | return format == wxPATH_DOS && |
| 1985 | path.length() >= wxMSWUniqueVolumePrefixLength && |
| 1986 | path.StartsWith(wxS("\\\\?\\Volume{")) && |
| 1987 | path[wxMSWUniqueVolumePrefixLength - 1] == wxFILE_SEP_PATH_DOS; |
| 1988 | } |
| 1989 | |
| 1990 | // ---------------------------------------------------------------------------- |
| 1991 | // path components manipulation |
| 1992 | // ---------------------------------------------------------------------------- |
| 1993 | |
| 1994 | /* static */ bool wxFileName::IsValidDirComponent(const wxString& dir) |
| 1995 | { |
| 1996 | if ( dir.empty() ) |
| 1997 | { |
| 1998 | wxFAIL_MSG( wxT("empty directory passed to wxFileName::InsertDir()") ); |
| 1999 | |
| 2000 | return false; |
| 2001 | } |
| 2002 | |
| 2003 | const size_t len = dir.length(); |
| 2004 | for ( size_t n = 0; n < len; n++ ) |
| 2005 | { |
| 2006 | if ( dir[n] == GetVolumeSeparator() || IsPathSeparator(dir[n]) ) |
| 2007 | { |
| 2008 | wxFAIL_MSG( wxT("invalid directory component in wxFileName") ); |
| 2009 | |
| 2010 | return false; |
| 2011 | } |
| 2012 | } |
| 2013 | |
| 2014 | return true; |
| 2015 | } |
| 2016 | |
| 2017 | bool wxFileName::AppendDir( const wxString& dir ) |
| 2018 | { |
| 2019 | if (!IsValidDirComponent(dir)) |
| 2020 | return false; |
| 2021 | m_dirs.Add(dir); |
| 2022 | return true; |
| 2023 | } |
| 2024 | |
| 2025 | void wxFileName::PrependDir( const wxString& dir ) |
| 2026 | { |
| 2027 | InsertDir(0, dir); |
| 2028 | } |
| 2029 | |
| 2030 | bool wxFileName::InsertDir(size_t before, const wxString& dir) |
| 2031 | { |
| 2032 | if (!IsValidDirComponent(dir)) |
| 2033 | return false; |
| 2034 | m_dirs.Insert(dir, before); |
| 2035 | return true; |
| 2036 | } |
| 2037 | |
| 2038 | void wxFileName::RemoveDir(size_t pos) |
| 2039 | { |
| 2040 | m_dirs.RemoveAt(pos); |
| 2041 | } |
| 2042 | |
| 2043 | // ---------------------------------------------------------------------------- |
| 2044 | // accessors |
| 2045 | // ---------------------------------------------------------------------------- |
| 2046 | |
| 2047 | void wxFileName::SetFullName(const wxString& fullname) |
| 2048 | { |
| 2049 | SplitPath(fullname, NULL /* no volume */, NULL /* no path */, |
| 2050 | &m_name, &m_ext, &m_hasExt); |
| 2051 | } |
| 2052 | |
| 2053 | wxString wxFileName::GetFullName() const |
| 2054 | { |
| 2055 | wxString fullname = m_name; |
| 2056 | if ( m_hasExt ) |
| 2057 | { |
| 2058 | fullname << wxFILE_SEP_EXT << m_ext; |
| 2059 | } |
| 2060 | |
| 2061 | return fullname; |
| 2062 | } |
| 2063 | |
| 2064 | wxString wxFileName::GetPath( int flags, wxPathFormat format ) const |
| 2065 | { |
| 2066 | format = GetFormat( format ); |
| 2067 | |
| 2068 | wxString fullpath; |
| 2069 | |
| 2070 | // return the volume with the path as well if requested |
| 2071 | if ( flags & wxPATH_GET_VOLUME ) |
| 2072 | { |
| 2073 | fullpath += wxGetVolumeString(GetVolume(), format); |
| 2074 | } |
| 2075 | |
| 2076 | // the leading character |
| 2077 | switch ( format ) |
| 2078 | { |
| 2079 | case wxPATH_MAC: |
| 2080 | if ( m_relative ) |
| 2081 | fullpath += wxFILE_SEP_PATH_MAC; |
| 2082 | break; |
| 2083 | |
| 2084 | case wxPATH_DOS: |
| 2085 | if ( !m_relative ) |
| 2086 | fullpath += wxFILE_SEP_PATH_DOS; |
| 2087 | break; |
| 2088 | |
| 2089 | default: |
| 2090 | wxFAIL_MSG( wxT("Unknown path format") ); |
| 2091 | // fall through |
| 2092 | |
| 2093 | case wxPATH_UNIX: |
| 2094 | if ( !m_relative ) |
| 2095 | { |
| 2096 | fullpath += wxFILE_SEP_PATH_UNIX; |
| 2097 | } |
| 2098 | break; |
| 2099 | |
| 2100 | case wxPATH_VMS: |
| 2101 | // no leading character here but use this place to unset |
| 2102 | // wxPATH_GET_SEPARATOR flag: under VMS it doesn't make sense |
| 2103 | // as, if I understand correctly, there should never be a dot |
| 2104 | // before the closing bracket |
| 2105 | flags &= ~wxPATH_GET_SEPARATOR; |
| 2106 | } |
| 2107 | |
| 2108 | if ( m_dirs.empty() ) |
| 2109 | { |
| 2110 | // there is nothing more |
| 2111 | return fullpath; |
| 2112 | } |
| 2113 | |
| 2114 | // then concatenate all the path components using the path separator |
| 2115 | if ( format == wxPATH_VMS ) |
| 2116 | { |
| 2117 | fullpath += wxT('['); |
| 2118 | } |
| 2119 | |
| 2120 | const size_t dirCount = m_dirs.GetCount(); |
| 2121 | for ( size_t i = 0; i < dirCount; i++ ) |
| 2122 | { |
| 2123 | switch (format) |
| 2124 | { |
| 2125 | case wxPATH_MAC: |
| 2126 | if ( m_dirs[i] == wxT(".") ) |
| 2127 | { |
| 2128 | // skip appending ':', this shouldn't be done in this |
| 2129 | // case as "::" is interpreted as ".." under Unix |
| 2130 | continue; |
| 2131 | } |
| 2132 | |
| 2133 | // convert back from ".." to nothing |
| 2134 | if ( !m_dirs[i].IsSameAs(wxT("..")) ) |
| 2135 | fullpath += m_dirs[i]; |
| 2136 | break; |
| 2137 | |
| 2138 | default: |
| 2139 | wxFAIL_MSG( wxT("Unexpected path format") ); |
| 2140 | // still fall through |
| 2141 | |
| 2142 | case wxPATH_DOS: |
| 2143 | case wxPATH_UNIX: |
| 2144 | fullpath += m_dirs[i]; |
| 2145 | break; |
| 2146 | |
| 2147 | case wxPATH_VMS: |
| 2148 | // TODO: What to do with ".." under VMS |
| 2149 | |
| 2150 | // convert back from ".." to nothing |
| 2151 | if ( !m_dirs[i].IsSameAs(wxT("..")) ) |
| 2152 | fullpath += m_dirs[i]; |
| 2153 | break; |
| 2154 | } |
| 2155 | |
| 2156 | if ( (flags & wxPATH_GET_SEPARATOR) || (i != dirCount - 1) ) |
| 2157 | fullpath += GetPathSeparator(format); |
| 2158 | } |
| 2159 | |
| 2160 | if ( format == wxPATH_VMS ) |
| 2161 | { |
| 2162 | fullpath += wxT(']'); |
| 2163 | } |
| 2164 | |
| 2165 | return fullpath; |
| 2166 | } |
| 2167 | |
| 2168 | wxString wxFileName::GetFullPath( wxPathFormat format ) const |
| 2169 | { |
| 2170 | // we already have a function to get the path |
| 2171 | wxString fullpath = GetPath(wxPATH_GET_VOLUME | wxPATH_GET_SEPARATOR, |
| 2172 | format); |
| 2173 | |
| 2174 | // now just add the file name and extension to it |
| 2175 | fullpath += GetFullName(); |
| 2176 | |
| 2177 | return fullpath; |
| 2178 | } |
| 2179 | |
| 2180 | // Return the short form of the path (returns identity on non-Windows platforms) |
| 2181 | wxString wxFileName::GetShortPath() const |
| 2182 | { |
| 2183 | wxString path(GetFullPath()); |
| 2184 | |
| 2185 | #if defined(__WINDOWS__) && defined(__WIN32__) && !defined(__WXMICROWIN__) && !defined(__WXWINCE__) |
| 2186 | DWORD sz = ::GetShortPathName(path.t_str(), NULL, 0); |
| 2187 | if ( sz != 0 ) |
| 2188 | { |
| 2189 | wxString pathOut; |
| 2190 | if ( ::GetShortPathName |
| 2191 | ( |
| 2192 | path.t_str(), |
| 2193 | wxStringBuffer(pathOut, sz), |
| 2194 | sz |
| 2195 | ) != 0 ) |
| 2196 | { |
| 2197 | return pathOut; |
| 2198 | } |
| 2199 | } |
| 2200 | #endif // Windows |
| 2201 | |
| 2202 | return path; |
| 2203 | } |
| 2204 | |
| 2205 | // Return the long form of the path (returns identity on non-Windows platforms) |
| 2206 | wxString wxFileName::GetLongPath() const |
| 2207 | { |
| 2208 | wxString pathOut, |
| 2209 | path = GetFullPath(); |
| 2210 | |
| 2211 | #if defined(__WIN32__) && !defined(__WXWINCE__) && !defined(__WXMICROWIN__) |
| 2212 | |
| 2213 | #if wxUSE_DYNLIB_CLASS |
| 2214 | typedef DWORD (WINAPI *GET_LONG_PATH_NAME)(const wxChar *, wxChar *, DWORD); |
| 2215 | |
| 2216 | // this is MT-safe as in the worst case we're going to resolve the function |
| 2217 | // twice -- but as the result is the same in both threads, it's ok |
| 2218 | static GET_LONG_PATH_NAME s_pfnGetLongPathName = NULL; |
| 2219 | if ( !s_pfnGetLongPathName ) |
| 2220 | { |
| 2221 | static bool s_triedToLoad = false; |
| 2222 | |
| 2223 | if ( !s_triedToLoad ) |
| 2224 | { |
| 2225 | s_triedToLoad = true; |
| 2226 | |
| 2227 | wxDynamicLibrary dllKernel(wxT("kernel32")); |
| 2228 | |
| 2229 | const wxChar* GetLongPathName = wxT("GetLongPathName") |
| 2230 | #if wxUSE_UNICODE |
| 2231 | wxT("W"); |
| 2232 | #else // ANSI |
| 2233 | wxT("A"); |
| 2234 | #endif // Unicode/ANSI |
| 2235 | |
| 2236 | if ( dllKernel.HasSymbol(GetLongPathName) ) |
| 2237 | { |
| 2238 | s_pfnGetLongPathName = (GET_LONG_PATH_NAME) |
| 2239 | dllKernel.GetSymbol(GetLongPathName); |
| 2240 | } |
| 2241 | |
| 2242 | // note that kernel32.dll can be unloaded, it stays in memory |
| 2243 | // anyhow as all Win32 programs link to it and so it's safe to call |
| 2244 | // GetLongPathName() even after unloading it |
| 2245 | } |
| 2246 | } |
| 2247 | |
| 2248 | if ( s_pfnGetLongPathName ) |
| 2249 | { |
| 2250 | DWORD dwSize = (*s_pfnGetLongPathName)(path.t_str(), NULL, 0); |
| 2251 | if ( dwSize > 0 ) |
| 2252 | { |
| 2253 | if ( (*s_pfnGetLongPathName) |
| 2254 | ( |
| 2255 | path.t_str(), |
| 2256 | wxStringBuffer(pathOut, dwSize), |
| 2257 | dwSize |
| 2258 | ) != 0 ) |
| 2259 | { |
| 2260 | return pathOut; |
| 2261 | } |
| 2262 | } |
| 2263 | } |
| 2264 | #endif // wxUSE_DYNLIB_CLASS |
| 2265 | |
| 2266 | // The OS didn't support GetLongPathName, or some other error. |
| 2267 | // We need to call FindFirstFile on each component in turn. |
| 2268 | |
| 2269 | WIN32_FIND_DATA findFileData; |
| 2270 | HANDLE hFind; |
| 2271 | |
| 2272 | if ( HasVolume() ) |
| 2273 | pathOut = GetVolume() + |
| 2274 | GetVolumeSeparator(wxPATH_DOS) + |
| 2275 | GetPathSeparator(wxPATH_DOS); |
| 2276 | else |
| 2277 | pathOut.clear(); |
| 2278 | |
| 2279 | wxArrayString dirs = GetDirs(); |
| 2280 | dirs.Add(GetFullName()); |
| 2281 | |
| 2282 | wxString tmpPath; |
| 2283 | |
| 2284 | size_t count = dirs.GetCount(); |
| 2285 | for ( size_t i = 0; i < count; i++ ) |
| 2286 | { |
| 2287 | const wxString& dir = dirs[i]; |
| 2288 | |
| 2289 | // We're using pathOut to collect the long-name path, but using a |
| 2290 | // temporary for appending the last path component which may be |
| 2291 | // short-name |
| 2292 | tmpPath = pathOut + dir; |
| 2293 | |
| 2294 | // We must not process "." or ".." here as they would be (unexpectedly) |
| 2295 | // replaced by the corresponding directory names so just leave them |
| 2296 | // alone |
| 2297 | // |
| 2298 | // And we can't pass a drive and root dir to FindFirstFile (VZ: why?) |
| 2299 | if ( tmpPath.empty() || dir == '.' || dir == ".." || |
| 2300 | tmpPath.Last() == GetVolumeSeparator(wxPATH_DOS) ) |
| 2301 | { |
| 2302 | tmpPath += wxFILE_SEP_PATH; |
| 2303 | pathOut = tmpPath; |
| 2304 | continue; |
| 2305 | } |
| 2306 | |
| 2307 | hFind = ::FindFirstFile(tmpPath.t_str(), &findFileData); |
| 2308 | if (hFind == INVALID_HANDLE_VALUE) |
| 2309 | { |
| 2310 | // Error: most likely reason is that path doesn't exist, so |
| 2311 | // append any unprocessed parts and return |
| 2312 | for ( i += 1; i < count; i++ ) |
| 2313 | tmpPath += wxFILE_SEP_PATH + dirs[i]; |
| 2314 | |
| 2315 | return tmpPath; |
| 2316 | } |
| 2317 | |
| 2318 | pathOut += findFileData.cFileName; |
| 2319 | if ( (i < (count-1)) ) |
| 2320 | pathOut += wxFILE_SEP_PATH; |
| 2321 | |
| 2322 | ::FindClose(hFind); |
| 2323 | } |
| 2324 | #else // !Win32 |
| 2325 | pathOut = path; |
| 2326 | #endif // Win32/!Win32 |
| 2327 | |
| 2328 | return pathOut; |
| 2329 | } |
| 2330 | |
| 2331 | wxPathFormat wxFileName::GetFormat( wxPathFormat format ) |
| 2332 | { |
| 2333 | if (format == wxPATH_NATIVE) |
| 2334 | { |
| 2335 | #if defined(__WINDOWS__) || defined(__OS2__) || defined(__DOS__) |
| 2336 | format = wxPATH_DOS; |
| 2337 | #elif defined(__VMS) |
| 2338 | format = wxPATH_VMS; |
| 2339 | #else |
| 2340 | format = wxPATH_UNIX; |
| 2341 | #endif |
| 2342 | } |
| 2343 | return format; |
| 2344 | } |
| 2345 | |
| 2346 | #ifdef wxHAS_FILESYSTEM_VOLUMES |
| 2347 | |
| 2348 | /* static */ |
| 2349 | wxString wxFileName::GetVolumeString(char drive, int flags) |
| 2350 | { |
| 2351 | wxASSERT_MSG( !(flags & ~wxPATH_GET_SEPARATOR), "invalid flag specified" ); |
| 2352 | |
| 2353 | wxString vol(drive); |
| 2354 | vol += wxFILE_SEP_DSK; |
| 2355 | if ( flags & wxPATH_GET_SEPARATOR ) |
| 2356 | vol += wxFILE_SEP_PATH; |
| 2357 | |
| 2358 | return vol; |
| 2359 | } |
| 2360 | |
| 2361 | #endif // wxHAS_FILESYSTEM_VOLUMES |
| 2362 | |
| 2363 | // ---------------------------------------------------------------------------- |
| 2364 | // path splitting function |
| 2365 | // ---------------------------------------------------------------------------- |
| 2366 | |
| 2367 | /* static */ |
| 2368 | void |
| 2369 | wxFileName::SplitVolume(const wxString& fullpathWithVolume, |
| 2370 | wxString *pstrVolume, |
| 2371 | wxString *pstrPath, |
| 2372 | wxPathFormat format) |
| 2373 | { |
| 2374 | format = GetFormat(format); |
| 2375 | |
| 2376 | wxString fullpath = fullpathWithVolume; |
| 2377 | |
| 2378 | if ( IsMSWUniqueVolumeNamePath(fullpath, format) ) |
| 2379 | { |
| 2380 | // special Windows unique volume names hack: transform |
| 2381 | // \\?\Volume{guid}\path into Volume{guid}:path |
| 2382 | // note: this check must be done before the check for UNC path |
| 2383 | |
| 2384 | // we know the last backslash from the unique volume name is located |
| 2385 | // there from IsMSWUniqueVolumeNamePath |
| 2386 | fullpath[wxMSWUniqueVolumePrefixLength - 1] = wxFILE_SEP_DSK; |
| 2387 | |
| 2388 | // paths starting with a unique volume name should always be absolute |
| 2389 | fullpath.insert(wxMSWUniqueVolumePrefixLength, 1, wxFILE_SEP_PATH_DOS); |
| 2390 | |
| 2391 | // remove the leading "\\?\" part |
| 2392 | fullpath.erase(0, 4); |
| 2393 | } |
| 2394 | else if ( IsUNCPath(fullpath, format) ) |
| 2395 | { |
| 2396 | // special Windows UNC paths hack: transform \\share\path into share:path |
| 2397 | |
| 2398 | fullpath.erase(0, 2); |
| 2399 | |
| 2400 | size_t posFirstSlash = |
| 2401 | fullpath.find_first_of(GetPathTerminators(format)); |
| 2402 | if ( posFirstSlash != wxString::npos ) |
| 2403 | { |
| 2404 | fullpath[posFirstSlash] = wxFILE_SEP_DSK; |
| 2405 | |
| 2406 | // UNC paths are always absolute, right? (FIXME) |
| 2407 | fullpath.insert(posFirstSlash + 1, 1, wxFILE_SEP_PATH_DOS); |
| 2408 | } |
| 2409 | } |
| 2410 | |
| 2411 | // We separate the volume here |
| 2412 | if ( format == wxPATH_DOS || format == wxPATH_VMS ) |
| 2413 | { |
| 2414 | wxString sepVol = GetVolumeSeparator(format); |
| 2415 | |
| 2416 | // we have to exclude the case of a colon in the very beginning of the |
| 2417 | // string as it can't be a volume separator (nor can this be a valid |
| 2418 | // DOS file name at all but we'll leave dealing with this to our caller) |
| 2419 | size_t posFirstColon = fullpath.find_first_of(sepVol); |
| 2420 | if ( posFirstColon && posFirstColon != wxString::npos ) |
| 2421 | { |
| 2422 | if ( pstrVolume ) |
| 2423 | { |
| 2424 | *pstrVolume = fullpath.Left(posFirstColon); |
| 2425 | } |
| 2426 | |
| 2427 | // remove the volume name and the separator from the full path |
| 2428 | fullpath.erase(0, posFirstColon + sepVol.length()); |
| 2429 | } |
| 2430 | } |
| 2431 | |
| 2432 | if ( pstrPath ) |
| 2433 | *pstrPath = fullpath; |
| 2434 | } |
| 2435 | |
| 2436 | /* static */ |
| 2437 | void wxFileName::SplitPath(const wxString& fullpathWithVolume, |
| 2438 | wxString *pstrVolume, |
| 2439 | wxString *pstrPath, |
| 2440 | wxString *pstrName, |
| 2441 | wxString *pstrExt, |
| 2442 | bool *hasExt, |
| 2443 | wxPathFormat format) |
| 2444 | { |
| 2445 | format = GetFormat(format); |
| 2446 | |
| 2447 | wxString fullpath; |
| 2448 | SplitVolume(fullpathWithVolume, pstrVolume, &fullpath, format); |
| 2449 | |
| 2450 | // find the positions of the last dot and last path separator in the path |
| 2451 | size_t posLastDot = fullpath.find_last_of(wxFILE_SEP_EXT); |
| 2452 | size_t posLastSlash = fullpath.find_last_of(GetPathTerminators(format)); |
| 2453 | |
| 2454 | // check whether this dot occurs at the very beginning of a path component |
| 2455 | if ( (posLastDot != wxString::npos) && |
| 2456 | (posLastDot == 0 || |
| 2457 | IsPathSeparator(fullpath[posLastDot - 1]) || |
| 2458 | (format == wxPATH_VMS && fullpath[posLastDot - 1] == wxT(']'))) ) |
| 2459 | { |
| 2460 | // dot may be (and commonly -- at least under Unix -- is) the first |
| 2461 | // character of the filename, don't treat the entire filename as |
| 2462 | // extension in this case |
| 2463 | posLastDot = wxString::npos; |
| 2464 | } |
| 2465 | |
| 2466 | // if we do have a dot and a slash, check that the dot is in the name part |
| 2467 | if ( (posLastDot != wxString::npos) && |
| 2468 | (posLastSlash != wxString::npos) && |
| 2469 | (posLastDot < posLastSlash) ) |
| 2470 | { |
| 2471 | // the dot is part of the path, not the start of the extension |
| 2472 | posLastDot = wxString::npos; |
| 2473 | } |
| 2474 | |
| 2475 | // now fill in the variables provided by user |
| 2476 | if ( pstrPath ) |
| 2477 | { |
| 2478 | if ( posLastSlash == wxString::npos ) |
| 2479 | { |
| 2480 | // no path at all |
| 2481 | pstrPath->Empty(); |
| 2482 | } |
| 2483 | else |
| 2484 | { |
| 2485 | // take everything up to the path separator but take care to make |
| 2486 | // the path equal to something like '/', not empty, for the files |
| 2487 | // immediately under root directory |
| 2488 | size_t len = posLastSlash; |
| 2489 | |
| 2490 | // this rule does not apply to mac since we do not start with colons (sep) |
| 2491 | // except for relative paths |
| 2492 | if ( !len && format != wxPATH_MAC) |
| 2493 | len++; |
| 2494 | |
| 2495 | *pstrPath = fullpath.Left(len); |
| 2496 | |
| 2497 | // special VMS hack: remove the initial bracket |
| 2498 | if ( format == wxPATH_VMS ) |
| 2499 | { |
| 2500 | if ( (*pstrPath)[0u] == wxT('[') ) |
| 2501 | pstrPath->erase(0, 1); |
| 2502 | } |
| 2503 | } |
| 2504 | } |
| 2505 | |
| 2506 | if ( pstrName ) |
| 2507 | { |
| 2508 | // take all characters starting from the one after the last slash and |
| 2509 | // up to, but excluding, the last dot |
| 2510 | size_t nStart = posLastSlash == wxString::npos ? 0 : posLastSlash + 1; |
| 2511 | size_t count; |
| 2512 | if ( posLastDot == wxString::npos ) |
| 2513 | { |
| 2514 | // take all until the end |
| 2515 | count = wxString::npos; |
| 2516 | } |
| 2517 | else if ( posLastSlash == wxString::npos ) |
| 2518 | { |
| 2519 | count = posLastDot; |
| 2520 | } |
| 2521 | else // have both dot and slash |
| 2522 | { |
| 2523 | count = posLastDot - posLastSlash - 1; |
| 2524 | } |
| 2525 | |
| 2526 | *pstrName = fullpath.Mid(nStart, count); |
| 2527 | } |
| 2528 | |
| 2529 | // finally deal with the extension here: we have an added complication that |
| 2530 | // extension may be empty (but present) as in "foo." where trailing dot |
| 2531 | // indicates the empty extension at the end -- and hence we must remember |
| 2532 | // that we have it independently of pstrExt |
| 2533 | if ( posLastDot == wxString::npos ) |
| 2534 | { |
| 2535 | // no extension |
| 2536 | if ( pstrExt ) |
| 2537 | pstrExt->clear(); |
| 2538 | if ( hasExt ) |
| 2539 | *hasExt = false; |
| 2540 | } |
| 2541 | else |
| 2542 | { |
| 2543 | // take everything after the dot |
| 2544 | if ( pstrExt ) |
| 2545 | *pstrExt = fullpath.Mid(posLastDot + 1); |
| 2546 | if ( hasExt ) |
| 2547 | *hasExt = true; |
| 2548 | } |
| 2549 | } |
| 2550 | |
| 2551 | /* static */ |
| 2552 | void wxFileName::SplitPath(const wxString& fullpath, |
| 2553 | wxString *path, |
| 2554 | wxString *name, |
| 2555 | wxString *ext, |
| 2556 | wxPathFormat format) |
| 2557 | { |
| 2558 | wxString volume; |
| 2559 | SplitPath(fullpath, &volume, path, name, ext, format); |
| 2560 | |
| 2561 | if ( path ) |
| 2562 | { |
| 2563 | path->Prepend(wxGetVolumeString(volume, format)); |
| 2564 | } |
| 2565 | } |
| 2566 | |
| 2567 | /* static */ |
| 2568 | wxString wxFileName::StripExtension(const wxString& fullpath) |
| 2569 | { |
| 2570 | wxFileName fn(fullpath); |
| 2571 | fn.SetExt(""); |
| 2572 | return fn.GetFullPath(); |
| 2573 | } |
| 2574 | |
| 2575 | // ---------------------------------------------------------------------------- |
| 2576 | // file permissions functions |
| 2577 | // ---------------------------------------------------------------------------- |
| 2578 | |
| 2579 | bool wxFileName::SetPermissions(int permissions) |
| 2580 | { |
| 2581 | // Don't do anything for a symlink but first make sure it is one. |
| 2582 | if ( m_dontFollowLinks && |
| 2583 | Exists(wxFILE_EXISTS_SYMLINK|wxFILE_EXISTS_NO_FOLLOW) ) |
| 2584 | { |
| 2585 | // Looks like changing permissions for a symlinc is only supported |
| 2586 | // on BSD where lchmod is present and correctly implemented. |
| 2587 | // http://lists.gnu.org/archive/html/bug-coreutils/2009-09/msg00268.html |
| 2588 | return false; |
| 2589 | } |
| 2590 | |
| 2591 | #ifdef __WINDOWS__ |
| 2592 | int accMode = 0; |
| 2593 | |
| 2594 | if ( permissions & (wxS_IRUSR|wxS_IRGRP|wxS_IROTH) ) |
| 2595 | accMode = _S_IREAD; |
| 2596 | |
| 2597 | if ( permissions & (wxS_IWUSR|wxS_IWGRP|wxS_IWOTH) ) |
| 2598 | accMode |= _S_IWRITE; |
| 2599 | |
| 2600 | permissions = accMode; |
| 2601 | #endif // __WINDOWS__ |
| 2602 | |
| 2603 | return wxChmod(GetFullPath(), permissions) == 0; |
| 2604 | } |
| 2605 | |
| 2606 | // ---------------------------------------------------------------------------- |
| 2607 | // time functions |
| 2608 | // ---------------------------------------------------------------------------- |
| 2609 | |
| 2610 | #if wxUSE_DATETIME |
| 2611 | |
| 2612 | bool wxFileName::SetTimes(const wxDateTime *dtAccess, |
| 2613 | const wxDateTime *dtMod, |
| 2614 | const wxDateTime *dtCreate) const |
| 2615 | { |
| 2616 | #if defined(__WIN32__) |
| 2617 | FILETIME ftAccess, ftCreate, ftWrite; |
| 2618 | |
| 2619 | if ( dtCreate ) |
| 2620 | ConvertWxToFileTime(&ftCreate, *dtCreate); |
| 2621 | if ( dtAccess ) |
| 2622 | ConvertWxToFileTime(&ftAccess, *dtAccess); |
| 2623 | if ( dtMod ) |
| 2624 | ConvertWxToFileTime(&ftWrite, *dtMod); |
| 2625 | |
| 2626 | wxString path; |
| 2627 | int flags; |
| 2628 | if ( IsDir() ) |
| 2629 | { |
| 2630 | if ( wxGetOsVersion() == wxOS_WINDOWS_9X ) |
| 2631 | { |
| 2632 | wxLogError(_("Setting directory access times is not supported " |
| 2633 | "under this OS version")); |
| 2634 | return false; |
| 2635 | } |
| 2636 | |
| 2637 | path = GetPath(); |
| 2638 | flags = FILE_FLAG_BACKUP_SEMANTICS; |
| 2639 | } |
| 2640 | else // file |
| 2641 | { |
| 2642 | path = GetFullPath(); |
| 2643 | flags = 0; |
| 2644 | } |
| 2645 | |
| 2646 | wxFileHandle fh(path, wxFileHandle::WriteAttr, flags); |
| 2647 | if ( fh.IsOk() ) |
| 2648 | { |
| 2649 | if ( ::SetFileTime(fh, |
| 2650 | dtCreate ? &ftCreate : NULL, |
| 2651 | dtAccess ? &ftAccess : NULL, |
| 2652 | dtMod ? &ftWrite : NULL) ) |
| 2653 | { |
| 2654 | return true; |
| 2655 | } |
| 2656 | } |
| 2657 | #elif defined(__UNIX_LIKE__) || (defined(__DOS__) && defined(__WATCOMC__)) |
| 2658 | wxUnusedVar(dtCreate); |
| 2659 | |
| 2660 | if ( !dtAccess && !dtMod ) |
| 2661 | { |
| 2662 | // can't modify the creation time anyhow, don't try |
| 2663 | return true; |
| 2664 | } |
| 2665 | |
| 2666 | // if dtAccess or dtMod is not specified, use the other one (which must be |
| 2667 | // non NULL because of the test above) for both times |
| 2668 | utimbuf utm; |
| 2669 | utm.actime = dtAccess ? dtAccess->GetTicks() : dtMod->GetTicks(); |
| 2670 | utm.modtime = dtMod ? dtMod->GetTicks() : dtAccess->GetTicks(); |
| 2671 | if ( utime(GetFullPath().fn_str(), &utm) == 0 ) |
| 2672 | { |
| 2673 | return true; |
| 2674 | } |
| 2675 | #else // other platform |
| 2676 | wxUnusedVar(dtAccess); |
| 2677 | wxUnusedVar(dtMod); |
| 2678 | wxUnusedVar(dtCreate); |
| 2679 | #endif // platforms |
| 2680 | |
| 2681 | wxLogSysError(_("Failed to modify file times for '%s'"), |
| 2682 | GetFullPath().c_str()); |
| 2683 | |
| 2684 | return false; |
| 2685 | } |
| 2686 | |
| 2687 | bool wxFileName::Touch() const |
| 2688 | { |
| 2689 | #if defined(__UNIX_LIKE__) |
| 2690 | // under Unix touching file is simple: just pass NULL to utime() |
| 2691 | if ( utime(GetFullPath().fn_str(), NULL) == 0 ) |
| 2692 | { |
| 2693 | return true; |
| 2694 | } |
| 2695 | |
| 2696 | wxLogSysError(_("Failed to touch the file '%s'"), GetFullPath().c_str()); |
| 2697 | |
| 2698 | return false; |
| 2699 | #else // other platform |
| 2700 | wxDateTime dtNow = wxDateTime::Now(); |
| 2701 | |
| 2702 | return SetTimes(&dtNow, &dtNow, NULL /* don't change create time */); |
| 2703 | #endif // platforms |
| 2704 | } |
| 2705 | |
| 2706 | bool wxFileName::GetTimes(wxDateTime *dtAccess, |
| 2707 | wxDateTime *dtMod, |
| 2708 | wxDateTime *dtCreate) const |
| 2709 | { |
| 2710 | #if defined(__WIN32__) |
| 2711 | // we must use different methods for the files and directories under |
| 2712 | // Windows as CreateFile(GENERIC_READ) doesn't work for the directories and |
| 2713 | // CreateFile(FILE_FLAG_BACKUP_SEMANTICS) works -- but only under NT and |
| 2714 | // not 9x |
| 2715 | bool ok; |
| 2716 | FILETIME ftAccess, ftCreate, ftWrite; |
| 2717 | if ( IsDir() ) |
| 2718 | { |
| 2719 | // implemented in msw/dir.cpp |
| 2720 | extern bool wxGetDirectoryTimes(const wxString& dirname, |
| 2721 | FILETIME *, FILETIME *, FILETIME *); |
| 2722 | |
| 2723 | // we should pass the path without the trailing separator to |
| 2724 | // wxGetDirectoryTimes() |
| 2725 | ok = wxGetDirectoryTimes(GetPath(wxPATH_GET_VOLUME), |
| 2726 | &ftAccess, &ftCreate, &ftWrite); |
| 2727 | } |
| 2728 | else // file |
| 2729 | { |
| 2730 | wxFileHandle fh(GetFullPath(), wxFileHandle::ReadAttr); |
| 2731 | if ( fh.IsOk() ) |
| 2732 | { |
| 2733 | ok = ::GetFileTime(fh, |
| 2734 | dtCreate ? &ftCreate : NULL, |
| 2735 | dtAccess ? &ftAccess : NULL, |
| 2736 | dtMod ? &ftWrite : NULL) != 0; |
| 2737 | } |
| 2738 | else |
| 2739 | { |
| 2740 | ok = false; |
| 2741 | } |
| 2742 | } |
| 2743 | |
| 2744 | if ( ok ) |
| 2745 | { |
| 2746 | if ( dtCreate ) |
| 2747 | ConvertFileTimeToWx(dtCreate, ftCreate); |
| 2748 | if ( dtAccess ) |
| 2749 | ConvertFileTimeToWx(dtAccess, ftAccess); |
| 2750 | if ( dtMod ) |
| 2751 | ConvertFileTimeToWx(dtMod, ftWrite); |
| 2752 | |
| 2753 | return true; |
| 2754 | } |
| 2755 | #elif defined(__UNIX_LIKE__) || defined(__WXMAC__) || defined(__OS2__) || (defined(__DOS__) && defined(__WATCOMC__)) |
| 2756 | // no need to test for IsDir() here |
| 2757 | wxStructStat stBuf; |
| 2758 | if ( StatAny(stBuf, *this) ) |
| 2759 | { |
| 2760 | // Android defines st_*time fields as unsigned long, but time_t as long, |
| 2761 | // hence the static_casts. |
| 2762 | if ( dtAccess ) |
| 2763 | dtAccess->Set(static_cast<time_t>(stBuf.st_atime)); |
| 2764 | if ( dtMod ) |
| 2765 | dtMod->Set(static_cast<time_t>(stBuf.st_mtime)); |
| 2766 | if ( dtCreate ) |
| 2767 | dtCreate->Set(static_cast<time_t>(stBuf.st_ctime)); |
| 2768 | |
| 2769 | return true; |
| 2770 | } |
| 2771 | #else // other platform |
| 2772 | wxUnusedVar(dtAccess); |
| 2773 | wxUnusedVar(dtMod); |
| 2774 | wxUnusedVar(dtCreate); |
| 2775 | #endif // platforms |
| 2776 | |
| 2777 | wxLogSysError(_("Failed to retrieve file times for '%s'"), |
| 2778 | GetFullPath().c_str()); |
| 2779 | |
| 2780 | return false; |
| 2781 | } |
| 2782 | |
| 2783 | #endif // wxUSE_DATETIME |
| 2784 | |
| 2785 | |
| 2786 | // ---------------------------------------------------------------------------- |
| 2787 | // file size functions |
| 2788 | // ---------------------------------------------------------------------------- |
| 2789 | |
| 2790 | #if wxUSE_LONGLONG |
| 2791 | |
| 2792 | /* static */ |
| 2793 | wxULongLong wxFileName::GetSize(const wxString &filename) |
| 2794 | { |
| 2795 | if (!wxFileExists(filename)) |
| 2796 | return wxInvalidSize; |
| 2797 | |
| 2798 | #if defined(__WIN32__) |
| 2799 | wxFileHandle f(filename, wxFileHandle::ReadAttr); |
| 2800 | if (!f.IsOk()) |
| 2801 | return wxInvalidSize; |
| 2802 | |
| 2803 | DWORD lpFileSizeHigh; |
| 2804 | DWORD ret = GetFileSize(f, &lpFileSizeHigh); |
| 2805 | if ( ret == INVALID_FILE_SIZE && ::GetLastError() != NO_ERROR ) |
| 2806 | return wxInvalidSize; |
| 2807 | |
| 2808 | return wxULongLong(lpFileSizeHigh, ret); |
| 2809 | #else // ! __WIN32__ |
| 2810 | wxStructStat st; |
| 2811 | if (wxStat( filename, &st) != 0) |
| 2812 | return wxInvalidSize; |
| 2813 | return wxULongLong(st.st_size); |
| 2814 | #endif |
| 2815 | } |
| 2816 | |
| 2817 | /* static */ |
| 2818 | wxString wxFileName::GetHumanReadableSize(const wxULongLong &bs, |
| 2819 | const wxString &nullsize, |
| 2820 | int precision, |
| 2821 | wxSizeConvention conv) |
| 2822 | { |
| 2823 | // deal with trivial case first |
| 2824 | if ( bs == 0 || bs == wxInvalidSize ) |
| 2825 | return nullsize; |
| 2826 | |
| 2827 | // depending on the convention used the multiplier may be either 1000 or |
| 2828 | // 1024 and the binary infix may be empty (for "KB") or "i" (for "KiB") |
| 2829 | double multiplier = 1024.; |
| 2830 | wxString biInfix; |
| 2831 | |
| 2832 | switch ( conv ) |
| 2833 | { |
| 2834 | case wxSIZE_CONV_TRADITIONAL: |
| 2835 | // nothing to do, this corresponds to the default values of both |
| 2836 | // the multiplier and infix string |
| 2837 | break; |
| 2838 | |
| 2839 | case wxSIZE_CONV_IEC: |
| 2840 | biInfix = "i"; |
| 2841 | break; |
| 2842 | |
| 2843 | case wxSIZE_CONV_SI: |
| 2844 | multiplier = 1000; |
| 2845 | break; |
| 2846 | } |
| 2847 | |
| 2848 | const double kiloByteSize = multiplier; |
| 2849 | const double megaByteSize = multiplier * kiloByteSize; |
| 2850 | const double gigaByteSize = multiplier * megaByteSize; |
| 2851 | const double teraByteSize = multiplier * gigaByteSize; |
| 2852 | |
| 2853 | const double bytesize = bs.ToDouble(); |
| 2854 | |
| 2855 | wxString result; |
| 2856 | if ( bytesize < kiloByteSize ) |
| 2857 | result.Printf("%s B", bs.ToString()); |
| 2858 | else if ( bytesize < megaByteSize ) |
| 2859 | result.Printf("%.*f K%sB", precision, bytesize/kiloByteSize, biInfix); |
| 2860 | else if (bytesize < gigaByteSize) |
| 2861 | result.Printf("%.*f M%sB", precision, bytesize/megaByteSize, biInfix); |
| 2862 | else if (bytesize < teraByteSize) |
| 2863 | result.Printf("%.*f G%sB", precision, bytesize/gigaByteSize, biInfix); |
| 2864 | else |
| 2865 | result.Printf("%.*f T%sB", precision, bytesize/teraByteSize, biInfix); |
| 2866 | |
| 2867 | return result; |
| 2868 | } |
| 2869 | |
| 2870 | wxULongLong wxFileName::GetSize() const |
| 2871 | { |
| 2872 | return GetSize(GetFullPath()); |
| 2873 | } |
| 2874 | |
| 2875 | wxString wxFileName::GetHumanReadableSize(const wxString& failmsg, |
| 2876 | int precision, |
| 2877 | wxSizeConvention conv) const |
| 2878 | { |
| 2879 | return GetHumanReadableSize(GetSize(), failmsg, precision, conv); |
| 2880 | } |
| 2881 | |
| 2882 | #endif // wxUSE_LONGLONG |
| 2883 | |
| 2884 | // ---------------------------------------------------------------------------- |
| 2885 | // Mac-specific functions |
| 2886 | // ---------------------------------------------------------------------------- |
| 2887 | |
| 2888 | #if defined( __WXOSX_MAC__ ) && wxOSX_USE_CARBON |
| 2889 | |
| 2890 | namespace |
| 2891 | { |
| 2892 | |
| 2893 | class MacDefaultExtensionRecord |
| 2894 | { |
| 2895 | public: |
| 2896 | MacDefaultExtensionRecord() |
| 2897 | { |
| 2898 | m_type = |
| 2899 | m_creator = 0 ; |
| 2900 | } |
| 2901 | |
| 2902 | // default copy ctor, assignment operator and dtor are ok |
| 2903 | |
| 2904 | MacDefaultExtensionRecord(const wxString& ext, OSType type, OSType creator) |
| 2905 | : m_ext(ext) |
| 2906 | { |
| 2907 | m_type = type; |
| 2908 | m_creator = creator; |
| 2909 | } |
| 2910 | |
| 2911 | wxString m_ext; |
| 2912 | OSType m_type; |
| 2913 | OSType m_creator; |
| 2914 | }; |
| 2915 | |
| 2916 | WX_DECLARE_OBJARRAY(MacDefaultExtensionRecord, MacDefaultExtensionArray); |
| 2917 | |
| 2918 | bool gMacDefaultExtensionsInited = false; |
| 2919 | |
| 2920 | #include "wx/arrimpl.cpp" |
| 2921 | |
| 2922 | WX_DEFINE_EXPORTED_OBJARRAY(MacDefaultExtensionArray); |
| 2923 | |
| 2924 | MacDefaultExtensionArray gMacDefaultExtensions; |
| 2925 | |
| 2926 | // load the default extensions |
| 2927 | const MacDefaultExtensionRecord gDefaults[] = |
| 2928 | { |
| 2929 | MacDefaultExtensionRecord( "txt", 'TEXT', 'ttxt' ), |
| 2930 | MacDefaultExtensionRecord( "tif", 'TIFF', '****' ), |
| 2931 | MacDefaultExtensionRecord( "jpg", 'JPEG', '****' ), |
| 2932 | }; |
| 2933 | |
| 2934 | void MacEnsureDefaultExtensionsLoaded() |
| 2935 | { |
| 2936 | if ( !gMacDefaultExtensionsInited ) |
| 2937 | { |
| 2938 | // we could load the pc exchange prefs here too |
| 2939 | for ( size_t i = 0 ; i < WXSIZEOF( gDefaults ) ; ++i ) |
| 2940 | { |
| 2941 | gMacDefaultExtensions.Add( gDefaults[i] ) ; |
| 2942 | } |
| 2943 | gMacDefaultExtensionsInited = true; |
| 2944 | } |
| 2945 | } |
| 2946 | |
| 2947 | } // anonymous namespace |
| 2948 | |
| 2949 | bool wxFileName::MacSetTypeAndCreator( wxUint32 type , wxUint32 creator ) |
| 2950 | { |
| 2951 | FSRef fsRef ; |
| 2952 | FSCatalogInfo catInfo; |
| 2953 | FileInfo *finfo ; |
| 2954 | |
| 2955 | if ( wxMacPathToFSRef( GetFullPath() , &fsRef ) == noErr ) |
| 2956 | { |
| 2957 | if ( FSGetCatalogInfo (&fsRef, kFSCatInfoFinderInfo, &catInfo, NULL, NULL, NULL) == noErr ) |
| 2958 | { |
| 2959 | finfo = (FileInfo*)&catInfo.finderInfo; |
| 2960 | finfo->fileType = type ; |
| 2961 | finfo->fileCreator = creator ; |
| 2962 | FSSetCatalogInfo( &fsRef, kFSCatInfoFinderInfo, &catInfo ) ; |
| 2963 | return true ; |
| 2964 | } |
| 2965 | } |
| 2966 | return false ; |
| 2967 | } |
| 2968 | |
| 2969 | bool wxFileName::MacGetTypeAndCreator( wxUint32 *type , wxUint32 *creator ) const |
| 2970 | { |
| 2971 | FSRef fsRef ; |
| 2972 | FSCatalogInfo catInfo; |
| 2973 | FileInfo *finfo ; |
| 2974 | |
| 2975 | if ( wxMacPathToFSRef( GetFullPath() , &fsRef ) == noErr ) |
| 2976 | { |
| 2977 | if ( FSGetCatalogInfo (&fsRef, kFSCatInfoFinderInfo, &catInfo, NULL, NULL, NULL) == noErr ) |
| 2978 | { |
| 2979 | finfo = (FileInfo*)&catInfo.finderInfo; |
| 2980 | *type = finfo->fileType ; |
| 2981 | *creator = finfo->fileCreator ; |
| 2982 | return true ; |
| 2983 | } |
| 2984 | } |
| 2985 | return false ; |
| 2986 | } |
| 2987 | |
| 2988 | bool wxFileName::MacSetDefaultTypeAndCreator() |
| 2989 | { |
| 2990 | wxUint32 type , creator ; |
| 2991 | if ( wxFileName::MacFindDefaultTypeAndCreator(GetExt() , &type , |
| 2992 | &creator ) ) |
| 2993 | { |
| 2994 | return MacSetTypeAndCreator( type , creator ) ; |
| 2995 | } |
| 2996 | return false; |
| 2997 | } |
| 2998 | |
| 2999 | bool wxFileName::MacFindDefaultTypeAndCreator( const wxString& ext , wxUint32 *type , wxUint32 *creator ) |
| 3000 | { |
| 3001 | MacEnsureDefaultExtensionsLoaded() ; |
| 3002 | wxString extl = ext.Lower() ; |
| 3003 | for( int i = gMacDefaultExtensions.Count() - 1 ; i >= 0 ; --i ) |
| 3004 | { |
| 3005 | if ( gMacDefaultExtensions.Item(i).m_ext == extl ) |
| 3006 | { |
| 3007 | *type = gMacDefaultExtensions.Item(i).m_type ; |
| 3008 | *creator = gMacDefaultExtensions.Item(i).m_creator ; |
| 3009 | return true ; |
| 3010 | } |
| 3011 | } |
| 3012 | return false ; |
| 3013 | } |
| 3014 | |
| 3015 | void wxFileName::MacRegisterDefaultTypeAndCreator( const wxString& ext , wxUint32 type , wxUint32 creator ) |
| 3016 | { |
| 3017 | MacEnsureDefaultExtensionsLoaded(); |
| 3018 | MacDefaultExtensionRecord rec(ext.Lower(), type, creator); |
| 3019 | gMacDefaultExtensions.Add( rec ); |
| 3020 | } |
| 3021 | |
| 3022 | #endif // defined( __WXOSX_MAC__ ) && wxOSX_USE_CARBON |