]> git.saurik.com Git - wxWidgets.git/blob - src/common/filefn.cpp
Add support for dynamic event tables
[wxWidgets.git] / src / common / filefn.cpp
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 #ifdef __GNUG__
13 #pragma implementation "filefn.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18 #include "wx/defs.h"
19
20 #ifdef __BORLANDC__
21 #pragma hdrstop
22 #endif
23
24 #ifndef WX_PRECOMP
25 #include "wx/defs.h"
26 #endif
27
28 #include "wx/utils.h"
29
30 #include <ctype.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #if !defined(__WATCOMC__)
35 #if !(defined(_MSC_VER) && (_MSC_VER > 800))
36 #include <errno.h>
37 #endif
38 #endif
39 #include <time.h>
40 #include <sys/types.h>
41 #include <sys/stat.h>
42
43 #include <unistd.h>
44 #include <dirent.h>
45
46 #ifdef __WINDOWS__
47 #ifndef __GNUWIN32__
48 #include <direct.h>
49 #include <dos.h>
50 #endif
51 #endif
52
53 #ifdef __GNUWIN32__
54 #include <sys/unistd.h>
55 // #include <sys/stat.h>
56
57 #ifndef __MINGW32__
58 #include <std.h>
59 #endif
60
61 #define stricmp strcasecmp
62 #endif
63
64 #ifdef __BORLANDC__ // Please someone tell me which version of Borland needs
65 // this (3.1 I believe) and how to test for it.
66 // If this works for Borland 4.0 as well, then no worries.
67 #include <dir.h>
68 #endif
69
70 #ifdef __WINDOWS__
71 #include "windows.h"
72 #endif
73
74 #define _MAXPATHLEN 500
75
76 #if !USE_SHARED_LIBRARY
77 IMPLEMENT_DYNAMIC_CLASS(wxPathList, wxStringList)
78 #endif
79
80 extern char *wxBuffer;
81
82 void wxPathList::Add (const wxString& path)
83 {
84 wxStringList::Add ((char *)(const char *)path);
85 }
86
87 // Add paths e.g. from the PATH environment variable
88 void wxPathList::AddEnvList (const wxString& envVariable)
89 {
90 static const char PATH_TOKS[] =
91 #ifdef __WINDOWS__
92 " ;"; // Don't seperate with colon in DOS (used for drive)
93 #else
94 " :;";
95 #endif
96
97 char *val = getenv (WXSTRINGCAST envVariable);
98 if (val && *val)
99 {
100 char *s = copystring (val);
101 char *token = strtok (s, PATH_TOKS);
102
103 if (token)
104 {
105 Add (copystring (token));
106 while (token)
107 {
108 if ((token = strtok (NULL, PATH_TOKS)) != NULL)
109 Add (wxString(token));
110 }
111 }
112 delete[]s;
113 }
114 }
115
116 // Given a full filename (with path), ensure that that file can
117 // be accessed again USING FILENAME ONLY by adding the path
118 // to the list if not already there.
119 void wxPathList::EnsureFileAccessible (const wxString& path)
120 {
121 wxString path1(path);
122 char *path_only = wxPathOnly (WXSTRINGCAST path1);
123 if (path_only)
124 {
125 if (!Member (wxString(path_only)))
126 Add (wxString(path_only));
127 }
128 }
129
130 bool wxPathList::Member (const wxString& path)
131 {
132 for (wxNode * node = First (); node != NULL; node = node->Next ())
133 {
134 wxString path2((char *) node->Data ());
135 if (
136 #if defined(__WINDOWS__) || defined(__VMS__)
137 // Case INDEPENDENT
138 path.CompareTo (path2, wxString::ignoreCase) == 0
139 #else
140 // Case sensitive File System
141 path.CompareTo (path2) == 0
142 #endif
143 )
144 return TRUE;
145 }
146 return FALSE;
147 }
148
149 wxString wxPathList::FindValidPath (const wxString& file)
150 {
151 if (wxFileExists (wxExpandPath(wxBuffer, file)))
152 return wxString(wxBuffer);
153
154 char buf[_MAXPATHLEN];
155 strcpy(buf, wxBuffer);
156
157 char *filename = IsAbsolutePath (buf) ? wxFileNameFromPath (buf) : (char *)buf;
158
159 for (wxNode * node = First (); node; node = node->Next ())
160 {
161 char *path = (char *) node->Data ();
162 strcpy (wxBuffer, path);
163 char ch = wxBuffer[strlen(wxBuffer)-1];
164 if (ch != '\\' && ch != '/')
165 strcat (wxBuffer, "/");
166 strcat (wxBuffer, filename);
167 #ifdef __WINDOWS__
168 Unix2DosFilename (wxBuffer);
169 #endif
170 if (wxFileExists (wxBuffer))
171 {
172 return wxString(wxBuffer); // Found!
173 }
174 } // for()
175
176 return wxString(""); // Not found
177 }
178
179 wxString wxPathList::FindAbsoluteValidPath (const wxString& file)
180 {
181 wxString f = FindValidPath(file);
182 if (wxIsAbsolutePath(f))
183 return f;
184 else
185 {
186 char buf[500];
187 wxGetWorkingDirectory(buf, 499);
188 int len = (int)strlen(buf);
189 char lastCh = 0;
190 if (len > 0)
191 lastCh = buf[len-1];
192 if (lastCh != '/' && lastCh != '\\')
193 {
194 #ifdef __WINDOWS__
195 strcat(buf, "\\");
196 #else
197 strcat(buf, "/");
198 #endif
199 }
200 strcat(buf, (const char *)f);
201 strcpy(wxBuffer, buf);
202 return wxString(wxBuffer);
203 }
204 }
205
206 bool
207 wxFileExists (const wxString& filename)
208 {
209 struct stat stbuf;
210
211 if (filename && stat ((char *)(const char *)filename, &stbuf) == 0)
212 return TRUE;
213 return FALSE;
214 }
215
216 /* Vadim's alternative implementation
217
218 // does the file exist?
219 bool wxFileExists(const char *pszFileName)
220 {
221 struct stat st;
222 return !access(pszFileName, 0) &&
223 !stat(pszFileName, &st) &&
224 (st.st_mode & S_IFREG);
225 }
226 */
227
228 bool
229 wxIsAbsolutePath (const wxString& filename)
230 {
231 if (filename != "")
232 {
233 if (filename[0] == '/'
234 #ifdef __VMS__
235 || (filename[0] == '[' && filename[1] != '.')
236 #endif
237 #ifdef __WINDOWS__
238 /* MSDOS */
239 || filename[0] == '\\' || (isalpha (filename[0]) && filename[1] == ':')
240 #endif
241 )
242 return TRUE;
243 }
244 return FALSE;
245 }
246
247 /*
248 * Strip off any extension (dot something) from end of file,
249 * IF one exists. Inserts zero into buffer.
250 *
251 */
252
253 void wxStripExtension(char *buffer)
254 {
255 int len = strlen(buffer);
256 int i = len-1;
257 while (i > 0)
258 {
259 if (buffer[i] == '.')
260 {
261 buffer[i] = 0;
262 break;
263 }
264 i --;
265 }
266 }
267
268 // Destructive removal of /./ and /../ stuff
269 char *wxRealPath (char *path)
270 {
271 #ifdef __WINDOWS__
272 static const char SEP = '\\';
273 Unix2DosFilename(path);
274 #else
275 static const char SEP = '/';
276 #endif
277 if (path[0] && path[1]) {
278 /* MATTHEW: special case "/./x" */
279 char *p;
280 if (path[2] == SEP && path[1] == '.')
281 p = &path[0];
282 else
283 p = &path[2];
284 for (; *p; p++)
285 {
286 if (*p == SEP)
287 {
288 if (p[1] == '.' && p[2] == '.' && (p[3] == SEP || p[3] == '\0'))
289 {
290 char *q;
291 for (q = p - 1; q >= path && *q != SEP; q--);
292 if (q[0] == SEP && (q[1] != '.' || q[2] != '.' || q[3] != SEP)
293 && (q - 1 <= path || q[-1] != SEP))
294 {
295 strcpy (q, p + 3);
296 if (path[0] == '\0')
297 {
298 path[0] = SEP;
299 path[1] = '\0';
300 }
301 #ifdef __WINDOWS__
302 /* Check that path[2] is NULL! */
303 else if (path[1] == ':' && !path[2])
304 {
305 path[2] = SEP;
306 path[3] = '\0';
307 }
308 #endif
309 p = q - 1;
310 }
311 }
312 else if (p[1] == '.' && (p[2] == SEP || p[2] == '\0'))
313 strcpy (p, p + 2);
314 }
315 }
316 }
317 return path;
318 }
319
320 // Must be destroyed
321 char *wxCopyAbsolutePath(const wxString& filename)
322 {
323 if (filename == "")
324 return NULL;
325
326 if (! IsAbsolutePath(wxExpandPath(wxBuffer, filename))) {
327 char buf[_MAXPATHLEN];
328 buf[0] = '\0';
329 wxGetWorkingDirectory(buf, sizeof(buf)/sizeof(char));
330 char ch = buf[strlen(buf) - 1];
331 #ifdef __WINDOWS__
332 if (ch != '\\' && ch != '/')
333 strcat(buf, "\\");
334 #else
335 if (ch != '/')
336 strcat(buf, "/");
337 #endif
338 strcat(buf, wxBuffer);
339 return copystring( wxRealPath(buf) );
340 }
341 return copystring( wxBuffer );
342 }
343
344 /*-
345 Handles:
346 ~/ => home dir
347 ~user/ => user's home dir
348 If the environment variable a = "foo" and b = "bar" then:
349 Unix:
350 $a => foo
351 $a$b => foobar
352 $a.c => foo.c
353 xxx$a => xxxfoo
354 ${a}! => foo!
355 $(b)! => bar!
356 \$a => \$a
357 MSDOS:
358 $a ==> $a
359 $(a) ==> foo
360 $(a)$b ==> foo$b
361 $(a)$(b)==> foobar
362 test.$$ ==> test.$$
363 */
364
365 /* input name in name, pathname output to buf. */
366
367 char *wxExpandPath(char *buf, const char *name)
368 {
369 register char *d, *s, *nm;
370 char lnm[_MAXPATHLEN];
371 int q;
372
373 // Some compilers don't like this line.
374 // const char trimchars[] = "\n \t";
375
376 char trimchars[4];
377 trimchars[0] = '\n';
378 trimchars[1] = ' ';
379 trimchars[2] = '\t';
380 trimchars[3] = 0;
381
382 #ifdef __WINDOWS__
383 const char SEP = '\\';
384 #else
385 const char SEP = '/';
386 #endif
387 buf[0] = '\0';
388 if (name == NULL || *name == '\0')
389 return buf;
390 nm = copystring(name); // Make a scratch copy
391 char *nm_tmp = nm;
392
393 /* Skip leading whitespace and cr */
394 while (strchr((char *)trimchars, *nm) != NULL)
395 nm++;
396 /* And strip off trailing whitespace and cr */
397 s = nm + (q = strlen(nm)) - 1;
398 while (q-- && strchr((char *)trimchars, *s) != NULL)
399 *s = '\0';
400
401 s = nm;
402 d = lnm;
403 #ifdef __WINDOWS__
404 q = FALSE;
405 #else
406 q = nm[0] == '\\' && nm[1] == '~';
407 #endif
408
409 /* Expand inline environment variables */
410 while ((*d++ = *s)) {
411 #ifndef __WINDOWS__
412 if (*s == '\\') {
413 if ((*(d - 1) = *++s)) {
414 s++;
415 continue;
416 } else
417 break;
418 } else
419 #endif
420 #ifdef __WINDOWS__
421 if (*s++ == '$' && (*s == '{' || *s == ')'))
422 #else
423 if (*s++ == '$')
424 #endif
425 {
426 register char *start = d;
427 register braces = (*s == '{' || *s == '(');
428 register char *value;
429 while ((*d++ = *s))
430 if (braces ? (*s == '}' || *s == ')') : !(isalnum(*s) || *s == '_') )
431 break;
432 else
433 s++;
434 *--d = 0;
435 value = getenv(braces ? start + 1 : start);
436 if (value) {
437 for ((d = start - 1); (*d++ = *value++););
438 d--;
439 if (braces && *s)
440 s++;
441 }
442 }
443 }
444
445 /* Expand ~ and ~user */
446 nm = lnm;
447 s = "";
448 if (nm[0] == '~' && !q)
449 {
450 /* prefix ~ */
451 if (nm[1] == SEP || nm[1] == 0)
452 { /* ~/filename */
453 if ((s = wxGetUserHome("")) != NULL) {
454 if (*++nm)
455 nm++;
456 }
457 } else
458 { /* ~user/filename */
459 register char *nnm;
460 register char *home;
461 for (s = nm; *s && *s != SEP; s++);
462 int was_sep; /* MATTHEW: Was there a separator, or NULL? */
463 was_sep = (*s == SEP);
464 nnm = *s ? s + 1 : s;
465 *s = 0;
466 if ((home = wxGetUserHome(wxString(nm + 1))) == NULL) {
467 if (was_sep) /* replace only if it was there: */
468 *s = SEP;
469 s = "";
470 } else {
471 nm = nnm;
472 s = home;
473 }
474 }
475 }
476
477 d = buf;
478 if (s && *s) { /* MATTHEW: s could be NULL if user '~' didn't exist */
479 /* Copy home dir */
480 while ('\0' != (*d++ = *s++))
481 /* loop */;
482 // Handle root home
483 if (d - 1 > buf && *(d - 2) != SEP)
484 *(d - 1) = SEP;
485 }
486 s = nm;
487 while ((*d++ = *s++));
488
489 delete[] nm_tmp; // clean up alloc
490 /* Now clean up the buffer */
491 return wxRealPath(buf);
492 }
493
494
495 /* Contract Paths to be build upon an environment variable
496 component:
497
498 example: "/usr/openwin/lib", OPENWINHOME --> ${OPENWINHOME}/lib
499
500 The call wxExpandPath can convert these back!
501 */
502 char *
503 wxContractPath (const wxString& filename, const wxString& envname, const wxString& user)
504 {
505 static char dest[_MAXPATHLEN];
506
507 if (filename == "")
508 return NULL;
509
510 strcpy (dest, WXSTRINGCAST filename);
511 #ifdef __WINDOWS__
512 Unix2DosFilename(dest);
513 #endif
514
515 // Handle environment
516 char *val = NULL;
517 char *tcp = NULL;
518 if (envname != NULL && (val = getenv (WXSTRINGCAST envname)) != NULL &&
519 (tcp = strstr (dest, val)) != NULL)
520 {
521 strcpy (wxBuffer, tcp + strlen (val));
522 *tcp++ = '$';
523 *tcp++ = '{';
524 strcpy (tcp, WXSTRINGCAST envname);
525 strcat (tcp, "}");
526 strcat (tcp, wxBuffer);
527 }
528
529 // Handle User's home (ignore root homes!)
530 size_t len = 0;
531 if ((val = wxGetUserHome (user)) != NULL &&
532 (len = strlen(val)) > 2 &&
533 strncmp(dest, val, len) == 0)
534 {
535 strcpy(wxBuffer, "~");
536 if (user && *user)
537 strcat(wxBuffer, user);
538 #ifdef __WINDOWS__
539 // strcat(wxBuffer, "\\");
540 #else
541 // strcat(wxBuffer, "/");
542 #endif
543 strcat(wxBuffer, dest + len);
544 strcpy (dest, wxBuffer);
545 }
546
547 return dest;
548 }
549
550 // Return just the filename, not the path
551 // (basename)
552 char *wxFileNameFromPath (char *path)
553 {
554 if (path)
555 {
556 register char *tcp;
557
558 tcp = path + strlen (path);
559 while (--tcp >= path)
560 {
561 if (*tcp == '/' || *tcp == '\\'
562 #ifdef __VMS__
563 || *tcp == ':' || *tcp == ']')
564 #else
565 )
566 #endif
567 return tcp + 1;
568 } /* while */
569 #ifdef __WINDOWS__
570 if (isalpha (*path) && *(path + 1) == ':')
571 return path + 2;
572 #endif
573 }
574 return path;
575 }
576
577 wxString wxFileNameFromPath (const wxString& path1)
578 {
579 if (path1 != "")
580 {
581
582 char *path = WXSTRINGCAST path1 ;
583 register char *tcp;
584
585 tcp = path + strlen (path);
586 while (--tcp >= path)
587 {
588 if (*tcp == '/' || *tcp == '\\'
589 #ifdef __VMS__
590 || *tcp == ':' || *tcp == ']')
591 #else
592 )
593 #endif
594 return wxString(tcp + 1);
595 } /* while */
596 #ifdef __WINDOWS__
597 if (isalpha (*path) && *(path + 1) == ':')
598 return wxString(path + 2);
599 #endif
600 }
601 return wxString("");
602 }
603
604 // Return just the directory, or NULL if no directory
605 char *
606 wxPathOnly (char *path)
607 {
608 if (path && *path)
609 {
610 static char buf[_MAXPATHLEN];
611
612 // Local copy
613 strcpy (buf, path);
614
615 int l = strlen(path);
616 bool done = FALSE;
617
618 int i = l - 1;
619
620 // Search backward for a backward or forward slash
621 while (!done && i > -1)
622 {
623 // ] is for VMS
624 if (path[i] == '/' || path[i] == '\\' || path[i] == ']')
625 {
626 done = TRUE;
627 #ifdef __VMS__
628 buf[i+1] = 0;
629 #else
630 buf[i] = 0;
631 #endif
632
633 return buf;
634 }
635 else i --;
636 }
637
638 #ifdef __WINDOWS__
639 // Try Drive specifier
640 if (isalpha (buf[0]) && buf[1] == ':')
641 {
642 // A:junk --> A:. (since A:.\junk Not A:\junk)
643 buf[2] = '.';
644 buf[3] = '\0';
645 return buf;
646 }
647 #endif
648 }
649
650 return NULL;
651 }
652
653 // Return just the directory, or NULL if no directory
654 wxString wxPathOnly (const wxString& path)
655 {
656 if (path != "")
657 {
658 char buf[_MAXPATHLEN];
659
660 // Local copy
661 strcpy (buf, WXSTRINGCAST path);
662
663 int l = path.Length();
664 bool done = FALSE;
665
666 int i = l - 1;
667
668 // Search backward for a backward or forward slash
669 while (!done && i > -1)
670 {
671 // ] is for VMS
672 if (path[i] == '/' || path[i] == '\\' || path[i] == ']')
673 {
674 done = TRUE;
675 #ifdef __VMS__
676 buf[i+1] = 0;
677 #else
678 buf[i] = 0;
679 #endif
680
681 return wxString(buf);
682 }
683 else i --;
684 }
685
686 #ifdef __WINDOWS__
687 // Try Drive specifier
688 if (isalpha (buf[0]) && buf[1] == ':')
689 {
690 // A:junk --> A:. (since A:.\junk Not A:\junk)
691 buf[2] = '.';
692 buf[3] = '\0';
693 return wxString(buf);
694 }
695 #endif
696 }
697
698 return wxString("");
699 }
700
701 // Utility for converting delimiters in DOS filenames to UNIX style
702 // and back again - or we get nasty problems with delimiters.
703 // Also, convert to lower case, since case is significant in UNIX.
704
705 void
706 wxDos2UnixFilename (char *s)
707 {
708 if (s)
709 while (*s)
710 {
711 if (*s == '\\')
712 *s = '/';
713 #ifdef __WINDOWS__
714 else
715 *s = wxToLower (*s); // Case INDEPENDENT
716 #endif
717 s++;
718 }
719 }
720
721 void
722 wxUnix2DosFilename (char *WXUNUSED(s))
723 {
724 // Yes, I really mean this to happen under DOS only! JACS
725 #ifdef __WINDOWS__
726 if (s)
727 while (*s)
728 {
729 if (*s == '/')
730 *s = '\\';
731 s++;
732 }
733 #endif
734 }
735
736 // Concatenate two files to form third
737 bool
738 wxConcatFiles (const wxString& file1, const wxString& file2, const wxString& file3)
739 {
740 char *outfile = wxGetTempFileName("cat");
741
742 FILE *fp1 = NULL;
743 FILE *fp2 = NULL;
744 FILE *fp3 = NULL;
745 // Open the inputs and outputs
746 if ((fp1 = fopen (WXSTRINGCAST file1, "rb")) == NULL ||
747 (fp2 = fopen (WXSTRINGCAST file2, "rb")) == NULL ||
748 (fp3 = fopen (outfile, "wb")) == NULL)
749 {
750 if (fp1)
751 fclose (fp1);
752 if (fp2)
753 fclose (fp2);
754 if (fp3)
755 fclose (fp3);
756 return FALSE;
757 }
758
759 int ch;
760 while ((ch = getc (fp1)) != EOF)
761 (void) putc (ch, fp3);
762 fclose (fp1);
763
764 while ((ch = getc (fp2)) != EOF)
765 (void) putc (ch, fp3);
766 fclose (fp2);
767
768 fclose (fp3);
769 bool result = wxRenameFile(outfile, file3);
770 delete[] outfile;
771 return result;
772 }
773
774 // Copy files
775 bool
776 wxCopyFile (const wxString& file1, const wxString& file2)
777 {
778 FILE *fd1;
779 FILE *fd2;
780 int ch;
781
782 if ((fd1 = fopen (WXSTRINGCAST file1, "rb")) == NULL)
783 return FALSE;
784 if ((fd2 = fopen (WXSTRINGCAST file2, "wb")) == NULL)
785 {
786 fclose (fd1);
787 return FALSE;
788 }
789
790 while ((ch = getc (fd1)) != EOF)
791 (void) putc (ch, fd2);
792
793 fclose (fd1);
794 fclose (fd2);
795 return TRUE;
796 }
797
798 bool
799 wxRenameFile (const wxString& file1, const wxString& file2)
800 {
801 // Normal system call
802 if (0 == rename (WXSTRINGCAST file1, WXSTRINGCAST file2))
803 return TRUE;
804 // Try to copy
805 if (wxCopyFile(file1, file2)) {
806 wxRemoveFile(file1);
807 return TRUE;
808 }
809 // Give up
810 return FALSE;
811 }
812
813 bool wxRemoveFile(const wxString& file)
814 {
815 #if defined(_MSC_VER) || defined(__BORLANDC__)
816 int flag = remove(WXSTRINGCAST file);
817 #else
818 int flag = unlink(WXSTRINGCAST file);
819 #endif
820 return (flag == 0) ;
821 }
822
823 bool wxMkdir(const wxString& dir)
824 {
825 #ifdef __VMS__
826 return FALSE;
827 #elif (defined(__GNUWIN32__) && !defined(__MINGW32__)) || !defined(__WINDOWS__)
828 return (mkdir (WXSTRINGCAST dir, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == 0);
829 #else
830 return (mkdir(WXSTRINGCAST dir) == 0);
831 #endif
832 }
833
834 bool wxRmdir(const wxString& dir, int WXUNUSED(flags))
835 {
836 #ifdef __VMS__
837 return FALSE;
838 #else
839 return (rmdir(WXSTRINGCAST dir) == 0);
840 #endif
841 }
842
843 #if 0
844 bool wxDirExists(const wxString& dir)
845 {
846 #ifdef __VMS__
847 return FALSE;
848 #elif !defined(__WINDOWS__)
849 struct stat sbuf;
850 return (stat(dir, &sbuf) != -1) && S_ISDIR(sbuf.st_mode) ? TRUE : FALSE;
851 #else
852
853 /* MATTHEW: [6] Always use same code for Win32, call FindClose */
854 #if defined(__WIN32__)
855 WIN32_FIND_DATA fileInfo;
856 #else
857 #ifdef __BORLANDC__
858 struct ffblk fileInfo;
859 #else
860 struct find_t fileInfo;
861 #endif
862 #endif
863
864 #if defined(__WIN32__)
865 HANDLE h = FindFirstFile((LPTSTR) WXSTRINGCAST dir,(LPWIN32_FIND_DATA)&fileInfo);
866
867 if (h==INVALID_HANDLE_VALUE)
868 return FALSE;
869 else {
870 FindClose(h);
871 return ((fileInfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY);
872 }
873 #else
874 // In Borland findfirst has a different argument
875 // ordering from _dos_findfirst. But _dos_findfirst
876 // _should_ be ok in both MS and Borland... why not?
877 #ifdef __BORLANDC__
878 return ((findfirst(WXSTRINGCAST dir, &fileInfo, _A_SUBDIR) == 0 && (fileInfo.ff_attrib & _A_SUBDIR) != 0));
879 #else
880 return (((_dos_findfirst(WXSTRINGCAST dir, _A_SUBDIR, &fileInfo) == 0) && (fileInfo.attrib & _A_SUBDIR)) != 0);
881 #endif
882 #endif
883
884 #endif
885 }
886
887 #endif
888
889 // does the path exists? (may have or not '/' or '\\' at the end)
890 bool wxPathExists(const char *pszPathName)
891 {
892 // Windows API returns -1 from stat for "c:\dir\" if "c:\dir" exists
893 // OTOH, we should change "d:" to "d:\" and leave "\" as is.
894 wxString strPath(pszPathName);
895 if ( wxEndsWithPathSeparator(pszPathName) && pszPathName[1] != '\0' )
896 strPath.Last() = '\0';
897
898 struct stat st;
899 return stat(strPath, &st) == 0 && (st.st_mode & S_IFDIR);
900 }
901
902 // Get a temporary filename, opening and closing the file.
903 char *wxGetTempFileName(const wxString& prefix, char *buf)
904 {
905 #ifdef __WINDOWS__
906
907 #ifndef __WIN32__
908 char tmp[144];
909 ::GetTempFileName(0, WXSTRINGCAST prefix, 0, tmp);
910 #else
911 char tmp[MAX_PATH];
912 char tmpPath[MAX_PATH];
913 ::GetTempPath(MAX_PATH, tmpPath);
914 ::GetTempFileName(tmpPath, WXSTRINGCAST prefix, 0, tmp);
915 #endif
916 if (buf) strcpy(buf, tmp);
917 else buf = copystring(tmp);
918 return buf;
919
920 #else
921 static short last_temp = 0; // cache last to speed things a bit
922 // At most 1000 temp files to a process! We use a ring count.
923 char tmp[100];
924
925 for (short suffix = last_temp + 1; suffix != last_temp; ++suffix %= 1000)
926 {
927 sprintf (tmp, "/tmp/%s%d.%03x", WXSTRINGCAST prefix, (int) getpid (), (int) suffix);
928 if (!wxFileExists( tmp ))
929 {
930 // Touch the file to create it (reserve name)
931 FILE *fd = fopen (tmp, "w");
932 if (fd)
933 fclose (fd);
934 last_temp = suffix;
935 if (buf)
936 strcpy( buf, tmp);
937 else
938 buf = copystring( tmp );
939 return buf;
940 }
941 }
942 cerr << "wxWindows: error finding temporary file name.\n";
943 if (buf) buf[0] = 0;
944 return NULL;
945 #endif
946 }
947
948 // Get first file name matching given wild card.
949
950 #ifdef __UNIX__
951
952 // Get first file name matching given wild card.
953 // Flags are reserved for future use.
954
955 #ifndef __VMS__
956 static DIR *wxDirStream = NULL;
957 static char *wxFileSpec = NULL;
958 static int wxFindFileFlags = 0;
959 #endif
960
961 char *wxFindFirstFile(const char *spec, int flags)
962 {
963 #ifndef __VMS__
964 if (wxDirStream)
965 closedir(wxDirStream); // edz 941103: better housekeping
966
967 wxFindFileFlags = flags;
968
969 if (wxFileSpec)
970 delete[] wxFileSpec;
971 wxFileSpec = copystring(spec);
972
973 // Find path only so we can concatenate
974 // found file onto path
975 char *p = wxPathOnly(wxFileSpec);
976
977 /* MATTHEW: special case: path is really "/" */
978 if (p && !*p && *wxFileSpec == '/')
979 p = "/";
980 /* MATTHEW: p is NULL => Local directory */
981 if (!p)
982 p = ".";
983
984 if ((wxDirStream=opendir(p))==NULL)
985 return NULL;
986
987 /* MATTHEW: [5] wxFindNextFile can do the rest of the work */
988 return wxFindNextFile();
989 #endif
990 // ifndef __VMS__
991 return NULL;
992 }
993
994 char *wxFindNextFile(void)
995 {
996 #ifndef __VMS__
997 static char buf[400];
998
999 /* MATTHEW: [2] Don't crash if we read too many times */
1000 if (!wxDirStream)
1001 return NULL;
1002
1003 // Find path only so we can concatenate
1004 // found file onto path
1005 char *p = wxPathOnly(wxFileSpec);
1006 char *n = wxFileNameFromPath(wxFileSpec);
1007
1008 /* MATTHEW: special case: path is really "/" */
1009 if (p && !*p && *wxFileSpec == '/')
1010 p = "/";
1011
1012 // Do the reading
1013 struct dirent *nextDir;
1014 for (nextDir = readdir(wxDirStream); nextDir != NULL; nextDir = readdir(wxDirStream))
1015 {
1016
1017 /* MATTHEW: [5] Only return "." and ".." when they match, and only return
1018 directories when flags & wxDIR */
1019 if (wxMatchWild(n, nextDir->d_name)) {
1020 bool isdir;
1021
1022 buf[0] = 0;
1023 if (p && *p) {
1024 strcpy(buf, p);
1025 if (strcmp(p, "/") != 0)
1026 strcat(buf, "/");
1027 }
1028 strcat(buf, nextDir->d_name);
1029
1030 if ((strcmp(nextDir->d_name, ".") == 0) ||
1031 (strcmp(nextDir->d_name, "..") == 0)) {
1032 if (wxFindFileFlags && !(wxFindFileFlags & wxDIR))
1033 continue;
1034 isdir = TRUE;
1035 } else
1036 isdir = wxDirExists(buf);
1037
1038 if (!wxFindFileFlags
1039 || ((wxFindFileFlags & wxDIR) && isdir)
1040 || ((wxFindFileFlags & wxFILE) && !isdir))
1041 return buf;
1042 }
1043 }
1044 closedir(wxDirStream);
1045 wxDirStream = NULL;
1046 #endif
1047 // ifndef __VMS__
1048
1049 return NULL;
1050 }
1051
1052 #elif defined(__WINDOWS__)
1053
1054 #ifdef __WIN32__
1055 HANDLE wxFileStrucHandle = INVALID_HANDLE_VALUE;
1056 WIN32_FIND_DATA wxFileStruc;
1057 #else
1058 #ifdef __BORLANDC__
1059 static struct ffblk wxFileStruc;
1060 #else
1061 static struct _find_t wxFileStruc;
1062 #endif
1063 #endif
1064 static wxString wxFileSpec = "";
1065 static int wxFindFileFlags;
1066
1067 char *wxFindFirstFile(const wxString& spec, int flags)
1068 {
1069 wxFileSpec = spec;
1070 wxFindFileFlags = flags; /* MATTHEW: [5] Remember flags */
1071
1072 // Find path only so we can concatenate
1073 // found file onto path
1074 wxString path1(wxFileSpec);
1075 char *p = wxPathOnly(WXSTRINGCAST path1);
1076 if (p && (strlen(p) > 0))
1077 strcpy(wxBuffer, p);
1078 else
1079 wxBuffer[0] = 0;
1080
1081 #ifdef __WIN32__
1082 if (wxFileStrucHandle != INVALID_HANDLE_VALUE)
1083 FindClose(wxFileStrucHandle);
1084
1085 wxFileStrucHandle = ::FindFirstFile(WXSTRINGCAST spec, &wxFileStruc);
1086
1087 if (wxFileStrucHandle == INVALID_HANDLE_VALUE)
1088 return NULL;
1089
1090 bool isdir = !!(wxFileStruc.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
1091
1092 if (isdir && !(flags & wxDIR))
1093 return wxFindNextFile();
1094 else if (!isdir && flags && !(flags & wxFILE))
1095 return wxFindNextFile();
1096
1097 if (wxBuffer[0] != 0)
1098 strcat(wxBuffer, "\\");
1099 strcat(wxBuffer, wxFileStruc.cFileName);
1100 return wxBuffer;
1101 #else
1102
1103 int flag = _A_NORMAL;
1104 if (flags & wxDIR) /* MATTHEW: [5] Use & */
1105 flag = _A_SUBDIR;
1106
1107 #ifdef __BORLANDC__
1108 if (findfirst(WXSTRINGCAST spec, &wxFileStruc, flag) == 0)
1109 #else
1110 if (_dos_findfirst(WXSTRINGCAST spec, flag, &wxFileStruc) == 0)
1111 #endif
1112 {
1113 /* MATTHEW: [5] Check directory flag */
1114 char attrib;
1115
1116 #ifdef __BORLANDC__
1117 attrib = wxFileStruc.ff_attrib;
1118 #else
1119 attrib = wxFileStruc.attrib;
1120 #endif
1121
1122 if (attrib & _A_SUBDIR) {
1123 if (!(wxFindFileFlags & wxDIR))
1124 return wxFindNextFile();
1125 } else if (wxFindFileFlags && !(wxFindFileFlags & wxFILE))
1126 return wxFindNextFile();
1127
1128 if (wxBuffer[0] != 0)
1129 strcat(wxBuffer, "\\");
1130
1131 #ifdef __BORLANDC__
1132 strcat(wxBuffer, wxFileStruc.ff_name);
1133 #else
1134 strcat(wxBuffer, wxFileStruc.name);
1135 #endif
1136 return wxBuffer;
1137 }
1138 else
1139 return NULL;
1140 #endif // __WIN32__
1141 }
1142
1143 char *wxFindNextFile(void)
1144 {
1145 // Find path only so we can concatenate
1146 // found file onto path
1147 wxString p2(wxFileSpec);
1148 char *p = wxPathOnly(WXSTRINGCAST p2);
1149 if (p && (strlen(p) > 0))
1150 strcpy(wxBuffer, p);
1151 else
1152 wxBuffer[0] = 0;
1153
1154 try_again:
1155
1156 #ifdef __WIN32__
1157 if (wxFileStrucHandle == INVALID_HANDLE_VALUE)
1158 return NULL;
1159
1160 bool success = (FindNextFile(wxFileStrucHandle, &wxFileStruc) != 0);
1161 if (!success) {
1162 FindClose(wxFileStrucHandle);
1163 wxFileStrucHandle = INVALID_HANDLE_VALUE;
1164 return NULL;
1165 }
1166
1167 bool isdir = !!(wxFileStruc.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
1168
1169 if (isdir && !(wxFindFileFlags & wxDIR))
1170 goto try_again;
1171 else if (!isdir && wxFindFileFlags && !(wxFindFileFlags & wxFILE))
1172 goto try_again;
1173
1174 if (wxBuffer[0] != 0)
1175 strcat(wxBuffer, "\\");
1176 strcat(wxBuffer, wxFileStruc.cFileName);
1177 return wxBuffer;
1178 #else
1179
1180 #ifdef __BORLANDC__
1181 if (findnext(&wxFileStruc) == 0)
1182 #else
1183 if (_dos_findnext(&wxFileStruc) == 0)
1184 #endif
1185 {
1186 /* MATTHEW: [5] Check directory flag */
1187 char attrib;
1188
1189 #ifdef __BORLANDC__
1190 attrib = wxFileStruc.ff_attrib;
1191 #else
1192 attrib = wxFileStruc.attrib;
1193 #endif
1194
1195 if (attrib & _A_SUBDIR) {
1196 if (!(wxFindFileFlags & wxDIR))
1197 goto try_again;
1198 } else if (wxFindFileFlags && !(wxFindFileFlags & wxFILE))
1199 goto try_again;
1200
1201
1202 if (wxBuffer[0] != 0)
1203 strcat(wxBuffer, "\\");
1204 #ifdef __BORLANDC__
1205 strcat(wxBuffer, wxFileStruc.ff_name);
1206 #else
1207 strcat(wxBuffer, wxFileStruc.name);
1208 #endif
1209 return wxBuffer;
1210 }
1211 else
1212 return NULL;
1213 #endif
1214 }
1215
1216 #endif
1217 // __WINDOWS__
1218
1219 // Get current working directory.
1220 // If buf is NULL, allocates space using new, else
1221 // copies into buf.
1222 char *wxGetWorkingDirectory(char *buf, int sz)
1223 {
1224 if (!buf)
1225 buf = new char[sz+1];
1226 #ifdef _MSC_VER
1227 if (_getcwd(buf, sz) == NULL) {
1228 #else
1229 if (getcwd(buf, sz) == NULL) {
1230 #endif
1231 buf[0] = '.';
1232 buf[1] = '\0';
1233 }
1234 return buf;
1235 }
1236
1237 bool wxSetWorkingDirectory(const wxString& d)
1238 {
1239 #ifdef __UNIX__
1240 return (chdir(d) == 0);
1241 #elif defined(__WINDOWS__)
1242
1243 #ifdef __WIN32__
1244 return (bool)(SetCurrentDirectory(d) != 0);
1245 #else
1246 // Must change drive, too.
1247 bool isDriveSpec = ((strlen(d) > 1) && (d[1] == ':'));
1248 if (isDriveSpec)
1249 {
1250 char firstChar = d[0];
1251
1252 // To upper case
1253 if (firstChar > 90)
1254 firstChar = firstChar - 32;
1255
1256 // To a drive number
1257 unsigned int driveNo = firstChar - 64;
1258 if (driveNo > 0)
1259 {
1260 unsigned int noDrives;
1261 _dos_setdrive(driveNo, &noDrives);
1262 }
1263 }
1264 bool success = (chdir(WXSTRINGCAST d) == 0);
1265
1266 return success;
1267 #endif
1268
1269 #endif
1270 }
1271
1272 bool wxEndsWithPathSeparator(const char *pszFileName)
1273 {
1274 size_t len = Strlen(pszFileName);
1275 if ( len == 0 )
1276 return FALSE;
1277 else
1278 return wxIsPathSeparator(pszFileName[len - 1]);
1279 }
1280
1281 // find a file in a list of directories, returns false if not found
1282 bool wxFindFileInPath(wxString *pStr, const char *pszPath, const char *pszFile)
1283 {
1284 // we assume that it's not empty
1285 wxCHECK_RET( Strlen(pszFile) != 0, FALSE);
1286
1287 // skip path separator in the beginning of the file name if present
1288 if ( wxIsPathSeparator(*pszFile) )
1289 pszFile++;
1290
1291 // copy the path (strtok will modify it)
1292 char *szPath = new char[strlen(pszPath) + 1];
1293 strcpy(szPath, pszPath);
1294
1295 wxString strFile;
1296 char *pc;
1297 for ( pc = strtok(szPath, PATH_SEP); pc; pc = strtok(NULL, PATH_SEP) ) {
1298 // search for the file in this directory
1299 strFile = pc;
1300 if ( !wxEndsWithPathSeparator(pc) )
1301 strFile += FILE_SEP_PATH;
1302 strFile += pszFile;
1303
1304 if ( FileExists(strFile) ) {
1305 *pStr = strFile;
1306 break;
1307 }
1308 }
1309
1310 delete [] szPath;
1311
1312 return pc != NULL; // if true => we breaked from the loop
1313 }
1314