#endif
#endif
+#if defined(__WXMAC__)
+ #include "wx/mac/private.h" // includes mac headers
+#endif
+
#include <time.h>
#ifndef __MWERKS__
#include <windows.h>
#endif
+// TODO: Borland probably has _wgetcwd as well?
+#ifdef _MSC_VER
+ #define HAVE_WGETCWD
+#endif
+
// ----------------------------------------------------------------------------
// constants
// ----------------------------------------------------------------------------
-#define _MAXPATHLEN 500
+#ifndef _MAXPATHLEN
+ #define _MAXPATHLEN 1024
+#endif
extern wxChar *wxBuffer;
return f;
wxString buf;
- wxGetWorkingDirectory(buf.GetWriteBuf(_MAXPATHLEN), _MAXPATHLEN - 1);
- buf.UngetWriteBuf();
+ wxGetWorkingDirectory(wxStringBuffer(buf, _MAXPATHLEN), _MAXPATHLEN);
+
if ( !wxEndsWithPathSeparator(buf) )
{
buf += wxFILE_SEP_PATH;
/* Expand ~ and ~user */
nm = lnm;
- s = wxT("");
if (nm[0] == wxT('~') && !q)
{
/* prefix ~ */
if ((home = WXSTRINGCAST wxGetUserHome(wxString(nm + 1))) == NULL) {
if (was_sep) /* replace only if it was there: */
*s = SEP;
- s = wxT("");
+ s = NULL;
} else {
nm = nnm;
s = home;
// copies into buf.
wxChar *wxGetWorkingDirectory(wxChar *buf, int sz)
{
- if (!buf)
- buf = new wxChar[sz+1];
-#if wxUSE_UNICODE
- char *cbuf = new char[sz+1];
-#ifdef _MSC_VER
- if (_getcwd(cbuf, sz) == NULL) {
-#elif defined(__WXMAC__) && !defined(__DARWIN__)
- enum
- {
- SFSaveDisk = 0x214, CurDirStore = 0x398
- };
- FSSpec cwdSpec ;
-
- FSMakeFSSpec( - *(short *) SFSaveDisk , *(long *) CurDirStore , NULL , &cwdSpec ) ;
- wxString res = wxMacFSSpec2UnixFilename( &cwdSpec ) ;
- strcpy( buf , res ) ;
- if (0) {
-#else
- if (getcwd(cbuf, sz) == NULL) {
-#endif
- delete [] cbuf;
-#else // wxUnicode
-#ifdef _MSC_VER
- if (_getcwd(buf, sz) == NULL) {
-#elif defined(__WXMAC__) && !defined(__DARWIN__)
- FSSpec cwdSpec ;
- FCBPBRec pb;
- OSErr error;
- Str255 fileName ;
- pb.ioNamePtr = (StringPtr) &fileName;
- pb.ioVRefNum = 0;
- pb.ioRefNum = LMGetCurApRefNum();
- pb.ioFCBIndx = 0;
- error = PBGetFCBInfoSync(&pb);
- if ( error == noErr )
+ if ( !buf )
{
- cwdSpec.vRefNum = pb.ioFCBVRefNum;
- cwdSpec.parID = pb.ioFCBParID;
- cwdSpec.name[0] = 0 ;
- wxString res = wxMacFSSpec2MacFilename( &cwdSpec ) ;
-
- strcpy( buf , res ) ;
- buf[res.length()]=0 ;
+ buf = new wxChar[sz + 1];
}
- else
- buf[0] = 0 ;
- /*
- this version will not always give back the application directory on mac
- enum
- {
- SFSaveDisk = 0x214, CurDirStore = 0x398
- };
- FSSpec cwdSpec ;
- FSMakeFSSpec( - *(short *) SFSaveDisk , *(long *) CurDirStore , NULL , &cwdSpec ) ;
- wxString res = wxMacFSSpec2UnixFilename( &cwdSpec ) ;
- strcpy( buf , res ) ;
- */
- if (0) {
-#elif defined(__VISAGECPP__) || (defined (__OS2__) && defined (__WATCOMC__))
- APIRET rc;
- rc = ::DosQueryCurrentDir( 0 // current drive
- ,buf
- ,(PULONG)&sz
- );
- if (rc != 0) {
-#else
- if (getcwd(buf, sz) == NULL) {
-#endif
-#endif
- buf[0] = wxT('.');
- buf[1] = wxT('\0');
- }
+ bool ok;
+
+ // for the compilers which have Unicode version of _getcwd(), call it
+ // directly, for the others call the ANSI version and do the translation
#if wxUSE_UNICODE
- else {
- wxConvFile.MB2WC(buf, cbuf, sz);
- delete [] cbuf;
- }
-#endif
+ #ifdef HAVE_WGETCWD
+ ok = _wgetcwd(buf, sz) != NULL;
+ #else // !HAVE_WGETCWD
+ wxCharBuffer cbuf(sz);
+ #endif
+#endif //
+
+#if !wxUSE_UNICODE || !defined(HAVE_WGETCWD)
+ #ifdef _MSC_VER
+ ok = _getcwd(buf, sz) != NULL;
+ #elif defined(__WXMAC__) && !defined(__DARWIN__)
+ FSSpec cwdSpec ;
+ FCBPBRec pb;
+ OSErr error;
+ Str255 fileName ;
+ pb.ioNamePtr = (StringPtr) &fileName;
+ pb.ioVRefNum = 0;
+ pb.ioRefNum = LMGetCurApRefNum();
+ pb.ioFCBIndx = 0;
+ error = PBGetFCBInfoSync(&pb);
+ if ( error == noErr )
+ {
+ cwdSpec.vRefNum = pb.ioFCBVRefNum;
+ cwdSpec.parID = pb.ioFCBParID;
+ cwdSpec.name[0] = 0 ;
+ wxString res = wxMacFSSpec2MacFilename( &cwdSpec ) ;
+
+ strcpy( buf , res ) ;
+ buf[res.length()]=0 ;
+ ok = TRUE;
+ }
+ else
+ {
+ ok = FALSE;
+ }
+ #elif defined(__VISAGECPP__) || (defined (__OS2__) && defined (__WATCOMC__))
+ APIRET rc;
+ rc = ::DosQueryCurrentDir( 0 // current drive
+ ,buf
+ ,(PULONG)&sz
+ );
+ ok = rc != 0;
+ #else // !Win32/VC++ !Mac !OS2
+ ok = getcwd(buf, sz) != NULL;
+ #endif // platform
+#endif // !wxUSE_UNICODE || !HAVE_WGETCWD
+
+ if ( !ok )
+ {
+ wxLogSysError(_("Failed to get the working directory"));
+
+ // VZ: the old code used to return "." on error which didn't make any
+ // sense at all to me - empty string is a better error indicator
+ // (NULL might be even better but I'm afraid this could lead to
+ // problems with the old code assuming the return is never NULL)
+ buf[0] = _T('\0');
+ }
+ else // ok, but we might need to massage the path into the right format
+ {
#ifdef __DJGPP__
- // VS: DJGPP is a strange mix of DOS and UNIX API and returns paths with
- // / deliminers. We don't like that.
- for (wxChar *ch = buf; *ch; ch++)
- if (*ch == wxT('/')) *ch = wxT('\\');
-#endif
+ // VS: DJGPP is a strange mix of DOS and UNIX API and returns paths
+ // with / deliminers. We don't like that.
+ for (wxChar *ch = buf; *ch; ch++)
+ {
+ if (*ch == wxT('/'))
+ *ch = wxT('\\');
+ }
+#endif // __DJGPP__
+
+#ifdef __CYGWIN10__
+ // another example of DOS/Unix mix
+ wxString pathUnix = buf;
+ cygwin_conv_to_full_win32_path(pathUnix, buf);
+#endif // __CYGWIN10__
+
+ // finally convert the result to Unicode if needed
+#if wxUSE_UNICODE && !defined(HAVE_WGETCWD)
+ wxConvFile.MB2WC(buf, cbuf, sz);
+#endif // wxUSE_UNICODE
+ }
- return buf;
+ return buf;
}
wxString wxGetCwd()
{
- static const size_t maxPathLen = 1024;
-
wxString str;
- wxGetWorkingDirectory(str.GetWriteBuf(maxPathLen), maxPathLen);
- str.UngetWriteBuf();
+
+ // we can't create wxStringBuffer object inline: Sun CC generates buggy
+ // code in this case!
+ {
+ wxStringBuffer buf(str, _MAXPATHLEN);
+ wxGetWorkingDirectory(buf, _MAXPATHLEN);
+ }
return str;
}
bool wxEndsWithPathSeparator(const wxChar *pszFileName)
{
- size_t len = wxStrlen(pszFileName);
- if ( len == 0 )
- return FALSE;
- else
- return wxIsPathSeparator(pszFileName[len - 1]);
+ size_t len = wxStrlen(pszFileName);
+
+ return len && wxIsPathSeparator(pszFileName[len - 1]);
}
// find a file in a list of directories, returns false if not found
#ifdef __VISUALC__
#pragma warning(default:4706) // assignment within conditional expression
#endif // VC++
+
+//------------------------------------------------------------------------
+// Missing functions in Unicode for Win9x
+//------------------------------------------------------------------------
+
+// NB: MSLU only covers Win32 API, it doesn't provide Unicode implementation of
+// libc functions. Unfortunately, some of MSVCRT wchar_t functions
+// (e.g. _wopen) don't work on Windows 9x, so we have to workaround it
+// by calling the char version. We still want to use wchar_t version on
+// NT/2000/XP, though, because they allow for Unicode file names.
+#if wxUSE_UNICODE_MSLU
+
+ #if defined( __VISUALC__ ) \
+ || ( defined(__MINGW32__) && wxCHECK_W32API_VERSION( 0, 5 ) ) \
+ || ( defined(__MWERKS__) && defined(__WXMSW__) )
+ WXDLLEXPORT int wxOpen(const wxChar *name, int flags, int mode)
+ {
+ if ( wxGetOsVersion() == wxWINDOWS_NT )
+ return _wopen(name, flags, mode);
+ else
+ return _open(wxConvFile.cWX2MB(name), flags, mode);
+ }
+ #endif
+
+#endif // wxUSE_UNICODE_MSLU