~ubuntu-dev/wxwidgets2.6/upstream-debian

« back to all changes in this revision

Viewing changes to src/common/filename.cpp

  • Committer: Daniel T Chen
  • Date: 2006-06-26 10:15:11 UTC
  • Revision ID: crimsun@ubuntu.com-20060626101511-a4436cec4c6d9b35
ImportĀ DebianĀ 2.6.3.2.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
// RCS-ID:      $Id: filename.cpp,v 1.158.2.6 2006/03/02 12:46:06 JS Exp $
 
8
// Copyright:   (c) 2000 Robert Roebling
 
9
// Licence:     wxWindows licence
 
10
/////////////////////////////////////////////////////////////////////////////
 
11
 
 
12
/*
 
13
   Here are brief descriptions of the filename formats supported by this class:
 
14
 
 
15
   wxPATH_UNIX: standard Unix format, used under Darwin as well, absolute file
 
16
                names have the form:
 
17
                /dir1/dir2/.../dirN/filename, "." and ".." stand for the
 
18
                current and parent directory respectively, "~" is parsed as the
 
19
                user HOME and "~username" as the HOME of that user
 
20
 
 
21
   wxPATH_DOS:  DOS/Windows format, absolute file names have the form:
 
22
                drive:\dir1\dir2\...\dirN\filename.ext where drive is a single
 
23
                letter. "." and ".." as for Unix but no "~".
 
24
 
 
25
                There are also UNC names of the form \\share\fullpath
 
26
 
 
27
   wxPATH_MAC:  Mac OS 8/9 and Mac OS X under CodeWarrior 7 format, absolute file
 
28
                names have the form
 
29
                    volume:dir1:...:dirN:filename
 
30
                and the relative file names are either
 
31
                    :dir1:...:dirN:filename
 
32
                or just
 
33
                    filename
 
34
                (although :filename works as well).
 
35
                Since the volume is just part of the file path, it is not
 
36
                treated like a separate entity as it is done under DOS and
 
37
                VMS, it is just treated as another dir.
 
38
 
 
39
   wxPATH_VMS:  VMS native format, absolute file names have the form
 
40
                    <device>:[dir1.dir2.dir3]file.txt
 
41
                or
 
42
                    <device>:[000000.dir1.dir2.dir3]file.txt
 
43
 
 
44
                the <device> is the physical device (i.e. disk). 000000 is the
 
45
                root directory on the device which can be omitted.
 
46
 
 
47
                Note that VMS uses different separators unlike Unix:
 
48
                 : always after the device. If the path does not contain : than
 
49
                   the default (the device of the current directory) is assumed.
 
50
                 [ start of directory specification
 
51
                 . separator between directory and subdirectory
 
52
                 ] between directory and file
 
53
 */
 
54
 
 
55
// ============================================================================
 
56
// declarations
 
57
// ============================================================================
 
58
 
 
59
// ----------------------------------------------------------------------------
 
60
// headers
 
61
// ----------------------------------------------------------------------------
 
62
 
 
63
#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
 
64
#pragma implementation "filename.h"
 
65
#endif
 
66
 
 
67
// For compilers that support precompilation, includes "wx.h".
 
68
#include "wx/wxprec.h"
 
69
 
 
70
#ifdef __BORLANDC__
 
71
#pragma hdrstop
 
72
#endif
 
73
 
 
74
#ifndef WX_PRECOMP
 
75
#include "wx/intl.h"
 
76
#include "wx/log.h"
 
77
#include "wx/file.h"
 
78
#endif
 
79
 
 
80
#include "wx/filename.h"
 
81
#include "wx/tokenzr.h"
 
82
#include "wx/config.h"          // for wxExpandEnvVars
 
83
#include "wx/utils.h"
 
84
#include "wx/file.h"
 
85
#include "wx/dynlib.h"
 
86
 
 
87
// For GetShort/LongPathName
 
88
#ifdef __WIN32__
 
89
#include "wx/msw/wrapwin.h"
 
90
#if defined(__MINGW32__)
 
91
#include "wx/msw/gccpriv.h"
 
92
#endif
 
93
#endif
 
94
 
 
95
#ifdef __WXWINCE__
 
96
#include "wx/msw/private.h"
 
97
#endif
 
98
 
 
99
#if defined(__WXMAC__)
 
100
  #include  "wx/mac/private.h"  // includes mac headers
 
101
#endif
 
102
 
 
103
// utime() is POSIX so should normally be available on all Unices
 
104
#ifdef __UNIX_LIKE__
 
105
#include <sys/types.h>
 
106
#include <utime.h>
 
107
#include <sys/stat.h>
 
108
#include <unistd.h>
 
109
#endif
 
110
 
 
111
#ifdef __DJGPP__
 
112
#include <unistd.h>
 
113
#endif
 
114
 
 
115
#ifdef __MWERKS__
 
116
#ifdef __MACH__
 
117
#include <sys/types.h>
 
118
#include <utime.h>
 
119
#include <sys/stat.h>
 
120
#include <unistd.h>
 
121
#else
 
122
#include <stat.h>
 
123
#include <unistd.h>
 
124
#include <unix.h>
 
125
#endif
 
126
#endif
 
127
 
 
128
#ifdef __WATCOMC__
 
129
#include <io.h>
 
130
#include <sys/utime.h>
 
131
#include <sys/stat.h>
 
132
#endif
 
133
 
 
134
#ifdef __VISAGECPP__
 
135
#ifndef MAX_PATH
 
136
#define MAX_PATH 256
 
137
#endif
 
138
#endif
 
139
 
 
140
#ifdef __EMX__
 
141
#include <os2.h>
 
142
#define MAX_PATH _MAX_PATH
 
143
#endif
 
144
 
 
145
// ----------------------------------------------------------------------------
 
146
// private classes
 
147
// ----------------------------------------------------------------------------
 
148
 
 
149
// small helper class which opens and closes the file - we use it just to get
 
150
// a file handle for the given file name to pass it to some Win32 API function
 
151
#if defined(__WIN32__) && !defined(__WXMICROWIN__)
 
152
 
 
153
class wxFileHandle
 
154
{
 
155
public:
 
156
    enum OpenMode
 
157
    {
 
158
        Read,
 
159
        Write
 
160
    };
 
161
 
 
162
    wxFileHandle(const wxString& filename, OpenMode mode)
 
163
    {
 
164
        m_hFile = ::CreateFile
 
165
                    (
 
166
                     filename,                      // name
 
167
                     mode == Read ? GENERIC_READ    // access mask
 
168
                                  : GENERIC_WRITE,
 
169
                     FILE_SHARE_READ |              // sharing mode
 
170
                     FILE_SHARE_WRITE,              // (allow everything)
 
171
                     NULL,                          // no secutity attr
 
172
                     OPEN_EXISTING,                 // creation disposition
 
173
                     0,                             // no flags
 
174
                     NULL                           // no template file
 
175
                    );
 
176
 
 
177
        if ( m_hFile == INVALID_HANDLE_VALUE )
 
178
        {
 
179
            wxLogSysError(_("Failed to open '%s' for %s"),
 
180
                          filename.c_str(),
 
181
                          mode == Read ? _("reading") : _("writing"));
 
182
        }
 
183
    }
 
184
 
 
185
    ~wxFileHandle()
 
186
    {
 
187
        if ( m_hFile != INVALID_HANDLE_VALUE )
 
188
        {
 
189
            if ( !::CloseHandle(m_hFile) )
 
190
            {
 
191
                wxLogSysError(_("Failed to close file handle"));
 
192
            }
 
193
        }
 
194
    }
 
195
 
 
196
    // return true only if the file could be opened successfully
 
197
    bool IsOk() const { return m_hFile != INVALID_HANDLE_VALUE; }
 
198
 
 
199
    // get the handle
 
200
    operator HANDLE() const { return m_hFile; }
 
201
 
 
202
private:
 
203
    HANDLE m_hFile;
 
204
};
 
205
 
 
206
#endif // __WIN32__
 
207
 
 
208
// ----------------------------------------------------------------------------
 
209
// private functions
 
210
// ----------------------------------------------------------------------------
 
211
 
 
212
#if wxUSE_DATETIME && defined(__WIN32__) && !defined(__WXMICROWIN__)
 
213
 
 
214
// convert between wxDateTime and FILETIME which is a 64-bit value representing
 
215
// the number of 100-nanosecond intervals since January 1, 1601.
 
216
 
 
217
static void ConvertFileTimeToWx(wxDateTime *dt, const FILETIME &ft)
 
218
{
 
219
    FILETIME ftcopy = ft;
 
220
    FILETIME ftLocal;
 
221
    if ( !::FileTimeToLocalFileTime(&ftcopy, &ftLocal) )
 
222
    {
 
223
        wxLogLastError(_T("FileTimeToLocalFileTime"));
 
224
    }
 
225
 
 
226
    SYSTEMTIME st;
 
227
    if ( !::FileTimeToSystemTime(&ftLocal, &st) )
 
228
    {
 
229
        wxLogLastError(_T("FileTimeToSystemTime"));
 
230
    }
 
231
 
 
232
    dt->Set(st.wDay, wxDateTime::Month(st.wMonth - 1), st.wYear,
 
233
            st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
 
234
}
 
235
 
 
236
static void ConvertWxToFileTime(FILETIME *ft, const wxDateTime& dt)
 
237
{
 
238
    SYSTEMTIME st;
 
239
    st.wDay = dt.GetDay();
 
240
    st.wMonth = (WORD)(dt.GetMonth() + 1);
 
241
    st.wYear = (WORD)dt.GetYear();
 
242
    st.wHour = dt.GetHour();
 
243
    st.wMinute = dt.GetMinute();
 
244
    st.wSecond = dt.GetSecond();
 
245
    st.wMilliseconds = dt.GetMillisecond();
 
246
 
 
247
    FILETIME ftLocal;
 
248
    if ( !::SystemTimeToFileTime(&st, &ftLocal) )
 
249
    {
 
250
        wxLogLastError(_T("SystemTimeToFileTime"));
 
251
    }
 
252
 
 
253
    if ( !::LocalFileTimeToFileTime(&ftLocal, ft) )
 
254
    {
 
255
        wxLogLastError(_T("LocalFileTimeToFileTime"));
 
256
    }
 
257
}
 
258
 
 
259
#endif // wxUSE_DATETIME && __WIN32__
 
260
 
 
261
// return a string with the volume par
 
262
static wxString wxGetVolumeString(const wxString& volume, wxPathFormat format)
 
263
{
 
264
    wxString path;
 
265
 
 
266
    if ( !volume.empty() )
 
267
    {
 
268
        format = wxFileName::GetFormat(format);
 
269
 
 
270
        // Special Windows UNC paths hack, part 2: undo what we did in
 
271
        // SplitPath() and make an UNC path if we have a drive which is not a
 
272
        // single letter (hopefully the network shares can't be one letter only
 
273
        // although I didn't find any authoritative docs on this)
 
274
        if ( format == wxPATH_DOS && volume.length() > 1 )
 
275
        {
 
276
            path << wxFILE_SEP_PATH_DOS << wxFILE_SEP_PATH_DOS << volume;
 
277
        }
 
278
        else if  ( format == wxPATH_DOS || format == wxPATH_VMS )
 
279
        {
 
280
            path << volume << wxFileName::GetVolumeSeparator(format);
 
281
        }
 
282
        // else ignore
 
283
    }
 
284
 
 
285
    return path;
 
286
}
 
287
 
 
288
// ============================================================================
 
289
// implementation
 
290
// ============================================================================
 
291
 
 
292
// ----------------------------------------------------------------------------
 
293
// wxFileName construction
 
294
// ----------------------------------------------------------------------------
 
295
 
 
296
void wxFileName::Assign( const wxFileName &filepath )
 
297
{
 
298
    m_volume = filepath.GetVolume();
 
299
    m_dirs = filepath.GetDirs();
 
300
    m_name = filepath.GetName();
 
301
    m_ext = filepath.GetExt();
 
302
    m_relative = filepath.m_relative;
 
303
    m_hasExt = filepath.m_hasExt;
 
304
}
 
305
 
 
306
void wxFileName::Assign(const wxString& volume,
 
307
                        const wxString& path,
 
308
                        const wxString& name,
 
309
                        const wxString& ext,
 
310
                        bool hasExt,
 
311
                        wxPathFormat format )
 
312
{
 
313
    SetPath( path, format );
 
314
 
 
315
    m_volume = volume;
 
316
    m_ext = ext;
 
317
    m_name = name;
 
318
 
 
319
    m_hasExt = hasExt;
 
320
}
 
321
 
 
322
void wxFileName::SetPath( const wxString& pathOrig, wxPathFormat format )
 
323
{
 
324
    m_dirs.Clear();
 
325
 
 
326
    if ( pathOrig.empty() )
 
327
    {
 
328
        // no path at all
 
329
        m_relative = true;
 
330
 
 
331
        return;
 
332
    }
 
333
 
 
334
    format = GetFormat( format );
 
335
 
 
336
    // 0) deal with possible volume part first
 
337
    wxString volume,
 
338
             path;
 
339
    SplitVolume(pathOrig, &volume, &path, format);
 
340
    if ( !volume.empty() )
 
341
    {
 
342
        m_relative = false;
 
343
 
 
344
        SetVolume(volume);
 
345
    }
 
346
 
 
347
    // 1) Determine if the path is relative or absolute.
 
348
    wxChar leadingChar = path[0u];
 
349
 
 
350
    switch (format)
 
351
    {
 
352
        case wxPATH_MAC:
 
353
            m_relative = leadingChar == wxT(':');
 
354
 
 
355
            // We then remove a leading ":". The reason is in our
 
356
            // storage form for relative paths:
 
357
            // ":dir:file.txt" actually means "./dir/file.txt" in
 
358
            // DOS notation and should get stored as
 
359
            // (relative) (dir) (file.txt)
 
360
            // "::dir:file.txt" actually means "../dir/file.txt"
 
361
            // stored as (relative) (..) (dir) (file.txt)
 
362
            // This is important only for the Mac as an empty dir
 
363
            // actually means <UP>, whereas under DOS, double
 
364
            // slashes can be ignored: "\\\\" is the same as "\\".
 
365
            if (m_relative)
 
366
                path.erase( 0, 1 );
 
367
            break;
 
368
 
 
369
        case wxPATH_VMS:
 
370
            // TODO: what is the relative path format here?
 
371
            m_relative = false;
 
372
            break;
 
373
 
 
374
        default:
 
375
            wxFAIL_MSG( _T("Unknown path format") );
 
376
            // !! Fall through !!
 
377
 
 
378
        case wxPATH_UNIX:
 
379
            // the paths of the form "~" or "~username" are absolute
 
380
            m_relative = leadingChar != wxT('/') && leadingChar != _T('~');
 
381
            break;
 
382
 
 
383
        case wxPATH_DOS:
 
384
            m_relative = !IsPathSeparator(leadingChar, format);
 
385
            break;
 
386
 
 
387
    }
 
388
 
 
389
    // 2) Break up the path into its members. If the original path
 
390
    //    was just "/" or "\\", m_dirs will be empty. We know from
 
391
    //    the m_relative field, if this means "nothing" or "root dir".
 
392
 
 
393
    wxStringTokenizer tn( path, GetPathSeparators(format) );
 
394
 
 
395
    while ( tn.HasMoreTokens() )
 
396
    {
 
397
        wxString token = tn.GetNextToken();
 
398
 
 
399
        // Remove empty token under DOS and Unix, interpret them
 
400
        // as .. under Mac.
 
401
        if (token.empty())
 
402
        {
 
403
            if (format == wxPATH_MAC)
 
404
                m_dirs.Add( wxT("..") );
 
405
            // else ignore
 
406
        }
 
407
        else
 
408
        {
 
409
           m_dirs.Add( token );
 
410
        }
 
411
    }
 
412
}
 
413
 
 
414
void wxFileName::Assign(const wxString& fullpath,
 
415
                        wxPathFormat format)
 
416
{
 
417
    wxString volume, path, name, ext;
 
418
    bool hasExt;
 
419
    SplitPath(fullpath, &volume, &path, &name, &ext, &hasExt, format);
 
420
 
 
421
    Assign(volume, path, name, ext, hasExt, format);
 
422
}
 
423
 
 
424
void wxFileName::Assign(const wxString& fullpathOrig,
 
425
                        const wxString& fullname,
 
426
                        wxPathFormat format)
 
427
{
 
428
    // always recognize fullpath as directory, even if it doesn't end with a
 
429
    // slash
 
430
    wxString fullpath = fullpathOrig;
 
431
    if ( !wxEndsWithPathSeparator(fullpath) )
 
432
    {
 
433
        fullpath += GetPathSeparator(format);
 
434
    }
 
435
 
 
436
    wxString volume, path, name, ext;
 
437
    bool hasExt;
 
438
 
 
439
    // do some consistency checks in debug mode: the name should be really just
 
440
    // the filename and the path should be really just a path
 
441
#ifdef __WXDEBUG__
 
442
    wxString volDummy, pathDummy, nameDummy, extDummy;
 
443
 
 
444
    SplitPath(fullname, &volDummy, &pathDummy, &name, &ext, &hasExt, format);
 
445
 
 
446
    wxASSERT_MSG( volDummy.empty() && pathDummy.empty(),
 
447
                  _T("the file name shouldn't contain the path") );
 
448
 
 
449
    SplitPath(fullpath, &volume, &path, &nameDummy, &extDummy, format);
 
450
 
 
451
    wxASSERT_MSG( nameDummy.empty() && extDummy.empty(),
 
452
                  _T("the path shouldn't contain file name nor extension") );
 
453
 
 
454
#else // !__WXDEBUG__
 
455
    SplitPath(fullname, NULL /* no volume */, NULL /* no path */,
 
456
                        &name, &ext, &hasExt, format);
 
457
    SplitPath(fullpath, &volume, &path, NULL, NULL, format);
 
458
#endif // __WXDEBUG__/!__WXDEBUG__
 
459
 
 
460
    Assign(volume, path, name, ext, hasExt, format);
 
461
}
 
462
 
 
463
void wxFileName::Assign(const wxString& pathOrig,
 
464
                        const wxString& name,
 
465
                        const wxString& ext,
 
466
                        wxPathFormat format)
 
467
{
 
468
    wxString volume,
 
469
             path;
 
470
    SplitVolume(pathOrig, &volume, &path, format);
 
471
 
 
472
    Assign(volume, path, name, ext, format);
 
473
}
 
474
 
 
475
void wxFileName::AssignDir(const wxString& dir, wxPathFormat format)
 
476
{
 
477
    Assign(dir, wxEmptyString, format);
 
478
}
 
479
 
 
480
void wxFileName::Clear()
 
481
{
 
482
    m_dirs.Clear();
 
483
 
 
484
    m_volume =
 
485
    m_name =
 
486
    m_ext = wxEmptyString;
 
487
 
 
488
    // we don't have any absolute path for now
 
489
    m_relative = true;
 
490
 
 
491
    // nor any extension
 
492
    m_hasExt = false;
 
493
}
 
494
 
 
495
/* static */
 
496
wxFileName wxFileName::FileName(const wxString& file, wxPathFormat format)
 
497
{
 
498
    return wxFileName(file, format);
 
499
}
 
500
 
 
501
/* static */
 
502
wxFileName wxFileName::DirName(const wxString& dir, wxPathFormat format)
 
503
{
 
504
    wxFileName fn;
 
505
    fn.AssignDir(dir, format);
 
506
    return fn;
 
507
}
 
508
 
 
509
// ----------------------------------------------------------------------------
 
510
// existence tests
 
511
// ----------------------------------------------------------------------------
 
512
 
 
513
bool wxFileName::FileExists() const
 
514
{
 
515
    return wxFileName::FileExists( GetFullPath() );
 
516
}
 
517
 
 
518
bool wxFileName::FileExists( const wxString &file )
 
519
{
 
520
    return ::wxFileExists( file );
 
521
}
 
522
 
 
523
bool wxFileName::DirExists() const
 
524
{
 
525
    return wxFileName::DirExists( GetFullPath() );
 
526
}
 
527
 
 
528
bool wxFileName::DirExists( const wxString &dir )
 
529
{
 
530
    return ::wxDirExists( dir );
 
531
}
 
532
 
 
533
// ----------------------------------------------------------------------------
 
534
// CWD and HOME stuff
 
535
// ----------------------------------------------------------------------------
 
536
 
 
537
void wxFileName::AssignCwd(const wxString& volume)
 
538
{
 
539
    AssignDir(wxFileName::GetCwd(volume));
 
540
}
 
541
 
 
542
/* static */
 
543
wxString wxFileName::GetCwd(const wxString& volume)
 
544
{
 
545
    // if we have the volume, we must get the current directory on this drive
 
546
    // and to do this we have to chdir to this volume - at least under Windows,
 
547
    // I don't know how to get the current drive on another volume elsewhere
 
548
    // (TODO)
 
549
    wxString cwdOld;
 
550
    if ( !volume.empty() )
 
551
    {
 
552
        cwdOld = wxGetCwd();
 
553
        SetCwd(volume + GetVolumeSeparator());
 
554
    }
 
555
 
 
556
    wxString cwd = ::wxGetCwd();
 
557
 
 
558
    if ( !volume.empty() )
 
559
    {
 
560
        SetCwd(cwdOld);
 
561
    }
 
562
 
 
563
    return cwd;
 
564
}
 
565
 
 
566
bool wxFileName::SetCwd()
 
567
{
 
568
    return wxFileName::SetCwd( GetFullPath() );
 
569
}
 
570
 
 
571
bool wxFileName::SetCwd( const wxString &cwd )
 
572
{
 
573
    return ::wxSetWorkingDirectory( cwd );
 
574
}
 
575
 
 
576
void wxFileName::AssignHomeDir()
 
577
{
 
578
    AssignDir(wxFileName::GetHomeDir());
 
579
}
 
580
 
 
581
wxString wxFileName::GetHomeDir()
 
582
{
 
583
    return ::wxGetHomeDir();
 
584
}
 
585
 
 
586
#if wxUSE_FILE
 
587
 
 
588
void wxFileName::AssignTempFileName(const wxString& prefix, wxFile *fileTemp)
 
589
{
 
590
    wxString tempname = CreateTempFileName(prefix, fileTemp);
 
591
    if ( tempname.empty() )
 
592
    {
 
593
        // error, failed to get temp file name
 
594
        Clear();
 
595
    }
 
596
    else // ok
 
597
    {
 
598
        Assign(tempname);
 
599
    }
 
600
}
 
601
 
 
602
/* static */
 
603
wxString
 
604
wxFileName::CreateTempFileName(const wxString& prefix, wxFile *fileTemp)
 
605
{
 
606
    wxString path, dir, name;
 
607
 
 
608
    // use the directory specified by the prefix
 
609
    SplitPath(prefix, &dir, &name, NULL /* extension */);
 
610
 
 
611
    if (dir.empty())
 
612
    {
 
613
        dir = wxGetenv(_T("TMPDIR"));
 
614
        if (dir.empty())
 
615
        {
 
616
            dir = wxGetenv(_T("TMP"));
 
617
            if (dir.empty())
 
618
            {
 
619
                dir = wxGetenv(_T("TEMP"));
 
620
            }
 
621
        }
 
622
    }
 
623
 
 
624
#if defined(__WXWINCE__)
 
625
    if (dir.empty())
 
626
    {
 
627
        // FIXME. Create \temp dir?
 
628
        if (DirExists(wxT("\\temp")))
 
629
            dir = wxT("\\temp");
 
630
    }
 
631
    path = dir + wxT("\\") + name;
 
632
    int i = 1;
 
633
    while (FileExists(path))
 
634
    {
 
635
        path = dir + wxT("\\") + name ;
 
636
        path << i;
 
637
        i ++;
 
638
    }
 
639
 
 
640
#elif defined(__WINDOWS__) && !defined(__WXMICROWIN__)
 
641
 
 
642
    if ( dir.empty() )
 
643
    {
 
644
        if ( !::GetTempPath(MAX_PATH, wxStringBuffer(dir, MAX_PATH + 1)) )
 
645
        {
 
646
            wxLogLastError(_T("GetTempPath"));
 
647
        }
 
648
 
 
649
        if ( dir.empty() )
 
650
        {
 
651
            // GetTempFileName() fails if we pass it an empty string
 
652
            dir = _T('.');
 
653
        }
 
654
    }
 
655
    else // we have a dir to create the file in
 
656
    {
 
657
        // ensure we use only the back slashes as GetTempFileName(), unlike all
 
658
        // the other APIs, is picky and doesn't accept the forward ones
 
659
        dir.Replace(_T("/"), _T("\\"));
 
660
    }
 
661
 
 
662
    if ( !::GetTempFileName(dir, name, 0, wxStringBuffer(path, MAX_PATH + 1)) )
 
663
    {
 
664
        wxLogLastError(_T("GetTempFileName"));
 
665
 
 
666
        path.clear();
 
667
    }
 
668
 
 
669
#else // !Windows
 
670
    if ( dir.empty() )
 
671
    {
 
672
        // default
 
673
#if defined(__DOS__) || defined(__OS2__)
 
674
        dir = _T(".");
 
675
#elif defined(__WXMAC__)
 
676
        dir = wxMacFindFolder(short(kOnSystemDisk), kTemporaryFolderType, kCreateFolder);
 
677
#else
 
678
        dir = _T("/tmp");
 
679
#endif
 
680
    }
 
681
 
 
682
    path = dir;
 
683
 
 
684
    if ( !wxEndsWithPathSeparator(dir) &&
 
685
            (name.empty() || !wxIsPathSeparator(name[0u])) )
 
686
    {
 
687
        path += wxFILE_SEP_PATH;
 
688
    }
 
689
 
 
690
    path += name;
 
691
 
 
692
#if defined(HAVE_MKSTEMP)
 
693
    // scratch space for mkstemp()
 
694
    path += _T("XXXXXX");
 
695
 
 
696
    // we need to copy the path to the buffer in which mkstemp() can modify it
 
697
    wxCharBuffer buf( wxConvFile.cWX2MB( path ) );
 
698
 
 
699
    // cast is safe because the string length doesn't change
 
700
    int fdTemp = mkstemp( (char*)(const char*) buf );
 
701
    if ( fdTemp == -1 )
 
702
    {
 
703
        // this might be not necessary as mkstemp() on most systems should have
 
704
        // already done it but it doesn't hurt neither...
 
705
        path.clear();
 
706
    }
 
707
    else // mkstemp() succeeded
 
708
    {
 
709
        path = wxConvFile.cMB2WX( (const char*) buf );
 
710
 
 
711
        // avoid leaking the fd
 
712
        if ( fileTemp )
 
713
        {
 
714
            fileTemp->Attach(fdTemp);
 
715
        }
 
716
        else
 
717
        {
 
718
            close(fdTemp);
 
719
        }
 
720
    }
 
721
#else // !HAVE_MKSTEMP
 
722
 
 
723
#ifdef HAVE_MKTEMP
 
724
    // same as above
 
725
    path += _T("XXXXXX");
 
726
 
 
727
    wxCharBuffer buf = wxConvFile.cWX2MB( path );
 
728
    if ( !mktemp( (const char*) buf ) )
 
729
    {
 
730
        path.clear();
 
731
    }
 
732
    else
 
733
    {
 
734
        path = wxConvFile.cMB2WX( (const char*) buf );
 
735
    }
 
736
#else // !HAVE_MKTEMP (includes __DOS__)
 
737
    // generate the unique file name ourselves
 
738
    #if !defined(__DOS__) && !defined(__PALMOS__) && (!defined(__MWERKS__) || defined(__DARWIN__) )
 
739
    path << (unsigned int)getpid();
 
740
    #endif
 
741
 
 
742
    wxString pathTry;
 
743
 
 
744
    static const size_t numTries = 1000;
 
745
    for ( size_t n = 0; n < numTries; n++ )
 
746
    {
 
747
        // 3 hex digits is enough for numTries == 1000 < 4096
 
748
        pathTry = path + wxString::Format(_T("%.03x"), (unsigned int) n);
 
749
        if ( !FileExists(pathTry) )
 
750
        {
 
751
            break;
 
752
        }
 
753
 
 
754
        pathTry.clear();
 
755
    }
 
756
 
 
757
    path = pathTry;
 
758
#endif // HAVE_MKTEMP/!HAVE_MKTEMP
 
759
 
 
760
#endif // HAVE_MKSTEMP/!HAVE_MKSTEMP
 
761
 
 
762
#endif // Windows/!Windows
 
763
 
 
764
    if ( path.empty() )
 
765
    {
 
766
        wxLogSysError(_("Failed to create a temporary file name"));
 
767
    }
 
768
    else if ( fileTemp && !fileTemp->IsOpened() )
 
769
    {
 
770
        // open the file - of course, there is a race condition here, this is
 
771
        // why we always prefer using mkstemp()...
 
772
        //
 
773
        // NB: GetTempFileName() under Windows creates the file, so using
 
774
        //     write_excl there would fail
 
775
        if ( !fileTemp->Open(path,
 
776
#if defined(__WINDOWS__) && !defined(__WXMICROWIN__)
 
777
                             wxFile::write,
 
778
#else
 
779
                             wxFile::write_excl,
 
780
#endif
 
781
                             wxS_IRUSR | wxS_IWUSR) )
 
782
        {
 
783
            // FIXME: If !ok here should we loop and try again with another
 
784
            //        file name?  That is the standard recourse if open(O_EXCL)
 
785
            //        fails, though of course it should be protected against
 
786
            //        possible infinite looping too.
 
787
 
 
788
            wxLogError(_("Failed to open temporary file."));
 
789
 
 
790
            path.clear();
 
791
        }
 
792
    }
 
793
 
 
794
    return path;
 
795
}
 
796
 
 
797
#endif // wxUSE_FILE
 
798
 
 
799
// ----------------------------------------------------------------------------
 
800
// directory operations
 
801
// ----------------------------------------------------------------------------
 
802
 
 
803
bool wxFileName::Mkdir( int perm, int flags )
 
804
{
 
805
    return wxFileName::Mkdir( GetFullPath(), perm, flags );
 
806
}
 
807
 
 
808
bool wxFileName::Mkdir( const wxString& dir, int perm, int flags )
 
809
{
 
810
    if ( flags & wxPATH_MKDIR_FULL )
 
811
    {
 
812
        // split the path in components
 
813
        wxFileName filename;
 
814
        filename.AssignDir(dir);
 
815
 
 
816
        wxString currPath;
 
817
        if ( filename.HasVolume())
 
818
        {
 
819
            currPath << wxGetVolumeString(filename.GetVolume(), wxPATH_NATIVE);
 
820
        }
 
821
 
 
822
        wxArrayString dirs = filename.GetDirs();
 
823
        size_t count = dirs.GetCount();
 
824
        for ( size_t i = 0; i < count; i++ )
 
825
        {
 
826
            if ( i > 0 ||
 
827
#if defined(__WXMAC__) && !defined(__DARWIN__)
 
828
            // relative pathnames are exactely the other way round under mac...
 
829
                !filename.IsAbsolute()
 
830
#else
 
831
                filename.IsAbsolute()
 
832
#endif
 
833
            )
 
834
                currPath += wxFILE_SEP_PATH;
 
835
            currPath += dirs[i];
 
836
 
 
837
            if (!DirExists(currPath))
 
838
            {
 
839
                if (!wxMkdir(currPath, perm))
 
840
                {
 
841
                    // no need to try creating further directories
 
842
                    return false;
 
843
                }
 
844
            }
 
845
        }
 
846
 
 
847
        return true;
 
848
 
 
849
    }
 
850
 
 
851
    return ::wxMkdir( dir, perm );
 
852
}
 
853
 
 
854
bool wxFileName::Rmdir()
 
855
{
 
856
    return wxFileName::Rmdir( GetFullPath() );
 
857
}
 
858
 
 
859
bool wxFileName::Rmdir( const wxString &dir )
 
860
{
 
861
    return ::wxRmdir( dir );
 
862
}
 
863
 
 
864
// ----------------------------------------------------------------------------
 
865
// path normalization
 
866
// ----------------------------------------------------------------------------
 
867
 
 
868
bool wxFileName::Normalize(int flags,
 
869
                           const wxString& cwd,
 
870
                           wxPathFormat format)
 
871
{
 
872
    // deal with env vars renaming first as this may seriously change the path
 
873
    if ( flags & wxPATH_NORM_ENV_VARS )
 
874
    {
 
875
        wxString pathOrig = GetFullPath(format);
 
876
        wxString path = wxExpandEnvVars(pathOrig);
 
877
        if ( path != pathOrig )
 
878
        {
 
879
            Assign(path);
 
880
        }
 
881
    }
 
882
 
 
883
 
 
884
    // the existing path components
 
885
    wxArrayString dirs = GetDirs();
 
886
 
 
887
    // the path to prepend in front to make the path absolute
 
888
    wxFileName curDir;
 
889
 
 
890
    format = GetFormat(format);
 
891
 
 
892
    // make the path absolute
 
893
    if ( (flags & wxPATH_NORM_ABSOLUTE) && !IsAbsolute(format) )
 
894
    {
 
895
        if ( cwd.empty() )
 
896
        {
 
897
            curDir.AssignCwd(GetVolume());
 
898
        }
 
899
        else // cwd provided
 
900
        {
 
901
            curDir.AssignDir(cwd);
 
902
        }
 
903
 
 
904
        // the path may be not absolute because it doesn't have the volume name
 
905
        // but in this case we shouldn't modify the directory components of it
 
906
        // but just set the current volume
 
907
        if ( !HasVolume() && curDir.HasVolume() )
 
908
        {
 
909
            SetVolume(curDir.GetVolume());
 
910
 
 
911
            if ( !m_relative )
 
912
            {
 
913
                // yes, it was the case - we don't need curDir then
 
914
                curDir.Clear();
 
915
            }
 
916
        }
 
917
    }
 
918
 
 
919
    // handle ~ stuff under Unix only
 
920
    if ( (format == wxPATH_UNIX) && (flags & wxPATH_NORM_TILDE) )
 
921
    {
 
922
        if ( !dirs.IsEmpty() )
 
923
        {
 
924
            wxString dir = dirs[0u];
 
925
            if ( !dir.empty() && dir[0u] == _T('~') )
 
926
            {
 
927
                curDir.AssignDir(wxGetUserHome(dir.c_str() + 1));
 
928
 
 
929
                dirs.RemoveAt(0u);
 
930
            }
 
931
        }
 
932
    }
 
933
 
 
934
    // transform relative path into abs one
 
935
    if ( curDir.IsOk() )
 
936
    {
 
937
        wxArrayString dirsNew = curDir.GetDirs();
 
938
        size_t count = dirs.GetCount();
 
939
        for ( size_t n = 0; n < count; n++ )
 
940
        {
 
941
            dirsNew.Add(dirs[n]);
 
942
        }
 
943
 
 
944
        dirs = dirsNew;
 
945
    }
 
946
 
 
947
    // now deal with ".", ".." and the rest
 
948
    m_dirs.Empty();
 
949
    size_t count = dirs.GetCount();
 
950
    for ( size_t n = 0; n < count; n++ )
 
951
    {
 
952
        wxString dir = dirs[n];
 
953
 
 
954
        if ( flags & wxPATH_NORM_DOTS )
 
955
        {
 
956
            if ( dir == wxT(".") )
 
957
            {
 
958
                // just ignore
 
959
                continue;
 
960
            }
 
961
 
 
962
            if ( dir == wxT("..") )
 
963
            {
 
964
                if ( m_dirs.IsEmpty() )
 
965
                {
 
966
                    wxLogError(_("The path '%s' contains too many \"..\"!"),
 
967
                               GetFullPath().c_str());
 
968
                    return false;
 
969
                }
 
970
 
 
971
                m_dirs.RemoveAt(m_dirs.GetCount() - 1);
 
972
                continue;
 
973
            }
 
974
        }
 
975
 
 
976
        if ( (flags & wxPATH_NORM_CASE) && !IsCaseSensitive(format) )
 
977
        {
 
978
            dir.MakeLower();
 
979
        }
 
980
 
 
981
        m_dirs.Add(dir);
 
982
    }
 
983
 
 
984
#if defined(__WIN32__) && !defined(__WXWINCE__) && wxUSE_OLE
 
985
    if ( (flags & wxPATH_NORM_SHORTCUT) )
 
986
    {
 
987
        wxString filename;
 
988
        if (GetShortcutTarget(GetFullPath(format), filename))
 
989
        {
 
990
            // Repeat this since we may now have a new path
 
991
            if ( (flags & wxPATH_NORM_CASE) && !IsCaseSensitive(format) )
 
992
            {
 
993
                filename.MakeLower();
 
994
            }
 
995
            m_relative = false;
 
996
            Assign(filename);
 
997
        }
 
998
    }
 
999
#endif
 
1000
 
 
1001
    if ( (flags & wxPATH_NORM_CASE) && !IsCaseSensitive(format) )
 
1002
    {
 
1003
        // VZ: expand env vars here too?
 
1004
 
 
1005
        m_volume.MakeLower();
 
1006
        m_name.MakeLower();
 
1007
        m_ext.MakeLower();
 
1008
    }
 
1009
 
 
1010
    // we do have the path now
 
1011
    //
 
1012
    // NB: need to do this before (maybe) calling Assign() below
 
1013
    m_relative = false;
 
1014
 
 
1015
#if defined(__WIN32__)
 
1016
    if ( (flags & wxPATH_NORM_LONG) && (format == wxPATH_DOS) )
 
1017
    {
 
1018
        Assign(GetLongPath());
 
1019
    }
 
1020
#endif // Win32
 
1021
 
 
1022
    return true;
 
1023
}
 
1024
 
 
1025
// ----------------------------------------------------------------------------
 
1026
// get the shortcut target
 
1027
// ----------------------------------------------------------------------------
 
1028
 
 
1029
// WinCE (3) doesn't have CLSID_ShellLink, IID_IShellLink definitions.
 
1030
// The .lnk file is a plain text file so it should be easy to
 
1031
// make it work. Hint from Google Groups:
 
1032
// "If you open up a lnk file, you'll see a
 
1033
// number, followed by a pound sign (#), followed by more text. The
 
1034
// number is the number of characters that follows the pound sign. The
 
1035
// characters after the pound sign are the command line (which _can_
 
1036
// include arguments) to be executed. Any path (e.g. \windows\program
 
1037
// files\myapp.exe) that includes spaces needs to be enclosed in
 
1038
// quotation marks."
 
1039
 
 
1040
#if defined(__WIN32__) && !defined(__WXWINCE__) && wxUSE_OLE
 
1041
// The following lines are necessary under WinCE
 
1042
// #include "wx/msw/private.h"
 
1043
// #include <ole2.h>
 
1044
#include <shlobj.h>
 
1045
#if defined(__WXWINCE__)
 
1046
#include <shlguid.h>
 
1047
#endif
 
1048
 
 
1049
bool wxFileName::GetShortcutTarget(const wxString& shortcutPath, wxString& targetFilename, wxString* arguments)
 
1050
{
 
1051
    wxString path, file, ext;
 
1052
    wxSplitPath(shortcutPath, & path, & file, & ext);
 
1053
 
 
1054
    HRESULT hres;
 
1055
    IShellLink* psl;
 
1056
    bool success = false;
 
1057
 
 
1058
    // Assume it's not a shortcut if it doesn't end with lnk
 
1059
    if (ext.CmpNoCase(wxT("lnk"))!=0)
 
1060
        return false;
 
1061
 
 
1062
    // create a ShellLink object
 
1063
    hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
 
1064
                            IID_IShellLink, (LPVOID*) &psl);
 
1065
 
 
1066
    if (SUCCEEDED(hres))
 
1067
    {
 
1068
        IPersistFile* ppf;
 
1069
        hres = psl->QueryInterface( IID_IPersistFile, (LPVOID *) &ppf);
 
1070
        if (SUCCEEDED(hres))
 
1071
        {
 
1072
            WCHAR wsz[MAX_PATH];
 
1073
 
 
1074
            MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, shortcutPath.mb_str(), -1, wsz,
 
1075
                                MAX_PATH);
 
1076
 
 
1077
            hres = ppf->Load(wsz, 0);
 
1078
            if (SUCCEEDED(hres))
 
1079
            {
 
1080
                wxChar buf[2048];
 
1081
                // Wrong prototype in early versions
 
1082
#if defined(__MINGW32__) && !wxCHECK_W32API_VERSION(2, 2)
 
1083
                psl->GetPath((CHAR*) buf, 2048, NULL, SLGP_UNCPRIORITY);
 
1084
#else
 
1085
                psl->GetPath(buf, 2048, NULL, SLGP_UNCPRIORITY);
 
1086
#endif
 
1087
                targetFilename = wxString(buf);
 
1088
                success = (shortcutPath != targetFilename);
 
1089
 
 
1090
                psl->GetArguments(buf, 2048);
 
1091
                wxString args(buf);
 
1092
                if (!args.empty() && arguments)
 
1093
                {
 
1094
                    *arguments = args;
 
1095
                }
 
1096
            }
 
1097
        }
 
1098
    }
 
1099
    psl->Release();
 
1100
    return success;
 
1101
}
 
1102
#endif
 
1103
 
 
1104
 
 
1105
// ----------------------------------------------------------------------------
 
1106
// absolute/relative paths
 
1107
// ----------------------------------------------------------------------------
 
1108
 
 
1109
bool wxFileName::IsAbsolute(wxPathFormat format) const
 
1110
{
 
1111
    // if our path doesn't start with a path separator, it's not an absolute
 
1112
    // path
 
1113
    if ( m_relative )
 
1114
        return false;
 
1115
 
 
1116
    if ( !GetVolumeSeparator(format).empty() )
 
1117
    {
 
1118
        // this format has volumes and an absolute path must have one, it's not
 
1119
        // enough to have the full path to bean absolute file under Windows
 
1120
        if ( GetVolume().empty() )
 
1121
            return false;
 
1122
    }
 
1123
 
 
1124
    return true;
 
1125
}
 
1126
 
 
1127
bool wxFileName::MakeRelativeTo(const wxString& pathBase, wxPathFormat format)
 
1128
{
 
1129
    wxFileName fnBase = wxFileName::DirName(pathBase, format);
 
1130
 
 
1131
    // get cwd only once - small time saving
 
1132
    wxString cwd = wxGetCwd();
 
1133
    Normalize(wxPATH_NORM_ALL & ~wxPATH_NORM_CASE, cwd, format);
 
1134
    fnBase.Normalize(wxPATH_NORM_ALL & ~wxPATH_NORM_CASE, cwd, format);
 
1135
 
 
1136
    bool withCase = IsCaseSensitive(format);
 
1137
 
 
1138
    // we can't do anything if the files live on different volumes
 
1139
    if ( !GetVolume().IsSameAs(fnBase.GetVolume(), withCase) )
 
1140
    {
 
1141
        // nothing done
 
1142
        return false;
 
1143
    }
 
1144
 
 
1145
    // same drive, so we don't need our volume
 
1146
    m_volume.clear();
 
1147
 
 
1148
    // remove common directories starting at the top
 
1149
    while ( !m_dirs.IsEmpty() && !fnBase.m_dirs.IsEmpty() &&
 
1150
                m_dirs[0u].IsSameAs(fnBase.m_dirs[0u], withCase) )
 
1151
    {
 
1152
        m_dirs.RemoveAt(0);
 
1153
        fnBase.m_dirs.RemoveAt(0);
 
1154
    }
 
1155
 
 
1156
    // add as many ".." as needed
 
1157
    size_t count = fnBase.m_dirs.GetCount();
 
1158
    for ( size_t i = 0; i < count; i++ )
 
1159
    {
 
1160
        m_dirs.Insert(wxT(".."), 0u);
 
1161
    }
 
1162
 
 
1163
    if ( format == wxPATH_UNIX || format == wxPATH_DOS )
 
1164
    {
 
1165
        // a directory made relative with respect to itself is '.' under Unix
 
1166
        // and DOS, by definition (but we don't have to insert "./" for the
 
1167
        // files)
 
1168
        if ( m_dirs.IsEmpty() && IsDir() )
 
1169
        {
 
1170
            m_dirs.Add(_T('.'));
 
1171
        }
 
1172
    }
 
1173
 
 
1174
    m_relative = true;
 
1175
 
 
1176
    // we were modified
 
1177
    return true;
 
1178
}
 
1179
 
 
1180
// ----------------------------------------------------------------------------
 
1181
// filename kind tests
 
1182
// ----------------------------------------------------------------------------
 
1183
 
 
1184
bool wxFileName::SameAs(const wxFileName& filepath, wxPathFormat format) const
 
1185
{
 
1186
    wxFileName fn1 = *this,
 
1187
               fn2 = filepath;
 
1188
 
 
1189
    // get cwd only once - small time saving
 
1190
    wxString cwd = wxGetCwd();
 
1191
    fn1.Normalize(wxPATH_NORM_ALL | wxPATH_NORM_CASE, cwd, format);
 
1192
    fn2.Normalize(wxPATH_NORM_ALL | wxPATH_NORM_CASE, cwd, format);
 
1193
 
 
1194
    if ( fn1.GetFullPath() == fn2.GetFullPath() )
 
1195
        return true;
 
1196
 
 
1197
    // TODO: compare inodes for Unix, this works even when filenames are
 
1198
    //       different but files are the same (symlinks) (VZ)
 
1199
 
 
1200
    return false;
 
1201
}
 
1202
 
 
1203
/* static */
 
1204
bool wxFileName::IsCaseSensitive( wxPathFormat format )
 
1205
{
 
1206
    // only Unix filenames are truely case-sensitive
 
1207
    return GetFormat(format) == wxPATH_UNIX;
 
1208
}
 
1209
 
 
1210
/* static */
 
1211
wxString wxFileName::GetForbiddenChars(wxPathFormat format)
 
1212
{
 
1213
    // Inits to forbidden characters that are common to (almost) all platforms.
 
1214
    wxString strForbiddenChars = wxT("*?");
 
1215
 
 
1216
    // If asserts, wxPathFormat has been changed. In case of a new path format
 
1217
    // addition, the following code might have to be updated.
 
1218
    wxCOMPILE_TIME_ASSERT(wxPATH_MAX == 5, wxPathFormatChanged);
 
1219
    switch ( GetFormat(format) )
 
1220
    {
 
1221
        default :
 
1222
            wxFAIL_MSG( wxT("Unknown path format") );
 
1223
            // !! Fall through !!
 
1224
 
 
1225
        case wxPATH_UNIX:
 
1226
            break;
 
1227
 
 
1228
        case wxPATH_MAC:
 
1229
            // On a Mac even names with * and ? are allowed (Tested with OS
 
1230
            // 9.2.1 and OS X 10.2.5)
 
1231
            strForbiddenChars = wxEmptyString;
 
1232
            break;
 
1233
 
 
1234
        case wxPATH_DOS:
 
1235
            strForbiddenChars += wxT("\\/:\"<>|");
 
1236
            break;
 
1237
 
 
1238
        case wxPATH_VMS:
 
1239
            break;
 
1240
    }
 
1241
 
 
1242
    return strForbiddenChars;
 
1243
}
 
1244
 
 
1245
/* static */
 
1246
wxString wxFileName::GetVolumeSeparator(wxPathFormat WXUNUSED_IN_WINCE(format))
 
1247
{
 
1248
#ifdef __WXWINCE__
 
1249
    return wxEmptyString;
 
1250
#else
 
1251
    wxString sepVol;
 
1252
 
 
1253
    if ( (GetFormat(format) == wxPATH_DOS) ||
 
1254
         (GetFormat(format) == wxPATH_VMS) )
 
1255
    {
 
1256
        sepVol = wxFILE_SEP_DSK;
 
1257
    }
 
1258
    //else: leave empty
 
1259
 
 
1260
    return sepVol;
 
1261
#endif
 
1262
}
 
1263
 
 
1264
/* static */
 
1265
wxString wxFileName::GetPathSeparators(wxPathFormat format)
 
1266
{
 
1267
    wxString seps;
 
1268
    switch ( GetFormat(format) )
 
1269
    {
 
1270
        case wxPATH_DOS:
 
1271
            // accept both as native APIs do but put the native one first as
 
1272
            // this is the one we use in GetFullPath()
 
1273
            seps << wxFILE_SEP_PATH_DOS << wxFILE_SEP_PATH_UNIX;
 
1274
            break;
 
1275
 
 
1276
        default:
 
1277
            wxFAIL_MSG( _T("Unknown wxPATH_XXX style") );
 
1278
            // fall through
 
1279
 
 
1280
        case wxPATH_UNIX:
 
1281
            seps = wxFILE_SEP_PATH_UNIX;
 
1282
            break;
 
1283
 
 
1284
        case wxPATH_MAC:
 
1285
            seps = wxFILE_SEP_PATH_MAC;
 
1286
            break;
 
1287
 
 
1288
        case wxPATH_VMS:
 
1289
            seps = wxFILE_SEP_PATH_VMS;
 
1290
            break;
 
1291
    }
 
1292
 
 
1293
    return seps;
 
1294
}
 
1295
 
 
1296
/* static */
 
1297
wxString wxFileName::GetPathTerminators(wxPathFormat format)
 
1298
{
 
1299
    format = GetFormat(format);
 
1300
 
 
1301
    // under VMS the end of the path is ']', not the path separator used to
 
1302
    // separate the components
 
1303
    return format == wxPATH_VMS ? wxString(_T(']')) : GetPathSeparators(format);
 
1304
}
 
1305
 
 
1306
/* static */
 
1307
bool wxFileName::IsPathSeparator(wxChar ch, wxPathFormat format)
 
1308
{
 
1309
    // wxString::Find() doesn't work as expected with NUL - it will always find
 
1310
    // it, so test for it separately
 
1311
    return ch != _T('\0') && GetPathSeparators(format).Find(ch) != wxNOT_FOUND;
 
1312
}
 
1313
 
 
1314
// ----------------------------------------------------------------------------
 
1315
// path components manipulation
 
1316
// ----------------------------------------------------------------------------
 
1317
 
 
1318
/* static */ bool wxFileName::IsValidDirComponent(const wxString& dir)
 
1319
{
 
1320
    if ( dir.empty() )
 
1321
    {
 
1322
        wxFAIL_MSG( _T("empty directory passed to wxFileName::InsertDir()") );
 
1323
 
 
1324
        return false;
 
1325
    }
 
1326
 
 
1327
    const size_t len = dir.length();
 
1328
    for ( size_t n = 0; n < len; n++ )
 
1329
    {
 
1330
        if ( dir[n] == GetVolumeSeparator() || IsPathSeparator(dir[n]) )
 
1331
        {
 
1332
            wxFAIL_MSG( _T("invalid directory component in wxFileName") );
 
1333
 
 
1334
            return false;
 
1335
        }
 
1336
    }
 
1337
 
 
1338
    return true;
 
1339
}
 
1340
 
 
1341
void wxFileName::AppendDir( const wxString& dir )
 
1342
{
 
1343
    if ( IsValidDirComponent(dir) )
 
1344
        m_dirs.Add( dir );
 
1345
}
 
1346
 
 
1347
void wxFileName::PrependDir( const wxString& dir )
 
1348
{
 
1349
    InsertDir(0, dir);
 
1350
}
 
1351
 
 
1352
void wxFileName::InsertDir(size_t before, const wxString& dir)
 
1353
{
 
1354
    if ( IsValidDirComponent(dir) )
 
1355
        m_dirs.Insert(dir, before);
 
1356
}
 
1357
 
 
1358
void wxFileName::RemoveDir(size_t pos)
 
1359
{
 
1360
    m_dirs.RemoveAt(pos);
 
1361
}
 
1362
 
 
1363
// ----------------------------------------------------------------------------
 
1364
// accessors
 
1365
// ----------------------------------------------------------------------------
 
1366
 
 
1367
void wxFileName::SetFullName(const wxString& fullname)
 
1368
{
 
1369
    SplitPath(fullname, NULL /* no volume */, NULL /* no path */,
 
1370
                        &m_name, &m_ext, &m_hasExt);
 
1371
}
 
1372
 
 
1373
wxString wxFileName::GetFullName() const
 
1374
{
 
1375
    wxString fullname = m_name;
 
1376
    if ( m_hasExt )
 
1377
    {
 
1378
        fullname << wxFILE_SEP_EXT << m_ext;
 
1379
    }
 
1380
 
 
1381
    return fullname;
 
1382
}
 
1383
 
 
1384
wxString wxFileName::GetPath( int flags, wxPathFormat format ) const
 
1385
{
 
1386
    format = GetFormat( format );
 
1387
 
 
1388
    wxString fullpath;
 
1389
 
 
1390
    // return the volume with the path as well if requested
 
1391
    if ( flags & wxPATH_GET_VOLUME )
 
1392
    {
 
1393
        fullpath += wxGetVolumeString(GetVolume(), format);
 
1394
    }
 
1395
 
 
1396
    // the leading character
 
1397
    switch ( format )
 
1398
    {
 
1399
        case wxPATH_MAC:
 
1400
            if ( m_relative )
 
1401
                fullpath += wxFILE_SEP_PATH_MAC;
 
1402
            break;
 
1403
 
 
1404
        case wxPATH_DOS:
 
1405
            if ( !m_relative )
 
1406
                fullpath += wxFILE_SEP_PATH_DOS;
 
1407
            break;
 
1408
 
 
1409
        default:
 
1410
            wxFAIL_MSG( wxT("Unknown path format") );
 
1411
            // fall through
 
1412
 
 
1413
        case wxPATH_UNIX:
 
1414
            if ( !m_relative )
 
1415
            {
 
1416
                // normally the absolute file names start with a slash
 
1417
                // with one exception: the ones like "~/foo.bar" don't
 
1418
                // have it
 
1419
                if ( m_dirs.IsEmpty() || m_dirs[0u] != _T('~') )
 
1420
                {
 
1421
                    fullpath += wxFILE_SEP_PATH_UNIX;
 
1422
                }
 
1423
            }
 
1424
            break;
 
1425
 
 
1426
        case wxPATH_VMS:
 
1427
            // no leading character here but use this place to unset
 
1428
            // wxPATH_GET_SEPARATOR flag: under VMS it doesn't make sense
 
1429
            // as, if I understand correctly, there should never be a dot
 
1430
            // before the closing bracket
 
1431
            flags &= ~wxPATH_GET_SEPARATOR;
 
1432
    }
 
1433
 
 
1434
    if ( m_dirs.empty() )
 
1435
    {
 
1436
        // there is nothing more
 
1437
        return fullpath;
 
1438
    }
 
1439
 
 
1440
    // then concatenate all the path components using the path separator
 
1441
    if ( format == wxPATH_VMS )
 
1442
    {
 
1443
        fullpath += wxT('[');
 
1444
    }
 
1445
 
 
1446
    const size_t dirCount = m_dirs.GetCount();
 
1447
    for ( size_t i = 0; i < dirCount; i++ )
 
1448
    {
 
1449
        switch (format)
 
1450
        {
 
1451
            case wxPATH_MAC:
 
1452
                if ( m_dirs[i] == wxT(".") )
 
1453
                {
 
1454
                    // skip appending ':', this shouldn't be done in this
 
1455
                    // case as "::" is interpreted as ".." under Unix
 
1456
                    continue;
 
1457
                }
 
1458
 
 
1459
                // convert back from ".." to nothing
 
1460
                if ( !m_dirs[i].IsSameAs(wxT("..")) )
 
1461
                     fullpath += m_dirs[i];
 
1462
                break;
 
1463
 
 
1464
            default:
 
1465
                wxFAIL_MSG( wxT("Unexpected path format") );
 
1466
                // still fall through
 
1467
 
 
1468
            case wxPATH_DOS:
 
1469
            case wxPATH_UNIX:
 
1470
                fullpath += m_dirs[i];
 
1471
                break;
 
1472
 
 
1473
            case wxPATH_VMS:
 
1474
                // TODO: What to do with ".." under VMS
 
1475
 
 
1476
                // convert back from ".." to nothing
 
1477
                if ( !m_dirs[i].IsSameAs(wxT("..")) )
 
1478
                    fullpath += m_dirs[i];
 
1479
                break;
 
1480
        }
 
1481
 
 
1482
        if ( (flags & wxPATH_GET_SEPARATOR) || (i != dirCount - 1) )
 
1483
            fullpath += GetPathSeparator(format);
 
1484
    }
 
1485
 
 
1486
    if ( format == wxPATH_VMS )
 
1487
    {
 
1488
        fullpath += wxT(']');
 
1489
    }
 
1490
 
 
1491
    return fullpath;
 
1492
}
 
1493
 
 
1494
wxString wxFileName::GetFullPath( wxPathFormat format ) const
 
1495
{
 
1496
    // we already have a function to get the path
 
1497
    wxString fullpath = GetPath(wxPATH_GET_VOLUME | wxPATH_GET_SEPARATOR,
 
1498
                                format);
 
1499
 
 
1500
    // now just add the file name and extension to it
 
1501
    fullpath += GetFullName();
 
1502
 
 
1503
    return fullpath;
 
1504
}
 
1505
 
 
1506
// Return the short form of the path (returns identity on non-Windows platforms)
 
1507
wxString wxFileName::GetShortPath() const
 
1508
{
 
1509
    wxString path(GetFullPath());
 
1510
 
 
1511
#if defined(__WXMSW__) && defined(__WIN32__) && !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
 
1512
    DWORD sz = ::GetShortPathName(path, NULL, 0);
 
1513
    if ( sz != 0 )
 
1514
    {
 
1515
        wxString pathOut;
 
1516
        if ( ::GetShortPathName
 
1517
               (
 
1518
                path,
 
1519
                wxStringBuffer(pathOut, sz),
 
1520
                sz
 
1521
               ) != 0 )
 
1522
        {
 
1523
            return pathOut;
 
1524
        }
 
1525
    }
 
1526
#endif // Windows
 
1527
 
 
1528
    return path;
 
1529
}
 
1530
 
 
1531
// Return the long form of the path (returns identity on non-Windows platforms)
 
1532
wxString wxFileName::GetLongPath() const
 
1533
{
 
1534
    wxString pathOut,
 
1535
             path = GetFullPath();
 
1536
 
 
1537
#if defined(__WIN32__) && !defined(__WXMICROWIN__)
 
1538
 
 
1539
#if wxUSE_DYNAMIC_LOADER
 
1540
    typedef DWORD (WINAPI *GET_LONG_PATH_NAME)(const wxChar *, wxChar *, DWORD);
 
1541
 
 
1542
    // this is MT-safe as in the worst case we're going to resolve the function
 
1543
    // twice -- but as the result is the same in both threads, it's ok
 
1544
    static GET_LONG_PATH_NAME s_pfnGetLongPathName = NULL;
 
1545
    if ( !s_pfnGetLongPathName )
 
1546
    {
 
1547
        static bool s_triedToLoad = false;
 
1548
 
 
1549
        if ( !s_triedToLoad )
 
1550
        {
 
1551
            s_triedToLoad = true;
 
1552
 
 
1553
            wxDynamicLibrary dllKernel(_T("kernel32"));
 
1554
 
 
1555
            const wxChar* GetLongPathName = _T("GetLongPathName")
 
1556
#if wxUSE_UNICODE
 
1557
                              _T("W");
 
1558
#else // ANSI
 
1559
                              _T("A");
 
1560
#endif // Unicode/ANSI
 
1561
 
 
1562
            if ( dllKernel.HasSymbol(GetLongPathName) )
 
1563
            {
 
1564
                s_pfnGetLongPathName = (GET_LONG_PATH_NAME)
 
1565
                    dllKernel.GetSymbol(GetLongPathName);
 
1566
            }
 
1567
 
 
1568
            // note that kernel32.dll can be unloaded, it stays in memory
 
1569
            // anyhow as all Win32 programs link to it and so it's safe to call
 
1570
            // GetLongPathName() even after unloading it
 
1571
        }
 
1572
    }
 
1573
 
 
1574
    if ( s_pfnGetLongPathName )
 
1575
    {
 
1576
        DWORD dwSize = (*s_pfnGetLongPathName)(path, NULL, 0);
 
1577
        if ( dwSize > 0 )
 
1578
        {
 
1579
            if ( (*s_pfnGetLongPathName)
 
1580
                 (
 
1581
                  path,
 
1582
                  wxStringBuffer(pathOut, dwSize),
 
1583
                  dwSize
 
1584
                 ) != 0 )
 
1585
            {
 
1586
                return pathOut;
 
1587
            }
 
1588
        }
 
1589
    }
 
1590
#endif // wxUSE_DYNAMIC_LOADER
 
1591
 
 
1592
    // The OS didn't support GetLongPathName, or some other error.
 
1593
    // We need to call FindFirstFile on each component in turn.
 
1594
 
 
1595
    WIN32_FIND_DATA findFileData;
 
1596
    HANDLE hFind;
 
1597
 
 
1598
    if ( HasVolume() )
 
1599
        pathOut = GetVolume() +
 
1600
                  GetVolumeSeparator(wxPATH_DOS) +
 
1601
                  GetPathSeparator(wxPATH_DOS);
 
1602
    else
 
1603
        pathOut = wxEmptyString;
 
1604
 
 
1605
    wxArrayString dirs = GetDirs();
 
1606
    dirs.Add(GetFullName());
 
1607
 
 
1608
    wxString tmpPath;
 
1609
 
 
1610
    size_t count = dirs.GetCount();
 
1611
    for ( size_t i = 0; i < count; i++ )
 
1612
    {
 
1613
        // We're using pathOut to collect the long-name path, but using a
 
1614
        // temporary for appending the last path component which may be
 
1615
        // short-name
 
1616
        tmpPath = pathOut + dirs[i];
 
1617
 
 
1618
        if ( tmpPath.empty() )
 
1619
            continue;
 
1620
 
 
1621
        // can't see this being necessary? MF
 
1622
        if ( tmpPath.Last() == GetVolumeSeparator(wxPATH_DOS) )
 
1623
        {
 
1624
            // Can't pass a drive and root dir to FindFirstFile,
 
1625
            // so continue to next dir
 
1626
            tmpPath += wxFILE_SEP_PATH;
 
1627
            pathOut = tmpPath;
 
1628
            continue;
 
1629
        }
 
1630
 
 
1631
        hFind = ::FindFirstFile(tmpPath, &findFileData);
 
1632
        if (hFind == INVALID_HANDLE_VALUE)
 
1633
        {
 
1634
            // Error: most likely reason is that path doesn't exist, so
 
1635
            // append any unprocessed parts and return
 
1636
            for ( i += 1; i < count; i++ )
 
1637
                tmpPath += wxFILE_SEP_PATH + dirs[i];
 
1638
 
 
1639
            return tmpPath;
 
1640
        }
 
1641
 
 
1642
        pathOut += findFileData.cFileName;
 
1643
        if ( (i < (count-1)) )
 
1644
            pathOut += wxFILE_SEP_PATH;
 
1645
 
 
1646
        ::FindClose(hFind);
 
1647
    }
 
1648
#else // !Win32
 
1649
    pathOut = path;
 
1650
#endif // Win32/!Win32
 
1651
 
 
1652
    return pathOut;
 
1653
}
 
1654
 
 
1655
wxPathFormat wxFileName::GetFormat( wxPathFormat format )
 
1656
{
 
1657
    if (format == wxPATH_NATIVE)
 
1658
    {
 
1659
#if defined(__WXMSW__) || defined(__OS2__) || defined(__DOS__)
 
1660
        format = wxPATH_DOS;
 
1661
#elif defined(__WXMAC__) && !defined(__DARWIN__)
 
1662
        format = wxPATH_MAC;
 
1663
#elif defined(__VMS)
 
1664
        format = wxPATH_VMS;
 
1665
#else
 
1666
        format = wxPATH_UNIX;
 
1667
#endif
 
1668
    }
 
1669
    return format;
 
1670
}
 
1671
 
 
1672
// ----------------------------------------------------------------------------
 
1673
// path splitting function
 
1674
// ----------------------------------------------------------------------------
 
1675
 
 
1676
/* static */
 
1677
void
 
1678
wxFileName::SplitVolume(const wxString& fullpathWithVolume,
 
1679
                        wxString *pstrVolume,
 
1680
                        wxString *pstrPath,
 
1681
                        wxPathFormat format)
 
1682
{
 
1683
    format = GetFormat(format);
 
1684
 
 
1685
    wxString fullpath = fullpathWithVolume;
 
1686
 
 
1687
    // special Windows UNC paths hack: transform \\share\path into share:path
 
1688
    if ( format == wxPATH_DOS )
 
1689
    {
 
1690
        if ( fullpath.length() >= 4 &&
 
1691
                fullpath[0u] == wxFILE_SEP_PATH_DOS &&
 
1692
                    fullpath[1u] == wxFILE_SEP_PATH_DOS )
 
1693
        {
 
1694
            fullpath.erase(0, 2);
 
1695
 
 
1696
            size_t posFirstSlash =
 
1697
                fullpath.find_first_of(GetPathTerminators(format));
 
1698
            if ( posFirstSlash != wxString::npos )
 
1699
            {
 
1700
                fullpath[posFirstSlash] = wxFILE_SEP_DSK;
 
1701
 
 
1702
                // UNC paths are always absolute, right? (FIXME)
 
1703
                fullpath.insert(posFirstSlash + 1, 1, wxFILE_SEP_PATH_DOS);
 
1704
            }
 
1705
        }
 
1706
    }
 
1707
 
 
1708
    // We separate the volume here
 
1709
    if ( format == wxPATH_DOS || format == wxPATH_VMS )
 
1710
    {
 
1711
        wxString sepVol = GetVolumeSeparator(format);
 
1712
 
 
1713
        size_t posFirstColon = fullpath.find_first_of(sepVol);
 
1714
        if ( posFirstColon != wxString::npos )
 
1715
        {
 
1716
            if ( pstrVolume )
 
1717
            {
 
1718
                *pstrVolume = fullpath.Left(posFirstColon);
 
1719
            }
 
1720
 
 
1721
            // remove the volume name and the separator from the full path
 
1722
            fullpath.erase(0, posFirstColon + sepVol.length());
 
1723
        }
 
1724
    }
 
1725
 
 
1726
    if ( pstrPath )
 
1727
        *pstrPath = fullpath;
 
1728
}
 
1729
 
 
1730
/* static */
 
1731
void wxFileName::SplitPath(const wxString& fullpathWithVolume,
 
1732
                           wxString *pstrVolume,
 
1733
                           wxString *pstrPath,
 
1734
                           wxString *pstrName,
 
1735
                           wxString *pstrExt,
 
1736
                           bool *hasExt,
 
1737
                           wxPathFormat format)
 
1738
{
 
1739
    format = GetFormat(format);
 
1740
 
 
1741
    wxString fullpath;
 
1742
    SplitVolume(fullpathWithVolume, pstrVolume, &fullpath, format);
 
1743
 
 
1744
    // find the positions of the last dot and last path separator in the path
 
1745
    size_t posLastDot = fullpath.find_last_of(wxFILE_SEP_EXT);
 
1746
    size_t posLastSlash = fullpath.find_last_of(GetPathTerminators(format));
 
1747
 
 
1748
    // check whether this dot occurs at the very beginning of a path component
 
1749
    if ( (posLastDot != wxString::npos) &&
 
1750
         (posLastDot == 0 ||
 
1751
            IsPathSeparator(fullpath[posLastDot - 1]) ||
 
1752
            (format == wxPATH_VMS && fullpath[posLastDot - 1] == _T(']'))) )
 
1753
    {
 
1754
        // dot may be (and commonly -- at least under Unix -- is) the first
 
1755
        // character of the filename, don't treat the entire filename as
 
1756
        // extension in this case
 
1757
        posLastDot = wxString::npos;
 
1758
    }
 
1759
 
 
1760
    // if we do have a dot and a slash, check that the dot is in the name part
 
1761
    if ( (posLastDot != wxString::npos) &&
 
1762
         (posLastSlash != wxString::npos) &&
 
1763
         (posLastDot < posLastSlash) )
 
1764
    {
 
1765
        // the dot is part of the path, not the start of the extension
 
1766
        posLastDot = wxString::npos;
 
1767
    }
 
1768
 
 
1769
    // now fill in the variables provided by user
 
1770
    if ( pstrPath )
 
1771
    {
 
1772
        if ( posLastSlash == wxString::npos )
 
1773
        {
 
1774
            // no path at all
 
1775
            pstrPath->Empty();
 
1776
        }
 
1777
        else
 
1778
        {
 
1779
            // take everything up to the path separator but take care to make
 
1780
            // the path equal to something like '/', not empty, for the files
 
1781
            // immediately under root directory
 
1782
            size_t len = posLastSlash;
 
1783
 
 
1784
            // this rule does not apply to mac since we do not start with colons (sep)
 
1785
            // except for relative paths
 
1786
            if ( !len && format != wxPATH_MAC)
 
1787
                len++;
 
1788
 
 
1789
            *pstrPath = fullpath.Left(len);
 
1790
 
 
1791
            // special VMS hack: remove the initial bracket
 
1792
            if ( format == wxPATH_VMS )
 
1793
            {
 
1794
                if ( (*pstrPath)[0u] == _T('[') )
 
1795
                    pstrPath->erase(0, 1);
 
1796
            }
 
1797
        }
 
1798
    }
 
1799
 
 
1800
    if ( pstrName )
 
1801
    {
 
1802
        // take all characters starting from the one after the last slash and
 
1803
        // up to, but excluding, the last dot
 
1804
        size_t nStart = posLastSlash == wxString::npos ? 0 : posLastSlash + 1;
 
1805
        size_t count;
 
1806
        if ( posLastDot == wxString::npos )
 
1807
        {
 
1808
            // take all until the end
 
1809
            count = wxString::npos;
 
1810
        }
 
1811
        else if ( posLastSlash == wxString::npos )
 
1812
        {
 
1813
            count = posLastDot;
 
1814
        }
 
1815
        else // have both dot and slash
 
1816
        {
 
1817
            count = posLastDot - posLastSlash - 1;
 
1818
        }
 
1819
 
 
1820
        *pstrName = fullpath.Mid(nStart, count);
 
1821
    }
 
1822
 
 
1823
    // finally deal with the extension here: we have an added complication that
 
1824
    // extension may be empty (but present) as in "foo." where trailing dot
 
1825
    // indicates the empty extension at the end -- and hence we must remember
 
1826
    // that we have it independently of pstrExt
 
1827
    if ( posLastDot == wxString::npos )
 
1828
    {
 
1829
        // no extension
 
1830
        if ( pstrExt )
 
1831
            pstrExt->clear();
 
1832
        if ( hasExt )
 
1833
            *hasExt = false;
 
1834
    }
 
1835
    else
 
1836
    {
 
1837
        // take everything after the dot
 
1838
        if ( pstrExt )
 
1839
            *pstrExt = fullpath.Mid(posLastDot + 1);
 
1840
        if ( hasExt )
 
1841
            *hasExt = true;
 
1842
    }
 
1843
}
 
1844
 
 
1845
/* static */
 
1846
void wxFileName::SplitPath(const wxString& fullpath,
 
1847
                           wxString *path,
 
1848
                           wxString *name,
 
1849
                           wxString *ext,
 
1850
                           wxPathFormat format)
 
1851
{
 
1852
    wxString volume;
 
1853
    SplitPath(fullpath, &volume, path, name, ext, format);
 
1854
 
 
1855
    if ( path )
 
1856
    {
 
1857
        path->Prepend(wxGetVolumeString(volume, format));
 
1858
    }
 
1859
}
 
1860
 
 
1861
// ----------------------------------------------------------------------------
 
1862
// time functions
 
1863
// ----------------------------------------------------------------------------
 
1864
 
 
1865
#if wxUSE_DATETIME
 
1866
 
 
1867
bool wxFileName::SetTimes(const wxDateTime *dtAccess,
 
1868
                          const wxDateTime *dtMod,
 
1869
                          const wxDateTime *dtCreate)
 
1870
{
 
1871
#if defined(__WIN32__)
 
1872
    if ( IsDir() )
 
1873
    {
 
1874
        // VZ: please let me know how to do this if you can
 
1875
        wxFAIL_MSG( _T("SetTimes() not implemented for the directories") );
 
1876
    }
 
1877
    else // file
 
1878
    {
 
1879
        wxFileHandle fh(GetFullPath(), wxFileHandle::Write);
 
1880
        if ( fh.IsOk() )
 
1881
        {
 
1882
            FILETIME ftAccess, ftCreate, ftWrite;
 
1883
 
 
1884
            if ( dtCreate )
 
1885
                ConvertWxToFileTime(&ftCreate, *dtCreate);
 
1886
            if ( dtAccess )
 
1887
                ConvertWxToFileTime(&ftAccess, *dtAccess);
 
1888
            if ( dtMod )
 
1889
                ConvertWxToFileTime(&ftWrite, *dtMod);
 
1890
 
 
1891
            if ( ::SetFileTime(fh,
 
1892
                               dtCreate ? &ftCreate : NULL,
 
1893
                               dtAccess ? &ftAccess : NULL,
 
1894
                               dtMod ? &ftWrite : NULL) )
 
1895
            {
 
1896
                return true;
 
1897
            }
 
1898
        }
 
1899
    }
 
1900
#elif defined(__UNIX_LIKE__) || (defined(__DOS__) && defined(__WATCOMC__))
 
1901
    wxUnusedVar(dtCreate);
 
1902
 
 
1903
    if ( !dtAccess && !dtMod )
 
1904
    {
 
1905
        // can't modify the creation time anyhow, don't try
 
1906
        return true;
 
1907
    }
 
1908
 
 
1909
    // if dtAccess or dtMod is not specified, use the other one (which must be
 
1910
    // non NULL because of the test above) for both times
 
1911
    utimbuf utm;
 
1912
    utm.actime = dtAccess ? dtAccess->GetTicks() : dtMod->GetTicks();
 
1913
    utm.modtime = dtMod ? dtMod->GetTicks() : dtAccess->GetTicks();
 
1914
    if ( utime(GetFullPath().fn_str(), &utm) == 0 )
 
1915
    {
 
1916
        return true;
 
1917
    }
 
1918
#else // other platform
 
1919
    wxUnusedVar(dtAccess);
 
1920
    wxUnusedVar(dtMod);
 
1921
    wxUnusedVar(dtCreate);
 
1922
#endif // platforms
 
1923
 
 
1924
    wxLogSysError(_("Failed to modify file times for '%s'"),
 
1925
                  GetFullPath().c_str());
 
1926
 
 
1927
    return false;
 
1928
}
 
1929
 
 
1930
bool wxFileName::Touch()
 
1931
{
 
1932
#if defined(__UNIX_LIKE__)
 
1933
    // under Unix touching file is simple: just pass NULL to utime()
 
1934
    if ( utime(GetFullPath().fn_str(), NULL) == 0 )
 
1935
    {
 
1936
        return true;
 
1937
    }
 
1938
 
 
1939
    wxLogSysError(_("Failed to touch the file '%s'"), GetFullPath().c_str());
 
1940
 
 
1941
    return false;
 
1942
#else // other platform
 
1943
    wxDateTime dtNow = wxDateTime::Now();
 
1944
 
 
1945
    return SetTimes(&dtNow, &dtNow, NULL /* don't change create time */);
 
1946
#endif // platforms
 
1947
}
 
1948
 
 
1949
bool wxFileName::GetTimes(wxDateTime *dtAccess,
 
1950
                          wxDateTime *dtMod,
 
1951
                          wxDateTime *dtCreate) const
 
1952
{
 
1953
#if defined(__WIN32__)
 
1954
    // we must use different methods for the files and directories under
 
1955
    // Windows as CreateFile(GENERIC_READ) doesn't work for the directories and
 
1956
    // CreateFile(FILE_FLAG_BACKUP_SEMANTICS) works -- but only under NT and
 
1957
    // not 9x
 
1958
    bool ok;
 
1959
    FILETIME ftAccess, ftCreate, ftWrite;
 
1960
    if ( IsDir() )
 
1961
    {
 
1962
        // implemented in msw/dir.cpp
 
1963
        extern bool wxGetDirectoryTimes(const wxString& dirname,
 
1964
                                        FILETIME *, FILETIME *, FILETIME *);
 
1965
 
 
1966
        // we should pass the path without the trailing separator to
 
1967
        // wxGetDirectoryTimes()
 
1968
        ok = wxGetDirectoryTimes(GetPath(wxPATH_GET_VOLUME),
 
1969
                                 &ftAccess, &ftCreate, &ftWrite);
 
1970
    }
 
1971
    else // file
 
1972
    {
 
1973
        wxFileHandle fh(GetFullPath(), wxFileHandle::Read);
 
1974
        if ( fh.IsOk() )
 
1975
        {
 
1976
            ok = ::GetFileTime(fh,
 
1977
                               dtCreate ? &ftCreate : NULL,
 
1978
                               dtAccess ? &ftAccess : NULL,
 
1979
                               dtMod ? &ftWrite : NULL) != 0;
 
1980
        }
 
1981
        else
 
1982
        {
 
1983
            ok = false;
 
1984
        }
 
1985
    }
 
1986
 
 
1987
    if ( ok )
 
1988
    {
 
1989
        if ( dtCreate )
 
1990
            ConvertFileTimeToWx(dtCreate, ftCreate);
 
1991
        if ( dtAccess )
 
1992
            ConvertFileTimeToWx(dtAccess, ftAccess);
 
1993
        if ( dtMod )
 
1994
            ConvertFileTimeToWx(dtMod, ftWrite);
 
1995
 
 
1996
        return true;
 
1997
    }
 
1998
#elif defined(__UNIX_LIKE__) || defined(__WXMAC__) || defined(__OS2__) || (defined(__DOS__) && defined(__WATCOMC__))
 
1999
    wxStructStat stBuf;
 
2000
    if ( wxStat( GetFullPath().c_str(), &stBuf) == 0 )
 
2001
    {
 
2002
        if ( dtAccess )
 
2003
            dtAccess->Set(stBuf.st_atime);
 
2004
        if ( dtMod )
 
2005
            dtMod->Set(stBuf.st_mtime);
 
2006
        if ( dtCreate )
 
2007
            dtCreate->Set(stBuf.st_ctime);
 
2008
 
 
2009
        return true;
 
2010
    }
 
2011
#else // other platform
 
2012
    wxUnusedVar(dtAccess);
 
2013
    wxUnusedVar(dtMod);
 
2014
    wxUnusedVar(dtCreate);
 
2015
#endif // platforms
 
2016
 
 
2017
    wxLogSysError(_("Failed to retrieve file times for '%s'"),
 
2018
                  GetFullPath().c_str());
 
2019
 
 
2020
    return false;
 
2021
}
 
2022
 
 
2023
#endif // wxUSE_DATETIME
 
2024
 
 
2025
#ifdef __WXMAC__
 
2026
 
 
2027
const short kMacExtensionMaxLength = 16 ;
 
2028
class MacDefaultExtensionRecord
 
2029
{
 
2030
public :
 
2031
  MacDefaultExtensionRecord()
 
2032
  {
 
2033
    m_ext[0] = 0 ;
 
2034
    m_type = m_creator = 0 ;
 
2035
  }
 
2036
  MacDefaultExtensionRecord( const MacDefaultExtensionRecord& from )
 
2037
  {
 
2038
    wxStrcpy( m_ext , from.m_ext ) ;
 
2039
    m_type = from.m_type ;
 
2040
    m_creator = from.m_creator ;
 
2041
  }
 
2042
  MacDefaultExtensionRecord( const wxChar * extension , OSType type , OSType creator )
 
2043
  {
 
2044
    wxStrncpy( m_ext , extension , kMacExtensionMaxLength ) ;
 
2045
    m_ext[kMacExtensionMaxLength] = 0 ;
 
2046
    m_type = type ;
 
2047
    m_creator = creator ;
 
2048
  }
 
2049
  wxChar m_ext[kMacExtensionMaxLength] ;
 
2050
  OSType m_type ;
 
2051
  OSType m_creator ;
 
2052
}  ;
 
2053
 
 
2054
#include "wx/dynarray.h"
 
2055
WX_DECLARE_OBJARRAY(MacDefaultExtensionRecord, MacDefaultExtensionArray) ;
 
2056
 
 
2057
bool gMacDefaultExtensionsInited = false ;
 
2058
 
 
2059
#include "wx/arrimpl.cpp"
 
2060
 
 
2061
WX_DEFINE_EXPORTED_OBJARRAY(MacDefaultExtensionArray) ;
 
2062
 
 
2063
MacDefaultExtensionArray gMacDefaultExtensions ;
 
2064
 
 
2065
// load the default extensions
 
2066
MacDefaultExtensionRecord gDefaults[] =
 
2067
{
 
2068
    MacDefaultExtensionRecord( wxT("txt") , 'TEXT' , 'ttxt' ) ,
 
2069
    MacDefaultExtensionRecord( wxT("tif") , 'TIFF' , '****' ) ,
 
2070
    MacDefaultExtensionRecord( wxT("jpg") , 'JPEG' , '****' ) ,
 
2071
} ;
 
2072
 
 
2073
static void MacEnsureDefaultExtensionsLoaded()
 
2074
{
 
2075
    if ( !gMacDefaultExtensionsInited )
 
2076
    {
 
2077
        // we could load the pc exchange prefs here too
 
2078
        for ( size_t i = 0 ; i < WXSIZEOF( gDefaults ) ; ++i )
 
2079
        {
 
2080
            gMacDefaultExtensions.Add( gDefaults[i] ) ;
 
2081
        }
 
2082
        gMacDefaultExtensionsInited = true ;
 
2083
    }
 
2084
}
 
2085
 
 
2086
bool wxFileName::MacSetTypeAndCreator( wxUint32 type , wxUint32 creator )
 
2087
{
 
2088
    FSRef fsRef ;
 
2089
    FSCatalogInfo catInfo;
 
2090
    FileInfo *finfo ;
 
2091
 
 
2092
    if ( wxMacPathToFSRef( GetFullPath() , &fsRef ) == noErr )
 
2093
    {
 
2094
        if ( FSGetCatalogInfo (&fsRef, kFSCatInfoFinderInfo, &catInfo, NULL, NULL, NULL) == noErr )
 
2095
        {
 
2096
            finfo = (FileInfo*)&catInfo.finderInfo;
 
2097
            finfo->fileType = type ;
 
2098
            finfo->fileCreator = creator ;
 
2099
            FSSetCatalogInfo( &fsRef, kFSCatInfoFinderInfo, &catInfo ) ;
 
2100
            return true ;
 
2101
        }
 
2102
    }
 
2103
    return false ;
 
2104
}
 
2105
 
 
2106
bool wxFileName::MacGetTypeAndCreator( wxUint32 *type , wxUint32 *creator )
 
2107
{
 
2108
    FSRef fsRef ;
 
2109
    FSCatalogInfo catInfo;
 
2110
    FileInfo *finfo ;
 
2111
 
 
2112
    if ( wxMacPathToFSRef( GetFullPath() , &fsRef ) == noErr )
 
2113
    {
 
2114
        if ( FSGetCatalogInfo (&fsRef, kFSCatInfoFinderInfo, &catInfo, NULL, NULL, NULL) == noErr )
 
2115
        {
 
2116
            finfo = (FileInfo*)&catInfo.finderInfo;
 
2117
            *type = finfo->fileType ;
 
2118
            *creator = finfo->fileCreator ;
 
2119
            return true ;
 
2120
        }
 
2121
    }
 
2122
    return false ;
 
2123
}
 
2124
 
 
2125
bool wxFileName::MacSetDefaultTypeAndCreator()
 
2126
{
 
2127
    wxUint32 type , creator ;
 
2128
    if ( wxFileName::MacFindDefaultTypeAndCreator(GetExt() , &type ,
 
2129
      &creator ) )
 
2130
    {
 
2131
        return MacSetTypeAndCreator( type , creator ) ;
 
2132
    }
 
2133
    return false;
 
2134
}
 
2135
 
 
2136
bool wxFileName::MacFindDefaultTypeAndCreator( const wxString& ext , wxUint32 *type , wxUint32 *creator )
 
2137
{
 
2138
  MacEnsureDefaultExtensionsLoaded() ;
 
2139
  wxString extl = ext.Lower() ;
 
2140
  for( int i = gMacDefaultExtensions.Count() - 1 ; i >= 0 ; --i )
 
2141
  {
 
2142
    if ( gMacDefaultExtensions.Item(i).m_ext == extl )
 
2143
    {
 
2144
      *type = gMacDefaultExtensions.Item(i).m_type ;
 
2145
      *creator = gMacDefaultExtensions.Item(i).m_creator ;
 
2146
      return true ;
 
2147
    }
 
2148
  }
 
2149
  return false ;
 
2150
}
 
2151
 
 
2152
void wxFileName::MacRegisterDefaultTypeAndCreator( const wxString& ext , wxUint32 type , wxUint32 creator )
 
2153
{
 
2154
  MacEnsureDefaultExtensionsLoaded() ;
 
2155
  MacDefaultExtensionRecord rec ;
 
2156
  rec.m_type = type ;
 
2157
  rec.m_creator = creator ;
 
2158
  wxStrncpy( rec.m_ext , ext.Lower().c_str() , kMacExtensionMaxLength ) ;
 
2159
  gMacDefaultExtensions.Add( rec ) ;
 
2160
}
 
2161
#endif