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