]> git.saurik.com Git - wxWidgets.git/blob - src/common/utilscmn.cpp
wxProcess fixes (Detach() added), cleared/corrected wxExecute() documentation
[wxWidgets.git] / src / common / utilscmn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: utilscmn.cpp
3 // Purpose: Miscellaneous utility functions and classes
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 "utils.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef WX_PRECOMP
24 #include "wx/defs.h"
25 #include "wx/utils.h"
26 #include "wx/window.h"
27 #include "wx/menu.h"
28 #include "wx/frame.h"
29 #include "wx/msgdlg.h"
30 #include "wx/textdlg.h"
31 #endif
32
33 #include "wx/ioswrap.h"
34
35 #if wxUSE_IOSTREAMH
36 #include <fstream.h>
37 #else
38 #include <fstream>
39 #endif
40
41 #include <ctype.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #if !defined(__WATCOMC__)
46 #if !(defined(_MSC_VER) && (_MSC_VER > 800))
47 #include <errno.h>
48 #endif
49 #endif
50 #include <time.h>
51 #ifndef __MWERKS__
52 #include <sys/types.h>
53 #include <sys/stat.h>
54 #endif
55
56 #ifdef __SALFORDC__
57 #include <clib.h>
58 #endif
59
60 // Pattern matching code.
61 // Yes, this path is deliberate (for Borland compilation)
62 #ifdef wx_mac /* MATTHEW: [5] Mac doesn't like paths with "/" */
63 #include "glob.inc"
64 #else
65 #include "../common/glob.inc"
66 #endif
67
68 #ifdef __WXMSW__
69 #include "windows.h"
70 #endif
71
72 #define _MAXPATHLEN 500
73
74 extern char *wxBuffer;
75
76 #ifdef __WXMAC__
77 int strcasecmp(const char *str_1, const char *str_2)
78 {
79 register char c1, c2;
80 do {
81 c1 = tolower(*str_1++);
82 c2 = tolower(*str_2++);
83 } while ( c1 && (c1 == c2) );
84
85 return c1 - c2;
86 }
87
88 int strncasecmp(const char *str_1, const char *str_2, size_t maxchar)
89 {
90
91 register char c1, c2;
92 while( maxchar--)
93 {
94 c1 = tolower(*str_1++);
95 c2 = tolower(*str_2++);
96
97 if ( !c1 || c1!=c2 )
98 return c1 - c2;
99
100 } ;
101
102 return 0 ;
103
104 }
105 #endif
106 #ifdef __VMS__
107 // we have no strI functions under VMS, therefore I have implemented
108 // an inefficient but portable version: convert copies of strings to lowercase
109 // and then use the normal comparison
110 static void myLowerString(char *s)
111 {
112 while(*s){
113 if(isalpha(*s)) *s = (char)tolower(*s);
114 s++;
115 }
116 }
117
118 int strcasecmp(const char *str_1, const char *str_2)
119 {
120 char *temp1 = new char[strlen(str_1)+1];
121 char *temp2 = new char[strlen(str_2)+1];
122 strcpy(temp1,str_1);
123 strcpy(temp2,str_2);
124 myLowerString(temp1);
125 myLowerString(temp2);
126
127 int result = strcmp(temp1,temp2);
128 delete[] temp1;
129 delete[] temp2;
130
131 return(result);
132 }
133
134 int strncasecmp(const char *str_1, const char *str_2, size_t maxchar)
135 {
136 char *temp1 = new char[strlen(str_1)+1];
137 char *temp2 = new char[strlen(str_2)+1];
138 strcpy(temp1,str_1);
139 strcpy(temp2,str_2);
140 myLowerString(temp1);
141 myLowerString(temp2);
142
143 int result = strncmp(temp1,temp2,maxchar);
144 delete[] temp1;
145 delete[] temp2;
146
147 return(result);
148 }
149 #endif
150
151 #ifdef __WINDOWS__
152
153 #ifndef __GNUWIN32__
154 #ifndef __MWERKS__
155 #define strcasecmp stricmp
156 #define strncasecmp strnicmp
157 #else
158 #define strcasecmp _stricmp
159 #define strncasecmp _strnicmp
160 #endif
161 #endif
162
163 #else
164 // This declaration is missing in SunOS!
165 // (Yes, I know it is NOT ANSI-C but its in BSD libc)
166 #if defined(__xlC) || defined(__AIX__) || defined(__GNUG__)
167 extern "C"
168 {
169 int strcasecmp (const char *, const char *);
170 int strncasecmp (const char *, const char *, size_t);
171 }
172 #endif
173 #endif /* __WXMSW__ */
174
175
176 char *
177 copystring (const char *s)
178 {
179 if (s == NULL) s = "";
180 size_t len = strlen (s) + 1;
181
182 char *news = new char[len];
183 memcpy (news, s, len); // Should be the fastest
184
185 return news;
186 }
187
188 // Id generation
189 static long wxCurrentId = 100;
190
191 long
192 wxNewId (void)
193 {
194 return wxCurrentId++;
195 }
196
197 long
198 wxGetCurrentId(void) { return wxCurrentId; }
199
200 void
201 wxRegisterId (long id)
202 {
203 if (id >= wxCurrentId)
204 wxCurrentId = id + 1;
205 }
206
207 void
208 StringToFloat (char *s, float *number)
209 {
210 if (s && *s && number)
211 *number = (float) strtod (s, (char **) NULL);
212 }
213
214 void
215 StringToDouble (char *s, double *number)
216 {
217 if (s && *s && number)
218 *number = strtod (s, (char **) NULL);
219 }
220
221 char *
222 FloatToString (float number, const char *fmt)
223 {
224 static char buf[256];
225
226 // sprintf (buf, "%.2f", number);
227 sprintf (buf, fmt, number);
228 return buf;
229 }
230
231 char *
232 DoubleToString (double number, const char *fmt)
233 {
234 static char buf[256];
235
236 sprintf (buf, fmt, number);
237 return buf;
238 }
239
240 void
241 StringToInt (char *s, int *number)
242 {
243 if (s && *s && number)
244 *number = (int) strtol (s, (char **) NULL, 10);
245 }
246
247 void
248 StringToLong (char *s, long *number)
249 {
250 if (s && *s && number)
251 *number = strtol (s, (char **) NULL, 10);
252 }
253
254 char *
255 IntToString (int number)
256 {
257 static char buf[20];
258
259 sprintf (buf, "%d", number);
260 return buf;
261 }
262
263 char *
264 LongToString (long number)
265 {
266 static char buf[20];
267
268 sprintf (buf, "%ld", number);
269 return buf;
270 }
271
272 // Array used in DecToHex conversion routine.
273 static char hexArray[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B',
274 'C', 'D', 'E', 'F' };
275
276 // Convert 2-digit hex number to decimal
277 int wxHexToDec(const wxString& buf)
278 {
279 int firstDigit, secondDigit;
280
281 if (buf.GetChar(0) >= 'A')
282 firstDigit = buf.GetChar(0) - 'A' + 10;
283 else
284 firstDigit = buf.GetChar(0) - '0';
285
286 if (buf.GetChar(1) >= 'A')
287 secondDigit = buf.GetChar(1) - 'A' + 10;
288 else
289 secondDigit = buf.GetChar(1) - '0';
290
291 return firstDigit * 16 + secondDigit;
292 }
293
294 // Convert decimal integer to 2-character hex string
295 void wxDecToHex(int dec, char *buf)
296 {
297 int firstDigit = (int)(dec/16.0);
298 int secondDigit = (int)(dec - (firstDigit*16.0));
299 buf[0] = hexArray[firstDigit];
300 buf[1] = hexArray[secondDigit];
301 buf[2] = 0;
302 }
303
304 // Convert decimal integer to 2-character hex string
305 wxString wxDecToHex(int dec)
306 {
307 char buf[3];
308 wxDecToHex(dec, buf);
309 return wxString(buf);
310 }
311
312 // Match a string INDEPENDENT OF CASE
313 bool
314 StringMatch (char *str1, char *str2, bool subString, bool exact)
315 {
316 if (str1 == NULL || str2 == NULL)
317 return FALSE;
318 if (str1 == str2)
319 return TRUE;
320
321 if (subString)
322 {
323 int len1 = strlen (str1);
324 int len2 = strlen (str2);
325 int i;
326
327 // Search for str1 in str2
328 // Slow .... but acceptable for short strings
329 for (i = 0; i <= len2 - len1; i++)
330 {
331 if (strncasecmp (str1, str2 + i, len1) == 0)
332 return TRUE;
333 }
334 }
335 else if (exact)
336 {
337 if (strcasecmp (str1, str2) == 0)
338 return TRUE;
339 }
340 else
341 {
342 int len1 = strlen (str1);
343 int len2 = strlen (str2);
344
345 if (strncasecmp (str1, str2, wxMin (len1, len2)) == 0)
346 return TRUE;
347 }
348
349 return FALSE;
350 }
351
352 // Return the current date/time
353 // [volatile]
354 wxString wxNow( void )
355 {
356 time_t now = time((time_t *) NULL);
357 char *date = ctime(&now);
358 date[24] = '\0';
359 return wxString(date);
360 }
361
362 /* Get Full RFC822 style email address */
363 bool
364 wxGetEmailAddress (char *address, int maxSize)
365 {
366 char host[65];
367 char user[65];
368
369 if (wxGetHostName(host, 64) == FALSE)
370 return FALSE;
371 if (wxGetUserId(user, 64) == FALSE)
372 return FALSE;
373
374 char tmp[130];
375 strcpy(tmp, user);
376 strcat(tmp, "@");
377 strcat(tmp, host);
378
379 strncpy(address, tmp, maxSize - 1);
380 address[maxSize-1] = '\0';
381 return TRUE;
382 }
383
384 /*
385 * Strip out any menu codes
386 */
387
388 char *wxStripMenuCodes (char *in, char *out)
389 {
390 if (!in)
391 return (char *) NULL;
392
393 if (!out)
394 out = copystring(in);
395
396 char *tmpOut = out;
397
398 while (*in)
399 {
400 if (*in == '&')
401 {
402 // Check && -> &, &x -> x
403 if (*++in == '&')
404 *out++ = *in++;
405 }
406 else if (*in == '\t')
407 {
408 // Remove all stuff after \t in X mode, and let the stuff as is
409 // in Windows mode.
410 // Accelerators are handled in wx_item.cc for Motif, and are not
411 // YET supported in XView
412 break;
413 }
414 else
415 *out++ = *in++;
416 } // while
417
418 *out = '\0';
419
420 return tmpOut;
421 }
422
423 wxString wxStripMenuCodes(const wxString& str)
424 {
425 char *buf = new char[str.Length() + 1];
426 wxStripMenuCodes((char*) (const char*) str, buf);
427 wxString str1(buf);
428 delete[] buf;
429 return str1;
430 }
431
432 /*
433 * Window search functions
434 *
435 */
436
437 /*
438 * If parent is non-NULL, look through children for a label or title
439 * matching the specified string. If NULL, look through all top-level windows.
440 *
441 */
442
443 static wxWindow *wxFindWindowByLabel1 (const wxString& title, wxWindow * parent);
444
445 wxWindow *
446 wxFindWindowByLabel (const wxString& title, wxWindow * parent)
447 {
448 if (parent)
449 {
450 return wxFindWindowByLabel1 (title, parent);
451 }
452 else
453 {
454 for (wxNode * node = wxTopLevelWindows.First (); node; node = node->Next ())
455 {
456 wxWindow *win = (wxWindow *) node->Data ();
457 wxWindow *retwin = wxFindWindowByLabel1 (title, win);
458 if (retwin)
459 return retwin;
460 } // for()
461
462 }
463 return (wxWindow *) NULL;
464 }
465
466 // Recursive
467 static wxWindow *
468 wxFindWindowByLabel1 (const wxString& title, wxWindow * parent)
469 {
470 if (parent)
471 {
472 if (parent->GetLabel() == title)
473 return parent;
474 }
475
476 if (parent)
477 {
478 for (wxNode * node = parent->GetChildren().First (); node; node = node->Next ())
479 {
480 wxWindow *win = (wxWindow *) node->Data ();
481 wxWindow *retwin = wxFindWindowByLabel1 (title, win);
482 if (retwin)
483 return retwin;
484 } // for()
485
486 }
487
488 return (wxWindow *) NULL; // Not found
489
490 }
491
492 /*
493 * If parent is non-NULL, look through children for a name
494 * matching the specified string. If NULL, look through all top-level windows.
495 *
496 */
497
498 static wxWindow *wxFindWindowByName1 (const wxString& title, wxWindow * parent);
499
500 wxWindow *
501 wxFindWindowByName (const wxString& title, wxWindow * parent)
502 {
503 if (parent)
504 {
505 return wxFindWindowByName1 (title, parent);
506 }
507 else
508 {
509 for (wxNode * node = wxTopLevelWindows.First (); node; node = node->Next ())
510 {
511 wxWindow *win = (wxWindow *) node->Data ();
512 wxWindow *retwin = wxFindWindowByName1 (title, win);
513 if (retwin)
514 return retwin;
515 } // for()
516
517 }
518 // Failed? Try by label instead.
519 return wxFindWindowByLabel(title, parent);
520 }
521
522 // Recursive
523 static wxWindow *
524 wxFindWindowByName1 (const wxString& title, wxWindow * parent)
525 {
526 if (parent)
527 {
528 if ( parent->GetName() == title )
529 return parent;
530 }
531
532 if (parent)
533 {
534 for (wxNode * node = parent->GetChildren().First (); node; node = node->Next ())
535 {
536 wxWindow *win = (wxWindow *) node->Data ();
537 wxWindow *retwin = wxFindWindowByName1 (title, win);
538 if (retwin)
539 return retwin;
540 } // for()
541
542 }
543
544 return (wxWindow *) NULL; // Not found
545
546 }
547
548 // Returns menu item id or -1 if none.
549 int
550 wxFindMenuItemId (wxFrame * frame, const wxString& menuString, const wxString& itemString)
551 {
552 wxMenuBar *menuBar = frame->GetMenuBar ();
553 if (!menuBar)
554 return -1;
555 return menuBar->FindMenuItem (menuString, itemString);
556 }
557
558 /*
559 On Fri, 21 Jul 1995, Paul Craven wrote:
560
561 > Is there a way to find the path of running program's executable? I can get
562 > my home directory, and the current directory, but I don't know how to get the
563 > executable directory.
564 >
565
566 The code below (warty as it is), does what you want on most Unix,
567 DOS, and Mac platforms (it's from the ALS Prolog main).
568
569 || Ken Bowen Applied Logic Systems, Inc. PO Box 180,
570 ||==== Voice: +1 (617)965-9191 Newton Centre,
571 || FAX: +1 (617)965-1636 MA 02159 USA
572 Email: ken@als.com WWW: http://www.als.com
573 ------------------------------------------------------------------------
574 */
575
576 // This code is commented out but it may be integrated with wxWin at
577 // a later date, after testing. Thanks Ken!
578 #if 0
579
580 /*--------------------------------------------------------------------*
581 | whereami is given a filename f in the form: whereami(argv[0])
582 | It returns the directory in which the executable file (containing
583 | this code [main.c] ) may be found. A dot will be returned to indicate
584 | the current directory.
585 *--------------------------------------------------------------------*/
586
587 static void
588 whereami(name)
589 char *name;
590 {
591 register char *cutoff = NULL; /* stifle -Wall */
592 register char *s;
593 register char *t;
594 int cc;
595 char ebuf[4096];
596
597 /*
598 * See if the file is accessible either through the current directory
599 * or through an absolute path.
600 */
601
602 if (access(name, R_OK) == 0) {
603
604 /*-------------------------------------------------------------*
605 * The file was accessible without any other work. But the current
606 * working directory might change on us, so if it was accessible
607 * through the cwd, then we should get it for later accesses.
608 *-------------------------------------------------------------*/
609
610 t = imagedir;
611 if (!absolute_pathname(name)) {
612 #if defined(DOS) || defined(__WIN32__)
613 int drive;
614 char *newrbuf;
615
616 newrbuf = imagedir;
617 #ifndef __DJGPP__
618 if (*(name + 1) == ':') {
619 if (*name >= 'a' && *name <= 'z')
620 drive = (int) (*name - 'a' + 1);
621 else
622 drive = (int) (*name - 'A' + 1);
623 *newrbuf++ = *name;
624 *newrbuf++ = *(name + 1);
625 *newrbuf++ = DIR_SEPARATOR;
626 }
627 else {
628 drive = 0;
629 *newrbuf++ = DIR_SEPARATOR;
630 }
631 if (getcwd(newrbuf, drive) == 0) { /* } */
632 #else
633 if (getcwd(newrbuf, 1024) == 0) { /* } */
634 #endif
635 #else /* DOS */
636 #ifdef HAVE_GETWD
637 if (getwd(imagedir) == 0) { /* } */
638 #else /* !HAVE_GETWD */
639 if (getcwd(imagedir, 1024) == 0) {
640 #endif /* !HAVE_GETWD */
641 #endif /* DOS */
642 fatal_error(FE_GETCWD, 0);
643 }
644 for (; *t; t++) /* Set t to end of buffer */
645 ;
646 if (*(t - 1) == DIR_SEPARATOR) /* leave slash if already
647 * last char
648 */
649 cutoff = t - 1;
650 else {
651 cutoff = t; /* otherwise put one in */
652 *t++ = DIR_SEPARATOR;
653 }
654 }
655 #if (!defined(__MAC__) && !defined(__DJGPP__) && !defined(__GO32__) && !defined(__WIN32__))
656 else
657 (*t++ = DIR_SEPARATOR);
658 #endif
659
660 /*-------------------------------------------------------------*
661 * Copy the rest of the string and set the cutoff if it was not
662 * already set. If the first character of name is a slash, cutoff
663 * is not presently set but will be on the first iteration of the
664 * loop below.
665 *-------------------------------------------------------------*/
666
667 for ((*name == DIR_SEPARATOR ? (s = name+1) : (s = name));;) {
668 if (*s == DIR_SEPARATOR)
669 cutoff = t;
670 if (!(*t++ = *s++))
671 break;
672 }
673
674 }
675 else {
676
677 /*-------------------------------------------------------------*
678 * Get the path list from the environment. If the path list is
679 * inaccessible for any reason, leave with fatal error.
680 *-------------------------------------------------------------*/
681
682 #ifdef __MAC__
683 if ((s = getenv("Commands")) == (char *) 0)
684 #else
685 if ((s = getenv("PATH")) == (char *) 0)
686 #endif
687 fatal_error(FE_PATH, 0);
688
689 /*
690 * Copy path list into ebuf and set the source pointer to the
691 * beginning of this buffer.
692 */
693
694 strcpy(ebuf, s);
695 s = ebuf;
696
697 for (;;) {
698 t = imagedir;
699 while (*s && *s != PATH_SEPARATOR)
700 *t++ = *s++;
701 if (t > imagedir && *(t - 1) == DIR_SEPARATOR)
702 ; /* do nothing -- slash already is in place */
703 else
704 *t++ = DIR_SEPARATOR; /* put in the slash */
705 cutoff = t - 1; /* set cutoff */
706 strcpy(t, name);
707 if (access(imagedir, R_OK) == 0)
708 break;
709
710 if (*s)
711 s++; /* advance source pointer */
712 else
713 fatal_error(FE_INFND, 0);
714 }
715
716 }
717
718 /*-------------------------------------------------------------*
719 | At this point the full pathname should exist in imagedir and
720 | cutoff should be set to the final slash. We must now determine
721 | whether the file name is a symbolic link or not and chase it down
722 | if it is. Note that we reuse ebuf for getting the link.
723 *-------------------------------------------------------------*/
724
725 #ifdef HAVE_SYMLINK
726 while ((cc = readlink(imagedir, ebuf, 512)) != -1) {
727 ebuf[cc] = 0;
728 s = ebuf;
729 if (*s == DIR_SEPARATOR) {
730 t = imagedir;
731 }
732 else {
733 t = cutoff + 1;
734 }
735 for (;;) {
736 if (*s == DIR_SEPARATOR)
737 cutoff = t; /* mark the last slash seen */
738 if (!(*t++ = *s++)) /* copy the character */
739 break;
740 }
741 }
742
743 #endif /* HAVE_SYMLINK */
744
745 strcpy(imagename, cutoff + 1); /* keep the image name */
746 *(cutoff + 1) = 0; /* chop off the filename part */
747 }
748
749 #endif
750
751 /*
752 * N.B. these convenience functions must be separate from msgdlgg.cpp, textdlgg.cpp
753 * since otherwise the generic code may be pulled in unnecessarily.
754 */
755
756 int wxMessageBox(const wxString& message, const wxString& caption, long style,
757 wxWindow *parent, int WXUNUSED(x), int WXUNUSED(y) )
758 {
759 wxMessageDialog dialog(parent, message, caption, style);
760
761 int ans = dialog.ShowModal();
762 switch ( ans )
763 {
764 case wxID_OK:
765 return wxOK;
766 break;
767 case wxID_YES:
768 return wxYES;
769 break;
770 case wxID_NO:
771 return wxNO;
772 break;
773 default:
774 case wxID_CANCEL:
775 return wxCANCEL;
776 break;
777 }
778 return ans;
779 }
780
781 wxString wxGetTextFromUser(const wxString& message, const wxString& caption,
782 const wxString& defaultValue, wxWindow *parent,
783 int x, int y, bool WXUNUSED(centre) )
784 {
785 wxTextEntryDialog dialog(parent, message, caption, defaultValue, wxOK|wxCANCEL, wxPoint(x, y));
786 if (dialog.ShowModal() == wxID_OK)
787 return dialog.GetValue();
788 else
789 return wxString("");
790 }
791
792 #ifdef __MWERKS__
793 char *strdup(const char *s)
794 {
795 return strcpy( (char*) malloc( strlen( s ) + 1 ) , s ) ;
796 }
797
798 int isascii( int c )
799 {
800 return ( c >= 0 && c < 128 ) ;
801 }
802 #endif
803
804 // Overloaded functions, taking a wxString
805 bool wxGetHostName(wxString& name)
806 {
807 bool success = wxGetHostName(wxBuffer, 500);
808 if (success)
809 {
810 name = wxBuffer;
811 return TRUE;
812 }
813 else
814 return FALSE;
815 }
816
817 bool wxGetUserId(wxString& buf)
818 {
819 bool success = wxGetUserId(wxBuffer, 500);
820 if (success)
821 {
822 buf = wxBuffer;
823 return TRUE;
824 }
825 else
826 return FALSE;
827 }
828
829 bool wxGetUserName(wxString& buf)
830 {
831 bool success = wxGetUserName(wxBuffer, 500);
832 if (success)
833 {
834 buf = wxBuffer;
835 return TRUE;
836 }
837 else
838 return FALSE;
839 }
840