1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: File- and directory-related functions
4 // Author: Julian Smart
8 // Copyright: (c) 1998 Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "filefn.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
34 #include "wx/file.h" // This does include filefn.h
35 #include "wx/filename.h"
38 // there are just too many of those...
40 #pragma warning(disable:4706) // assignment within conditional expression
47 #if !defined(__WATCOMC__)
48 #if !(defined(_MSC_VER) && (_MSC_VER > 800))
53 #if defined(__WXMAC__)
54 #include "wx/mac/private.h" // includes mac headers
59 // No, Cygwin doesn't appear to have fnmatch.h after all.
60 #if defined(HAVE_FNMATCH_H)
65 #include "wx/msw/private.h"
66 #include "wx/msw/mslu.h"
68 // sys/cygwin.h is needed for cygwin_conv_to_full_win32_path()
70 // note that it must be included after <windows.h>
73 #include <sys/cygwin.h>
75 #endif // __GNUWIN32__
77 // io.h is needed for _get_osfhandle()
78 // Already included by filefn.h for many Windows compilers
79 #if defined __MWERKS__ || defined __CYGWIN__
88 // TODO: Borland probably has _wgetcwd as well?
93 // ----------------------------------------------------------------------------
95 // ----------------------------------------------------------------------------
98 #define _MAXPATHLEN 1024
102 # include "MoreFilesX.h"
105 // ----------------------------------------------------------------------------
107 // ----------------------------------------------------------------------------
109 // MT-FIXME: get rid of this horror and all code using it
110 static wxChar wxFileFunctionsBuffer
[4*_MAXPATHLEN
];
112 #if defined(__VISAGECPP__) && __IBMCPP__ >= 400
114 // VisualAge C++ V4.0 cannot have any external linkage const decs
115 // in headers included by more than one primary source
117 const int wxInvalidOffset
= -1;
120 // ----------------------------------------------------------------------------
122 // ----------------------------------------------------------------------------
124 // we need to translate Mac filenames before passing them to OS functions
125 #define OS_FILENAME(s) (s.fn_str())
127 // ============================================================================
129 // ============================================================================
131 #ifdef wxNEED_WX_UNISTD_H
133 WXDLLEXPORT
int wxStat( const wxChar
*file_name
, wxStructStat
*buf
)
135 return stat( wxConvFile
.cWX2MB( file_name
), buf
);
138 WXDLLEXPORT
int wxAccess( const wxChar
*pathname
, int mode
)
140 return access( wxConvFile
.cWX2MB( pathname
), mode
);
143 WXDLLEXPORT
int wxOpen( const wxChar
*pathname
, int flags
, mode_t mode
)
145 return open( wxConvFile
.cWX2MB( pathname
), flags
, mode
);
149 // wxNEED_WX_UNISTD_H
151 // ----------------------------------------------------------------------------
153 // ----------------------------------------------------------------------------
155 // IMPLEMENT_DYNAMIC_CLASS(wxPathList, wxStringList)
157 static inline wxChar
* MYcopystring(const wxString
& s
)
159 wxChar
* copy
= new wxChar
[s
.length() + 1];
160 return wxStrcpy(copy
, s
.c_str());
163 static inline wxChar
* MYcopystring(const wxChar
* s
)
165 wxChar
* copy
= new wxChar
[wxStrlen(s
) + 1];
166 return wxStrcpy(copy
, s
);
169 void wxPathList::Add (const wxString
& path
)
171 wxStringList::Add (WXSTRINGCAST path
);
174 // Add paths e.g. from the PATH environment variable
175 void wxPathList::AddEnvList (const wxString
& envVariable
)
177 // No environment variables on WinCE
179 static const wxChar PATH_TOKS
[] =
180 #if defined(__WINDOWS__) || defined(__OS2__)
182 The space has been removed from the tokenizers, otherwise a
183 path such as "C:\Program Files" would be split into 2 paths:
184 "C:\Program" and "Files"
186 // wxT(" ;"); // Don't seperate with colon in DOS (used for drive)
187 wxT(";"); // Don't seperate with colon in DOS (used for drive)
192 wxChar
*val
= wxGetenv (WXSTRINGCAST envVariable
);
195 wxChar
*s
= MYcopystring (val
);
196 wxChar
*save_ptr
, *token
= wxStrtok (s
, PATH_TOKS
, &save_ptr
);
203 if ( (token
= wxStrtok ((wxChar
*) NULL
, PATH_TOKS
, &save_ptr
))
211 // suppress warning about unused variable save_ptr when wxStrtok() is a
212 // macro which throws away its third argument
218 wxUnusedVar(envVariable
);
219 #endif // !__WXWINCE__/__WXWINCE__
222 // Given a full filename (with path), ensure that that file can
223 // be accessed again USING FILENAME ONLY by adding the path
224 // to the list if not already there.
225 void wxPathList::EnsureFileAccessible (const wxString
& path
)
227 wxString
path_only(wxPathOnly(path
));
228 if ( !path_only
.empty() )
230 if ( !Member(path_only
) )
235 bool wxPathList::Member (const wxString
& path
)
237 for (wxStringList::compatibility_iterator node
= GetFirst(); node
; node
= node
->GetNext())
239 wxString
path2( node
->GetData() );
241 #if defined(__WINDOWS__) || defined(__OS2__) || defined(__VMS__) || defined(__WXMAC__)
243 path
.CompareTo (path2
, wxString::ignoreCase
) == 0
245 // Case sensitive File System
246 path
.CompareTo (path2
) == 0
254 wxString
wxPathList::FindValidPath (const wxString
& file
)
256 if (wxFileExists (wxExpandPath(wxFileFunctionsBuffer
, file
)))
257 return wxString(wxFileFunctionsBuffer
);
259 wxChar buf
[_MAXPATHLEN
];
260 wxStrcpy(buf
, wxFileFunctionsBuffer
);
262 wxChar
*filename
= wxIsAbsolutePath (buf
) ? wxFileNameFromPath (buf
) : (wxChar
*)buf
;
264 for (wxStringList::compatibility_iterator node
= GetFirst(); node
; node
= node
->GetNext())
266 const wxChar
*path
= node
->GetData();
267 wxStrcpy (wxFileFunctionsBuffer
, path
);
268 wxChar ch
= wxFileFunctionsBuffer
[wxStrlen(wxFileFunctionsBuffer
)-1];
269 if (ch
!= wxT('\\') && ch
!= wxT('/'))
270 wxStrcat (wxFileFunctionsBuffer
, wxT("/"));
271 wxStrcat (wxFileFunctionsBuffer
, filename
);
273 wxUnix2DosFilename (wxFileFunctionsBuffer
);
275 if (wxFileExists (wxFileFunctionsBuffer
))
277 return wxString(wxFileFunctionsBuffer
); // Found!
281 return wxEmptyString
; // Not found
284 wxString
wxPathList::FindAbsoluteValidPath (const wxString
& file
)
286 wxString f
= FindValidPath(file
);
287 if ( f
.empty() || wxIsAbsolutePath(f
) )
291 wxGetWorkingDirectory(wxStringBuffer(buf
, _MAXPATHLEN
), _MAXPATHLEN
);
293 if ( !wxEndsWithPathSeparator(buf
) )
295 buf
+= wxFILE_SEP_PATH
;
303 wxFileExists (const wxString
& filename
)
305 #if defined(__WXPALMOS__)
307 #elif defined(__WIN32__) && !defined(__WXMICROWIN__)
308 // we must use GetFileAttributes() instead of the ANSI C functions because
309 // it can cope with network (UNC) paths unlike them
310 DWORD ret
= ::GetFileAttributes(filename
);
312 return (ret
!= (DWORD
)-1) && !(ret
& FILE_ATTRIBUTE_DIRECTORY
);
315 #ifndef wxNEED_WX_UNISTD_H
316 return wxStat( filename
.fn_str() , &st
) == 0 && (st
.st_mode
& S_IFREG
);
318 return wxStat( filename
, &st
) == 0 && (st
.st_mode
& S_IFREG
);
320 #endif // __WIN32__/!__WIN32__
324 wxIsAbsolutePath (const wxString
& filename
)
326 if (!filename
.empty())
328 #if defined(__WXMAC__) && !defined(__DARWIN__)
329 // Classic or Carbon CodeWarrior like
330 // Carbon with Apple DevTools is Unix like
332 // This seems wrong to me, but there is no fix. since
333 // "MacOS:MyText.txt" is absolute whereas "MyDir:MyText.txt"
334 // is not. Or maybe ":MyDir:MyText.txt" has to be used? RR.
335 if (filename
.Find(':') != wxNOT_FOUND
&& filename
[0] != ':')
338 // Unix like or Windows
339 if (filename
[0] == wxT('/'))
343 if ((filename
[0] == wxT('[') && filename
[1] != wxT('.')))
346 #if defined(__WINDOWS__) || defined(__OS2__)
348 if (filename
[0] == wxT('\\') || (wxIsalpha (filename
[0]) && filename
[1] == wxT(':')))
356 * Strip off any extension (dot something) from end of file,
357 * IF one exists. Inserts zero into buffer.
361 void wxStripExtension(wxChar
*buffer
)
363 int len
= wxStrlen(buffer
);
367 if (buffer
[i
] == wxT('.'))
376 void wxStripExtension(wxString
& buffer
)
378 //RN: Be careful about the handling the case where
379 //buffer.Length() == 0
380 for(size_t i
= buffer
.Length() - 1; i
!= wxString::npos
; --i
)
382 if (buffer
.GetChar(i
) == wxT('.'))
384 buffer
= buffer
.Left(i
);
390 // Destructive removal of /./ and /../ stuff
391 wxChar
*wxRealPath (wxChar
*path
)
394 static const wxChar SEP
= wxT('\\');
395 wxUnix2DosFilename(path
);
397 static const wxChar SEP
= wxT('/');
399 if (path
[0] && path
[1]) {
400 /* MATTHEW: special case "/./x" */
402 if (path
[2] == SEP
&& path
[1] == wxT('.'))
410 if (p
[1] == wxT('.') && p
[2] == wxT('.') && (p
[3] == SEP
|| p
[3] == wxT('\0')))
413 for (q
= p
- 1; q
>= path
&& *q
!= SEP
; q
--)
418 if (q
[0] == SEP
&& (q
[1] != wxT('.') || q
[2] != wxT('.') || q
[3] != SEP
)
419 && (q
- 1 <= path
|| q
[-1] != SEP
))
422 if (path
[0] == wxT('\0'))
427 #if defined(__WXMSW__) || defined(__OS2__)
428 /* Check that path[2] is NULL! */
429 else if (path
[1] == wxT(':') && !path
[2])
438 else if (p
[1] == wxT('.') && (p
[2] == SEP
|| p
[2] == wxT('\0')))
447 wxChar
*wxCopyAbsolutePath(const wxString
& filename
)
449 if (filename
.empty())
450 return (wxChar
*) NULL
;
452 if (! wxIsAbsolutePath(wxExpandPath(wxFileFunctionsBuffer
, filename
))) {
453 wxChar buf
[_MAXPATHLEN
];
455 wxGetWorkingDirectory(buf
, WXSIZEOF(buf
));
456 wxChar ch
= buf
[wxStrlen(buf
) - 1];
458 if (ch
!= wxT('\\') && ch
!= wxT('/'))
459 wxStrcat(buf
, wxT("\\"));
462 wxStrcat(buf
, wxT("/"));
464 wxStrcat(buf
, wxFileFunctionsBuffer
);
465 return MYcopystring( wxRealPath(buf
) );
467 return MYcopystring( wxFileFunctionsBuffer
);
473 ~user/ => user's home dir
474 If the environment variable a = "foo" and b = "bar" then:
491 /* input name in name, pathname output to buf. */
493 wxChar
*wxExpandPath(wxChar
*buf
, const wxChar
*name
)
495 register wxChar
*d
, *s
, *nm
;
496 wxChar lnm
[_MAXPATHLEN
];
499 // Some compilers don't like this line.
500 // const wxChar trimchars[] = wxT("\n \t");
503 trimchars
[0] = wxT('\n');
504 trimchars
[1] = wxT(' ');
505 trimchars
[2] = wxT('\t');
509 const wxChar SEP
= wxT('\\');
511 const wxChar SEP
= wxT('/');
514 if (name
== NULL
|| *name
== wxT('\0'))
516 nm
= MYcopystring(name
); // Make a scratch copy
519 /* Skip leading whitespace and cr */
520 while (wxStrchr((wxChar
*)trimchars
, *nm
) != NULL
)
522 /* And strip off trailing whitespace and cr */
523 s
= nm
+ (q
= wxStrlen(nm
)) - 1;
524 while (q
-- && wxStrchr((wxChar
*)trimchars
, *s
) != NULL
)
532 q
= nm
[0] == wxT('\\') && nm
[1] == wxT('~');
535 /* Expand inline environment variables */
553 while ((*d
++ = *s
) != 0) {
555 if (*s
== wxT('\\')) {
556 if ((*(d
- 1) = *++s
)) {
564 // No env variables on WinCE
567 if (*s
++ == wxT('$') && (*s
== wxT('{') || *s
== wxT(')')))
569 if (*s
++ == wxT('$'))
572 register wxChar
*start
= d
;
573 register int braces
= (*s
== wxT('{') || *s
== wxT('('));
574 register wxChar
*value
;
575 while ((*d
++ = *s
) != 0)
576 if (braces
? (*s
== wxT('}') || *s
== wxT(')')) : !(wxIsalnum(*s
) || *s
== wxT('_')) )
581 value
= wxGetenv(braces
? start
+ 1 : start
);
583 for ((d
= start
- 1); (*d
++ = *value
++) != 0;)
597 /* Expand ~ and ~user */
599 if (nm
[0] == wxT('~') && !q
)
602 if (nm
[1] == SEP
|| nm
[1] == 0)
604 // FIXME: wxGetUserHome could return temporary storage in Unicode mode
605 if ((s
= WXSTRINGCAST
wxGetUserHome(wxEmptyString
)) != NULL
) {
610 { /* ~user/filename */
611 register wxChar
*nnm
;
612 register wxChar
*home
;
613 for (s
= nm
; *s
&& *s
!= SEP
; s
++)
617 int was_sep
; /* MATTHEW: Was there a separator, or NULL? */
618 was_sep
= (*s
== SEP
);
619 nnm
= *s
? s
+ 1 : s
;
621 // FIXME: wxGetUserHome could return temporary storage in Unicode mode
622 if ((home
= WXSTRINGCAST
wxGetUserHome(wxString(nm
+ 1))) == NULL
) {
623 if (was_sep
) /* replace only if it was there: */
634 if (s
&& *s
) { /* MATTHEW: s could be NULL if user '~' didn't exist */
636 while (wxT('\0') != (*d
++ = *s
++))
639 if (d
- 1 > buf
&& *(d
- 2) != SEP
)
643 while ((*d
++ = *s
++) != 0)
647 delete[] nm_tmp
; // clean up alloc
648 /* Now clean up the buffer */
649 return wxRealPath(buf
);
652 /* Contract Paths to be build upon an environment variable
655 example: "/usr/openwin/lib", OPENWINHOME --> ${OPENWINHOME}/lib
657 The call wxExpandPath can convert these back!
660 wxContractPath (const wxString
& filename
, const wxString
& envname
, const wxString
& user
)
662 static wxChar dest
[_MAXPATHLEN
];
664 if (filename
.empty())
665 return (wxChar
*) NULL
;
667 wxStrcpy (dest
, WXSTRINGCAST filename
);
669 wxUnix2DosFilename(dest
);
672 // Handle environment
676 if (!envname
.empty() && (val
= wxGetenv (WXSTRINGCAST envname
)) != NULL
&&
677 (tcp
= wxStrstr (dest
, val
)) != NULL
)
679 wxStrcpy (wxFileFunctionsBuffer
, tcp
+ wxStrlen (val
));
682 wxStrcpy (tcp
, WXSTRINGCAST envname
);
683 wxStrcat (tcp
, wxT("}"));
684 wxStrcat (tcp
, wxFileFunctionsBuffer
);
687 wxUnusedVar(envname
);
690 // Handle User's home (ignore root homes!)
691 val
= wxGetUserHome (user
);
695 const size_t len
= wxStrlen(val
);
699 if (wxStrncmp(dest
, val
, len
) == 0)
701 wxStrcpy(wxFileFunctionsBuffer
, wxT("~"));
703 wxStrcat(wxFileFunctionsBuffer
, (const wxChar
*) user
);
704 wxStrcat(wxFileFunctionsBuffer
, dest
+ len
);
705 wxStrcpy (dest
, wxFileFunctionsBuffer
);
711 // Return just the filename, not the path (basename)
712 wxChar
*wxFileNameFromPath (wxChar
*path
)
715 wxString n
= wxFileNameFromPath(p
);
717 return path
+ p
.length() - n
.length();
720 wxString
wxFileNameFromPath (const wxString
& path
)
723 wxFileName::SplitPath(path
, NULL
, &name
, &ext
);
725 wxString fullname
= name
;
728 fullname
<< wxFILE_SEP_EXT
<< ext
;
734 // Return just the directory, or NULL if no directory
736 wxPathOnly (wxChar
*path
)
740 static wxChar buf
[_MAXPATHLEN
];
743 wxStrcpy (buf
, path
);
745 int l
= wxStrlen(path
);
748 // Search backward for a backward or forward slash
751 #if defined(__WXMAC__) && !defined(__DARWIN__)
752 // Classic or Carbon CodeWarrior like
753 // Carbon with Apple DevTools is Unix like
754 if (path
[i
] == wxT(':') )
760 // Unix like or Windows
761 if (path
[i
] == wxT('/') || path
[i
] == wxT('\\'))
768 if (path
[i
] == wxT(']'))
777 #if defined(__WXMSW__) || defined(__OS2__)
778 // Try Drive specifier
779 if (wxIsalpha (buf
[0]) && buf
[1] == wxT(':'))
781 // A:junk --> A:. (since A:.\junk Not A:\junk)
788 return (wxChar
*) NULL
;
791 // Return just the directory, or NULL if no directory
792 wxString
wxPathOnly (const wxString
& path
)
796 wxChar buf
[_MAXPATHLEN
];
799 wxStrcpy (buf
, WXSTRINGCAST path
);
801 int l
= path
.Length();
804 // Search backward for a backward or forward slash
807 #if defined(__WXMAC__) && !defined(__DARWIN__)
808 // Classic or Carbon CodeWarrior like
809 // Carbon with Apple DevTools is Unix like
810 if (path
[i
] == wxT(':') )
813 return wxString(buf
);
816 // Unix like or Windows
817 if (path
[i
] == wxT('/') || path
[i
] == wxT('\\'))
819 // Don't return an empty string
823 return wxString(buf
);
827 if (path
[i
] == wxT(']'))
830 return wxString(buf
);
836 #if defined(__WXMSW__) || defined(__OS2__)
837 // Try Drive specifier
838 if (wxIsalpha (buf
[0]) && buf
[1] == wxT(':'))
840 // A:junk --> A:. (since A:.\junk Not A:\junk)
843 return wxString(buf
);
847 return wxEmptyString
;
850 // Utility for converting delimiters in DOS filenames to UNIX style
851 // and back again - or we get nasty problems with delimiters.
852 // Also, convert to lower case, since case is significant in UNIX.
854 #if defined(__WXMAC__)
856 #if TARGET_API_MAC_OSX
857 #define kDefaultPathStyle kCFURLPOSIXPathStyle
859 #define kDefaultPathStyle kCFURLHFSPathStyle
862 wxString
wxMacFSRefToPath( const FSRef
*fsRef
, CFStringRef additionalPathComponent
)
865 fullURLRef
= CFURLCreateFromFSRef(NULL
, fsRef
);
866 if ( additionalPathComponent
)
868 CFURLRef parentURLRef
= fullURLRef
;
869 fullURLRef
= CFURLCreateCopyAppendingPathComponent(NULL
, parentURLRef
,
870 additionalPathComponent
,false);
871 CFRelease( parentURLRef
) ;
873 CFStringRef cfString
= CFURLCopyFileSystemPath(fullURLRef
, kDefaultPathStyle
);
874 CFRelease( fullURLRef
) ;
875 return wxMacCFStringHolder(cfString
).AsString(wxLocale::GetSystemEncoding());
878 OSStatus
wxMacPathToFSRef( const wxString
&path
, FSRef
*fsRef
)
880 OSStatus err
= noErr
;
881 CFURLRef url
= CFURLCreateWithFileSystemPath(kCFAllocatorDefault
, wxMacCFStringHolder(path
,wxLocale::GetSystemEncoding() ) , kDefaultPathStyle
, false);
884 if ( CFURLGetFSRef(url
, fsRef
) == false )
895 wxString
wxMacHFSUniStrToString( ConstHFSUniStr255Param uniname
)
897 CFStringRef cfname
= CFStringCreateWithCharacters( kCFAllocatorDefault
,
900 return wxMacCFStringHolder(cfname
).AsString() ;
903 wxString
wxMacFSSpec2MacFilename( const FSSpec
*spec
)
906 if ( FSpMakeFSRef( spec
, &fsRef
) == noErr
)
908 return wxMacFSRefToPath( &fsRef
) ;
910 return wxEmptyString
;
913 void wxMacFilename2FSSpec( const wxString
& path
, FSSpec
*spec
)
915 OSStatus err
= noErr
;
917 wxMacPathToFSRef( path
, &fsRef
) ;
918 err
= FSRefMakeFSSpec( &fsRef
, spec
) ;
924 wxDos2UnixFilename (wxChar
*s
)
933 *s
= (wxChar
)wxTolower (*s
); // Case INDEPENDENT
940 #if defined(__WXMSW__) || defined(__OS2__)
941 wxUnix2DosFilename (wxChar
*s
)
943 wxUnix2DosFilename (wxChar
*WXUNUSED(s
) )
946 // Yes, I really mean this to happen under DOS only! JACS
947 #if defined(__WXMSW__) || defined(__OS2__)
958 // Concatenate two files to form third
960 wxConcatFiles (const wxString
& file1
, const wxString
& file2
, const wxString
& file3
)
963 if ( !wxGetTempFileName( wxT("cat"), outfile
) )
966 FILE *fp1
wxDUMMY_INITIALIZE(NULL
);
969 // Open the inputs and outputs
970 if ((fp1
= wxFopen ( file1
, wxT("rb"))) == NULL
||
971 (fp2
= wxFopen ( file2
, wxT("rb"))) == NULL
||
972 (fp3
= wxFopen ( outfile
, wxT("wb"))) == NULL
)
984 while ((ch
= getc (fp1
)) != EOF
)
985 (void) putc (ch
, fp3
);
988 while ((ch
= getc (fp2
)) != EOF
)
989 (void) putc (ch
, fp3
);
993 bool result
= wxRenameFile(outfile
, file3
);
999 wxCopyFile (const wxString
& file1
, const wxString
& file2
, bool overwrite
)
1001 #if defined(__WIN32__) && !defined(__WXMICROWIN__)
1002 // CopyFile() copies file attributes and modification time too, so use it
1003 // instead of our code if available
1005 // NB: 3rd parameter is bFailIfExists i.e. the inverse of overwrite
1006 if ( !::CopyFile(file1
, file2
, !overwrite
) )
1008 wxLogSysError(_("Failed to copy the file '%s' to '%s'"),
1009 file1
.c_str(), file2
.c_str());
1013 #elif defined(__OS2__)
1014 if ( ::DosCopy(file2
, file2
, overwrite
? DCPY_EXISTING
: 0) != 0 )
1016 #elif defined(__PALMOS__)
1017 // TODO with http://www.palmos.com/dev/support/docs/protein_books/Memory_Databases_Files/
1019 #elif wxUSE_FILE // !Win32
1022 // get permissions of file1
1023 if ( wxStat( file1
.c_str(), &fbuf
) != 0 )
1025 // the file probably doesn't exist or we haven't the rights to read
1027 wxLogSysError(_("Impossible to get permissions for file '%s'"),
1032 // open file1 for reading
1033 wxFile
fileIn(file1
, wxFile::read
);
1034 if ( !fileIn
.IsOpened() )
1037 // remove file2, if it exists. This is needed for creating
1038 // file2 with the correct permissions in the next step
1039 if ( wxFileExists(file2
) && (!overwrite
|| !wxRemoveFile(file2
)))
1041 wxLogSysError(_("Impossible to overwrite the file '%s'"),
1046 // reset the umask as we want to create the file with exactly the same
1047 // permissions as the original one
1050 // create file2 with the same permissions than file1 and open it for
1054 if ( !fileOut
.Create(file2
, overwrite
, fbuf
.st_mode
& 0777) )
1057 // copy contents of file1 to file2
1062 count
= fileIn
.Read(buf
, WXSIZEOF(buf
));
1063 if ( fileIn
.Error() )
1070 if ( fileOut
.Write(buf
, count
) < count
)
1074 // we can expect fileIn to be closed successfully, but we should ensure
1075 // that fileOut was closed as some write errors (disk full) might not be
1076 // detected before doing this
1077 if ( !fileIn
.Close() || !fileOut
.Close() )
1080 #if !defined(__VISAGECPP__) && !defined(__WXMAC__) || defined(__UNIX__)
1081 // no chmod in VA. Should be some permission API for HPFS386 partitions
1083 if ( chmod(OS_FILENAME(file2
), fbuf
.st_mode
) != 0 )
1085 wxLogSysError(_("Impossible to set permissions for the file '%s'"),
1089 #endif // OS/2 || Mac
1091 #else // !Win32 && ! wxUSE_FILE
1093 // impossible to simulate with wxWidgets API
1096 wxUnusedVar(overwrite
);
1099 #endif // __WXMSW__ && __WIN32__
1105 wxRenameFile (const wxString
& file1
, const wxString
& file2
)
1107 #if !defined(__WXWINCE__) && !defined(__WXPALMOS__)
1108 // Normal system call
1109 if ( wxRename (file1
, file2
) == 0 )
1114 if (wxCopyFile(file1
, file2
)) {
1115 wxRemoveFile(file1
);
1122 bool wxRemoveFile(const wxString
& file
)
1124 #if defined(__VISUALC__) \
1125 || defined(__BORLANDC__) \
1126 || defined(__WATCOMC__) \
1127 || defined(__DMC__) \
1128 || defined(__GNUWIN32__) \
1129 || (defined(__MWERKS__) && defined(__MSL__))
1130 int res
= wxRemove(file
);
1131 #elif defined(__WXMAC__)
1132 int res
= unlink(wxFNCONV(file
));
1133 #elif defined(__WXPALMOS__)
1135 // TODO with VFSFileDelete()
1137 int res
= unlink(OS_FILENAME(file
));
1143 bool wxMkdir(const wxString
& dir
, int perm
)
1145 #if defined(__WXPALMOS__)
1147 #elif defined(__WXMAC__) && !defined(__UNIX__)
1148 return (mkdir( wxFNCONV(dir
) , 0 ) == 0);
1150 const wxChar
*dirname
= dir
.c_str();
1152 // assume mkdir() has 2 args on non Windows-OS/2 platforms and on Windows too
1153 // for the GNU compiler
1154 #if (!(defined(__WXMSW__) || defined(__OS2__) || defined(__DOS__))) || (defined(__GNUWIN32__) && !defined(__MINGW32__)) || defined(__WINE__) || defined(__WXMICROWIN__)
1157 if ( mkdir(wxFNCONV(dirname
)) != 0 )
1159 if ( mkdir(wxFNCONV(dirname
), perm
) != 0 )
1161 #elif defined(__OS2__)
1162 if (::DosCreateDir((PSZ
)dirname
, NULL
) != 0) // enhance for EAB's??
1163 #elif defined(__DOS__)
1164 #if defined(__WATCOMC__)
1166 if ( wxMkDir(wxFNSTRINGCAST
wxFNCONV(dirname
)) != 0 )
1167 #elif defined(__DJGPP__)
1168 if ( mkdir(wxFNCONV(dirname
), perm
) != 0 )
1170 #error "Unsupported DOS compiler!"
1172 #else // !MSW, !DOS and !OS/2 VAC++
1175 if ( !CreateDirectory(dirname
, NULL
) )
1177 if ( wxMkDir(dir
.fn_str()) != 0 )
1181 wxLogSysError(_("Directory '%s' couldn't be created"), dirname
);
1190 bool wxRmdir(const wxString
& dir
, int WXUNUSED(flags
))
1192 #if defined(__VMS__)
1193 return false; //to be changed since rmdir exists in VMS7.x
1194 #elif defined(__OS2__)
1195 return (::DosDeleteDir((PSZ
)dir
.c_str()) == 0);
1196 #elif defined(__WXWINCE__)
1197 return (CreateDirectory(dir
, NULL
) != 0);
1198 #elif defined(__WXPALMOS__)
1199 // TODO with VFSFileRename()
1202 return (wxRmDir(OS_FILENAME(dir
)) == 0);
1206 // does the path exists? (may have or not '/' or '\\' at the end)
1207 bool wxDirExists(const wxChar
*pszPathName
)
1209 wxString
strPath(pszPathName
);
1211 #if defined(__WINDOWS__) || defined(__OS2__)
1212 // Windows fails to find directory named "c:\dir\" even if "c:\dir" exists,
1213 // so remove all trailing backslashes from the path - but don't do this for
1214 // the pathes "d:\" (which are different from "d:") nor for just "\"
1215 while ( wxEndsWithPathSeparator(strPath
) )
1217 size_t len
= strPath
.length();
1218 if ( len
== 1 || (len
== 3 && strPath
[len
- 2] == _T(':')) )
1221 strPath
.Truncate(len
- 1);
1223 #endif // __WINDOWS__
1226 // OS/2 can't handle "d:", it wants either "d:\" or "d:."
1227 if (strPath
.length() == 2 && strPath
[1u] == _T(':'))
1231 #if defined(__WXPALMOS__)
1233 #elif defined(__WIN32__) && !defined(__WXMICROWIN__)
1234 // stat() can't cope with network paths
1235 DWORD ret
= ::GetFileAttributes(strPath
);
1237 return (ret
!= (DWORD
)-1) && (ret
& FILE_ATTRIBUTE_DIRECTORY
);
1238 #elif defined(__OS2__)
1239 return (::DosSetCurrentDir(WXSTRINGCAST strPath
));
1243 #ifndef __VISAGECPP__
1244 return wxStat(strPath
.c_str(), &st
) == 0 && ((st
.st_mode
& S_IFMT
) == S_IFDIR
);
1246 // S_IFMT not supported in VA compilers.. st_mode is a 2byte value only
1247 return wxStat(pszPathName
, &st
) == 0 && (st
.st_mode
== S_IFDIR
);
1250 #endif // __WIN32__/!__WIN32__
1253 // Get a temporary filename, opening and closing the file.
1254 wxChar
*wxGetTempFileName(const wxString
& prefix
, wxChar
*buf
)
1257 wxString filename
= wxFileName::CreateTempFileName(prefix
);
1258 if ( filename
.empty() )
1262 wxStrcpy(buf
, filename
);
1264 buf
= MYcopystring(filename
);
1268 wxUnusedVar(prefix
);
1270 // wxFileName::CreateTempFileName needs wxFile class enabled
1275 bool wxGetTempFileName(const wxString
& prefix
, wxString
& buf
)
1277 buf
= wxGetTempFileName(prefix
);
1279 return !buf
.empty();
1282 // Get first file name matching given wild card.
1284 static wxDir
*gs_dir
= NULL
;
1285 static wxString gs_dirPath
;
1287 wxString
wxFindFirstFile(const wxChar
*spec
, int flags
)
1289 wxSplitPath(spec
, &gs_dirPath
, NULL
, NULL
);
1290 if ( gs_dirPath
.empty() )
1291 gs_dirPath
= wxT(".");
1292 if ( !wxEndsWithPathSeparator(gs_dirPath
) )
1293 gs_dirPath
<< wxFILE_SEP_PATH
;
1297 gs_dir
= new wxDir(gs_dirPath
);
1299 if ( !gs_dir
->IsOpened() )
1301 wxLogSysError(_("Can not enumerate files '%s'"), spec
);
1302 return wxEmptyString
;
1308 case wxDIR
: dirFlags
= wxDIR_DIRS
; break;
1309 case wxFILE
: dirFlags
= wxDIR_FILES
; break;
1310 default: dirFlags
= wxDIR_DIRS
| wxDIR_FILES
; break;
1314 gs_dir
->GetFirst(&result
, wxFileNameFromPath(wxString(spec
)), dirFlags
);
1315 if ( result
.empty() )
1321 return gs_dirPath
+ result
;
1324 wxString
wxFindNextFile()
1326 wxASSERT_MSG( gs_dir
, wxT("You must call wxFindFirstFile before!") );
1329 gs_dir
->GetNext(&result
);
1331 if ( result
.empty() )
1337 return gs_dirPath
+ result
;
1341 // Get current working directory.
1342 // If buf is NULL, allocates space using new, else
1344 wxChar
*wxGetWorkingDirectory(wxChar
*buf
, int sz
)
1346 #if defined(__WXPALMOS__)
1349 #elif defined(__WXWINCE__)
1357 buf
= new wxChar
[sz
+ 1];
1360 bool ok
wxDUMMY_INITIALIZE(false);
1362 // for the compilers which have Unicode version of _getcwd(), call it
1363 // directly, for the others call the ANSI version and do the translation
1366 #else // wxUSE_UNICODE
1367 bool needsANSI
= true;
1369 #if !defined(HAVE_WGETCWD) || wxUSE_UNICODE_MSLU
1370 // This is not legal code as the compiler
1371 // is allowed destroy the wxCharBuffer.
1372 // wxCharBuffer c_buffer(sz);
1373 // char *cbuf = (char*)(const char*)c_buffer;
1374 char cbuf
[_MAXPATHLEN
];
1378 #if wxUSE_UNICODE_MSLU
1379 if ( wxGetOsVersion() != wxWIN95
)
1381 char *cbuf
= NULL
; // never really used because needsANSI will always be false
1384 ok
= _wgetcwd(buf
, sz
) != NULL
;
1390 #endif // wxUSE_UNICODE
1392 #if defined(_MSC_VER) || defined(__MINGW32__)
1393 ok
= _getcwd(cbuf
, sz
) != NULL
;
1394 #elif defined(__WXMAC__) && !defined(__DARWIN__)
1396 if ( getcwd( lbuf
, sizeof( lbuf
) ) )
1398 wxString
res( lbuf
, *wxConvCurrent
) ;
1399 wxStrcpy( buf
, res
) ;
1404 #elif defined(__OS2__)
1406 ULONG ulDriveNum
= 0;
1407 ULONG ulDriveMap
= 0;
1408 rc
= ::DosQueryCurrentDisk(&ulDriveNum
, &ulDriveMap
);
1413 rc
= ::DosQueryCurrentDir( 0 // current drive
1417 cbuf
[0] = 'A' + (ulDriveNum
- 1);
1422 #else // !Win32/VC++ !Mac !OS2
1423 ok
= getcwd(cbuf
, sz
) != NULL
;
1426 #if wxUSE_UNICODE && !(defined(__WXMAC__) && !defined(__DARWIN__))
1427 // finally convert the result to Unicode if needed
1428 wxConvFile
.MB2WC(buf
, cbuf
, sz
);
1429 #endif // wxUSE_UNICODE
1434 wxLogSysError(_("Failed to get the working directory"));
1436 // VZ: the old code used to return "." on error which didn't make any
1437 // sense at all to me - empty string is a better error indicator
1438 // (NULL might be even better but I'm afraid this could lead to
1439 // problems with the old code assuming the return is never NULL)
1442 else // ok, but we might need to massage the path into the right format
1445 // VS: DJGPP is a strange mix of DOS and UNIX API and returns paths
1446 // with / deliminers. We don't like that.
1447 for (wxChar
*ch
= buf
; *ch
; ch
++)
1449 if (*ch
== wxT('/'))
1454 // MBN: we hope that in the case the user is compiling a GTK+/Motif app,
1455 // he needs Unix as opposed to Win32 pathnames
1456 #if defined( __CYGWIN__ ) && defined( __WINDOWS__ )
1457 // another example of DOS/Unix mix (Cygwin)
1458 wxString pathUnix
= buf
;
1459 cygwin_conv_to_full_win32_path(pathUnix
, buf
);
1460 #endif // __CYGWIN__
1475 wxChar
*buffer
= new wxChar
[_MAXPATHLEN
];
1476 wxGetWorkingDirectory(buffer
, _MAXPATHLEN
);
1477 wxString
str( buffer
);
1483 bool wxSetWorkingDirectory(const wxString
& d
)
1485 #if defined(__OS2__)
1486 return (::DosSetCurrentDir((PSZ
)d
.c_str()) == 0);
1487 #elif defined(__UNIX__) || defined(__WXMAC__) || defined(__DOS__)
1488 return (chdir(wxFNSTRINGCAST d
.fn_str()) == 0);
1489 #elif defined(__WINDOWS__)
1493 // No equivalent in WinCE
1497 return (bool)(SetCurrentDirectory(d
) != 0);
1500 // Must change drive, too.
1501 bool isDriveSpec
= ((strlen(d
) > 1) && (d
[1] == ':'));
1504 wxChar firstChar
= d
[0];
1508 firstChar
= firstChar
- 32;
1510 // To a drive number
1511 unsigned int driveNo
= firstChar
- 64;
1514 unsigned int noDrives
;
1515 _dos_setdrive(driveNo
, &noDrives
);
1518 bool success
= (chdir(WXSTRINGCAST d
) == 0);
1526 // Get the OS directory if appropriate (such as the Windows directory).
1527 // On non-Windows platform, probably just return the empty string.
1528 wxString
wxGetOSDirectory()
1531 return wxString(wxT("\\Windows"));
1532 #elif defined(__WINDOWS__) && !defined(__WXMICROWIN__)
1534 GetWindowsDirectory(buf
, 256);
1535 return wxString(buf
);
1536 #elif defined(__WXMAC__)
1537 return wxMacFindFolder(kOnSystemDisk
, 'macs', false);
1539 return wxEmptyString
;
1543 bool wxEndsWithPathSeparator(const wxChar
*pszFileName
)
1545 size_t len
= wxStrlen(pszFileName
);
1547 return len
&& wxIsPathSeparator(pszFileName
[len
- 1]);
1550 // find a file in a list of directories, returns false if not found
1551 bool wxFindFileInPath(wxString
*pStr
, const wxChar
*pszPath
, const wxChar
*pszFile
)
1553 // we assume that it's not empty
1554 wxCHECK_MSG( !wxIsEmpty(pszFile
), false,
1555 _T("empty file name in wxFindFileInPath"));
1557 // skip path separator in the beginning of the file name if present
1558 if ( wxIsPathSeparator(*pszFile
) )
1561 // copy the path (strtok will modify it)
1562 wxChar
*szPath
= new wxChar
[wxStrlen(pszPath
) + 1];
1563 wxStrcpy(szPath
, pszPath
);
1566 wxChar
*pc
, *save_ptr
;
1567 for ( pc
= wxStrtok(szPath
, wxPATH_SEP
, &save_ptr
);
1569 pc
= wxStrtok((wxChar
*) NULL
, wxPATH_SEP
, &save_ptr
) )
1571 // search for the file in this directory
1573 if ( !wxEndsWithPathSeparator(pc
) )
1574 strFile
+= wxFILE_SEP_PATH
;
1577 if ( wxFileExists(strFile
) ) {
1583 // suppress warning about unused variable save_ptr when wxStrtok() is a
1584 // macro which throws away its third argument
1589 return pc
!= NULL
; // if true => we breaked from the loop
1592 void WXDLLEXPORT
wxSplitPath(const wxChar
*pszFileName
,
1597 // it can be empty, but it shouldn't be NULL
1598 wxCHECK_RET( pszFileName
, wxT("NULL file name in wxSplitPath") );
1600 wxFileName::SplitPath(pszFileName
, pstrPath
, pstrName
, pstrExt
);
1603 time_t WXDLLEXPORT
wxFileModificationTime(const wxString
& filename
)
1605 #if defined(__WXPALMOS__)
1607 #elif defined(__WXWINCE__)
1608 FILETIME creationTime
, lastAccessTime
, lastWriteTime
;
1609 HANDLE fileHandle
= ::CreateFile(filename
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
1610 0, FILE_ATTRIBUTE_NORMAL
, 0);
1611 if (fileHandle
== INVALID_HANDLE_VALUE
)
1615 if (GetFileTime(fileHandle
, & creationTime
, & lastAccessTime
, & lastWriteTime
))
1617 CloseHandle(fileHandle
);
1619 wxDateTime dateTime
;
1621 if ( !::FileTimeToLocalFileTime(&lastWriteTime
, &ftLocal
) )
1623 wxLogLastError(_T("FileTimeToLocalFileTime"));
1627 if ( !::FileTimeToSystemTime(&ftLocal
, &st
) )
1629 wxLogLastError(_T("FileTimeToSystemTime"));
1632 dateTime
.Set(st
.wDay
, wxDateTime::Month(st
.wMonth
- 1), st
.wYear
,
1633 st
.wHour
, st
.wMinute
, st
.wSecond
, st
.wMilliseconds
);
1634 return dateTime
.GetTicks();
1641 wxStat( filename
, &buf
);
1643 return buf
.st_mtime
;
1648 // Parses the filterStr, returning the number of filters.
1649 // Returns 0 if none or if there's a problem.
1650 // filterStr is in the form: "All files (*.*)|*.*|JPEG Files (*.jpeg)|*.jpeg"
1652 int WXDLLEXPORT
wxParseCommonDialogsFilter(const wxString
& filterStr
, wxArrayString
& descriptions
, wxArrayString
& filters
)
1654 descriptions
.Clear();
1657 wxString
str(filterStr
);
1659 wxString description
, filter
;
1661 while( pos
!= wxNOT_FOUND
)
1663 pos
= str
.Find(wxT('|'));
1664 if ( pos
== wxNOT_FOUND
)
1666 // if there are no '|'s at all in the string just take the entire
1667 // string as filter and make description empty for later autocompletion
1668 if ( filters
.IsEmpty() )
1670 descriptions
.Add(wxEmptyString
);
1671 filters
.Add(filterStr
);
1675 wxFAIL_MSG( _T("missing '|' in the wildcard string!") );
1681 description
= str
.Left(pos
);
1682 str
= str
.Mid(pos
+ 1);
1683 pos
= str
.Find(wxT('|'));
1684 if ( pos
== wxNOT_FOUND
)
1690 filter
= str
.Left(pos
);
1691 str
= str
.Mid(pos
+ 1);
1694 descriptions
.Add(description
);
1695 filters
.Add(filter
);
1698 #if defined(__WXMOTIF__)
1699 // split it so there is one wildcard per entry
1700 for( size_t i
= 0 ; i
< descriptions
.GetCount() ; i
++ )
1702 pos
= filters
[i
].Find(wxT(';'));
1703 if (pos
!= wxNOT_FOUND
)
1705 // first split only filters
1706 descriptions
.Insert(descriptions
[i
],i
+1);
1707 filters
.Insert(filters
[i
].Mid(pos
+1),i
+1);
1708 filters
[i
]=filters
[i
].Left(pos
);
1710 // autoreplace new filter in description with pattern:
1711 // C/C++ Files(*.cpp;*.c;*.h)|*.cpp;*.c;*.h
1712 // cause split into:
1713 // C/C++ Files(*.cpp)|*.cpp
1714 // C/C++ Files(*.c;*.h)|*.c;*.h
1715 // and next iteration cause another split into:
1716 // C/C++ Files(*.cpp)|*.cpp
1717 // C/C++ Files(*.c)|*.c
1718 // C/C++ Files(*.h)|*.h
1719 for ( size_t k
=i
;k
<i
+2;k
++ )
1721 pos
= descriptions
[k
].Find(filters
[k
]);
1722 if (pos
!= wxNOT_FOUND
)
1724 wxString before
= descriptions
[k
].Left(pos
);
1725 wxString after
= descriptions
[k
].Mid(pos
+filters
[k
].Len());
1726 pos
= before
.Find(_T('('),true);
1727 if (pos
>before
.Find(_T(')'),true))
1729 before
= before
.Left(pos
+1);
1730 before
<< filters
[k
];
1731 pos
= after
.Find(_T(')'));
1732 int pos1
= after
.Find(_T('('));
1733 if (pos
!= wxNOT_FOUND
&& (pos
<pos1
|| pos1
==wxNOT_FOUND
))
1735 before
<< after
.Mid(pos
);
1736 descriptions
[k
] = before
;
1746 for( size_t j
= 0 ; j
< descriptions
.GetCount() ; j
++ )
1748 if ( descriptions
[j
].empty() && !filters
[j
].empty() )
1750 descriptions
[j
].Printf(_("Files (%s)"), filters
[j
].c_str());
1754 return filters
.GetCount();
1758 //------------------------------------------------------------------------
1759 // wild character routines
1760 //------------------------------------------------------------------------
1762 bool wxIsWild( const wxString
& pattern
)
1764 wxString tmp
= pattern
;
1765 wxChar
*pat
= WXSTRINGCAST(tmp
);
1770 case wxT('?'): case wxT('*'): case wxT('['): case wxT('{'):
1781 * Written By Douglas A. Lewis <dalewis@cs.Buffalo.EDU>
1783 * The match procedure is public domain code (from ircII's reg.c)
1786 bool wxMatchWild( const wxString
& pat
, const wxString
& text
, bool dot_special
)
1790 /* Match if both are empty. */
1794 const wxChar
*m
= pat
.c_str(),
1805 if (dot_special
&& (*n
== wxT('.')))
1807 /* Never match so that hidden Unix files
1808 * are never found. */
1822 else if (*m
== wxT('?'))
1830 if (*m
== wxT('\\'))
1833 /* Quoting "nothing" is a bad thing */
1840 * If we are out of both strings or we just
1841 * saw a wildcard, then we can say we have a
1852 * We could check for *n == NULL at this point, but
1853 * since it's more common to have a character there,
1854 * check to see if they match first (m and n) and
1855 * then if they don't match, THEN we can check for
1873 * If there are no more characters in the
1874 * string, but we still need to find another
1875 * character (*m != NULL), then it will be
1876 * impossible to match it
1883 if (*np
== wxT(' '))
1907 // Return the type of an open file
1909 // Some file types on some platforms seem seekable but in fact are not.
1910 // The main use of this function is to allow such cases to be detected
1911 // (IsSeekable() is implemented as wxGetFileKind() == wxFILE_KIND_DISK).
1913 // This is important for the archive streams, which benefit greatly from
1914 // being able to seek on a stream, but which will produce corrupt archives
1915 // if they unknowingly seek on a non-seekable stream.
1917 // wxFILE_KIND_DISK is a good catch all return value, since other values
1918 // disable features of the archive streams. Some other value must be returned
1919 // for a file type that appears seekable but isn't.
1922 // * Pipes on Windows
1923 // * Files on VMS with a record format other than StreamLF
1925 wxFileKind
wxGetFileKind(int fd
)
1927 #if defined __WXMSW__ && !defined __WXWINCE__ && defined wxGetOSFHandle
1928 switch (::GetFileType(wxGetOSFHandle(fd
)) & ~FILE_TYPE_REMOTE
)
1930 case FILE_TYPE_CHAR
:
1931 return wxFILE_KIND_TERMINAL
;
1932 case FILE_TYPE_DISK
:
1933 return wxFILE_KIND_DISK
;
1934 case FILE_TYPE_PIPE
:
1935 return wxFILE_KIND_PIPE
;
1938 return wxFILE_KIND_UNKNOWN
;
1940 #elif defined(__UNIX__)
1942 return wxFILE_KIND_TERMINAL
;
1947 if (S_ISFIFO(st
.st_mode
))
1948 return wxFILE_KIND_PIPE
;
1949 if (!S_ISREG(st
.st_mode
))
1950 return wxFILE_KIND_UNKNOWN
;
1952 #if defined(__VMS__)
1953 if (st
.st_fab_rfm
!= FAB$C_STMLF
)
1954 return wxFILE_KIND_UNKNOWN
;
1957 return wxFILE_KIND_DISK
;
1960 #define wxFILEKIND_STUB
1962 return wxFILE_KIND_DISK
;
1966 wxFileKind
wxGetFileKind(FILE *fp
)
1968 // Note: The watcom rtl dll doesn't have fileno (the static lib does).
1969 // Should be fixed in version 1.4.
1970 #if defined(wxFILEKIND_STUB) || \
1971 (defined(__WATCOMC__) && __WATCOMC__ <= 1230 && defined(__SW_BR))
1973 return wxFILE_KIND_DISK
;
1975 return wxGetFileKind(fileno(fp
));
1980 #pragma warning(default:4706) // assignment within conditional expression