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