| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: filefn.cpp |
| 3 | // Purpose: File- and directory-related functions |
| 4 | // Author: Julian Smart |
| 5 | // Modified by: |
| 6 | // Created: 29/01/98 |
| 7 | // RCS-ID: $Id$ |
| 8 | // Copyright: (c) 1998 Julian Smart |
| 9 | // Licence: wxWindows license |
| 10 | ///////////////////////////////////////////////////////////////////////////// |
| 11 | |
| 12 | // ============================================================================ |
| 13 | // declarations |
| 14 | // ============================================================================ |
| 15 | |
| 16 | // ---------------------------------------------------------------------------- |
| 17 | // headers |
| 18 | // ---------------------------------------------------------------------------- |
| 19 | |
| 20 | #ifdef __GNUG__ |
| 21 | #pragma implementation "filefn.h" |
| 22 | #endif |
| 23 | |
| 24 | // For compilers that support precompilation, includes "wx.h". |
| 25 | #include "wx/wxprec.h" |
| 26 | #include "wx/defs.h" |
| 27 | |
| 28 | #ifdef __BORLANDC__ |
| 29 | #pragma hdrstop |
| 30 | #endif |
| 31 | |
| 32 | #include "wx/utils.h" |
| 33 | #include "wx/intl.h" |
| 34 | #include "wx/file.h" |
| 35 | #include "wx/filename.h" |
| 36 | #include "wx/dir.h" |
| 37 | |
| 38 | // there are just too many of those... |
| 39 | #ifdef __VISUALC__ |
| 40 | #pragma warning(disable:4706) // assignment within conditional expression |
| 41 | #endif // VC++ |
| 42 | |
| 43 | #include <ctype.h> |
| 44 | #include <stdio.h> |
| 45 | #include <stdlib.h> |
| 46 | #include <string.h> |
| 47 | #if !defined(__WATCOMC__) |
| 48 | #if !(defined(_MSC_VER) && (_MSC_VER > 800)) |
| 49 | #include <errno.h> |
| 50 | #endif |
| 51 | #endif |
| 52 | |
| 53 | #if defined(__WXMAC__) |
| 54 | #include "wx/mac/private.h" // includes mac headers |
| 55 | #endif |
| 56 | |
| 57 | #include <time.h> |
| 58 | |
| 59 | #ifndef __MWERKS__ |
| 60 | #include <sys/types.h> |
| 61 | #include <sys/stat.h> |
| 62 | #else |
| 63 | #include <stat.h> |
| 64 | #include <unistd.h> |
| 65 | #include <unix.h> |
| 66 | #endif |
| 67 | |
| 68 | #ifdef __UNIX__ |
| 69 | #include <unistd.h> |
| 70 | #include <dirent.h> |
| 71 | #include <fcntl.h> |
| 72 | #endif |
| 73 | |
| 74 | #ifdef __WXPM__ |
| 75 | #include <process.h> |
| 76 | #include "wx/os2/private.h" |
| 77 | #endif |
| 78 | #if defined(__WINDOWS__) && !defined(__WXMICROWIN__) |
| 79 | #if !defined( __GNUWIN32__ ) && !defined( __MWERKS__ ) && !defined(__SALFORDC__) |
| 80 | #include <direct.h> |
| 81 | #include <dos.h> |
| 82 | #include <io.h> |
| 83 | #endif // __WINDOWS__ |
| 84 | #endif // native Win compiler |
| 85 | |
| 86 | #if defined(__DOS__) |
| 87 | #ifdef __WATCOMC__ |
| 88 | #include <direct.h> |
| 89 | #include <dos.h> |
| 90 | #include <io.h> |
| 91 | #endif |
| 92 | #ifdef __DJGPP__ |
| 93 | #include <unistd.h> |
| 94 | #endif |
| 95 | #endif |
| 96 | |
| 97 | #ifdef __BORLANDC__ // Please someone tell me which version of Borland needs |
| 98 | // this (3.1 I believe) and how to test for it. |
| 99 | // If this works for Borland 4.0 as well, then no worries. |
| 100 | #include <dir.h> |
| 101 | #endif |
| 102 | |
| 103 | #ifdef __SALFORDC__ |
| 104 | #include <dir.h> |
| 105 | #include <unix.h> |
| 106 | #endif |
| 107 | |
| 108 | #include "wx/log.h" |
| 109 | |
| 110 | // No, Cygwin doesn't appear to have fnmatch.h after all. |
| 111 | #if defined(HAVE_FNMATCH_H) |
| 112 | #include "fnmatch.h" |
| 113 | #endif |
| 114 | |
| 115 | #ifdef __WINDOWS__ |
| 116 | #include <windows.h> |
| 117 | #include "wx/msw/mslu.h" |
| 118 | |
| 119 | // sys/cygwin.h is needed for cygwin_conv_to_full_win32_path() |
| 120 | // |
| 121 | // note that it must be included after <windows.h> |
| 122 | #ifdef __GNUWIN32__ |
| 123 | #ifdef __CYGWIN__ |
| 124 | #include <sys/cygwin.h> |
| 125 | #endif |
| 126 | |
| 127 | #ifndef __TWIN32__ |
| 128 | #include <sys/unistd.h> |
| 129 | #endif |
| 130 | #endif // __GNUWIN32__ |
| 131 | #endif // __WINDOWS__ |
| 132 | |
| 133 | // TODO: Borland probably has _wgetcwd as well? |
| 134 | #ifdef _MSC_VER |
| 135 | #define HAVE_WGETCWD |
| 136 | #endif |
| 137 | |
| 138 | // ---------------------------------------------------------------------------- |
| 139 | // constants |
| 140 | // ---------------------------------------------------------------------------- |
| 141 | |
| 142 | #ifndef _MAXPATHLEN |
| 143 | #define _MAXPATHLEN 1024 |
| 144 | #endif |
| 145 | |
| 146 | #ifdef __WXMAC__ |
| 147 | # ifdef __DARWIN__ |
| 148 | # include "MoreFilesX.h" |
| 149 | # else |
| 150 | # include "MoreFiles.h" |
| 151 | # include "MoreFilesExtras.h" |
| 152 | # include "FullPath.h" |
| 153 | # include "FSpCompat.h" |
| 154 | # endif |
| 155 | #endif |
| 156 | |
| 157 | // ---------------------------------------------------------------------------- |
| 158 | // private globals |
| 159 | // ---------------------------------------------------------------------------- |
| 160 | |
| 161 | // MT-FIXME: get rid of this horror and all code using it |
| 162 | static wxChar wxFileFunctionsBuffer[4*_MAXPATHLEN]; |
| 163 | |
| 164 | #if defined(__VISAGECPP__) && __IBMCPP__ >= 400 |
| 165 | // |
| 166 | // VisualAge C++ V4.0 cannot have any external linkage const decs |
| 167 | // in headers included by more than one primary source |
| 168 | // |
| 169 | const off_t wxInvalidOffset = (off_t)-1; |
| 170 | #endif |
| 171 | |
| 172 | // ---------------------------------------------------------------------------- |
| 173 | // macros |
| 174 | // ---------------------------------------------------------------------------- |
| 175 | |
| 176 | // we need to translate Mac filenames before passing them to OS functions |
| 177 | #define OS_FILENAME(s) (s.fn_str()) |
| 178 | |
| 179 | // ============================================================================ |
| 180 | // implementation |
| 181 | // ============================================================================ |
| 182 | |
| 183 | #ifdef wxNEED_WX_UNISTD_H |
| 184 | |
| 185 | WXDLLEXPORT int wxStat( const wxChar *file_name, wxStructStat *buf ) |
| 186 | { |
| 187 | return stat( wxConvFile.cWX2MB( file_name ), buf ); |
| 188 | } |
| 189 | |
| 190 | WXDLLEXPORT int wxAccess( const wxChar *pathname, int mode ) |
| 191 | { |
| 192 | return access( wxConvFile.cWX2MB( pathname ), mode ); |
| 193 | } |
| 194 | |
| 195 | WXDLLEXPORT int wxOpen( const wxChar *pathname, int flags, mode_t mode ) |
| 196 | { |
| 197 | return open( wxConvFile.cWX2MB( pathname ), flags, mode ); |
| 198 | } |
| 199 | |
| 200 | #endif |
| 201 | // wxNEED_WX_UNISTD_H |
| 202 | |
| 203 | // ---------------------------------------------------------------------------- |
| 204 | // wxPathList |
| 205 | // ---------------------------------------------------------------------------- |
| 206 | |
| 207 | IMPLEMENT_DYNAMIC_CLASS(wxPathList, wxStringList) |
| 208 | |
| 209 | void wxPathList::Add (const wxString& path) |
| 210 | { |
| 211 | wxStringList::Add (WXSTRINGCAST path); |
| 212 | } |
| 213 | |
| 214 | // Add paths e.g. from the PATH environment variable |
| 215 | void wxPathList::AddEnvList (const wxString& envVariable) |
| 216 | { |
| 217 | static const wxChar PATH_TOKS[] = |
| 218 | #ifdef __WINDOWS__ |
| 219 | wxT(" ;"); // Don't seperate with colon in DOS (used for drive) |
| 220 | #else |
| 221 | wxT(" :;"); |
| 222 | #endif |
| 223 | |
| 224 | wxChar *val = wxGetenv (WXSTRINGCAST envVariable); |
| 225 | if (val && *val) |
| 226 | { |
| 227 | wxChar *s = copystring (val); |
| 228 | wxChar *save_ptr, *token = wxStrtok (s, PATH_TOKS, &save_ptr); |
| 229 | |
| 230 | if (token) |
| 231 | { |
| 232 | Add (copystring (token)); |
| 233 | while (token) |
| 234 | { |
| 235 | if ((token = wxStrtok ((wxChar *) NULL, PATH_TOKS, &save_ptr)) != NULL) |
| 236 | Add (wxString(token)); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | // suppress warning about unused variable save_ptr when wxStrtok() is a |
| 241 | // macro which throws away its third argument |
| 242 | save_ptr = token; |
| 243 | |
| 244 | delete [] s; |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | // Given a full filename (with path), ensure that that file can |
| 249 | // be accessed again USING FILENAME ONLY by adding the path |
| 250 | // to the list if not already there. |
| 251 | void wxPathList::EnsureFileAccessible (const wxString& path) |
| 252 | { |
| 253 | wxString path_only(wxPathOnly(path)); |
| 254 | if ( !path_only.IsEmpty() ) |
| 255 | { |
| 256 | if ( !Member(path_only) ) |
| 257 | Add(path_only); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | bool wxPathList::Member (const wxString& path) |
| 262 | { |
| 263 | for (wxStringList::Node *node = GetFirst(); node; node = node->GetNext()) |
| 264 | { |
| 265 | wxString path2( node->GetData() ); |
| 266 | if ( |
| 267 | #if defined(__WINDOWS__) || defined(__VMS__) || defined (__WXMAC__) |
| 268 | // Case INDEPENDENT |
| 269 | path.CompareTo (path2, wxString::ignoreCase) == 0 |
| 270 | #else |
| 271 | // Case sensitive File System |
| 272 | path.CompareTo (path2) == 0 |
| 273 | #endif |
| 274 | ) |
| 275 | return TRUE; |
| 276 | } |
| 277 | return FALSE; |
| 278 | } |
| 279 | |
| 280 | wxString wxPathList::FindValidPath (const wxString& file) |
| 281 | { |
| 282 | if (wxFileExists (wxExpandPath(wxFileFunctionsBuffer, file))) |
| 283 | return wxString(wxFileFunctionsBuffer); |
| 284 | |
| 285 | wxChar buf[_MAXPATHLEN]; |
| 286 | wxStrcpy(buf, wxFileFunctionsBuffer); |
| 287 | |
| 288 | wxChar *filename = (wxChar*) NULL; /* shut up buggy egcs warning */ |
| 289 | filename = wxIsAbsolutePath (buf) ? wxFileNameFromPath (buf) : (wxChar *)buf; |
| 290 | |
| 291 | for (wxStringList::Node *node = GetFirst(); node; node = node->GetNext()) |
| 292 | { |
| 293 | wxChar *path = node->GetData(); |
| 294 | wxStrcpy (wxFileFunctionsBuffer, path); |
| 295 | wxChar ch = wxFileFunctionsBuffer[wxStrlen(wxFileFunctionsBuffer)-1]; |
| 296 | if (ch != wxT('\\') && ch != wxT('/')) |
| 297 | wxStrcat (wxFileFunctionsBuffer, wxT("/")); |
| 298 | wxStrcat (wxFileFunctionsBuffer, filename); |
| 299 | #ifdef __WINDOWS__ |
| 300 | wxUnix2DosFilename (wxFileFunctionsBuffer); |
| 301 | #endif |
| 302 | if (wxFileExists (wxFileFunctionsBuffer)) |
| 303 | { |
| 304 | return wxString(wxFileFunctionsBuffer); // Found! |
| 305 | } |
| 306 | } // for() |
| 307 | |
| 308 | return wxEmptyString; // Not found |
| 309 | } |
| 310 | |
| 311 | wxString wxPathList::FindAbsoluteValidPath (const wxString& file) |
| 312 | { |
| 313 | wxString f = FindValidPath(file); |
| 314 | if ( wxIsAbsolutePath(f) ) |
| 315 | return f; |
| 316 | |
| 317 | wxString buf; |
| 318 | wxGetWorkingDirectory(wxStringBuffer(buf, _MAXPATHLEN), _MAXPATHLEN); |
| 319 | |
| 320 | if ( !wxEndsWithPathSeparator(buf) ) |
| 321 | { |
| 322 | buf += wxFILE_SEP_PATH; |
| 323 | } |
| 324 | buf += f; |
| 325 | |
| 326 | return buf; |
| 327 | } |
| 328 | |
| 329 | bool |
| 330 | wxFileExists (const wxString& filename) |
| 331 | { |
| 332 | // we must use GetFileAttributes() instead of the ANSI C functions because |
| 333 | // it can cope with network (UNC) paths unlike them |
| 334 | #if defined(__WIN32__) && !defined(__WXMICROWIN__) |
| 335 | DWORD ret = ::GetFileAttributes(filename); |
| 336 | |
| 337 | return (ret != (DWORD)-1) && !(ret & FILE_ATTRIBUTE_DIRECTORY); |
| 338 | #else // !__WIN32__ |
| 339 | wxStructStat st; |
| 340 | return wxStat(filename, &st) == 0 && (st.st_mode & S_IFREG); |
| 341 | #endif // __WIN32__/!__WIN32__ |
| 342 | } |
| 343 | |
| 344 | bool |
| 345 | wxIsAbsolutePath (const wxString& filename) |
| 346 | { |
| 347 | if (filename != wxT("")) |
| 348 | { |
| 349 | #if defined(__WXMAC__) && !defined(__DARWIN__) |
| 350 | // Classic or Carbon CodeWarrior like |
| 351 | // Carbon with Apple DevTools is Unix like |
| 352 | |
| 353 | // This seems wrong to me, but there is no fix. since |
| 354 | // "MacOS:MyText.txt" is absolute whereas "MyDir:MyText.txt" |
| 355 | // is not. Or maybe ":MyDir:MyText.txt" has to be used? RR. |
| 356 | if (filename.Find(':') != wxNOT_FOUND && filename[0] != ':') |
| 357 | return TRUE ; |
| 358 | #else |
| 359 | // Unix like or Windows |
| 360 | if (filename[0] == wxT('/')) |
| 361 | return TRUE; |
| 362 | #endif |
| 363 | #ifdef __VMS__ |
| 364 | if ((filename[0] == wxT('[') && filename[1] != wxT('.'))) |
| 365 | return TRUE; |
| 366 | #endif |
| 367 | #ifdef __WINDOWS__ |
| 368 | // MSDOS like |
| 369 | if (filename[0] == wxT('\\') || (wxIsalpha (filename[0]) && filename[1] == wxT(':'))) |
| 370 | return TRUE; |
| 371 | #endif |
| 372 | } |
| 373 | return FALSE ; |
| 374 | } |
| 375 | |
| 376 | /* |
| 377 | * Strip off any extension (dot something) from end of file, |
| 378 | * IF one exists. Inserts zero into buffer. |
| 379 | * |
| 380 | */ |
| 381 | |
| 382 | void wxStripExtension(wxChar *buffer) |
| 383 | { |
| 384 | int len = wxStrlen(buffer); |
| 385 | int i = len-1; |
| 386 | while (i > 0) |
| 387 | { |
| 388 | if (buffer[i] == wxT('.')) |
| 389 | { |
| 390 | buffer[i] = 0; |
| 391 | break; |
| 392 | } |
| 393 | i --; |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | void wxStripExtension(wxString& buffer) |
| 398 | { |
| 399 | size_t len = buffer.Length(); |
| 400 | size_t i = len-1; |
| 401 | while (i > 0) |
| 402 | { |
| 403 | if (buffer.GetChar(i) == wxT('.')) |
| 404 | { |
| 405 | buffer = buffer.Left(i); |
| 406 | break; |
| 407 | } |
| 408 | i --; |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | // Destructive removal of /./ and /../ stuff |
| 413 | wxChar *wxRealPath (wxChar *path) |
| 414 | { |
| 415 | #ifdef __WXMSW__ |
| 416 | static const wxChar SEP = wxT('\\'); |
| 417 | wxUnix2DosFilename(path); |
| 418 | #else |
| 419 | static const wxChar SEP = wxT('/'); |
| 420 | #endif |
| 421 | if (path[0] && path[1]) { |
| 422 | /* MATTHEW: special case "/./x" */ |
| 423 | wxChar *p; |
| 424 | if (path[2] == SEP && path[1] == wxT('.')) |
| 425 | p = &path[0]; |
| 426 | else |
| 427 | p = &path[2]; |
| 428 | for (; *p; p++) |
| 429 | { |
| 430 | if (*p == SEP) |
| 431 | { |
| 432 | if (p[1] == wxT('.') && p[2] == wxT('.') && (p[3] == SEP || p[3] == wxT('\0'))) |
| 433 | { |
| 434 | wxChar *q; |
| 435 | for (q = p - 1; q >= path && *q != SEP; q--) |
| 436 | { |
| 437 | // Empty |
| 438 | } |
| 439 | |
| 440 | if (q[0] == SEP && (q[1] != wxT('.') || q[2] != wxT('.') || q[3] != SEP) |
| 441 | && (q - 1 <= path || q[-1] != SEP)) |
| 442 | { |
| 443 | wxStrcpy (q, p + 3); |
| 444 | if (path[0] == wxT('\0')) |
| 445 | { |
| 446 | path[0] = SEP; |
| 447 | path[1] = wxT('\0'); |
| 448 | } |
| 449 | #ifdef __WXMSW__ |
| 450 | /* Check that path[2] is NULL! */ |
| 451 | else if (path[1] == wxT(':') && !path[2]) |
| 452 | { |
| 453 | path[2] = SEP; |
| 454 | path[3] = wxT('\0'); |
| 455 | } |
| 456 | #endif |
| 457 | p = q - 1; |
| 458 | } |
| 459 | } |
| 460 | else if (p[1] == wxT('.') && (p[2] == SEP || p[2] == wxT('\0'))) |
| 461 | wxStrcpy (p, p + 2); |
| 462 | } |
| 463 | } |
| 464 | } |
| 465 | return path; |
| 466 | } |
| 467 | |
| 468 | // Must be destroyed |
| 469 | wxChar *wxCopyAbsolutePath(const wxString& filename) |
| 470 | { |
| 471 | if (filename == wxT("")) |
| 472 | return (wxChar *) NULL; |
| 473 | |
| 474 | if (! wxIsAbsolutePath(wxExpandPath(wxFileFunctionsBuffer, filename))) { |
| 475 | wxChar buf[_MAXPATHLEN]; |
| 476 | buf[0] = wxT('\0'); |
| 477 | wxGetWorkingDirectory(buf, WXSIZEOF(buf)); |
| 478 | wxChar ch = buf[wxStrlen(buf) - 1]; |
| 479 | #ifdef __WXMSW__ |
| 480 | if (ch != wxT('\\') && ch != wxT('/')) |
| 481 | wxStrcat(buf, wxT("\\")); |
| 482 | #else |
| 483 | if (ch != wxT('/')) |
| 484 | wxStrcat(buf, wxT("/")); |
| 485 | #endif |
| 486 | wxStrcat(buf, wxFileFunctionsBuffer); |
| 487 | return copystring( wxRealPath(buf) ); |
| 488 | } |
| 489 | return copystring( wxFileFunctionsBuffer ); |
| 490 | } |
| 491 | |
| 492 | /*- |
| 493 | Handles: |
| 494 | ~/ => home dir |
| 495 | ~user/ => user's home dir |
| 496 | If the environment variable a = "foo" and b = "bar" then: |
| 497 | Unix: |
| 498 | $a => foo |
| 499 | $a$b => foobar |
| 500 | $a.c => foo.c |
| 501 | xxx$a => xxxfoo |
| 502 | ${a}! => foo! |
| 503 | $(b)! => bar! |
| 504 | \$a => \$a |
| 505 | MSDOS: |
| 506 | $a ==> $a |
| 507 | $(a) ==> foo |
| 508 | $(a)$b ==> foo$b |
| 509 | $(a)$(b)==> foobar |
| 510 | test.$$ ==> test.$$ |
| 511 | */ |
| 512 | |
| 513 | /* input name in name, pathname output to buf. */ |
| 514 | |
| 515 | wxChar *wxExpandPath(wxChar *buf, const wxChar *name) |
| 516 | { |
| 517 | register wxChar *d, *s, *nm; |
| 518 | wxChar lnm[_MAXPATHLEN]; |
| 519 | int q; |
| 520 | |
| 521 | // Some compilers don't like this line. |
| 522 | // const wxChar trimchars[] = wxT("\n \t"); |
| 523 | |
| 524 | wxChar trimchars[4]; |
| 525 | trimchars[0] = wxT('\n'); |
| 526 | trimchars[1] = wxT(' '); |
| 527 | trimchars[2] = wxT('\t'); |
| 528 | trimchars[3] = 0; |
| 529 | |
| 530 | #ifdef __WXMSW__ |
| 531 | const wxChar SEP = wxT('\\'); |
| 532 | #else |
| 533 | const wxChar SEP = wxT('/'); |
| 534 | #endif |
| 535 | buf[0] = wxT('\0'); |
| 536 | if (name == NULL || *name == wxT('\0')) |
| 537 | return buf; |
| 538 | nm = copystring(name); // Make a scratch copy |
| 539 | wxChar *nm_tmp = nm; |
| 540 | |
| 541 | /* Skip leading whitespace and cr */ |
| 542 | while (wxStrchr((wxChar *)trimchars, *nm) != NULL) |
| 543 | nm++; |
| 544 | /* And strip off trailing whitespace and cr */ |
| 545 | s = nm + (q = wxStrlen(nm)) - 1; |
| 546 | while (q-- && wxStrchr((wxChar *)trimchars, *s) != NULL) |
| 547 | *s = wxT('\0'); |
| 548 | |
| 549 | s = nm; |
| 550 | d = lnm; |
| 551 | #ifdef __WXMSW__ |
| 552 | q = FALSE; |
| 553 | #else |
| 554 | q = nm[0] == wxT('\\') && nm[1] == wxT('~'); |
| 555 | #endif |
| 556 | |
| 557 | /* Expand inline environment variables */ |
| 558 | #ifdef __VISAGECPP__ |
| 559 | while (*d) |
| 560 | { |
| 561 | *d++ = *s; |
| 562 | if(*s == wxT('\\')) |
| 563 | { |
| 564 | *(d - 1) = *++s; |
| 565 | if (*d) |
| 566 | { |
| 567 | s++; |
| 568 | continue; |
| 569 | } |
| 570 | else |
| 571 | break; |
| 572 | } |
| 573 | else |
| 574 | #else |
| 575 | while ((*d++ = *s) != 0) { |
| 576 | # ifndef __WXMSW__ |
| 577 | if (*s == wxT('\\')) { |
| 578 | if ((*(d - 1) = *++s)) { |
| 579 | s++; |
| 580 | continue; |
| 581 | } else |
| 582 | break; |
| 583 | } else |
| 584 | # endif |
| 585 | #endif |
| 586 | #ifdef __WXMSW__ |
| 587 | if (*s++ == wxT('$') && (*s == wxT('{') || *s == wxT(')'))) |
| 588 | #else |
| 589 | if (*s++ == wxT('$')) |
| 590 | #endif |
| 591 | { |
| 592 | register wxChar *start = d; |
| 593 | register int braces = (*s == wxT('{') || *s == wxT('(')); |
| 594 | register wxChar *value; |
| 595 | while ((*d++ = *s) != 0) |
| 596 | if (braces ? (*s == wxT('}') || *s == wxT(')')) : !(wxIsalnum(*s) || *s == wxT('_')) ) |
| 597 | break; |
| 598 | else |
| 599 | s++; |
| 600 | *--d = 0; |
| 601 | value = wxGetenv(braces ? start + 1 : start); |
| 602 | if (value) { |
| 603 | for ((d = start - 1); (*d++ = *value++) != 0;) |
| 604 | { |
| 605 | // Empty |
| 606 | } |
| 607 | |
| 608 | d--; |
| 609 | if (braces && *s) |
| 610 | s++; |
| 611 | } |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | /* Expand ~ and ~user */ |
| 616 | nm = lnm; |
| 617 | if (nm[0] == wxT('~') && !q) |
| 618 | { |
| 619 | /* prefix ~ */ |
| 620 | if (nm[1] == SEP || nm[1] == 0) |
| 621 | { /* ~/filename */ |
| 622 | // FIXME: wxGetUserHome could return temporary storage in Unicode mode |
| 623 | if ((s = WXSTRINGCAST wxGetUserHome(wxT(""))) != NULL) { |
| 624 | if (*++nm) |
| 625 | nm++; |
| 626 | } |
| 627 | } else |
| 628 | { /* ~user/filename */ |
| 629 | register wxChar *nnm; |
| 630 | register wxChar *home; |
| 631 | for (s = nm; *s && *s != SEP; s++) |
| 632 | { |
| 633 | // Empty |
| 634 | } |
| 635 | int was_sep; /* MATTHEW: Was there a separator, or NULL? */ |
| 636 | was_sep = (*s == SEP); |
| 637 | nnm = *s ? s + 1 : s; |
| 638 | *s = 0; |
| 639 | // FIXME: wxGetUserHome could return temporary storage in Unicode mode |
| 640 | if ((home = WXSTRINGCAST wxGetUserHome(wxString(nm + 1))) == NULL) { |
| 641 | if (was_sep) /* replace only if it was there: */ |
| 642 | *s = SEP; |
| 643 | s = NULL; |
| 644 | } else { |
| 645 | nm = nnm; |
| 646 | s = home; |
| 647 | } |
| 648 | } |
| 649 | } |
| 650 | |
| 651 | d = buf; |
| 652 | if (s && *s) { /* MATTHEW: s could be NULL if user '~' didn't exist */ |
| 653 | /* Copy home dir */ |
| 654 | while (wxT('\0') != (*d++ = *s++)) |
| 655 | /* loop */; |
| 656 | // Handle root home |
| 657 | if (d - 1 > buf && *(d - 2) != SEP) |
| 658 | *(d - 1) = SEP; |
| 659 | } |
| 660 | s = nm; |
| 661 | while ((*d++ = *s++) != 0) |
| 662 | { |
| 663 | // Empty |
| 664 | } |
| 665 | delete[] nm_tmp; // clean up alloc |
| 666 | /* Now clean up the buffer */ |
| 667 | return wxRealPath(buf); |
| 668 | } |
| 669 | |
| 670 | /* Contract Paths to be build upon an environment variable |
| 671 | component: |
| 672 | |
| 673 | example: "/usr/openwin/lib", OPENWINHOME --> ${OPENWINHOME}/lib |
| 674 | |
| 675 | The call wxExpandPath can convert these back! |
| 676 | */ |
| 677 | wxChar * |
| 678 | wxContractPath (const wxString& filename, const wxString& envname, const wxString& user) |
| 679 | { |
| 680 | static wxChar dest[_MAXPATHLEN]; |
| 681 | |
| 682 | if (filename == wxT("")) |
| 683 | return (wxChar *) NULL; |
| 684 | |
| 685 | wxStrcpy (dest, WXSTRINGCAST filename); |
| 686 | #ifdef __WXMSW__ |
| 687 | wxUnix2DosFilename(dest); |
| 688 | #endif |
| 689 | |
| 690 | // Handle environment |
| 691 | const wxChar *val = (const wxChar *) NULL; |
| 692 | wxChar *tcp = (wxChar *) NULL; |
| 693 | if (envname != WXSTRINGCAST NULL && (val = wxGetenv (WXSTRINGCAST envname)) != NULL && |
| 694 | (tcp = wxStrstr (dest, val)) != NULL) |
| 695 | { |
| 696 | wxStrcpy (wxFileFunctionsBuffer, tcp + wxStrlen (val)); |
| 697 | *tcp++ = wxT('$'); |
| 698 | *tcp++ = wxT('{'); |
| 699 | wxStrcpy (tcp, WXSTRINGCAST envname); |
| 700 | wxStrcat (tcp, wxT("}")); |
| 701 | wxStrcat (tcp, wxFileFunctionsBuffer); |
| 702 | } |
| 703 | |
| 704 | // Handle User's home (ignore root homes!) |
| 705 | size_t len = 0; |
| 706 | if ((val = wxGetUserHome (user)) != NULL && |
| 707 | (len = wxStrlen(val)) > 2 && |
| 708 | wxStrncmp(dest, val, len) == 0) |
| 709 | { |
| 710 | wxStrcpy(wxFileFunctionsBuffer, wxT("~")); |
| 711 | if (user != wxT("")) |
| 712 | wxStrcat(wxFileFunctionsBuffer, (const wxChar*) user); |
| 713 | wxStrcat(wxFileFunctionsBuffer, dest + len); |
| 714 | wxStrcpy (dest, wxFileFunctionsBuffer); |
| 715 | } |
| 716 | |
| 717 | return dest; |
| 718 | } |
| 719 | |
| 720 | // Return just the filename, not the path (basename) |
| 721 | wxChar *wxFileNameFromPath (wxChar *path) |
| 722 | { |
| 723 | wxString p = path; |
| 724 | wxString n = wxFileNameFromPath(p); |
| 725 | |
| 726 | return path + p.length() - n.length(); |
| 727 | } |
| 728 | |
| 729 | wxString wxFileNameFromPath (const wxString& path) |
| 730 | { |
| 731 | wxString name, ext; |
| 732 | wxFileName::SplitPath(path, NULL, &name, &ext); |
| 733 | |
| 734 | wxString fullname = name; |
| 735 | if ( !ext.empty() ) |
| 736 | { |
| 737 | fullname << wxFILE_SEP_EXT << ext; |
| 738 | } |
| 739 | |
| 740 | return fullname; |
| 741 | } |
| 742 | |
| 743 | // Return just the directory, or NULL if no directory |
| 744 | wxChar * |
| 745 | wxPathOnly (wxChar *path) |
| 746 | { |
| 747 | if (path && *path) |
| 748 | { |
| 749 | static wxChar buf[_MAXPATHLEN]; |
| 750 | |
| 751 | // Local copy |
| 752 | wxStrcpy (buf, path); |
| 753 | |
| 754 | int l = wxStrlen(path); |
| 755 | int i = l - 1; |
| 756 | |
| 757 | // Search backward for a backward or forward slash |
| 758 | while (i > -1) |
| 759 | { |
| 760 | #if defined(__WXMAC__) && !defined(__DARWIN__) |
| 761 | // Classic or Carbon CodeWarrior like |
| 762 | // Carbon with Apple DevTools is Unix like |
| 763 | if (path[i] == wxT(':') ) |
| 764 | { |
| 765 | buf[i] = 0; |
| 766 | return buf; |
| 767 | } |
| 768 | #else |
| 769 | // Unix like or Windows |
| 770 | if (path[i] == wxT('/') || path[i] == wxT('\\')) |
| 771 | { |
| 772 | buf[i] = 0; |
| 773 | return buf; |
| 774 | } |
| 775 | #endif |
| 776 | #ifdef __VMS__ |
| 777 | if (path[i] == wxT(']')) |
| 778 | { |
| 779 | buf[i+1] = 0; |
| 780 | return buf; |
| 781 | } |
| 782 | #endif |
| 783 | i --; |
| 784 | } |
| 785 | |
| 786 | #if defined(__WXMSW__) || defined(__WXPM__) |
| 787 | // Try Drive specifier |
| 788 | if (wxIsalpha (buf[0]) && buf[1] == wxT(':')) |
| 789 | { |
| 790 | // A:junk --> A:. (since A:.\junk Not A:\junk) |
| 791 | buf[2] = wxT('.'); |
| 792 | buf[3] = wxT('\0'); |
| 793 | return buf; |
| 794 | } |
| 795 | #endif |
| 796 | } |
| 797 | return (wxChar *) NULL; |
| 798 | } |
| 799 | |
| 800 | // Return just the directory, or NULL if no directory |
| 801 | wxString wxPathOnly (const wxString& path) |
| 802 | { |
| 803 | if (path != wxT("")) |
| 804 | { |
| 805 | wxChar buf[_MAXPATHLEN]; |
| 806 | |
| 807 | // Local copy |
| 808 | wxStrcpy (buf, WXSTRINGCAST path); |
| 809 | |
| 810 | int l = path.Length(); |
| 811 | int i = l - 1; |
| 812 | |
| 813 | // Search backward for a backward or forward slash |
| 814 | while (i > -1) |
| 815 | { |
| 816 | #if defined(__WXMAC__) && !defined(__DARWIN__) |
| 817 | // Classic or Carbon CodeWarrior like |
| 818 | // Carbon with Apple DevTools is Unix like |
| 819 | if (path[i] == wxT(':') ) |
| 820 | { |
| 821 | buf[i] = 0; |
| 822 | return wxString(buf); |
| 823 | } |
| 824 | #else |
| 825 | // Unix like or Windows |
| 826 | if (path[i] == wxT('/') || path[i] == wxT('\\')) |
| 827 | { |
| 828 | buf[i] = 0; |
| 829 | return wxString(buf); |
| 830 | } |
| 831 | #endif |
| 832 | #ifdef __VMS__ |
| 833 | if (path[i] == wxT(']')) |
| 834 | { |
| 835 | buf[i+1] = 0; |
| 836 | return wxString(buf); |
| 837 | } |
| 838 | #endif |
| 839 | i --; |
| 840 | } |
| 841 | |
| 842 | #if defined(__WXMSW__) || defined(__WXPM__) |
| 843 | // Try Drive specifier |
| 844 | if (wxIsalpha (buf[0]) && buf[1] == wxT(':')) |
| 845 | { |
| 846 | // A:junk --> A:. (since A:.\junk Not A:\junk) |
| 847 | buf[2] = wxT('.'); |
| 848 | buf[3] = wxT('\0'); |
| 849 | return wxString(buf); |
| 850 | } |
| 851 | #endif |
| 852 | } |
| 853 | return wxString(wxT("")); |
| 854 | } |
| 855 | |
| 856 | // Utility for converting delimiters in DOS filenames to UNIX style |
| 857 | // and back again - or we get nasty problems with delimiters. |
| 858 | // Also, convert to lower case, since case is significant in UNIX. |
| 859 | |
| 860 | #if defined(__WXMAC__) |
| 861 | wxString wxMacFSSpec2MacFilename( const FSSpec *spec ) |
| 862 | { |
| 863 | #ifdef __DARWIN__ |
| 864 | int i; |
| 865 | int j; |
| 866 | OSErr theErr; |
| 867 | OSStatus theStatus; |
| 868 | Boolean isDirectory = FALSE; |
| 869 | Str255 theParentPath = "\p"; |
| 870 | FSSpec theParentSpec; |
| 871 | FSRef theParentRef; |
| 872 | char theFileName[FILENAME_MAX]; |
| 873 | char thePath[FILENAME_MAX]; |
| 874 | |
| 875 | strcpy(thePath, ""); |
| 876 | |
| 877 | // GD: Separate file name from path and make a FSRef to the parent |
| 878 | // directory. This is necessary since FSRefs cannot reference files |
| 879 | // that have not yet been created. |
| 880 | // Based on example code from Apple Technical Note TN2022 |
| 881 | // http://developer.apple.com/technotes/tn/tn2022.html |
| 882 | |
| 883 | // check whether we are converting a directory |
| 884 | isDirectory = ((spec->name)[spec->name[0]] == ':'); |
| 885 | // count length of file name |
| 886 | for (i = spec->name[0] - (isDirectory ? 1 : 0); ((spec->name[i] != ':') && (i > 0)); i--); |
| 887 | // copy file name |
| 888 | // prepend path separator since it will later be appended to the path |
| 889 | theFileName[0] = wxFILE_SEP_PATH; |
| 890 | for (j = i + 1; j <= spec->name[0] - (isDirectory ? 1 : 0); j++) { |
| 891 | theFileName[j - i] = spec->name[j]; |
| 892 | } |
| 893 | theFileName[j - i] = '\0'; |
| 894 | // copy path if any |
| 895 | for (j = 1; j <= i; j++) { |
| 896 | theParentPath[++theParentPath[0]] = spec->name[j]; |
| 897 | } |
| 898 | theErr = FSMakeFSSpec(spec->vRefNum, spec->parID, theParentPath, &theParentSpec); |
| 899 | if (theErr == noErr) { |
| 900 | // convert the FSSpec to an FSRef |
| 901 | theErr = FSpMakeFSRef(&theParentSpec, &theParentRef); |
| 902 | } |
| 903 | if (theErr == noErr) { |
| 904 | // get the POSIX path associated with the FSRef |
| 905 | theStatus = FSRefMakePath(&theParentRef, |
| 906 | (UInt8 *)thePath, sizeof(thePath)); |
| 907 | } |
| 908 | if (theStatus == noErr) { |
| 909 | // append file name to path |
| 910 | // includes previously prepended path separator |
| 911 | strcat(thePath, theFileName); |
| 912 | } |
| 913 | |
| 914 | // create path string for return value |
| 915 | wxString result( thePath ) ; |
| 916 | #else |
| 917 | Handle myPath ; |
| 918 | short length ; |
| 919 | |
| 920 | // get length of path and allocate handle |
| 921 | FSpGetFullPath( spec , &length , &myPath ) ; |
| 922 | ::SetHandleSize( myPath , length + 1 ) ; |
| 923 | ::HLock( myPath ) ; |
| 924 | (*myPath)[length] = 0 ; |
| 925 | if ((length > 0) && ((*myPath)[length-1] == ':')) |
| 926 | (*myPath)[length-1] = 0 ; |
| 927 | |
| 928 | // create path string for return value |
| 929 | wxString result( (char*) *myPath ) ; |
| 930 | |
| 931 | // free allocated handle |
| 932 | ::HUnlock( myPath ) ; |
| 933 | ::DisposeHandle( myPath ) ; |
| 934 | #endif |
| 935 | |
| 936 | return result ; |
| 937 | } |
| 938 | #ifndef __DARWIN__ |
| 939 | // Mac file names are POSIX (Unix style) under Darwin |
| 940 | // therefore the conversion functions below are not needed |
| 941 | |
| 942 | static char sMacFileNameConversion[ 1000 ] ; |
| 943 | |
| 944 | #endif |
| 945 | void wxMacFilename2FSSpec( const char *path , FSSpec *spec ) |
| 946 | { |
| 947 | OSStatus err = noErr ; |
| 948 | #ifdef __DARWIN__ |
| 949 | FSRef theRef; |
| 950 | |
| 951 | // get the FSRef associated with the POSIX path |
| 952 | err = FSPathMakeRef((const UInt8 *) path, &theRef, NULL); |
| 953 | // convert the FSRef to an FSSpec |
| 954 | err = FSGetCatalogInfo(&theRef, kFSCatInfoNone, NULL, NULL, spec, NULL); |
| 955 | #else |
| 956 | if ( strchr( path , ':' ) == NULL ) |
| 957 | { |
| 958 | // try whether it is a volume / or a mounted volume |
| 959 | strncpy( sMacFileNameConversion , path , 1000 ) ; |
| 960 | sMacFileNameConversion[998] = 0 ; |
| 961 | strcat( sMacFileNameConversion , ":" ) ; |
| 962 | err = FSpLocationFromFullPath( strlen(sMacFileNameConversion) , sMacFileNameConversion , spec ) ; |
| 963 | } |
| 964 | else |
| 965 | { |
| 966 | err = FSpLocationFromFullPath( strlen(path) , path , spec ) ; |
| 967 | } |
| 968 | #endif |
| 969 | } |
| 970 | |
| 971 | #ifndef __DARWIN__ |
| 972 | |
| 973 | wxString wxMac2UnixFilename (const char *str) |
| 974 | { |
| 975 | char *s = sMacFileNameConversion ; |
| 976 | strcpy( s , str ) ; |
| 977 | if (s) |
| 978 | { |
| 979 | memmove( s+1 , s ,strlen( s ) + 1) ; |
| 980 | if ( *s == ':' ) |
| 981 | *s = '.' ; |
| 982 | else |
| 983 | *s = '/' ; |
| 984 | |
| 985 | while (*s) |
| 986 | { |
| 987 | if (*s == ':') |
| 988 | *s = '/'; |
| 989 | else |
| 990 | *s = wxTolower(*s); // Case INDEPENDENT |
| 991 | s++; |
| 992 | } |
| 993 | } |
| 994 | return wxString(sMacFileNameConversion) ; |
| 995 | } |
| 996 | |
| 997 | wxString wxUnix2MacFilename (const char *str) |
| 998 | { |
| 999 | char *s = sMacFileNameConversion ; |
| 1000 | strcpy( s , str ) ; |
| 1001 | if (s) |
| 1002 | { |
| 1003 | if ( *s == '.' ) |
| 1004 | { |
| 1005 | // relative path , since it goes on with slash which is translated to a : |
| 1006 | memmove( s , s+1 ,strlen( s ) ) ; |
| 1007 | } |
| 1008 | else if ( *s == '/' ) |
| 1009 | { |
| 1010 | // absolute path -> on mac just start with the drive name |
| 1011 | memmove( s , s+1 ,strlen( s ) ) ; |
| 1012 | } |
| 1013 | else |
| 1014 | { |
| 1015 | wxASSERT_MSG( 1 , "unkown path beginning" ) ; |
| 1016 | } |
| 1017 | while (*s) |
| 1018 | { |
| 1019 | if (*s == '/' || *s == '\\') |
| 1020 | { |
| 1021 | // convert any back-directory situations |
| 1022 | if ( *(s+1) == '.' && *(s+2) == '.' && ( (*(s+3) == '/' || *(s+3) == '\\') ) ) |
| 1023 | { |
| 1024 | *s = ':'; |
| 1025 | memmove( s+1 , s+3 ,strlen( s+3 ) + 1 ) ; |
| 1026 | } |
| 1027 | else |
| 1028 | *s = ':'; |
| 1029 | } |
| 1030 | s++ ; |
| 1031 | } |
| 1032 | } |
| 1033 | return wxString (sMacFileNameConversion) ; |
| 1034 | } |
| 1035 | |
| 1036 | wxString wxMacFSSpec2UnixFilename( const FSSpec *spec ) |
| 1037 | { |
| 1038 | return wxMac2UnixFilename( wxMacFSSpec2MacFilename( spec) ) ; |
| 1039 | } |
| 1040 | |
| 1041 | void wxUnixFilename2FSSpec( const char *path , FSSpec *spec ) |
| 1042 | { |
| 1043 | wxString var = wxUnix2MacFilename( path ) ; |
| 1044 | wxMacFilename2FSSpec( var , spec ) ; |
| 1045 | } |
| 1046 | #endif // ! __DARWIN__ |
| 1047 | |
| 1048 | #endif // __WXMAC__ |
| 1049 | |
| 1050 | void |
| 1051 | wxDos2UnixFilename (char *s) |
| 1052 | { |
| 1053 | if (s) |
| 1054 | while (*s) |
| 1055 | { |
| 1056 | if (*s == '\\') |
| 1057 | *s = '/'; |
| 1058 | #ifdef __WXMSW__ |
| 1059 | else |
| 1060 | *s = wxTolower (*s); // Case INDEPENDENT |
| 1061 | #endif |
| 1062 | s++; |
| 1063 | } |
| 1064 | } |
| 1065 | |
| 1066 | void |
| 1067 | #if defined(__WXMSW__) || defined(__WXPM__) |
| 1068 | wxUnix2DosFilename (wxChar *s) |
| 1069 | #else |
| 1070 | wxUnix2DosFilename (wxChar *WXUNUSED(s) ) |
| 1071 | #endif |
| 1072 | { |
| 1073 | // Yes, I really mean this to happen under DOS only! JACS |
| 1074 | #if defined(__WXMSW__) || defined(__WXPM__) |
| 1075 | if (s) |
| 1076 | while (*s) |
| 1077 | { |
| 1078 | if (*s == wxT('/')) |
| 1079 | *s = wxT('\\'); |
| 1080 | s++; |
| 1081 | } |
| 1082 | #endif |
| 1083 | } |
| 1084 | |
| 1085 | // Concatenate two files to form third |
| 1086 | bool |
| 1087 | wxConcatFiles (const wxString& file1, const wxString& file2, const wxString& file3) |
| 1088 | { |
| 1089 | wxString outfile; |
| 1090 | if ( !wxGetTempFileName( wxT("cat"), outfile) ) |
| 1091 | return FALSE; |
| 1092 | |
| 1093 | FILE *fp1 = (FILE *) NULL; |
| 1094 | FILE *fp2 = (FILE *) NULL; |
| 1095 | FILE *fp3 = (FILE *) NULL; |
| 1096 | // Open the inputs and outputs |
| 1097 | if ((fp1 = wxFopen ( file1, wxT("rb"))) == NULL || |
| 1098 | (fp2 = wxFopen ( file2, wxT("rb"))) == NULL || |
| 1099 | (fp3 = wxFopen ( outfile, wxT("wb"))) == NULL) |
| 1100 | { |
| 1101 | if (fp1) |
| 1102 | fclose (fp1); |
| 1103 | if (fp2) |
| 1104 | fclose (fp2); |
| 1105 | if (fp3) |
| 1106 | fclose (fp3); |
| 1107 | return FALSE; |
| 1108 | } |
| 1109 | |
| 1110 | int ch; |
| 1111 | while ((ch = getc (fp1)) != EOF) |
| 1112 | (void) putc (ch, fp3); |
| 1113 | fclose (fp1); |
| 1114 | |
| 1115 | while ((ch = getc (fp2)) != EOF) |
| 1116 | (void) putc (ch, fp3); |
| 1117 | fclose (fp2); |
| 1118 | |
| 1119 | fclose (fp3); |
| 1120 | bool result = wxRenameFile(outfile, file3); |
| 1121 | return result; |
| 1122 | } |
| 1123 | |
| 1124 | // Copy files |
| 1125 | bool |
| 1126 | wxCopyFile (const wxString& file1, const wxString& file2, bool overwrite) |
| 1127 | { |
| 1128 | #if defined(__WIN32__) && !defined(__WXMICROWIN__) |
| 1129 | // CopyFile() copies file attributes and modification time too, so use it |
| 1130 | // instead of our code if available |
| 1131 | // |
| 1132 | // NB: 3rd parameter is bFailIfExists i.e. the inverse of overwrite |
| 1133 | if ( !::CopyFile(file1, file2, !overwrite) ) |
| 1134 | { |
| 1135 | wxLogSysError(_("Failed to copy the file '%s' to '%s'"), |
| 1136 | file1.c_str(), file2.c_str()); |
| 1137 | |
| 1138 | return FALSE; |
| 1139 | } |
| 1140 | #elif defined(__WXPM__) |
| 1141 | if ( ::DosCopy(file2, file2, overwrite ? DCPY_EXISTING : 0) != 0 ) |
| 1142 | return FALSE; |
| 1143 | #else // !Win32 |
| 1144 | |
| 1145 | wxStructStat fbuf; |
| 1146 | // get permissions of file1 |
| 1147 | if ( wxStat( file1.c_str(), &fbuf) != 0 ) |
| 1148 | { |
| 1149 | // the file probably doesn't exist or we haven't the rights to read |
| 1150 | // from it anyhow |
| 1151 | wxLogSysError(_("Impossible to get permissions for file '%s'"), |
| 1152 | file1.c_str()); |
| 1153 | return FALSE; |
| 1154 | } |
| 1155 | |
| 1156 | // open file1 for reading |
| 1157 | wxFile fileIn(file1, wxFile::read); |
| 1158 | if ( !fileIn.IsOpened() ) |
| 1159 | return FALSE; |
| 1160 | |
| 1161 | // remove file2, if it exists. This is needed for creating |
| 1162 | // file2 with the correct permissions in the next step |
| 1163 | if ( wxFileExists(file2) && (!overwrite || !wxRemoveFile(file2))) |
| 1164 | { |
| 1165 | wxLogSysError(_("Impossible to overwrite the file '%s'"), |
| 1166 | file2.c_str()); |
| 1167 | return FALSE; |
| 1168 | } |
| 1169 | |
| 1170 | #ifdef __UNIX__ |
| 1171 | // reset the umask as we want to create the file with exactly the same |
| 1172 | // permissions as the original one |
| 1173 | mode_t oldUmask = umask( 0 ); |
| 1174 | #endif // __UNIX__ |
| 1175 | |
| 1176 | // create file2 with the same permissions than file1 and open it for |
| 1177 | // writing |
| 1178 | |
| 1179 | wxFile fileOut; |
| 1180 | if ( !fileOut.Create(file2, overwrite, fbuf.st_mode & 0777) ) |
| 1181 | return FALSE; |
| 1182 | |
| 1183 | #ifdef __UNIX__ |
| 1184 | /// restore the old umask |
| 1185 | umask(oldUmask); |
| 1186 | #endif // __UNIX__ |
| 1187 | |
| 1188 | // copy contents of file1 to file2 |
| 1189 | char buf[4096]; |
| 1190 | size_t count; |
| 1191 | for ( ;; ) |
| 1192 | { |
| 1193 | count = fileIn.Read(buf, WXSIZEOF(buf)); |
| 1194 | if ( fileIn.Error() ) |
| 1195 | return FALSE; |
| 1196 | |
| 1197 | // end of file? |
| 1198 | if ( !count ) |
| 1199 | break; |
| 1200 | |
| 1201 | if ( fileOut.Write(buf, count) < count ) |
| 1202 | return FALSE; |
| 1203 | } |
| 1204 | |
| 1205 | // we can expect fileIn to be closed successfully, but we should ensure |
| 1206 | // that fileOut was closed as some write errors (disk full) might not be |
| 1207 | // detected before doing this |
| 1208 | if ( !fileIn.Close() || !fileOut.Close() ) |
| 1209 | return FALSE; |
| 1210 | |
| 1211 | #if !defined(__VISAGECPP__) && !defined(__WXMAC__) || defined(__UNIX__) |
| 1212 | // no chmod in VA. Should be some permission API for HPFS386 partitions |
| 1213 | // however |
| 1214 | if ( chmod(OS_FILENAME(file2), fbuf.st_mode) != 0 ) |
| 1215 | { |
| 1216 | wxLogSysError(_("Impossible to set permissions for the file '%s'"), |
| 1217 | file2.c_str()); |
| 1218 | return FALSE; |
| 1219 | } |
| 1220 | #endif // OS/2 || Mac |
| 1221 | #endif // __WXMSW__ && __WIN32__ |
| 1222 | |
| 1223 | return TRUE; |
| 1224 | } |
| 1225 | |
| 1226 | bool |
| 1227 | wxRenameFile (const wxString& file1, const wxString& file2) |
| 1228 | { |
| 1229 | // Normal system call |
| 1230 | if ( wxRename (file1, file2) == 0 ) |
| 1231 | return TRUE; |
| 1232 | |
| 1233 | // Try to copy |
| 1234 | if (wxCopyFile(file1, file2)) { |
| 1235 | wxRemoveFile(file1); |
| 1236 | return TRUE; |
| 1237 | } |
| 1238 | // Give up |
| 1239 | return FALSE; |
| 1240 | } |
| 1241 | |
| 1242 | bool wxRemoveFile(const wxString& file) |
| 1243 | { |
| 1244 | #if defined(__VISUALC__) \ |
| 1245 | || defined(__BORLANDC__) \ |
| 1246 | || defined(__WATCOMC__) \ |
| 1247 | || defined(__GNUWIN32__) |
| 1248 | int res = wxRemove(file); |
| 1249 | #else |
| 1250 | int res = unlink(OS_FILENAME(file)); |
| 1251 | #endif |
| 1252 | |
| 1253 | return res == 0; |
| 1254 | } |
| 1255 | |
| 1256 | bool wxMkdir(const wxString& dir, int perm) |
| 1257 | { |
| 1258 | #if defined(__WXMAC__) && !defined(__UNIX__) |
| 1259 | return (mkdir( dir , 0 ) == 0); |
| 1260 | #else // !Mac |
| 1261 | const wxChar *dirname = dir.c_str(); |
| 1262 | |
| 1263 | // assume mkdir() has 2 args on non Windows-OS/2 platforms and on Windows too |
| 1264 | // for the GNU compiler |
| 1265 | #if (!(defined(__WXMSW__) || defined(__WXPM__) || defined(__DOS__))) || (defined(__GNUWIN32__) && !defined(__MINGW32__)) || defined(__WINE__) || defined(__WXMICROWIN__) |
| 1266 | #ifndef MSVCRT |
| 1267 | if ( mkdir(wxFNCONV(dirname), perm) != 0 ) |
| 1268 | #else |
| 1269 | if ( mkdir(wxFNCONV(dirname)) != 0 ) |
| 1270 | #endif |
| 1271 | #elif defined(__WXPM__) |
| 1272 | if (::DosCreateDir((PSZ)dirname, NULL) != 0) // enhance for EAB's?? |
| 1273 | #elif defined(__DOS__) |
| 1274 | #if defined(__WATCOMC__) |
| 1275 | (void)perm; |
| 1276 | if ( wxMkDir(wxFNSTRINGCAST wxFNCONV(dirname)) != 0 ) |
| 1277 | #elif defined(__DJGPP__) |
| 1278 | if ( mkdir(wxFNCONV(dirname), perm) != 0 ) |
| 1279 | #else |
| 1280 | #error "Unsupported DOS compiler!" |
| 1281 | #endif |
| 1282 | #else // !MSW, !DOS and !OS/2 VAC++ |
| 1283 | (void)perm; |
| 1284 | if ( wxMkDir(wxFNSTRINGCAST wxFNCONV(dirname)) != 0 ) |
| 1285 | #endif // !MSW/MSW |
| 1286 | { |
| 1287 | wxLogSysError(_("Directory '%s' couldn't be created"), dirname); |
| 1288 | |
| 1289 | return FALSE; |
| 1290 | } |
| 1291 | |
| 1292 | return TRUE; |
| 1293 | #endif // Mac/!Mac |
| 1294 | } |
| 1295 | |
| 1296 | bool wxRmdir(const wxString& dir, int WXUNUSED(flags)) |
| 1297 | { |
| 1298 | #ifdef __VMS__ |
| 1299 | return FALSE; //to be changed since rmdir exists in VMS7.x |
| 1300 | #elif defined(__WXPM__) |
| 1301 | return (::DosDeleteDir((PSZ)dir.c_str()) == 0); |
| 1302 | #else |
| 1303 | |
| 1304 | #ifdef __SALFORDC__ |
| 1305 | return FALSE; // What to do? |
| 1306 | #else |
| 1307 | return (wxRmDir(OS_FILENAME(dir)) == 0); |
| 1308 | #endif |
| 1309 | |
| 1310 | #endif |
| 1311 | } |
| 1312 | |
| 1313 | // does the path exists? (may have or not '/' or '\\' at the end) |
| 1314 | bool wxPathExists(const wxChar *pszPathName) |
| 1315 | { |
| 1316 | wxString strPath(pszPathName); |
| 1317 | |
| 1318 | #ifdef __WINDOWS__ |
| 1319 | // Windows fails to find directory named "c:\dir\" even if "c:\dir" exists, |
| 1320 | // so remove all trailing backslashes from the path - but don't do this for |
| 1321 | // the pathes "d:\" (which are different from "d:") nor for just "\" |
| 1322 | while ( wxEndsWithPathSeparator(strPath) ) |
| 1323 | { |
| 1324 | size_t len = strPath.length(); |
| 1325 | if ( len == 1 || (len == 3 && strPath[len - 2] == _T(':')) ) |
| 1326 | break; |
| 1327 | |
| 1328 | strPath.Truncate(len - 1); |
| 1329 | } |
| 1330 | #endif // __WINDOWS__ |
| 1331 | |
| 1332 | #if defined(__WIN32__) && !defined(__WXMICROWIN__) |
| 1333 | // stat() can't cope with network paths |
| 1334 | DWORD ret = ::GetFileAttributes(strPath); |
| 1335 | |
| 1336 | return (ret != (DWORD)-1) && (ret & FILE_ATTRIBUTE_DIRECTORY); |
| 1337 | #else // !__WIN32__ |
| 1338 | |
| 1339 | wxStructStat st; |
| 1340 | #ifndef __VISAGECPP__ |
| 1341 | return wxStat(pszPathName, &st) == 0 && ((st.st_mode & S_IFMT) == S_IFDIR); |
| 1342 | #else |
| 1343 | // S_IFMT not supported in VA compilers.. st_mode is a 2byte value only |
| 1344 | return wxStat(pszPathName, &st) == 0 && (st.st_mode == S_IFDIR); |
| 1345 | #endif |
| 1346 | |
| 1347 | #endif // __WIN32__/!__WIN32__ |
| 1348 | } |
| 1349 | |
| 1350 | // Get a temporary filename, opening and closing the file. |
| 1351 | wxChar *wxGetTempFileName(const wxString& prefix, wxChar *buf) |
| 1352 | { |
| 1353 | wxString filename = wxFileName::CreateTempFileName(prefix); |
| 1354 | if ( filename.empty() ) |
| 1355 | return NULL; |
| 1356 | |
| 1357 | if ( buf ) |
| 1358 | wxStrcpy(buf, filename); |
| 1359 | else |
| 1360 | buf = copystring(filename); |
| 1361 | |
| 1362 | return buf; |
| 1363 | } |
| 1364 | |
| 1365 | bool wxGetTempFileName(const wxString& prefix, wxString& buf) |
| 1366 | { |
| 1367 | buf = wxFileName::CreateTempFileName(prefix); |
| 1368 | |
| 1369 | return !buf.empty(); |
| 1370 | } |
| 1371 | |
| 1372 | // Get first file name matching given wild card. |
| 1373 | |
| 1374 | static wxDir *gs_dir = NULL; |
| 1375 | static wxString gs_dirPath; |
| 1376 | |
| 1377 | wxString wxFindFirstFile(const wxChar *spec, int flags) |
| 1378 | { |
| 1379 | wxSplitPath(spec, &gs_dirPath, NULL, NULL); |
| 1380 | if ( gs_dirPath.IsEmpty() ) |
| 1381 | gs_dirPath = wxT("."); |
| 1382 | if ( gs_dirPath.Last() != wxFILE_SEP_PATH ) |
| 1383 | gs_dirPath << wxFILE_SEP_PATH; |
| 1384 | |
| 1385 | if (gs_dir) |
| 1386 | delete gs_dir; |
| 1387 | gs_dir = new wxDir(gs_dirPath); |
| 1388 | |
| 1389 | if ( !gs_dir->IsOpened() ) |
| 1390 | { |
| 1391 | wxLogSysError(_("Can not enumerate files '%s'"), spec); |
| 1392 | return wxEmptyString; |
| 1393 | } |
| 1394 | |
| 1395 | int dirFlags = 0; |
| 1396 | switch (flags) |
| 1397 | { |
| 1398 | case wxDIR: dirFlags = wxDIR_DIRS; break; |
| 1399 | case wxFILE: dirFlags = wxDIR_FILES; break; |
| 1400 | default: dirFlags = wxDIR_DIRS | wxDIR_FILES; break; |
| 1401 | } |
| 1402 | |
| 1403 | wxString result; |
| 1404 | gs_dir->GetFirst(&result, wxFileNameFromPath(wxString(spec)), dirFlags); |
| 1405 | if ( result.IsEmpty() ) |
| 1406 | { |
| 1407 | wxDELETE(gs_dir); |
| 1408 | return result; |
| 1409 | } |
| 1410 | |
| 1411 | return gs_dirPath + result; |
| 1412 | } |
| 1413 | |
| 1414 | wxString wxFindNextFile() |
| 1415 | { |
| 1416 | wxASSERT_MSG( gs_dir, wxT("You must call wxFindFirstFile before!") ); |
| 1417 | |
| 1418 | wxString result; |
| 1419 | gs_dir->GetNext(&result); |
| 1420 | |
| 1421 | if ( result.IsEmpty() ) |
| 1422 | { |
| 1423 | wxDELETE(gs_dir); |
| 1424 | return result; |
| 1425 | } |
| 1426 | |
| 1427 | return gs_dirPath + result; |
| 1428 | } |
| 1429 | |
| 1430 | |
| 1431 | // Get current working directory. |
| 1432 | // If buf is NULL, allocates space using new, else |
| 1433 | // copies into buf. |
| 1434 | wxChar *wxGetWorkingDirectory(wxChar *buf, int sz) |
| 1435 | { |
| 1436 | if ( !buf ) |
| 1437 | { |
| 1438 | buf = new wxChar[sz + 1]; |
| 1439 | } |
| 1440 | |
| 1441 | bool ok = FALSE; |
| 1442 | |
| 1443 | // for the compilers which have Unicode version of _getcwd(), call it |
| 1444 | // directly, for the others call the ANSI version and do the translation |
| 1445 | #if !wxUSE_UNICODE |
| 1446 | #define cbuf buf |
| 1447 | #else // wxUSE_UNICODE |
| 1448 | bool needsANSI = TRUE; |
| 1449 | |
| 1450 | #if !defined(HAVE_WGETCWD) || wxUSE_UNICODE_MSLU |
| 1451 | // This is not legal code as the compiler |
| 1452 | // is allowed destroy the wxCharBuffer. |
| 1453 | // wxCharBuffer c_buffer(sz); |
| 1454 | // char *cbuf = (char*)(const char*)c_buffer; |
| 1455 | char cbuf[_MAXPATHLEN]; |
| 1456 | #endif |
| 1457 | |
| 1458 | #ifdef HAVE_WGETCWD |
| 1459 | #if wxUSE_UNICODE_MSLU |
| 1460 | if ( wxGetOsVersion() != wxWIN95 ) |
| 1461 | #else |
| 1462 | char *cbuf = NULL; // never really used because needsANSI will always be FALSE |
| 1463 | #endif |
| 1464 | { |
| 1465 | ok = _wgetcwd(buf, sz) != NULL; |
| 1466 | needsANSI = FALSE; |
| 1467 | } |
| 1468 | #endif |
| 1469 | |
| 1470 | if ( needsANSI ) |
| 1471 | #endif // wxUSE_UNICODE |
| 1472 | { |
| 1473 | #ifdef _MSC_VER |
| 1474 | ok = _getcwd(cbuf, sz) != NULL; |
| 1475 | #elif defined(__WXMAC__) && !defined(__DARWIN__) |
| 1476 | FSSpec cwdSpec ; |
| 1477 | FCBPBRec pb; |
| 1478 | OSErr error; |
| 1479 | Str255 fileName ; |
| 1480 | pb.ioNamePtr = (StringPtr) &fileName; |
| 1481 | pb.ioVRefNum = 0; |
| 1482 | pb.ioRefNum = LMGetCurApRefNum(); |
| 1483 | pb.ioFCBIndx = 0; |
| 1484 | error = PBGetFCBInfoSync(&pb); |
| 1485 | if ( error == noErr ) |
| 1486 | { |
| 1487 | cwdSpec.vRefNum = pb.ioFCBVRefNum; |
| 1488 | cwdSpec.parID = pb.ioFCBParID; |
| 1489 | cwdSpec.name[0] = 0 ; |
| 1490 | wxString res = wxMacFSSpec2MacFilename( &cwdSpec ) ; |
| 1491 | |
| 1492 | strcpy( cbuf , res ) ; |
| 1493 | cbuf[res.length()]=0 ; |
| 1494 | |
| 1495 | ok = TRUE; |
| 1496 | } |
| 1497 | else |
| 1498 | { |
| 1499 | ok = FALSE; |
| 1500 | } |
| 1501 | #elif defined(__VISAGECPP__) || (defined (__OS2__) && defined (__WATCOMC__)) |
| 1502 | APIRET rc; |
| 1503 | rc = ::DosQueryCurrentDir( 0 // current drive |
| 1504 | ,cbuf |
| 1505 | ,(PULONG)&sz |
| 1506 | ); |
| 1507 | ok = rc != 0; |
| 1508 | #else // !Win32/VC++ !Mac !OS2 |
| 1509 | ok = getcwd(cbuf, sz) != NULL; |
| 1510 | #endif // platform |
| 1511 | |
| 1512 | #if wxUSE_UNICODE |
| 1513 | // finally convert the result to Unicode if needed |
| 1514 | wxConvFile.MB2WC(buf, cbuf, sz); |
| 1515 | #endif // wxUSE_UNICODE |
| 1516 | } |
| 1517 | |
| 1518 | if ( !ok ) |
| 1519 | { |
| 1520 | wxLogSysError(_("Failed to get the working directory")); |
| 1521 | |
| 1522 | // VZ: the old code used to return "." on error which didn't make any |
| 1523 | // sense at all to me - empty string is a better error indicator |
| 1524 | // (NULL might be even better but I'm afraid this could lead to |
| 1525 | // problems with the old code assuming the return is never NULL) |
| 1526 | buf[0] = _T('\0'); |
| 1527 | } |
| 1528 | else // ok, but we might need to massage the path into the right format |
| 1529 | { |
| 1530 | #ifdef __DJGPP__ |
| 1531 | // VS: DJGPP is a strange mix of DOS and UNIX API and returns paths |
| 1532 | // with / deliminers. We don't like that. |
| 1533 | for (wxChar *ch = buf; *ch; ch++) |
| 1534 | { |
| 1535 | if (*ch == wxT('/')) |
| 1536 | *ch = wxT('\\'); |
| 1537 | } |
| 1538 | #endif // __DJGPP__ |
| 1539 | |
| 1540 | // MBN: we hope that in the case the user is compiling a GTK+/Motif app, |
| 1541 | // he needs Unix as opposed to Win32 pathnames |
| 1542 | #if defined( __CYGWIN__ ) && defined( __WINDOWS__ ) |
| 1543 | // another example of DOS/Unix mix (Cygwin) |
| 1544 | wxString pathUnix = buf; |
| 1545 | cygwin_conv_to_full_win32_path(pathUnix, buf); |
| 1546 | #endif // __CYGWIN__ |
| 1547 | } |
| 1548 | |
| 1549 | return buf; |
| 1550 | |
| 1551 | #if !wxUSE_UNICODE |
| 1552 | #undef cbuf |
| 1553 | #endif |
| 1554 | } |
| 1555 | |
| 1556 | wxString wxGetCwd() |
| 1557 | { |
| 1558 | wxChar *buffer = new wxChar[_MAXPATHLEN]; |
| 1559 | wxGetWorkingDirectory(buffer, _MAXPATHLEN); |
| 1560 | wxString str( buffer ); |
| 1561 | delete [] buffer; |
| 1562 | |
| 1563 | return str; |
| 1564 | } |
| 1565 | |
| 1566 | bool wxSetWorkingDirectory(const wxString& d) |
| 1567 | { |
| 1568 | #if defined(__UNIX__) || defined(__WXMAC__) || defined(__DOS__) |
| 1569 | return (chdir(wxFNSTRINGCAST d.fn_str()) == 0); |
| 1570 | #elif defined(__WXPM__) |
| 1571 | return (::DosSetCurrentDir((PSZ)d.c_str()) == 0); |
| 1572 | #elif defined(__WINDOWS__) |
| 1573 | |
| 1574 | #ifdef __WIN32__ |
| 1575 | return (bool)(SetCurrentDirectory(d) != 0); |
| 1576 | #else |
| 1577 | // Must change drive, too. |
| 1578 | bool isDriveSpec = ((strlen(d) > 1) && (d[1] == ':')); |
| 1579 | if (isDriveSpec) |
| 1580 | { |
| 1581 | wxChar firstChar = d[0]; |
| 1582 | |
| 1583 | // To upper case |
| 1584 | if (firstChar > 90) |
| 1585 | firstChar = firstChar - 32; |
| 1586 | |
| 1587 | // To a drive number |
| 1588 | unsigned int driveNo = firstChar - 64; |
| 1589 | if (driveNo > 0) |
| 1590 | { |
| 1591 | unsigned int noDrives; |
| 1592 | _dos_setdrive(driveNo, &noDrives); |
| 1593 | } |
| 1594 | } |
| 1595 | bool success = (chdir(WXSTRINGCAST d) == 0); |
| 1596 | |
| 1597 | return success; |
| 1598 | #endif |
| 1599 | |
| 1600 | #endif |
| 1601 | } |
| 1602 | |
| 1603 | // Get the OS directory if appropriate (such as the Windows directory). |
| 1604 | // On non-Windows platform, probably just return the empty string. |
| 1605 | wxString wxGetOSDirectory() |
| 1606 | { |
| 1607 | #if defined(__WINDOWS__) && !defined(__WXMICROWIN__) |
| 1608 | wxChar buf[256]; |
| 1609 | GetWindowsDirectory(buf, 256); |
| 1610 | return wxString(buf); |
| 1611 | #else |
| 1612 | return wxEmptyString; |
| 1613 | #endif |
| 1614 | } |
| 1615 | |
| 1616 | bool wxEndsWithPathSeparator(const wxChar *pszFileName) |
| 1617 | { |
| 1618 | size_t len = wxStrlen(pszFileName); |
| 1619 | |
| 1620 | return len && wxIsPathSeparator(pszFileName[len - 1]); |
| 1621 | } |
| 1622 | |
| 1623 | // find a file in a list of directories, returns FALSE if not found |
| 1624 | bool wxFindFileInPath(wxString *pStr, const wxChar *pszPath, const wxChar *pszFile) |
| 1625 | { |
| 1626 | // we assume that it's not empty |
| 1627 | wxCHECK_MSG( !wxIsEmpty(pszFile), FALSE, |
| 1628 | _T("empty file name in wxFindFileInPath")); |
| 1629 | |
| 1630 | // skip path separator in the beginning of the file name if present |
| 1631 | if ( wxIsPathSeparator(*pszFile) ) |
| 1632 | pszFile++; |
| 1633 | |
| 1634 | // copy the path (strtok will modify it) |
| 1635 | wxChar *szPath = new wxChar[wxStrlen(pszPath) + 1]; |
| 1636 | wxStrcpy(szPath, pszPath); |
| 1637 | |
| 1638 | wxString strFile; |
| 1639 | wxChar *pc, *save_ptr; |
| 1640 | for ( pc = wxStrtok(szPath, wxPATH_SEP, &save_ptr); |
| 1641 | pc != NULL; |
| 1642 | pc = wxStrtok((wxChar *) NULL, wxPATH_SEP, &save_ptr) ) |
| 1643 | { |
| 1644 | // search for the file in this directory |
| 1645 | strFile = pc; |
| 1646 | if ( !wxEndsWithPathSeparator(pc) ) |
| 1647 | strFile += wxFILE_SEP_PATH; |
| 1648 | strFile += pszFile; |
| 1649 | |
| 1650 | if ( wxFileExists(strFile) ) { |
| 1651 | *pStr = strFile; |
| 1652 | break; |
| 1653 | } |
| 1654 | } |
| 1655 | |
| 1656 | // suppress warning about unused variable save_ptr when wxStrtok() is a |
| 1657 | // macro which throws away its third argument |
| 1658 | save_ptr = pc; |
| 1659 | |
| 1660 | delete [] szPath; |
| 1661 | |
| 1662 | return pc != NULL; // if TRUE => we breaked from the loop |
| 1663 | } |
| 1664 | |
| 1665 | void WXDLLEXPORT wxSplitPath(const wxChar *pszFileName, |
| 1666 | wxString *pstrPath, |
| 1667 | wxString *pstrName, |
| 1668 | wxString *pstrExt) |
| 1669 | { |
| 1670 | // it can be empty, but it shouldn't be NULL |
| 1671 | wxCHECK_RET( pszFileName, wxT("NULL file name in wxSplitPath") ); |
| 1672 | |
| 1673 | wxFileName::SplitPath(pszFileName, pstrPath, pstrName, pstrExt); |
| 1674 | } |
| 1675 | |
| 1676 | time_t WXDLLEXPORT wxFileModificationTime(const wxString& filename) |
| 1677 | { |
| 1678 | wxStructStat buf; |
| 1679 | wxStat( filename, &buf); |
| 1680 | |
| 1681 | return buf.st_mtime; |
| 1682 | } |
| 1683 | |
| 1684 | |
| 1685 | //------------------------------------------------------------------------ |
| 1686 | // wild character routines |
| 1687 | //------------------------------------------------------------------------ |
| 1688 | |
| 1689 | bool wxIsWild( const wxString& pattern ) |
| 1690 | { |
| 1691 | wxString tmp = pattern; |
| 1692 | wxChar *pat = WXSTRINGCAST(tmp); |
| 1693 | while (*pat) |
| 1694 | { |
| 1695 | switch (*pat++) |
| 1696 | { |
| 1697 | case wxT('?'): case wxT('*'): case wxT('['): case wxT('{'): |
| 1698 | return TRUE; |
| 1699 | case wxT('\\'): |
| 1700 | if (!*pat++) |
| 1701 | return FALSE; |
| 1702 | } |
| 1703 | } |
| 1704 | return FALSE; |
| 1705 | } |
| 1706 | |
| 1707 | /* |
| 1708 | * Written By Douglas A. Lewis <dalewis@cs.Buffalo.EDU> |
| 1709 | * |
| 1710 | * The match procedure is public domain code (from ircII's reg.c) |
| 1711 | */ |
| 1712 | |
| 1713 | bool wxMatchWild( const wxString& pat, const wxString& text, bool dot_special ) |
| 1714 | { |
| 1715 | if (text.empty()) |
| 1716 | { |
| 1717 | /* Match if both are empty. */ |
| 1718 | return pat.empty(); |
| 1719 | } |
| 1720 | |
| 1721 | const wxChar *m = pat.c_str(), |
| 1722 | *n = text.c_str(), |
| 1723 | *ma = NULL, |
| 1724 | *na = NULL, |
| 1725 | *mp = NULL, |
| 1726 | *np = NULL; |
| 1727 | int just = 0, |
| 1728 | pcount = 0, |
| 1729 | acount = 0, |
| 1730 | count = 0; |
| 1731 | |
| 1732 | if (dot_special && (*n == wxT('.'))) |
| 1733 | { |
| 1734 | /* Never match so that hidden Unix files |
| 1735 | * are never found. */ |
| 1736 | return FALSE; |
| 1737 | } |
| 1738 | |
| 1739 | for (;;) |
| 1740 | { |
| 1741 | if (*m == wxT('*')) |
| 1742 | { |
| 1743 | ma = ++m; |
| 1744 | na = n; |
| 1745 | just = 1; |
| 1746 | mp = NULL; |
| 1747 | acount = count; |
| 1748 | } |
| 1749 | else if (*m == wxT('?')) |
| 1750 | { |
| 1751 | m++; |
| 1752 | if (!*n++) |
| 1753 | return FALSE; |
| 1754 | } |
| 1755 | else |
| 1756 | { |
| 1757 | if (*m == wxT('\\')) |
| 1758 | { |
| 1759 | m++; |
| 1760 | /* Quoting "nothing" is a bad thing */ |
| 1761 | if (!*m) |
| 1762 | return FALSE; |
| 1763 | } |
| 1764 | if (!*m) |
| 1765 | { |
| 1766 | /* |
| 1767 | * If we are out of both strings or we just |
| 1768 | * saw a wildcard, then we can say we have a |
| 1769 | * match |
| 1770 | */ |
| 1771 | if (!*n) |
| 1772 | return TRUE; |
| 1773 | if (just) |
| 1774 | return TRUE; |
| 1775 | just = 0; |
| 1776 | goto not_matched; |
| 1777 | } |
| 1778 | /* |
| 1779 | * We could check for *n == NULL at this point, but |
| 1780 | * since it's more common to have a character there, |
| 1781 | * check to see if they match first (m and n) and |
| 1782 | * then if they don't match, THEN we can check for |
| 1783 | * the NULL of n |
| 1784 | */ |
| 1785 | just = 0; |
| 1786 | if (*m == *n) |
| 1787 | { |
| 1788 | m++; |
| 1789 | if (*n == wxT(' ')) |
| 1790 | mp = NULL; |
| 1791 | count++; |
| 1792 | n++; |
| 1793 | } |
| 1794 | else |
| 1795 | { |
| 1796 | |
| 1797 | not_matched: |
| 1798 | |
| 1799 | /* |
| 1800 | * If there are no more characters in the |
| 1801 | * string, but we still need to find another |
| 1802 | * character (*m != NULL), then it will be |
| 1803 | * impossible to match it |
| 1804 | */ |
| 1805 | if (!*n) |
| 1806 | return FALSE; |
| 1807 | if (mp) |
| 1808 | { |
| 1809 | m = mp; |
| 1810 | if (*np == wxT(' ')) |
| 1811 | { |
| 1812 | mp = NULL; |
| 1813 | goto check_percent; |
| 1814 | } |
| 1815 | n = ++np; |
| 1816 | count = pcount; |
| 1817 | } |
| 1818 | else |
| 1819 | check_percent: |
| 1820 | |
| 1821 | if (ma) |
| 1822 | { |
| 1823 | m = ma; |
| 1824 | n = ++na; |
| 1825 | count = acount; |
| 1826 | } |
| 1827 | else |
| 1828 | return FALSE; |
| 1829 | } |
| 1830 | } |
| 1831 | } |
| 1832 | } |
| 1833 | |
| 1834 | |
| 1835 | #ifdef __VISUALC__ |
| 1836 | #pragma warning(default:4706) // assignment within conditional expression |
| 1837 | #endif // VC++ |