]>
Commit | Line | Data |
---|---|---|
df5ddbca | 1 | ///////////////////////////////////////////////////////////////////////////// |
097ead30 VZ |
2 | // Name: src/common/filename.cpp |
3 | // Purpose: wxFileName - encapsulates a file path | |
844f90fb | 4 | // Author: Robert Roebling, Vadim Zeitlin |
df5ddbca RR |
5 | // Modified by: |
6 | // Created: 28.12.2000 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 2000 Robert Roebling | |
9 | // Licence: wxWindows license | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
097ead30 VZ |
12 | // ============================================================================ |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
df5ddbca RR |
20 | #ifdef __GNUG__ |
21 | #pragma implementation "filename.h" | |
22 | #endif | |
23 | ||
24 | // For compilers that support precompilation, includes "wx.h". | |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #ifdef __BORLANDC__ | |
28 | #pragma hdrstop | |
29 | #endif | |
30 | ||
31 | #ifndef WX_PRECOMP | |
32 | #include "wx/intl.h" | |
33 | #include "wx/log.h" | |
34 | #endif | |
35 | ||
36 | #include "wx/filename.h" | |
37 | #include "wx/tokenzr.h" | |
844f90fb | 38 | #include "wx/config.h" // for wxExpandEnvVars |
a35b27b1 | 39 | #include "wx/utils.h" |
5d978d07 JS |
40 | |
41 | #if wxUSE_DYNLIB_CLASS | |
05e7001c | 42 | #include "wx/dynlib.h" |
5d978d07 | 43 | #endif |
df5ddbca | 44 | |
9e9b65c1 JS |
45 | // For GetShort/LongPathName |
46 | #ifdef __WIN32__ | |
47 | #include <windows.h> | |
48 | #include "wx/msw/winundef.h" | |
49 | #endif | |
50 | ||
097ead30 VZ |
51 | // ============================================================================ |
52 | // implementation | |
53 | // ============================================================================ | |
54 | ||
55 | // ---------------------------------------------------------------------------- | |
844f90fb | 56 | // wxFileName construction |
097ead30 | 57 | // ---------------------------------------------------------------------------- |
df5ddbca | 58 | |
a35b27b1 RR |
59 | void wxFileName::Assign( const wxFileName &filepath ) |
60 | { | |
a35b27b1 RR |
61 | m_ext = filepath.GetExt(); |
62 | m_name = filepath.GetName(); | |
844f90fb | 63 | m_dirs = filepath.GetDirs(); |
df5ddbca RR |
64 | } |
65 | ||
844f90fb VZ |
66 | void wxFileName::Assign( const wxString& path, |
67 | const wxString& name, | |
68 | const wxString& ext, | |
69 | wxPathFormat format ) | |
df5ddbca | 70 | { |
844f90fb VZ |
71 | wxStringTokenizer tn(path, GetPathSeparators(format), |
72 | wxTOKEN_RET_EMPTY_ALL); | |
4fdfb558 | 73 | bool first = TRUE; |
844f90fb VZ |
74 | m_dirs.Clear(); |
75 | while ( tn.HasMoreTokens() ) | |
df5ddbca | 76 | { |
844f90fb VZ |
77 | wxString token = tn.GetNextToken(); |
78 | ||
4fdfb558 RR |
79 | // If the path starts with a slash, we need the first |
80 | // dir entry to be an empty for later reassembly. | |
4fdfb558 | 81 | if (first || !token.IsEmpty()) |
df5ddbca | 82 | m_dirs.Add( token ); |
844f90fb | 83 | |
4fdfb558 | 84 | first = FALSE; |
df5ddbca | 85 | } |
844f90fb VZ |
86 | |
87 | m_ext = ext; | |
88 | m_name = name; | |
89 | } | |
90 | ||
91 | void wxFileName::Assign(const wxString& fullpath, | |
92 | wxPathFormat format) | |
93 | { | |
94 | wxString path, name, ext; | |
9e8d8607 | 95 | SplitPath(fullpath, &path, &name, &ext, format); |
844f90fb VZ |
96 | |
97 | Assign(path, name, ext, format); | |
98 | } | |
99 | ||
100 | void wxFileName::Assign(const wxString& path, | |
101 | const wxString& fullname, | |
102 | wxPathFormat format) | |
103 | { | |
104 | wxString name, ext; | |
9e8d8607 | 105 | SplitPath(fullname, NULL /* no path */, &name, &ext, format); |
844f90fb VZ |
106 | |
107 | Assign(path, name, ext, format); | |
108 | } | |
109 | ||
110 | void wxFileName::Clear() | |
111 | { | |
112 | m_dirs.Clear(); | |
113 | m_name = | |
114 | m_ext = wxEmptyString; | |
115 | } | |
116 | ||
117 | /* static */ | |
118 | wxFileName wxFileName::FileName(const wxString& file) | |
119 | { | |
120 | return wxFileName(file); | |
121 | } | |
122 | ||
123 | /* static */ | |
124 | wxFileName wxFileName::DirName(const wxString& dir) | |
125 | { | |
126 | wxFileName fn; | |
127 | fn.AssignDir(dir); | |
128 | return fn; | |
df5ddbca RR |
129 | } |
130 | ||
844f90fb VZ |
131 | // ---------------------------------------------------------------------------- |
132 | // existence tests | |
133 | // ---------------------------------------------------------------------------- | |
134 | ||
df5ddbca RR |
135 | bool wxFileName::FileExists() |
136 | { | |
a35b27b1 RR |
137 | return wxFileName::FileExists( GetFullPath() ); |
138 | } | |
139 | ||
140 | bool wxFileName::FileExists( const wxString &file ) | |
141 | { | |
142 | return ::wxFileExists( file ); | |
df5ddbca RR |
143 | } |
144 | ||
145 | bool wxFileName::DirExists() | |
146 | { | |
a35b27b1 RR |
147 | return wxFileName::DirExists( GetFullPath() ); |
148 | } | |
149 | ||
150 | bool wxFileName::DirExists( const wxString &dir ) | |
151 | { | |
152 | return ::wxDirExists( dir ); | |
df5ddbca RR |
153 | } |
154 | ||
ce16e5d7 RR |
155 | wxDateTime wxFileName::GetModificationTime() |
156 | { | |
942bef71 | 157 | wxDateTime ret( wxFileModificationTime( GetFullPath() ) ); |
ce16e5d7 | 158 | |
ce16e5d7 RR |
159 | return ret; |
160 | } | |
161 | ||
844f90fb VZ |
162 | // ---------------------------------------------------------------------------- |
163 | // CWD and HOME stuff | |
164 | // ---------------------------------------------------------------------------- | |
165 | ||
df5ddbca RR |
166 | void wxFileName::AssignCwd() |
167 | { | |
844f90fb | 168 | AssignDir(wxFileName::GetCwd()); |
a35b27b1 RR |
169 | } |
170 | ||
844f90fb | 171 | /* static */ |
a35b27b1 RR |
172 | wxString wxFileName::GetCwd() |
173 | { | |
174 | return ::wxGetCwd(); | |
175 | } | |
176 | ||
177 | bool wxFileName::SetCwd() | |
178 | { | |
179 | return wxFileName::SetCwd( GetFullPath() ); | |
df5ddbca RR |
180 | } |
181 | ||
a35b27b1 | 182 | bool wxFileName::SetCwd( const wxString &cwd ) |
df5ddbca | 183 | { |
a35b27b1 | 184 | return ::wxSetWorkingDirectory( cwd ); |
df5ddbca RR |
185 | } |
186 | ||
a35b27b1 RR |
187 | void wxFileName::AssignHomeDir() |
188 | { | |
844f90fb | 189 | AssignDir(wxFileName::GetHomeDir()); |
a35b27b1 | 190 | } |
844f90fb | 191 | |
a35b27b1 RR |
192 | wxString wxFileName::GetHomeDir() |
193 | { | |
194 | return ::wxGetHomeDir(); | |
195 | } | |
844f90fb | 196 | |
df5ddbca RR |
197 | void wxFileName::AssignTempFileName( const wxString &prefix ) |
198 | { | |
844f90fb VZ |
199 | wxString fullname; |
200 | if ( wxGetTempFileName(prefix, fullname) ) | |
201 | { | |
202 | Assign(fullname); | |
203 | } | |
204 | else // error | |
205 | { | |
206 | Clear(); | |
207 | } | |
df5ddbca RR |
208 | } |
209 | ||
844f90fb VZ |
210 | // ---------------------------------------------------------------------------- |
211 | // directory operations | |
212 | // ---------------------------------------------------------------------------- | |
213 | ||
f0ce3409 | 214 | bool wxFileName::Mkdir( int perm, bool full ) |
a35b27b1 | 215 | { |
f0ce3409 | 216 | return wxFileName::Mkdir( GetFullPath(), perm, full ); |
a35b27b1 RR |
217 | } |
218 | ||
f0ce3409 | 219 | bool wxFileName::Mkdir( const wxString &dir, int perm, bool full ) |
df5ddbca | 220 | { |
f0ce3409 JS |
221 | if (full) |
222 | { | |
223 | wxFileName filename(dir); | |
224 | wxArrayString dirs = filename.GetDirs(); | |
77fe02a8 | 225 | dirs.Add(filename.GetName()); |
f0ce3409 JS |
226 | |
227 | size_t count = dirs.GetCount(); | |
228 | size_t i; | |
229 | wxString currPath; | |
230 | int noErrors = 0; | |
231 | for ( i = 0; i < count; i++ ) | |
232 | { | |
233 | currPath += dirs[i]; | |
234 | ||
235 | if (currPath.Last() == wxT(':')) | |
236 | { | |
237 | // Can't create a root directory so continue to next dir | |
238 | currPath += wxFILE_SEP_PATH; | |
239 | continue; | |
240 | } | |
241 | ||
242 | if (!DirExists(currPath)) | |
243 | if (!wxMkdir(currPath, perm)) | |
244 | noErrors ++; | |
245 | ||
246 | if ( (i < (count-1)) ) | |
247 | currPath += wxFILE_SEP_PATH; | |
248 | } | |
249 | ||
250 | return (noErrors == 0); | |
251 | ||
252 | } | |
253 | else | |
254 | return ::wxMkdir( dir, perm ); | |
df5ddbca RR |
255 | } |
256 | ||
a35b27b1 | 257 | bool wxFileName::Rmdir() |
df5ddbca | 258 | { |
a35b27b1 | 259 | return wxFileName::Rmdir( GetFullPath() ); |
df5ddbca RR |
260 | } |
261 | ||
a35b27b1 | 262 | bool wxFileName::Rmdir( const wxString &dir ) |
df5ddbca | 263 | { |
a35b27b1 | 264 | return ::wxRmdir( dir ); |
df5ddbca RR |
265 | } |
266 | ||
844f90fb VZ |
267 | // ---------------------------------------------------------------------------- |
268 | // path normalization | |
269 | // ---------------------------------------------------------------------------- | |
270 | ||
271 | bool wxFileName::Normalize(wxPathNormalize flags, | |
272 | const wxString& cwd, | |
273 | wxPathFormat format) | |
a35b27b1 | 274 | { |
844f90fb VZ |
275 | // the existing path components |
276 | wxArrayString dirs = GetDirs(); | |
277 | ||
278 | // the path to prepend in front to make the path absolute | |
279 | wxFileName curDir; | |
280 | ||
281 | format = GetFormat(format); | |
282 | ||
283 | // make the path absolute | |
284 | if ( (flags & wxPATH_NORM_ABSOLUTE) && !IsAbsolute() ) | |
a35b27b1 | 285 | { |
844f90fb VZ |
286 | if ( cwd.empty() ) |
287 | curDir.AssignCwd(); | |
a35b27b1 | 288 | else |
844f90fb | 289 | curDir.AssignDir(cwd); |
a35b27b1 | 290 | } |
844f90fb VZ |
291 | |
292 | // handle ~ stuff under Unix only | |
293 | if ( (format == wxPATH_UNIX) && (flags & wxPATH_NORM_TILDE) ) | |
a35b27b1 | 294 | { |
844f90fb VZ |
295 | if ( !dirs.IsEmpty() ) |
296 | { | |
297 | wxString dir = dirs[0u]; | |
298 | if ( !dir.empty() && dir[0u] == _T('~') ) | |
299 | { | |
300 | curDir.AssignDir(wxGetUserHome(dir.c_str() + 1)); | |
301 | ||
da2fd5ac | 302 | dirs.RemoveAt(0u); |
844f90fb VZ |
303 | } |
304 | } | |
a35b27b1 | 305 | } |
844f90fb VZ |
306 | |
307 | if ( curDir.IsOk() ) | |
a35b27b1 | 308 | { |
844f90fb VZ |
309 | wxArrayString dirsNew = curDir.GetDirs(); |
310 | size_t count = dirs.GetCount(); | |
311 | for ( size_t n = 0; n < count; n++ ) | |
312 | { | |
313 | dirsNew.Add(dirs[n]); | |
314 | } | |
315 | ||
316 | dirs = dirsNew; | |
a35b27b1 | 317 | } |
844f90fb VZ |
318 | |
319 | // now deal with ".", ".." and the rest | |
320 | m_dirs.Empty(); | |
321 | size_t count = dirs.GetCount(); | |
322 | for ( size_t n = 0; n < count; n++ ) | |
a35b27b1 | 323 | { |
844f90fb VZ |
324 | wxString dir = dirs[n]; |
325 | ||
326 | if ( flags && wxPATH_NORM_DOTS ) | |
327 | { | |
328 | if ( dir == wxT(".") ) | |
329 | { | |
330 | // just ignore | |
331 | continue; | |
332 | } | |
333 | ||
334 | if ( dir == wxT("..") ) | |
335 | { | |
336 | if ( m_dirs.IsEmpty() ) | |
337 | { | |
338 | wxLogError(_("The path '%s' contains too many \"..\"!"), | |
339 | GetFullPath().c_str()); | |
340 | return FALSE; | |
341 | } | |
342 | ||
343 | m_dirs.Remove(m_dirs.GetCount() - 1); | |
344 | continue; | |
345 | } | |
346 | } | |
347 | ||
348 | if ( flags & wxPATH_NORM_ENV_VARS ) | |
a35b27b1 | 349 | { |
844f90fb | 350 | dir = wxExpandEnvVars(dir); |
a35b27b1 | 351 | } |
844f90fb VZ |
352 | |
353 | if ( (flags & wxPATH_NORM_CASE) && !IsCaseSensitive(format) ) | |
354 | { | |
355 | dir.MakeLower(); | |
356 | } | |
357 | ||
358 | m_dirs.Add(dir); | |
a35b27b1 | 359 | } |
844f90fb VZ |
360 | |
361 | if ( (flags & wxPATH_NORM_CASE) && !IsCaseSensitive(format) ) | |
362 | { | |
363 | // VZ: expand env vars here too? | |
364 | ||
365 | m_name.MakeLower(); | |
366 | m_ext.MakeLower(); | |
367 | } | |
368 | ||
9e9b65c1 JS |
369 | #if defined(__WXMSW__) && defined(__WIN32__) |
370 | if (flags & wxPATH_NORM_LONG) | |
371 | { | |
372 | Assign(GetLongPath()); | |
373 | } | |
374 | #endif | |
375 | ||
a35b27b1 RR |
376 | return TRUE; |
377 | } | |
378 | ||
844f90fb VZ |
379 | // ---------------------------------------------------------------------------- |
380 | // filename kind tests | |
381 | // ---------------------------------------------------------------------------- | |
382 | ||
383 | bool wxFileName::SameAs( const wxFileName &filepath, wxPathFormat format) | |
df5ddbca | 384 | { |
844f90fb VZ |
385 | wxFileName fn1 = *this, |
386 | fn2 = filepath; | |
387 | ||
388 | // get cwd only once - small time saving | |
389 | wxString cwd = wxGetCwd(); | |
390 | fn1.Normalize(wxPATH_NORM_ALL, cwd, format); | |
391 | fn2.Normalize(wxPATH_NORM_ALL, cwd, format); | |
392 | ||
393 | if ( fn1.GetFullPath() == fn2.GetFullPath() ) | |
394 | return TRUE; | |
395 | ||
396 | // TODO: compare inodes for Unix, this works even when filenames are | |
397 | // different but files are the same (symlinks) (VZ) | |
398 | ||
399 | return FALSE; | |
df5ddbca RR |
400 | } |
401 | ||
844f90fb | 402 | /* static */ |
df5ddbca RR |
403 | bool wxFileName::IsCaseSensitive( wxPathFormat format ) |
404 | { | |
844f90fb VZ |
405 | // only DOS filenames are case-sensitive |
406 | return GetFormat(format) != wxPATH_DOS; | |
df5ddbca RR |
407 | } |
408 | ||
409 | bool wxFileName::IsRelative( wxPathFormat format ) | |
410 | { | |
844f90fb VZ |
411 | return !IsAbsolute(format); |
412 | } | |
413 | ||
414 | bool wxFileName::IsAbsolute( wxPathFormat format ) | |
415 | { | |
416 | wxChar ch = m_dirs.IsEmpty() ? _T('\0') : m_dirs[0u][0u]; | |
417 | ||
9e9b65c1 JS |
418 | // Hack to cope with e.g. c:\thing - need something better |
419 | wxChar driveSep = _T('\0'); | |
420 | if (!m_dirs.IsEmpty() && m_dirs[0].Length() > 1) | |
421 | driveSep = m_dirs[0u][1u]; | |
422 | ||
844f90fb VZ |
423 | // the path is absolute if it starts with a path separator or, only for |
424 | // Unix filenames, with "~" or "~user" | |
425 | return IsPathSeparator(ch, format) || | |
9e9b65c1 | 426 | driveSep == _T(':') || |
844f90fb VZ |
427 | (GetFormat(format) == wxPATH_UNIX && ch == _T('~') ); |
428 | } | |
429 | ||
430 | /* static */ | |
431 | wxString wxFileName::GetPathSeparators(wxPathFormat format) | |
432 | { | |
433 | wxString seps; | |
434 | switch ( GetFormat(format) ) | |
df5ddbca | 435 | { |
844f90fb VZ |
436 | case wxPATH_DOS: |
437 | // accept both as native APIs do | |
9e8d8607 | 438 | seps << wxFILE_SEP_PATH_UNIX << wxFILE_SEP_PATH_DOS; |
844f90fb VZ |
439 | break; |
440 | ||
441 | default: | |
442 | wxFAIL_MSG( _T("unknown wxPATH_XXX style") ); | |
443 | // fall through | |
444 | ||
445 | case wxPATH_UNIX: | |
9e8d8607 | 446 | seps = wxFILE_SEP_PATH_UNIX; |
844f90fb VZ |
447 | break; |
448 | ||
449 | case wxPATH_MAC: | |
9e8d8607 | 450 | seps = wxFILE_SEP_PATH_MAC; |
844f90fb | 451 | break; |
df5ddbca RR |
452 | } |
453 | ||
844f90fb | 454 | return seps; |
df5ddbca RR |
455 | } |
456 | ||
844f90fb VZ |
457 | /* static */ |
458 | bool wxFileName::IsPathSeparator(wxChar ch, wxPathFormat format) | |
df5ddbca | 459 | { |
844f90fb | 460 | return GetPathSeparators(format).Find(ch) != wxNOT_FOUND; |
df5ddbca RR |
461 | } |
462 | ||
463 | bool wxFileName::IsWild( wxPathFormat format ) | |
464 | { | |
844f90fb VZ |
465 | // FIXME: this is probably false for Mac and this is surely wrong for most |
466 | // of Unix shells (think about "[...]") | |
33ac7e6f | 467 | (void)format; |
844f90fb | 468 | return m_name.find_first_of(_T("*?")) != wxString::npos; |
df5ddbca RR |
469 | } |
470 | ||
844f90fb VZ |
471 | // ---------------------------------------------------------------------------- |
472 | // path components manipulation | |
473 | // ---------------------------------------------------------------------------- | |
474 | ||
df5ddbca RR |
475 | void wxFileName::AppendDir( const wxString &dir ) |
476 | { | |
477 | m_dirs.Add( dir ); | |
478 | } | |
479 | ||
480 | void wxFileName::PrependDir( const wxString &dir ) | |
481 | { | |
482 | m_dirs.Insert( dir, 0 ); | |
483 | } | |
484 | ||
485 | void wxFileName::InsertDir( int before, const wxString &dir ) | |
486 | { | |
487 | m_dirs.Insert( dir, before ); | |
488 | } | |
489 | ||
490 | void wxFileName::RemoveDir( int pos ) | |
491 | { | |
492 | m_dirs.Remove( (size_t)pos ); | |
493 | } | |
494 | ||
844f90fb VZ |
495 | // ---------------------------------------------------------------------------- |
496 | // accessors | |
497 | // ---------------------------------------------------------------------------- | |
498 | ||
7124df9b VZ |
499 | void wxFileName::SetFullName(const wxString& fullname) |
500 | { | |
501 | SplitPath(fullname, NULL /* no path */, &m_name, &m_ext); | |
502 | } | |
503 | ||
844f90fb | 504 | wxString wxFileName::GetFullName() const |
a35b27b1 | 505 | { |
844f90fb VZ |
506 | wxString fullname = m_name; |
507 | if ( !m_ext.empty() ) | |
a35b27b1 | 508 | { |
9e8d8607 | 509 | fullname << wxFILE_SEP_EXT << m_ext; |
a35b27b1 | 510 | } |
a35b27b1 | 511 | |
844f90fb | 512 | return fullname; |
a35b27b1 RR |
513 | } |
514 | ||
515 | wxString wxFileName::GetPath( bool add_separator, wxPathFormat format ) const | |
df5ddbca RR |
516 | { |
517 | format = GetFormat( format ); | |
844f90fb | 518 | |
df5ddbca | 519 | wxString ret; |
844f90fb VZ |
520 | size_t count = m_dirs.GetCount(); |
521 | for ( size_t i = 0; i < count; i++ ) | |
df5ddbca | 522 | { |
844f90fb VZ |
523 | ret += m_dirs[i]; |
524 | if ( add_separator || (i < count) ) | |
525 | ret += wxFILE_SEP_PATH; | |
df5ddbca | 526 | } |
844f90fb | 527 | |
df5ddbca RR |
528 | return ret; |
529 | } | |
530 | ||
531 | wxString wxFileName::GetFullPath( wxPathFormat format ) const | |
532 | { | |
33ac7e6f | 533 | (void)format; |
844f90fb | 534 | return GetPathWithSep() + GetFullName(); |
df5ddbca RR |
535 | } |
536 | ||
9e9b65c1 JS |
537 | // Return the short form of the path (returns identity on non-Windows platforms) |
538 | wxString wxFileName::GetShortPath() const | |
539 | { | |
8cb172b4 | 540 | #if defined(__WXMSW__) && defined(__WIN32__) && !defined(__WXMICROWIN__) |
9e9b65c1 | 541 | wxString path(GetFullPath()); |
75ef5722 JS |
542 | wxString pathOut; |
543 | DWORD sz = ::GetShortPathName(path, NULL, 0); | |
544 | bool ok = sz != 0; | |
545 | if ( ok ) | |
9e9b65c1 | 546 | { |
75ef5722 JS |
547 | ok = ::GetShortPathName |
548 | ( | |
549 | path, | |
550 | pathOut.GetWriteBuf(sz), | |
551 | sz | |
552 | ) != 0; | |
553 | pathOut.UngetWriteBuf(); | |
9e9b65c1 | 554 | } |
75ef5722 JS |
555 | if (ok) |
556 | return pathOut; | |
9e9b65c1 | 557 | else |
75ef5722 | 558 | return path; |
9e9b65c1 JS |
559 | #else |
560 | return GetFullPath(); | |
561 | #endif | |
562 | } | |
563 | ||
564 | // Return the long form of the path (returns identity on non-Windows platforms) | |
565 | wxString wxFileName::GetLongPath() const | |
566 | { | |
8cb172b4 | 567 | #if defined(__WXMSW__) && defined(__WIN32__) && !defined(__WXMICROWIN__) |
9e9b65c1 | 568 | wxString path(GetFullPath()); |
75ef5722 | 569 | wxString pathOut; |
05e7001c JS |
570 | bool success = FALSE; |
571 | ||
5d978d07 | 572 | #if wxUSE_DYNLIB_CLASS |
05e7001c JS |
573 | typedef DWORD (*GET_LONG_PATH_NAME)(const wxChar *, wxChar *, DWORD); |
574 | ||
575 | static bool s_triedToLoad = FALSE; | |
05e7001c JS |
576 | |
577 | if ( !s_triedToLoad ) | |
9e9b65c1 | 578 | { |
05e7001c | 579 | s_triedToLoad = TRUE; |
33ac7e6f | 580 | #if 0 |
05e7001c | 581 | wxDllType dllKernel = wxDllLoader::LoadLibrary(_T("kernel32")); |
33ac7e6f | 582 | if ( dllKernel ) |
05e7001c JS |
583 | { |
584 | // may succeed or fail depending on the Windows version | |
33ac7e6f | 585 | static GET_LONG_PATH_NAME s_pfnGetLongPathName = NULL; |
5d978d07 JS |
586 | #ifdef _UNICODE |
587 | s_pfnGetLongPathName = (GET_LONG_PATH_NAME) wxDllLoader::GetSymbol(dllKernel, _T("GetLongPathNameW")); | |
588 | #else | |
589 | s_pfnGetLongPathName = (GET_LONG_PATH_NAME) wxDllLoader::GetSymbol(dllKernel, _T("GetLongPathNameA")); | |
590 | #endif | |
05e7001c JS |
591 | |
592 | wxDllLoader::UnloadLibrary(dllKernel); | |
593 | ||
594 | if ( s_pfnGetLongPathName ) | |
595 | { | |
596 | DWORD dwSize = (*s_pfnGetLongPathName)(path, NULL, 0); | |
597 | bool ok = dwSize > 0; | |
598 | ||
599 | if ( ok ) | |
600 | { | |
601 | DWORD sz = (*s_pfnGetLongPathName)(path, NULL, 0); | |
602 | ok = sz != 0; | |
603 | if ( ok ) | |
604 | { | |
605 | ok = (*s_pfnGetLongPathName) | |
606 | ( | |
607 | path, | |
608 | pathOut.GetWriteBuf(sz), | |
609 | sz | |
610 | ) != 0; | |
611 | pathOut.UngetWriteBuf(); | |
612 | ||
613 | success = TRUE; | |
614 | } | |
615 | } | |
616 | } | |
617 | } | |
33ac7e6f | 618 | #endif |
9e9b65c1 | 619 | } |
05e7001c | 620 | if (success) |
75ef5722 | 621 | return pathOut; |
5d978d07 JS |
622 | #endif |
623 | // wxUSE_DYNLIB_CLASS | |
05e7001c JS |
624 | |
625 | if (!success) | |
626 | { | |
627 | // The OS didn't support GetLongPathName, or some other error. | |
628 | // We need to call FindFirstFile on each component in turn. | |
629 | ||
630 | WIN32_FIND_DATA findFileData; | |
631 | HANDLE hFind; | |
632 | pathOut = wxEmptyString; | |
633 | ||
77fe02a8 JS |
634 | wxArrayString dirs = GetDirs(); |
635 | dirs.Add(GetName()); | |
636 | ||
637 | size_t count = dirs.GetCount(); | |
05e7001c JS |
638 | size_t i; |
639 | wxString tmpPath; | |
5d978d07 | 640 | |
05e7001c JS |
641 | for ( i = 0; i < count; i++ ) |
642 | { | |
643 | // We're using pathOut to collect the long-name path, | |
644 | // but using a temporary for appending the last path component which may be short-name | |
77fe02a8 | 645 | tmpPath = pathOut + dirs[i]; |
05e7001c JS |
646 | |
647 | if (tmpPath.Last() == wxT(':')) | |
5d978d07 JS |
648 | { |
649 | // Can't pass a drive and root dir to FindFirstFile, | |
650 | // so continue to next dir | |
05e7001c | 651 | tmpPath += wxFILE_SEP_PATH; |
5d978d07 JS |
652 | pathOut = tmpPath; |
653 | continue; | |
654 | } | |
05e7001c JS |
655 | |
656 | hFind = ::FindFirstFile(tmpPath, &findFileData); | |
657 | if (hFind == INVALID_HANDLE_VALUE) | |
658 | { | |
659 | // Error: return immediately with the original path | |
660 | return path; | |
661 | } | |
662 | else | |
663 | { | |
664 | pathOut += findFileData.cFileName; | |
5d978d07 | 665 | if ( (i < (count-1)) ) |
05e7001c JS |
666 | pathOut += wxFILE_SEP_PATH; |
667 | ||
668 | ::FindClose(hFind); | |
669 | } | |
670 | } | |
671 | } | |
672 | return pathOut; | |
9e9b65c1 JS |
673 | #else |
674 | return GetFullPath(); | |
675 | #endif | |
676 | } | |
677 | ||
df5ddbca RR |
678 | wxPathFormat wxFileName::GetFormat( wxPathFormat format ) |
679 | { | |
680 | if (format == wxPATH_NATIVE) | |
681 | { | |
682 | #if defined(__WXMSW__) || defined(__WXPM__) | |
683 | format = wxPATH_DOS; | |
844f90fb | 684 | #elif defined(__WXMAC__) |
2597135a | 685 | format = wxPATH_UNIX; // that's the way the rest of wx' code works right now |
844f90fb | 686 | #else |
df5ddbca RR |
687 | format = wxPATH_UNIX; |
688 | #endif | |
689 | } | |
690 | return format; | |
691 | } | |
a35b27b1 | 692 | |
9e8d8607 VZ |
693 | // ---------------------------------------------------------------------------- |
694 | // path splitting function | |
695 | // ---------------------------------------------------------------------------- | |
696 | ||
697 | void wxFileName::SplitPath(const wxString& fullpath, | |
698 | wxString *pstrPath, | |
699 | wxString *pstrName, | |
700 | wxString *pstrExt, | |
701 | wxPathFormat format) | |
702 | { | |
703 | format = GetFormat(format); | |
704 | ||
705 | // find the positions of the last dot and last path separator in the path | |
706 | size_t posLastDot = fullpath.find_last_of(wxFILE_SEP_EXT); | |
707 | size_t posLastSlash = fullpath.find_last_of(GetPathSeparators(format)); | |
708 | ||
709 | if ( (posLastDot != wxString::npos) && (format == wxPATH_UNIX) ) | |
710 | { | |
711 | if ( (posLastDot == 0) || | |
712 | (fullpath[posLastDot - 1] == wxFILE_SEP_PATH_UNIX) ) | |
713 | { | |
714 | // under Unix, dot may be (and commonly is) the first character of | |
715 | // the filename, don't treat the entire filename as extension in | |
716 | // this case | |
717 | posLastDot = wxString::npos; | |
718 | } | |
719 | } | |
720 | ||
8e7dda21 VZ |
721 | // if we do have a dot and a slash, check that the dot is in the name part |
722 | if ( (posLastDot != wxString::npos) && | |
723 | (posLastSlash != wxString::npos) && | |
724 | (posLastDot < posLastSlash) ) | |
9e8d8607 VZ |
725 | { |
726 | // the dot is part of the path, not the start of the extension | |
727 | posLastDot = wxString::npos; | |
728 | } | |
729 | ||
730 | // now fill in the variables provided by user | |
731 | if ( pstrPath ) | |
732 | { | |
733 | if ( posLastSlash == wxString::npos ) | |
734 | { | |
735 | // no path at all | |
736 | pstrPath->Empty(); | |
737 | } | |
738 | else | |
739 | { | |
740 | // take all until the separator | |
741 | *pstrPath = fullpath.Left(posLastSlash); | |
742 | } | |
743 | } | |
744 | ||
745 | if ( pstrName ) | |
746 | { | |
42b1f941 VZ |
747 | // take all characters starting from the one after the last slash and |
748 | // up to, but excluding, the last dot | |
9e8d8607 | 749 | size_t nStart = posLastSlash == wxString::npos ? 0 : posLastSlash + 1; |
8e7dda21 VZ |
750 | size_t count; |
751 | if ( posLastDot == wxString::npos ) | |
752 | { | |
753 | // take all until the end | |
754 | count = wxString::npos; | |
755 | } | |
756 | else if ( posLastSlash == wxString::npos ) | |
757 | { | |
758 | count = posLastDot; | |
759 | } | |
760 | else // have both dot and slash | |
761 | { | |
762 | count = posLastDot - posLastSlash - 1; | |
763 | } | |
9e8d8607 VZ |
764 | |
765 | *pstrName = fullpath.Mid(nStart, count); | |
766 | } | |
767 | ||
768 | if ( pstrExt ) | |
769 | { | |
770 | if ( posLastDot == wxString::npos ) | |
771 | { | |
772 | // no extension | |
773 | pstrExt->Empty(); | |
774 | } | |
775 | else | |
776 | { | |
777 | // take everything after the dot | |
778 | *pstrExt = fullpath.Mid(posLastDot + 1); | |
779 | } | |
780 | } | |
781 | } |