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"
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
58 #include "wx/msw/wince/time.h"
59 #include "wx/msw/private.h"
66 #elif !defined(__MWERKS__)
67 #include <sys/types.h>
71 #include <sys/types.h>
84 // need to check for __OS2__ first since currently both
85 // __OS2__ and __UNIX__ are defined.
87 #include "wx/os2/private.h"
91 #elif defined(__UNIX__)
97 #if defined(__WINDOWS__) && !defined(__WXMICROWIN__)
98 #if !defined( __GNUWIN32__ ) && !defined( __MWERKS__ ) && !defined(__SALFORDC__) && !defined(__WXWINCE__)
102 #endif // __WINDOWS__
103 #endif // native Win compiler
116 #ifdef __BORLANDC__ // Please someone tell me which version of Borland needs
117 // this (3.1 I believe) and how to test for it.
118 // If this works for Borland 4.0 as well, then no worries.
129 // No, Cygwin doesn't appear to have fnmatch.h after all.
130 #if defined(HAVE_FNMATCH_H)
135 #include "wx/msw/wrapwin.h"
136 #include "wx/msw/mslu.h"
143 // sys/cygwin.h is needed for cygwin_conv_to_full_win32_path()
145 // note that it must be included after <windows.h>
148 #include <sys/cygwin.h>
150 #endif // __GNUWIN32__
151 #endif // __WINDOWS__
153 // TODO: Borland probably has _wgetcwd as well?
158 // ----------------------------------------------------------------------------
160 // ----------------------------------------------------------------------------
163 #define _MAXPATHLEN 1024
168 # include "MoreFilesX.h"
170 # include "MoreFiles.h"
171 # include "MoreFilesExtras.h"
172 # include "FullPath.h"
173 # include "FSpCompat.h"
177 // ----------------------------------------------------------------------------
179 // ----------------------------------------------------------------------------
181 // MT-FIXME: get rid of this horror and all code using it
182 static wxChar wxFileFunctionsBuffer
[4*_MAXPATHLEN
];
184 #if defined(__VISAGECPP__) && __IBMCPP__ >= 400
186 // VisualAge C++ V4.0 cannot have any external linkage const decs
187 // in headers included by more than one primary source
189 const off_t wxInvalidOffset
= (off_t
)-1;
192 // ----------------------------------------------------------------------------
194 // ----------------------------------------------------------------------------
196 // we need to translate Mac filenames before passing them to OS functions
197 #define OS_FILENAME(s) (s.fn_str())
199 // ============================================================================
201 // ============================================================================
203 #ifdef wxNEED_WX_UNISTD_H
205 WXDLLEXPORT
int wxStat( const wxChar
*file_name
, wxStructStat
*buf
)
207 return stat( wxConvFile
.cWX2MB( file_name
), buf
);
210 WXDLLEXPORT
int wxAccess( const wxChar
*pathname
, int mode
)
212 return access( wxConvFile
.cWX2MB( pathname
), mode
);
215 WXDLLEXPORT
int wxOpen( const wxChar
*pathname
, int flags
, mode_t mode
)
217 return open( wxConvFile
.cWX2MB( pathname
), flags
, mode
);
221 // wxNEED_WX_UNISTD_H
223 // ----------------------------------------------------------------------------
225 // ----------------------------------------------------------------------------
227 // IMPLEMENT_DYNAMIC_CLASS(wxPathList, wxStringList)
229 static inline wxChar
* MYcopystring(const wxString
& s
)
231 wxChar
* copy
= new wxChar
[s
.length() + 1];
232 return wxStrcpy(copy
, s
.c_str());
235 static inline wxChar
* MYcopystring(const wxChar
* s
)
237 wxChar
* copy
= new wxChar
[wxStrlen(s
) + 1];
238 return wxStrcpy(copy
, s
);
241 void wxPathList::Add (const wxString
& path
)
243 wxStringList::Add (WXSTRINGCAST path
);
246 // Add paths e.g. from the PATH environment variable
247 void wxPathList::AddEnvList (const wxString
& envVariable
)
249 // No environment variables on WinCE
251 static const wxChar PATH_TOKS
[] =
252 #if defined(__WINDOWS__) || defined(__OS2__)
254 The space has been removed from the tokenizers, otherwise a
255 path such as "C:\Program Files" would be split into 2 paths:
256 "C:\Program" and "Files"
258 // wxT(" ;"); // Don't seperate with colon in DOS (used for drive)
259 wxT(";"); // Don't seperate with colon in DOS (used for drive)
264 wxChar
*val
= wxGetenv (WXSTRINGCAST envVariable
);
267 wxChar
*s
= MYcopystring (val
);
268 wxChar
*save_ptr
, *token
= wxStrtok (s
, PATH_TOKS
, &save_ptr
);
275 if ( (token
= wxStrtok ((wxChar
*) NULL
, PATH_TOKS
, &save_ptr
))
283 // suppress warning about unused variable save_ptr when wxStrtok() is a
284 // macro which throws away its third argument
292 // Given a full filename (with path), ensure that that file can
293 // be accessed again USING FILENAME ONLY by adding the path
294 // to the list if not already there.
295 void wxPathList::EnsureFileAccessible (const wxString
& path
)
297 wxString
path_only(wxPathOnly(path
));
298 if ( !path_only
.IsEmpty() )
300 if ( !Member(path_only
) )
305 bool wxPathList::Member (const wxString
& path
)
307 for (wxStringList::compatibility_iterator node
= GetFirst(); node
; node
= node
->GetNext())
309 wxString
path2( node
->GetData() );
311 #if defined(__WINDOWS__) || defined(__OS2__) || defined(__VMS__) || defined (__WXMAC__)
313 path
.CompareTo (path2
, wxString::ignoreCase
) == 0
315 // Case sensitive File System
316 path
.CompareTo (path2
) == 0
324 wxString
wxPathList::FindValidPath (const wxString
& file
)
326 if (wxFileExists (wxExpandPath(wxFileFunctionsBuffer
, file
)))
327 return wxString(wxFileFunctionsBuffer
);
329 wxChar buf
[_MAXPATHLEN
];
330 wxStrcpy(buf
, wxFileFunctionsBuffer
);
332 wxChar
*filename
= wxIsAbsolutePath (buf
) ? wxFileNameFromPath (buf
) : (wxChar
*)buf
;
334 for (wxStringList::compatibility_iterator node
= GetFirst(); node
; node
= node
->GetNext())
336 const wxChar
*path
= node
->GetData();
337 wxStrcpy (wxFileFunctionsBuffer
, path
);
338 wxChar ch
= wxFileFunctionsBuffer
[wxStrlen(wxFileFunctionsBuffer
)-1];
339 if (ch
!= wxT('\\') && ch
!= wxT('/'))
340 wxStrcat (wxFileFunctionsBuffer
, wxT("/"));
341 wxStrcat (wxFileFunctionsBuffer
, filename
);
343 wxUnix2DosFilename (wxFileFunctionsBuffer
);
345 if (wxFileExists (wxFileFunctionsBuffer
))
347 return wxString(wxFileFunctionsBuffer
); // Found!
351 return wxEmptyString
; // Not found
354 wxString
wxPathList::FindAbsoluteValidPath (const wxString
& file
)
356 wxString f
= FindValidPath(file
);
357 if ( wxIsAbsolutePath(f
) )
361 wxGetWorkingDirectory(wxStringBuffer(buf
, _MAXPATHLEN
), _MAXPATHLEN
);
363 if ( !wxEndsWithPathSeparator(buf
) )
365 buf
+= wxFILE_SEP_PATH
;
373 wxFileExists (const wxString
& filename
)
375 // we must use GetFileAttributes() instead of the ANSI C functions because
376 // it can cope with network (UNC) paths unlike them
377 #if defined(__WIN32__) && !defined(__WXMICROWIN__)
378 DWORD ret
= ::GetFileAttributes(filename
);
380 return (ret
!= (DWORD
)-1) && !(ret
& FILE_ATTRIBUTE_DIRECTORY
);
383 return wxStat(filename
, &st
) == 0 && (st
.st_mode
& S_IFREG
);
384 #endif // __WIN32__/!__WIN32__
388 wxIsAbsolutePath (const wxString
& filename
)
390 if (filename
!= wxT(""))
392 #if defined(__WXMAC__) && !defined(__DARWIN__)
393 // Classic or Carbon CodeWarrior like
394 // Carbon with Apple DevTools is Unix like
396 // This seems wrong to me, but there is no fix. since
397 // "MacOS:MyText.txt" is absolute whereas "MyDir:MyText.txt"
398 // is not. Or maybe ":MyDir:MyText.txt" has to be used? RR.
399 if (filename
.Find(':') != wxNOT_FOUND
&& filename
[0] != ':')
402 // Unix like or Windows
403 if (filename
[0] == wxT('/'))
407 if ((filename
[0] == wxT('[') && filename
[1] != wxT('.')))
410 #if defined(__WINDOWS__) || defined(__OS2__)
412 if (filename
[0] == wxT('\\') || (wxIsalpha (filename
[0]) && filename
[1] == wxT(':')))
420 * Strip off any extension (dot something) from end of file,
421 * IF one exists. Inserts zero into buffer.
425 void wxStripExtension(wxChar
*buffer
)
427 int len
= wxStrlen(buffer
);
431 if (buffer
[i
] == wxT('.'))
440 void wxStripExtension(wxString
& buffer
)
442 size_t len
= buffer
.Length();
446 if (buffer
.GetChar(i
) == wxT('.'))
448 buffer
= buffer
.Left(i
);
455 // Destructive removal of /./ and /../ stuff
456 wxChar
*wxRealPath (wxChar
*path
)
459 static const wxChar SEP
= wxT('\\');
460 wxUnix2DosFilename(path
);
462 static const wxChar SEP
= wxT('/');
464 if (path
[0] && path
[1]) {
465 /* MATTHEW: special case "/./x" */
467 if (path
[2] == SEP
&& path
[1] == wxT('.'))
475 if (p
[1] == wxT('.') && p
[2] == wxT('.') && (p
[3] == SEP
|| p
[3] == wxT('\0')))
478 for (q
= p
- 1; q
>= path
&& *q
!= SEP
; q
--)
483 if (q
[0] == SEP
&& (q
[1] != wxT('.') || q
[2] != wxT('.') || q
[3] != SEP
)
484 && (q
- 1 <= path
|| q
[-1] != SEP
))
487 if (path
[0] == wxT('\0'))
492 #if defined(__WXMSW__) || defined(__OS2__)
493 /* Check that path[2] is NULL! */
494 else if (path
[1] == wxT(':') && !path
[2])
503 else if (p
[1] == wxT('.') && (p
[2] == SEP
|| p
[2] == wxT('\0')))
512 wxChar
*wxCopyAbsolutePath(const wxString
& filename
)
514 if (filename
== wxT(""))
515 return (wxChar
*) NULL
;
517 if (! wxIsAbsolutePath(wxExpandPath(wxFileFunctionsBuffer
, filename
))) {
518 wxChar buf
[_MAXPATHLEN
];
520 wxGetWorkingDirectory(buf
, WXSIZEOF(buf
));
521 wxChar ch
= buf
[wxStrlen(buf
) - 1];
523 if (ch
!= wxT('\\') && ch
!= wxT('/'))
524 wxStrcat(buf
, wxT("\\"));
527 wxStrcat(buf
, wxT("/"));
529 wxStrcat(buf
, wxFileFunctionsBuffer
);
530 return MYcopystring( wxRealPath(buf
) );
532 return MYcopystring( wxFileFunctionsBuffer
);
538 ~user/ => user's home dir
539 If the environment variable a = "foo" and b = "bar" then:
556 /* input name in name, pathname output to buf. */
558 wxChar
*wxExpandPath(wxChar
*buf
, const wxChar
*name
)
560 register wxChar
*d
, *s
, *nm
;
561 wxChar lnm
[_MAXPATHLEN
];
564 // Some compilers don't like this line.
565 // const wxChar trimchars[] = wxT("\n \t");
568 trimchars
[0] = wxT('\n');
569 trimchars
[1] = wxT(' ');
570 trimchars
[2] = wxT('\t');
574 const wxChar SEP
= wxT('\\');
576 const wxChar SEP
= wxT('/');
579 if (name
== NULL
|| *name
== wxT('\0'))
581 nm
= MYcopystring(name
); // Make a scratch copy
584 /* Skip leading whitespace and cr */
585 while (wxStrchr((wxChar
*)trimchars
, *nm
) != NULL
)
587 /* And strip off trailing whitespace and cr */
588 s
= nm
+ (q
= wxStrlen(nm
)) - 1;
589 while (q
-- && wxStrchr((wxChar
*)trimchars
, *s
) != NULL
)
597 q
= nm
[0] == wxT('\\') && nm
[1] == wxT('~');
600 /* Expand inline environment variables */
618 while ((*d
++ = *s
) != 0) {
620 if (*s
== wxT('\\')) {
621 if ((*(d
- 1) = *++s
)) {
629 // No env variables on WinCE
632 if (*s
++ == wxT('$') && (*s
== wxT('{') || *s
== wxT(')')))
634 if (*s
++ == wxT('$'))
637 register wxChar
*start
= d
;
638 register int braces
= (*s
== wxT('{') || *s
== wxT('('));
639 register wxChar
*value
;
640 while ((*d
++ = *s
) != 0)
641 if (braces
? (*s
== wxT('}') || *s
== wxT(')')) : !(wxIsalnum(*s
) || *s
== wxT('_')) )
646 value
= wxGetenv(braces
? start
+ 1 : start
);
648 for ((d
= start
- 1); (*d
++ = *value
++) != 0;)
662 /* Expand ~ and ~user */
664 if (nm
[0] == wxT('~') && !q
)
667 if (nm
[1] == SEP
|| nm
[1] == 0)
669 // FIXME: wxGetUserHome could return temporary storage in Unicode mode
670 if ((s
= WXSTRINGCAST
wxGetUserHome(wxT(""))) != NULL
) {
675 { /* ~user/filename */
676 register wxChar
*nnm
;
677 register wxChar
*home
;
678 for (s
= nm
; *s
&& *s
!= SEP
; s
++)
682 int was_sep
; /* MATTHEW: Was there a separator, or NULL? */
683 was_sep
= (*s
== SEP
);
684 nnm
= *s
? s
+ 1 : s
;
686 // FIXME: wxGetUserHome could return temporary storage in Unicode mode
687 if ((home
= WXSTRINGCAST
wxGetUserHome(wxString(nm
+ 1))) == NULL
) {
688 if (was_sep
) /* replace only if it was there: */
699 if (s
&& *s
) { /* MATTHEW: s could be NULL if user '~' didn't exist */
701 while (wxT('\0') != (*d
++ = *s
++))
704 if (d
- 1 > buf
&& *(d
- 2) != SEP
)
708 while ((*d
++ = *s
++) != 0)
712 delete[] nm_tmp
; // clean up alloc
713 /* Now clean up the buffer */
714 return wxRealPath(buf
);
717 /* Contract Paths to be build upon an environment variable
720 example: "/usr/openwin/lib", OPENWINHOME --> ${OPENWINHOME}/lib
722 The call wxExpandPath can convert these back!
725 wxContractPath (const wxString
& filename
, const wxString
& envname
, const wxString
& user
)
727 static wxChar dest
[_MAXPATHLEN
];
729 if (filename
== wxT(""))
730 return (wxChar
*) NULL
;
732 wxStrcpy (dest
, WXSTRINGCAST filename
);
734 wxUnix2DosFilename(dest
);
737 // Handle environment
741 if (envname
!= WXSTRINGCAST NULL
&& (val
= wxGetenv (WXSTRINGCAST envname
)) != NULL
&&
742 (tcp
= wxStrstr (dest
, val
)) != NULL
)
744 wxStrcpy (wxFileFunctionsBuffer
, tcp
+ wxStrlen (val
));
747 wxStrcpy (tcp
, WXSTRINGCAST envname
);
748 wxStrcat (tcp
, wxT("}"));
749 wxStrcat (tcp
, wxFileFunctionsBuffer
);
753 // Handle User's home (ignore root homes!)
754 val
= wxGetUserHome (user
);
758 const size_t len
= wxStrlen(val
);
762 if (wxStrncmp(dest
, val
, len
) == 0)
764 wxStrcpy(wxFileFunctionsBuffer
, wxT("~"));
766 wxStrcat(wxFileFunctionsBuffer
, (const wxChar
*) user
);
767 wxStrcat(wxFileFunctionsBuffer
, dest
+ len
);
768 wxStrcpy (dest
, wxFileFunctionsBuffer
);
774 // Return just the filename, not the path (basename)
775 wxChar
*wxFileNameFromPath (wxChar
*path
)
778 wxString n
= wxFileNameFromPath(p
);
780 return path
+ p
.length() - n
.length();
783 wxString
wxFileNameFromPath (const wxString
& path
)
786 wxFileName::SplitPath(path
, NULL
, &name
, &ext
);
788 wxString fullname
= name
;
791 fullname
<< wxFILE_SEP_EXT
<< ext
;
797 // Return just the directory, or NULL if no directory
799 wxPathOnly (wxChar
*path
)
803 static wxChar buf
[_MAXPATHLEN
];
806 wxStrcpy (buf
, path
);
808 int l
= wxStrlen(path
);
811 // Search backward for a backward or forward slash
814 #if defined(__WXMAC__) && !defined(__DARWIN__)
815 // Classic or Carbon CodeWarrior like
816 // Carbon with Apple DevTools is Unix like
817 if (path
[i
] == wxT(':') )
823 // Unix like or Windows
824 if (path
[i
] == wxT('/') || path
[i
] == wxT('\\'))
831 if (path
[i
] == wxT(']'))
840 #if defined(__WXMSW__) || defined(__OS2__)
841 // Try Drive specifier
842 if (wxIsalpha (buf
[0]) && buf
[1] == wxT(':'))
844 // A:junk --> A:. (since A:.\junk Not A:\junk)
851 return (wxChar
*) NULL
;
854 // Return just the directory, or NULL if no directory
855 wxString
wxPathOnly (const wxString
& path
)
859 wxChar buf
[_MAXPATHLEN
];
862 wxStrcpy (buf
, WXSTRINGCAST path
);
864 int l
= path
.Length();
867 // Search backward for a backward or forward slash
870 #if defined(__WXMAC__) && !defined(__DARWIN__)
871 // Classic or Carbon CodeWarrior like
872 // Carbon with Apple DevTools is Unix like
873 if (path
[i
] == wxT(':') )
876 return wxString(buf
);
879 // Unix like or Windows
880 if (path
[i
] == wxT('/') || path
[i
] == wxT('\\'))
883 return wxString(buf
);
887 if (path
[i
] == wxT(']'))
890 return wxString(buf
);
896 #if defined(__WXMSW__) || defined(__OS2__)
897 // Try Drive specifier
898 if (wxIsalpha (buf
[0]) && buf
[1] == wxT(':'))
900 // A:junk --> A:. (since A:.\junk Not A:\junk)
903 return wxString(buf
);
907 return wxString(wxT(""));
910 // Utility for converting delimiters in DOS filenames to UNIX style
911 // and back again - or we get nasty problems with delimiters.
912 // Also, convert to lower case, since case is significant in UNIX.
914 #if defined(__WXMAC__)
915 wxString
wxMacFSSpec2MacFilename( const FSSpec
*spec
)
921 OSStatus theStatus
= noErr
;
922 Boolean isDirectory
= FALSE
;
923 Str255 theParentPath
= "\p";
924 FSSpec theParentSpec
;
927 char theFileName
[FILENAME_MAX
];
928 char thePath
[FILENAME_MAX
];
930 // we loose the long filename by merely copying the spec->name
931 // so try the built-ins, which only work if the file exists, but still...
933 theErr
= FSpMakeFSRef(spec
, &theRef
);
934 if ( theErr
== noErr
)
937 fullURLRef
= ::CFURLCreateFromFSRef(NULL
, &theRef
);
939 CFURLPathStyle pathstyle
= kCFURLPOSIXPathStyle
;
941 CFURLPathStyle pathstyle
= kCFURLHFSPathStyle
;
943 CFStringRef cfString
= CFURLCopyFileSystemPath(fullURLRef
, pathstyle
);
944 ::CFRelease( fullURLRef
) ;
945 return wxMacCFStringHolder(cfString
).AsString(wxFont::GetDefaultEncoding());
950 // GD: Separate file name from path and make a FSRef to the parent
951 // directory. This is necessary since FSRefs cannot reference files
952 // that have not yet been created.
953 // Based on example code from Apple Technical Note TN2022
954 // http://developer.apple.com/technotes/tn/tn2022.html
956 // check whether we are converting a directory
957 isDirectory
= ((spec
->name
)[spec
->name
[0]] == ':');
958 // count length of file name
959 for (i
= spec
->name
[0] - (isDirectory
? 1 : 0); ((spec
->name
[i
] != ':') && (i
> 0)); i
--);
961 // prepend path separator since it will later be appended to the path
962 theFileName
[0] = wxFILE_SEP_PATH
;
963 for (j
= i
+ 1; j
<= spec
->name
[0] - (isDirectory
? 1 : 0); j
++) {
964 theFileName
[j
- i
] = spec
->name
[j
];
966 theFileName
[j
- i
] = '\0';
968 for (j
= 1; j
<= i
; j
++) {
969 theParentPath
[++theParentPath
[0]] = spec
->name
[j
];
971 theErr
= FSMakeFSSpec(spec
->vRefNum
, spec
->parID
, theParentPath
, &theParentSpec
);
972 if (theErr
== noErr
) {
973 // convert the FSSpec to an FSRef
974 theErr
= FSpMakeFSRef(&theParentSpec
, &theParentRef
);
976 if (theErr
== noErr
) {
977 // get the POSIX path associated with the FSRef
978 theStatus
= FSRefMakePath(&theParentRef
,
979 (UInt8
*)thePath
, sizeof(thePath
));
981 if (theStatus
== noErr
) {
982 // append file name to path
983 // includes previously prepended path separator
984 strcat(thePath
, theFileName
);
987 // create path string for return value
988 wxString
result( thePath
, wxConvLocal
) ;
993 // get length of path and allocate handle
994 FSpGetFullPath( spec
, &length
, &myPath
) ;
995 ::SetHandleSize( myPath
, length
+ 1 ) ;
997 (*myPath
)[length
] = 0 ;
998 if ((length
> 0) && ((*myPath
)[length
-1] == ':'))
999 (*myPath
)[length
-1] = 0 ;
1001 // create path string for return value
1002 wxString
result( *myPath
, wxConvLocal
) ;
1004 // free allocated handle
1005 ::HUnlock( myPath
) ;
1006 ::DisposeHandle( myPath
) ;
1012 // Mac file names are POSIX (Unix style) under Darwin
1013 // therefore the conversion functions below are not needed
1015 static wxChar sMacFileNameConversion
[ 1000 ] ;
1016 static char scMacFileNameConversion
[ 1000 ] ;
1019 void wxMacFilename2FSSpec( const char *path
, FSSpec
*spec
)
1021 OSStatus err
= noErr
;
1025 // get the FSRef associated with the POSIX path
1026 err
= FSPathMakeRef((const UInt8
*) path
, &theRef
, NULL
);
1027 // convert the FSRef to an FSSpec
1028 err
= FSGetCatalogInfo(&theRef
, kFSCatInfoNone
, NULL
, NULL
, spec
, NULL
);
1030 if ( strchr( path
, ':' ) == NULL
)
1032 // try whether it is a volume / or a mounted volume
1033 strncpy( scMacFileNameConversion
, path
, 1000 ) ;
1034 scMacFileNameConversion
[998] = 0 ;
1035 strcat( scMacFileNameConversion
, ":" ) ;
1036 err
= FSpLocationFromFullPath( strlen(scMacFileNameConversion
) , scMacFileNameConversion
, spec
) ;
1040 err
= FSpLocationFromFullPath( strlen(path
) , path
, spec
) ;
1046 WXDLLEXPORT
void wxMacFilename2FSSpec( const wxChar
*path
, FSSpec
*spec
)
1048 return wxMacFilename2FSSpec( wxConvFile
.cWC2MB(path
) , spec
) ;
1054 wxString
wxMac2UnixFilename (const wxChar
*str
)
1056 wxChar
*s
= sMacFileNameConversion
;
1057 wxStrcpy( s
, str
) ;
1060 memmove( s
+1 , s
,wxStrlen( s
) + 1 * sizeof(wxChar
)) ;
1071 *s
= wxTolower(*s
); // Case INDEPENDENT
1075 return wxString(sMacFileNameConversion
) ;
1078 wxString
wxUnix2MacFilename (const wxChar
*str
)
1080 wxChar
*s
= sMacFileNameConversion
;
1081 wxStrcpy( s
, str
) ;
1086 // relative path , since it goes on with slash which is translated to a :
1087 memmove( s
, s
+1 ,wxStrlen( s
) * sizeof(wxChar
)) ;
1089 else if ( *s
== '/' )
1091 // absolute path -> on mac just start with the drive name
1092 memmove( s
, s
+1 ,wxStrlen( s
) * sizeof(wxChar
) ) ;
1096 wxASSERT_MSG( 1 , wxT("unkown path beginning") ) ;
1100 if (*s
== '/' || *s
== '\\')
1102 // convert any back-directory situations
1103 if ( *(s
+1) == '.' && *(s
+2) == '.' && ( (*(s
+3) == '/' || *(s
+3) == '\\') ) )
1106 memmove( s
+1 , s
+3 ,(wxStrlen( s
+3 ) + 1)*sizeof(wxChar
) ) ;
1114 return wxString(sMacFileNameConversion
) ;
1117 wxString
wxMacFSSpec2UnixFilename( const FSSpec
*spec
)
1119 return wxMac2UnixFilename( wxMacFSSpec2MacFilename( spec
) ) ;
1122 void wxUnixFilename2FSSpec( const wxChar
*path
, FSSpec
*spec
)
1124 wxString var
= wxUnix2MacFilename( path
) ;
1125 wxMacFilename2FSSpec( var
, spec
) ;
1127 #endif // ! __DARWIN__
1132 wxDos2UnixFilename (wxChar
*s
)
1141 *s
= wxTolower (*s
); // Case INDEPENDENT
1148 #if defined(__WXMSW__) || defined(__OS2__)
1149 wxUnix2DosFilename (wxChar
*s
)
1151 wxUnix2DosFilename (wxChar
*WXUNUSED(s
) )
1154 // Yes, I really mean this to happen under DOS only! JACS
1155 #if defined(__WXMSW__) || defined(__OS2__)
1166 // Concatenate two files to form third
1168 wxConcatFiles (const wxString
& file1
, const wxString
& file2
, const wxString
& file3
)
1171 if ( !wxGetTempFileName( wxT("cat"), outfile
) )
1174 FILE *fp1
wxDUMMY_INITIALIZE(NULL
);
1177 // Open the inputs and outputs
1178 if ((fp1
= wxFopen ( file1
, wxT("rb"))) == NULL
||
1179 (fp2
= wxFopen ( file2
, wxT("rb"))) == NULL
||
1180 (fp3
= wxFopen ( outfile
, wxT("wb"))) == NULL
)
1192 while ((ch
= getc (fp1
)) != EOF
)
1193 (void) putc (ch
, fp3
);
1196 while ((ch
= getc (fp2
)) != EOF
)
1197 (void) putc (ch
, fp3
);
1201 bool result
= wxRenameFile(outfile
, file3
);
1207 wxCopyFile (const wxString
& file1
, const wxString
& file2
, bool overwrite
)
1209 #if defined(__WIN32__) && !defined(__WXMICROWIN__)
1210 // CopyFile() copies file attributes and modification time too, so use it
1211 // instead of our code if available
1213 // NB: 3rd parameter is bFailIfExists i.e. the inverse of overwrite
1214 if ( !::CopyFile(file1
, file2
, !overwrite
) )
1216 wxLogSysError(_("Failed to copy the file '%s' to '%s'"),
1217 file1
.c_str(), file2
.c_str());
1221 #elif defined(__OS2__)
1222 if ( ::DosCopy(file2
, file2
, overwrite
? DCPY_EXISTING
: 0) != 0 )
1227 // get permissions of file1
1228 if ( wxStat( file1
.c_str(), &fbuf
) != 0 )
1230 // the file probably doesn't exist or we haven't the rights to read
1232 wxLogSysError(_("Impossible to get permissions for file '%s'"),
1237 // open file1 for reading
1238 wxFile
fileIn(file1
, wxFile::read
);
1239 if ( !fileIn
.IsOpened() )
1242 // remove file2, if it exists. This is needed for creating
1243 // file2 with the correct permissions in the next step
1244 if ( wxFileExists(file2
) && (!overwrite
|| !wxRemoveFile(file2
)))
1246 wxLogSysError(_("Impossible to overwrite the file '%s'"),
1252 // reset the umask as we want to create the file with exactly the same
1253 // permissions as the original one
1254 mode_t oldUmask
= umask( 0 );
1257 // create file2 with the same permissions than file1 and open it for
1261 if ( !fileOut
.Create(file2
, overwrite
, fbuf
.st_mode
& 0777) )
1265 /// restore the old umask
1269 // copy contents of file1 to file2
1274 count
= fileIn
.Read(buf
, WXSIZEOF(buf
));
1275 if ( fileIn
.Error() )
1282 if ( fileOut
.Write(buf
, count
) < count
)
1286 // we can expect fileIn to be closed successfully, but we should ensure
1287 // that fileOut was closed as some write errors (disk full) might not be
1288 // detected before doing this
1289 if ( !fileIn
.Close() || !fileOut
.Close() )
1292 #if !defined(__VISAGECPP__) && !defined(__WXMAC__) || defined(__UNIX__)
1293 // no chmod in VA. Should be some permission API for HPFS386 partitions
1295 if ( chmod(OS_FILENAME(file2
), fbuf
.st_mode
) != 0 )
1297 wxLogSysError(_("Impossible to set permissions for the file '%s'"),
1301 #endif // OS/2 || Mac
1302 #endif // __WXMSW__ && __WIN32__
1308 wxRenameFile (const wxString
& file1
, const wxString
& file2
)
1311 // Normal system call
1312 if ( wxRename (file1
, file2
) == 0 )
1317 if (wxCopyFile(file1
, file2
)) {
1318 wxRemoveFile(file1
);
1325 bool wxRemoveFile(const wxString
& file
)
1327 #if defined(__VISUALC__) \
1328 || defined(__BORLANDC__) \
1329 || defined(__WATCOMC__) \
1330 || defined(__DMC__) \
1331 || defined(__GNUWIN32__) \
1332 || (defined(__MWERKS__) && defined(__MSL__))
1333 int res
= wxRemove(file
);
1334 #elif defined(__WXMAC__)
1335 int res
= unlink(wxFNCONV(file
));
1337 int res
= unlink(OS_FILENAME(file
));
1343 bool wxMkdir(const wxString
& dir
, int perm
)
1345 #if defined(__WXMAC__) && !defined(__UNIX__)
1346 return (mkdir( wxFNCONV(dir
) , 0 ) == 0);
1348 const wxChar
*dirname
= dir
.c_str();
1350 // assume mkdir() has 2 args on non Windows-OS/2 platforms and on Windows too
1351 // for the GNU compiler
1352 #if (!(defined(__WXMSW__) || defined(__OS2__) || defined(__DOS__))) || (defined(__GNUWIN32__) && !defined(__MINGW32__)) || defined(__WINE__) || defined(__WXMICROWIN__)
1354 if ( mkdir(wxFNCONV(dirname
), perm
) != 0 )
1356 if ( mkdir(wxFNCONV(dirname
)) != 0 )
1358 #elif defined(__OS2__)
1359 if (::DosCreateDir((PSZ
)dirname
, NULL
) != 0) // enhance for EAB's??
1360 #elif defined(__DOS__)
1361 #if defined(__WATCOMC__)
1363 if ( wxMkDir(wxFNSTRINGCAST
wxFNCONV(dirname
)) != 0 )
1364 #elif defined(__DJGPP__)
1365 if ( mkdir(wxFNCONV(dirname
), perm
) != 0 )
1367 #error "Unsupported DOS compiler!"
1369 #else // !MSW, !DOS and !OS/2 VAC++
1372 if ( !CreateDirectory(dirname
, NULL
) )
1374 if ( wxMkDir(wxFNSTRINGCAST
wxFNCONV(dirname
)) != 0 )
1378 wxLogSysError(_("Directory '%s' couldn't be created"), dirname
);
1387 bool wxRmdir(const wxString
& dir
, int WXUNUSED(flags
))
1390 return FALSE
; //to be changed since rmdir exists in VMS7.x
1391 #elif defined(__OS2__)
1392 return (::DosDeleteDir((PSZ
)dir
.c_str()) == 0);
1396 return (CreateDirectory(dir
, NULL
) != 0);
1398 return (wxRmDir(OS_FILENAME(dir
)) == 0);
1404 // does the path exists? (may have or not '/' or '\\' at the end)
1405 bool wxPathExists(const wxChar
*pszPathName
)
1407 wxString
strPath(pszPathName
);
1409 #if defined(__WINDOWS__) || defined(__OS2__)
1410 // Windows fails to find directory named "c:\dir\" even if "c:\dir" exists,
1411 // so remove all trailing backslashes from the path - but don't do this for
1412 // the pathes "d:\" (which are different from "d:") nor for just "\"
1413 while ( wxEndsWithPathSeparator(strPath
) )
1415 size_t len
= strPath
.length();
1416 if ( len
== 1 || (len
== 3 && strPath
[len
- 2] == _T(':')) )
1419 strPath
.Truncate(len
- 1);
1421 #endif // __WINDOWS__
1424 // OS/2 can't handle "d:", it wants either "d:\" or "d:."
1425 if (strPath
.length() == 2 && strPath
[1u] == _T(':'))
1429 #if defined(__WIN32__) && !defined(__WXMICROWIN__)
1430 // stat() can't cope with network paths
1431 DWORD ret
= ::GetFileAttributes(strPath
);
1433 return (ret
!= (DWORD
)-1) && (ret
& FILE_ATTRIBUTE_DIRECTORY
);
1437 #ifndef __VISAGECPP__
1438 return wxStat(strPath
.c_str(), &st
) == 0 && ((st
.st_mode
& S_IFMT
) == S_IFDIR
);
1440 // S_IFMT not supported in VA compilers.. st_mode is a 2byte value only
1441 return wxStat(pszPathName
, &st
) == 0 && (st
.st_mode
== S_IFDIR
);
1444 #endif // __WIN32__/!__WIN32__
1447 // Get a temporary filename, opening and closing the file.
1448 wxChar
*wxGetTempFileName(const wxString
& prefix
, wxChar
*buf
)
1450 wxString filename
= wxFileName::CreateTempFileName(prefix
);
1451 if ( filename
.empty() )
1455 wxStrcpy(buf
, filename
);
1457 buf
= MYcopystring(filename
);
1462 bool wxGetTempFileName(const wxString
& prefix
, wxString
& buf
)
1464 buf
= wxFileName::CreateTempFileName(prefix
);
1466 return !buf
.empty();
1469 // Get first file name matching given wild card.
1471 static wxDir
*gs_dir
= NULL
;
1472 static wxString gs_dirPath
;
1474 wxString
wxFindFirstFile(const wxChar
*spec
, int flags
)
1476 wxSplitPath(spec
, &gs_dirPath
, NULL
, NULL
);
1477 if ( gs_dirPath
.IsEmpty() )
1478 gs_dirPath
= wxT(".");
1479 if ( !wxEndsWithPathSeparator(gs_dirPath
) )
1480 gs_dirPath
<< wxFILE_SEP_PATH
;
1484 gs_dir
= new wxDir(gs_dirPath
);
1486 if ( !gs_dir
->IsOpened() )
1488 wxLogSysError(_("Can not enumerate files '%s'"), spec
);
1489 return wxEmptyString
;
1495 case wxDIR
: dirFlags
= wxDIR_DIRS
; break;
1496 case wxFILE
: dirFlags
= wxDIR_FILES
; break;
1497 default: dirFlags
= wxDIR_DIRS
| wxDIR_FILES
; break;
1501 gs_dir
->GetFirst(&result
, wxFileNameFromPath(wxString(spec
)), dirFlags
);
1502 if ( result
.IsEmpty() )
1508 return gs_dirPath
+ result
;
1511 wxString
wxFindNextFile()
1513 wxASSERT_MSG( gs_dir
, wxT("You must call wxFindFirstFile before!") );
1516 gs_dir
->GetNext(&result
);
1518 if ( result
.IsEmpty() )
1524 return gs_dirPath
+ result
;
1528 // Get current working directory.
1529 // If buf is NULL, allocates space using new, else
1531 wxChar
*wxGetWorkingDirectory(wxChar
*buf
, int sz
)
1538 buf
= new wxChar
[sz
+ 1];
1541 bool ok
wxDUMMY_INITIALIZE(FALSE
);
1543 // for the compilers which have Unicode version of _getcwd(), call it
1544 // directly, for the others call the ANSI version and do the translation
1547 #else // wxUSE_UNICODE
1548 bool needsANSI
= TRUE
;
1550 #if !defined(HAVE_WGETCWD) || wxUSE_UNICODE_MSLU
1551 // This is not legal code as the compiler
1552 // is allowed destroy the wxCharBuffer.
1553 // wxCharBuffer c_buffer(sz);
1554 // char *cbuf = (char*)(const char*)c_buffer;
1555 char cbuf
[_MAXPATHLEN
];
1559 #if wxUSE_UNICODE_MSLU
1560 if ( wxGetOsVersion() != wxWIN95
)
1562 char *cbuf
= NULL
; // never really used because needsANSI will always be FALSE
1565 ok
= _wgetcwd(buf
, sz
) != NULL
;
1571 #endif // wxUSE_UNICODE
1573 #if defined(_MSC_VER) || defined(__MINGW32__)
1574 ok
= _getcwd(cbuf
, sz
) != NULL
;
1575 #elif defined(__WXMAC__) && !defined(__DARWIN__)
1580 pb
.ioNamePtr
= (StringPtr
) &fileName
;
1582 pb
.ioRefNum
= LMGetCurApRefNum();
1584 error
= PBGetFCBInfoSync(&pb
);
1585 if ( error
== noErr
)
1587 cwdSpec
.vRefNum
= pb
.ioFCBVRefNum
;
1588 cwdSpec
.parID
= pb
.ioFCBParID
;
1589 cwdSpec
.name
[0] = 0 ;
1590 wxString res
= wxMacFSSpec2MacFilename( &cwdSpec
) ;
1591 wxStrcpy( buf
, res
) ;
1598 #elif defined(__OS2__)
1600 ULONG ulDriveNum
= 0;
1601 ULONG ulDriveMap
= 0;
1602 rc
= ::DosQueryCurrentDisk(&ulDriveNum
, &ulDriveMap
);
1607 rc
= ::DosQueryCurrentDir( 0 // current drive
1611 cbuf
[0] = 'A' + (ulDriveNum
- 1);
1616 #else // !Win32/VC++ !Mac !OS2
1617 ok
= getcwd(cbuf
, sz
) != NULL
;
1620 #if wxUSE_UNICODE && !(defined(__WXMAC__) && !defined(__DARWIN__))
1621 // finally convert the result to Unicode if needed
1622 wxConvFile
.MB2WC(buf
, cbuf
, sz
);
1623 #endif // wxUSE_UNICODE
1628 wxLogSysError(_("Failed to get the working directory"));
1630 // VZ: the old code used to return "." on error which didn't make any
1631 // sense at all to me - empty string is a better error indicator
1632 // (NULL might be even better but I'm afraid this could lead to
1633 // problems with the old code assuming the return is never NULL)
1636 else // ok, but we might need to massage the path into the right format
1639 // VS: DJGPP is a strange mix of DOS and UNIX API and returns paths
1640 // with / deliminers. We don't like that.
1641 for (wxChar
*ch
= buf
; *ch
; ch
++)
1643 if (*ch
== wxT('/'))
1648 // MBN: we hope that in the case the user is compiling a GTK+/Motif app,
1649 // he needs Unix as opposed to Win32 pathnames
1650 #if defined( __CYGWIN__ ) && defined( __WINDOWS__ )
1651 // another example of DOS/Unix mix (Cygwin)
1652 wxString pathUnix
= buf
;
1653 cygwin_conv_to_full_win32_path(pathUnix
, buf
);
1654 #endif // __CYGWIN__
1669 wxChar
*buffer
= new wxChar
[_MAXPATHLEN
];
1670 wxGetWorkingDirectory(buffer
, _MAXPATHLEN
);
1671 wxString
str( buffer
);
1677 bool wxSetWorkingDirectory(const wxString
& d
)
1679 #if defined(__OS2__)
1680 return (::DosSetCurrentDir((PSZ
)d
.c_str()) == 0);
1681 #elif defined(__UNIX__) || defined(__WXMAC__) || defined(__DOS__)
1682 return (chdir(wxFNSTRINGCAST d
.fn_str()) == 0);
1683 #elif defined(__WINDOWS__)
1687 // No equivalent in WinCE
1690 return (bool)(SetCurrentDirectory(d
) != 0);
1693 // Must change drive, too.
1694 bool isDriveSpec
= ((strlen(d
) > 1) && (d
[1] == ':'));
1697 wxChar firstChar
= d
[0];
1701 firstChar
= firstChar
- 32;
1703 // To a drive number
1704 unsigned int driveNo
= firstChar
- 64;
1707 unsigned int noDrives
;
1708 _dos_setdrive(driveNo
, &noDrives
);
1711 bool success
= (chdir(WXSTRINGCAST d
) == 0);
1719 // Get the OS directory if appropriate (such as the Windows directory).
1720 // On non-Windows platform, probably just return the empty string.
1721 wxString
wxGetOSDirectory()
1724 return wxString(wxT("\\Windows"));
1725 #elif defined(__WINDOWS__) && !defined(__WXMICROWIN__)
1727 GetWindowsDirectory(buf
, 256);
1728 return wxString(buf
);
1729 #elif defined(__WXMAC__)
1730 return wxMacFindFolder(kOnSystemDisk
, 'macs', false);
1732 return wxEmptyString
;
1736 bool wxEndsWithPathSeparator(const wxChar
*pszFileName
)
1738 size_t len
= wxStrlen(pszFileName
);
1740 return len
&& wxIsPathSeparator(pszFileName
[len
- 1]);
1743 // find a file in a list of directories, returns FALSE if not found
1744 bool wxFindFileInPath(wxString
*pStr
, const wxChar
*pszPath
, const wxChar
*pszFile
)
1746 // we assume that it's not empty
1747 wxCHECK_MSG( !wxIsEmpty(pszFile
), FALSE
,
1748 _T("empty file name in wxFindFileInPath"));
1750 // skip path separator in the beginning of the file name if present
1751 if ( wxIsPathSeparator(*pszFile
) )
1754 // copy the path (strtok will modify it)
1755 wxChar
*szPath
= new wxChar
[wxStrlen(pszPath
) + 1];
1756 wxStrcpy(szPath
, pszPath
);
1759 wxChar
*pc
, *save_ptr
;
1760 for ( pc
= wxStrtok(szPath
, wxPATH_SEP
, &save_ptr
);
1762 pc
= wxStrtok((wxChar
*) NULL
, wxPATH_SEP
, &save_ptr
) )
1764 // search for the file in this directory
1766 if ( !wxEndsWithPathSeparator(pc
) )
1767 strFile
+= wxFILE_SEP_PATH
;
1770 if ( wxFileExists(strFile
) ) {
1776 // suppress warning about unused variable save_ptr when wxStrtok() is a
1777 // macro which throws away its third argument
1782 return pc
!= NULL
; // if TRUE => we breaked from the loop
1785 void WXDLLEXPORT
wxSplitPath(const wxChar
*pszFileName
,
1790 // it can be empty, but it shouldn't be NULL
1791 wxCHECK_RET( pszFileName
, wxT("NULL file name in wxSplitPath") );
1793 wxFileName::SplitPath(pszFileName
, pstrPath
, pstrName
, pstrExt
);
1796 time_t WXDLLEXPORT
wxFileModificationTime(const wxString
& filename
)
1799 FILETIME creationTime
, lastAccessTime
, lastWriteTime
;
1800 HANDLE fileHandle
= ::CreateFile(filename
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
1801 0, FILE_ATTRIBUTE_NORMAL
, 0);
1802 if (fileHandle
== INVALID_HANDLE_VALUE
)
1806 if (GetFileTime(fileHandle
, & creationTime
, & lastAccessTime
, & lastWriteTime
))
1808 CloseHandle(fileHandle
);
1810 wxDateTime dateTime
;
1812 if ( !::FileTimeToLocalFileTime(&lastWriteTime
, &ftLocal
) )
1814 wxLogLastError(_T("FileTimeToLocalFileTime"));
1818 if ( !::FileTimeToSystemTime(&ftLocal
, &st
) )
1820 wxLogLastError(_T("FileTimeToSystemTime"));
1823 dateTime
.Set(st
.wDay
, wxDateTime::Month(st
.wMonth
- 1), st
.wYear
,
1824 st
.wHour
, st
.wMinute
, st
.wSecond
, st
.wMilliseconds
);
1825 return dateTime
.GetTicks();
1832 wxStat( filename
, &buf
);
1834 return buf
.st_mtime
;
1839 //------------------------------------------------------------------------
1840 // wild character routines
1841 //------------------------------------------------------------------------
1843 bool wxIsWild( const wxString
& pattern
)
1845 wxString tmp
= pattern
;
1846 wxChar
*pat
= WXSTRINGCAST(tmp
);
1851 case wxT('?'): case wxT('*'): case wxT('['): case wxT('{'):
1862 * Written By Douglas A. Lewis <dalewis@cs.Buffalo.EDU>
1864 * The match procedure is public domain code (from ircII's reg.c)
1867 bool wxMatchWild( const wxString
& pat
, const wxString
& text
, bool dot_special
)
1871 /* Match if both are empty. */
1875 const wxChar
*m
= pat
.c_str(),
1886 if (dot_special
&& (*n
== wxT('.')))
1888 /* Never match so that hidden Unix files
1889 * are never found. */
1903 else if (*m
== wxT('?'))
1911 if (*m
== wxT('\\'))
1914 /* Quoting "nothing" is a bad thing */
1921 * If we are out of both strings or we just
1922 * saw a wildcard, then we can say we have a
1933 * We could check for *n == NULL at this point, but
1934 * since it's more common to have a character there,
1935 * check to see if they match first (m and n) and
1936 * then if they don't match, THEN we can check for
1954 * If there are no more characters in the
1955 * string, but we still need to find another
1956 * character (*m != NULL), then it will be
1957 * impossible to match it
1964 if (*np
== wxT(' '))
1990 #pragma warning(default:4706) // assignment within conditional expression