]>
git.saurik.com Git - wxWidgets.git/blob - src/common/utilscmn.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Miscellaneous utility functions and classes
4 // Author: Julian Smart
8 // Copyright: (c) 1998 Julian Smart
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "utils.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
26 #include "wx/window.h"
29 #include "wx/msgdlg.h"
30 #include "wx/textdlg.h"
33 #include "wx/ioswrap.h"
45 #if !defined(__WATCOMC__)
46 #if !(defined(_MSC_VER) && (_MSC_VER > 800))
52 #include <sys/types.h>
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 "/" */
65 #include "../common/glob.inc"
72 #define _MAXPATHLEN 500
74 extern char *wxBuffer
;
77 int strcasecmp(const char *str_1
, const char *str_2
)
81 c1
= tolower(*str_1
++);
82 c2
= tolower(*str_2
++);
83 } while ( c1
&& (c1
== c2
) );
88 int strncasecmp(const char *str_1
, const char *str_2
, size_t maxchar
)
94 c1
= tolower(*str_1
++);
95 c2
= tolower(*str_2
++);
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
)
113 if(isalpha(*s
)) *s
= (char)tolower(*s
);
118 int strcasecmp(const char *str_1
, const char *str_2
)
120 char *temp1
= new char[strlen(str_1
)+1];
121 char *temp2
= new char[strlen(str_2
)+1];
124 myLowerString(temp1
);
125 myLowerString(temp2
);
127 int result
= strcmp(temp1
,temp2
);
134 int strncasecmp(const char *str_1
, const char *str_2
, size_t maxchar
)
136 char *temp1
= new char[strlen(str_1
)+1];
137 char *temp2
= new char[strlen(str_2
)+1];
140 myLowerString(temp1
);
141 myLowerString(temp2
);
143 int result
= strncmp(temp1
,temp2
,maxchar
);
155 #define strcasecmp stricmp
156 #define strncasecmp strnicmp
158 #define strcasecmp _stricmp
159 #define strncasecmp _strnicmp
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__)
169 int strcasecmp (const char *, const char *);
170 int strncasecmp (const char *, const char *, size_t);
173 #endif /* __WXMSW__ */
177 copystring (const char *s
)
179 if (s
== NULL
) s
= "";
180 size_t len
= strlen (s
) + 1;
182 char *news
= new char[len
];
183 memcpy (news
, s
, len
); // Should be the fastest
189 static long wxCurrentId
= 100;
194 return wxCurrentId
++;
198 wxGetCurrentId(void) { return wxCurrentId
; }
201 wxRegisterId (long id
)
203 if (id
>= wxCurrentId
)
204 wxCurrentId
= id
+ 1;
208 StringToFloat (char *s
, float *number
)
210 if (s
&& *s
&& number
)
211 *number
= (float) strtod (s
, (char **) NULL
);
215 StringToDouble (char *s
, double *number
)
217 if (s
&& *s
&& number
)
218 *number
= strtod (s
, (char **) NULL
);
222 FloatToString (float number
, const char *fmt
)
224 static char buf
[256];
226 // sprintf (buf, "%.2f", number);
227 sprintf (buf
, fmt
, number
);
232 DoubleToString (double number
, const char *fmt
)
234 static char buf
[256];
236 sprintf (buf
, fmt
, number
);
241 StringToInt (char *s
, int *number
)
243 if (s
&& *s
&& number
)
244 *number
= (int) strtol (s
, (char **) NULL
, 10);
248 StringToLong (char *s
, long *number
)
250 if (s
&& *s
&& number
)
251 *number
= strtol (s
, (char **) NULL
, 10);
255 IntToString (int number
)
259 sprintf (buf
, "%d", number
);
264 LongToString (long number
)
268 sprintf (buf
, "%ld", number
);
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' };
276 // Convert 2-digit hex number to decimal
277 int wxHexToDec(const wxString
& buf
)
279 int firstDigit
, secondDigit
;
281 if (buf
.GetChar(0) >= 'A')
282 firstDigit
= buf
.GetChar(0) - 'A' + 10;
284 firstDigit
= buf
.GetChar(0) - '0';
286 if (buf
.GetChar(1) >= 'A')
287 secondDigit
= buf
.GetChar(1) - 'A' + 10;
289 secondDigit
= buf
.GetChar(1) - '0';
291 return firstDigit
* 16 + secondDigit
;
294 // Convert decimal integer to 2-character hex string
295 void wxDecToHex(int dec
, char *buf
)
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
];
304 // Convert decimal integer to 2-character hex string
305 wxString
wxDecToHex(int dec
)
308 wxDecToHex(dec
, buf
);
309 return wxString(buf
);
312 // Match a string INDEPENDENT OF CASE
314 StringMatch (char *str1
, char *str2
, bool subString
, bool exact
)
316 if (str1
== NULL
|| str2
== NULL
)
323 int len1
= strlen (str1
);
324 int len2
= strlen (str2
);
327 // Search for str1 in str2
328 // Slow .... but acceptable for short strings
329 for (i
= 0; i
<= len2
- len1
; i
++)
331 if (strncasecmp (str1
, str2
+ i
, len1
) == 0)
337 if (strcasecmp (str1
, str2
) == 0)
342 int len1
= strlen (str1
);
343 int len2
= strlen (str2
);
345 if (strncasecmp (str1
, str2
, wxMin (len1
, len2
)) == 0)
352 // Return the current date/time
354 wxString
wxNow( void )
356 time_t now
= time((time_t *) NULL
);
357 char *date
= ctime(&now
);
359 return wxString(date
);
363 * Strip out any menu codes
366 char *wxStripMenuCodes (char *in
, char *out
)
369 return (char *) NULL
;
372 out
= copystring(in
);
380 // Check && -> &, &x -> x
384 else if (*in
== '\t')
386 // Remove all stuff after \t in X mode, and let the stuff as is
388 // Accelerators are handled in wx_item.cc for Motif, and are not
389 // YET supported in XView
401 wxString
wxStripMenuCodes(const wxString
& str
)
403 char *buf
= new char[str
.Length() + 1];
404 wxStripMenuCodes((char*) (const char*) str
, buf
);
411 * Window search functions
416 * If parent is non-NULL, look through children for a label or title
417 * matching the specified string. If NULL, look through all top-level windows.
421 static wxWindow
*wxFindWindowByLabel1 (const wxString
& title
, wxWindow
* parent
);
424 wxFindWindowByLabel (const wxString
& title
, wxWindow
* parent
)
428 return wxFindWindowByLabel1 (title
, parent
);
432 for (wxNode
* node
= wxTopLevelWindows
.First (); node
; node
= node
->Next ())
434 wxWindow
*win
= (wxWindow
*) node
->Data ();
435 wxWindow
*retwin
= wxFindWindowByLabel1 (title
, win
);
441 return (wxWindow
*) NULL
;
446 wxFindWindowByLabel1 (const wxString
& title
, wxWindow
* parent
)
450 if (parent
->GetLabel() == title
)
456 for (wxNode
* node
= parent
->GetChildren().First (); node
; node
= node
->Next ())
458 wxWindow
*win
= (wxWindow
*) node
->Data ();
459 wxWindow
*retwin
= wxFindWindowByLabel1 (title
, win
);
466 return (wxWindow
*) NULL
; // Not found
471 * If parent is non-NULL, look through children for a name
472 * matching the specified string. If NULL, look through all top-level windows.
476 static wxWindow
*wxFindWindowByName1 (const wxString
& title
, wxWindow
* parent
);
479 wxFindWindowByName (const wxString
& title
, wxWindow
* parent
)
483 return wxFindWindowByName1 (title
, parent
);
487 for (wxNode
* node
= wxTopLevelWindows
.First (); node
; node
= node
->Next ())
489 wxWindow
*win
= (wxWindow
*) node
->Data ();
490 wxWindow
*retwin
= wxFindWindowByName1 (title
, win
);
496 // Failed? Try by label instead.
497 return wxFindWindowByLabel(title
, parent
);
502 wxFindWindowByName1 (const wxString
& title
, wxWindow
* parent
)
506 if ( parent
->GetName() == title
)
512 for (wxNode
* node
= parent
->GetChildren().First (); node
; node
= node
->Next ())
514 wxWindow
*win
= (wxWindow
*) node
->Data ();
515 wxWindow
*retwin
= wxFindWindowByName1 (title
, win
);
522 return (wxWindow
*) NULL
; // Not found
526 // Returns menu item id or -1 if none.
528 wxFindMenuItemId (wxFrame
* frame
, const wxString
& menuString
, const wxString
& itemString
)
530 wxMenuBar
*menuBar
= frame
->GetMenuBar ();
533 return menuBar
->FindMenuItem (menuString
, itemString
);
537 On Fri, 21 Jul 1995, Paul Craven wrote:
539 > Is there a way to find the path of running program's executable? I can get
540 > my home directory, and the current directory, but I don't know how to get the
541 > executable directory.
544 The code below (warty as it is), does what you want on most Unix,
545 DOS, and Mac platforms (it's from the ALS Prolog main).
547 || Ken Bowen Applied Logic Systems, Inc. PO Box 180,
548 ||==== Voice: +1 (617)965-9191 Newton Centre,
549 || FAX: +1 (617)965-1636 MA 02159 USA
550 Email: ken@als.com WWW: http://www.als.com
551 ------------------------------------------------------------------------
554 // This code is commented out but it may be integrated with wxWin at
555 // a later date, after testing. Thanks Ken!
558 /*--------------------------------------------------------------------*
559 | whereami is given a filename f in the form: whereami(argv[0])
560 | It returns the directory in which the executable file (containing
561 | this code [main.c] ) may be found. A dot will be returned to indicate
562 | the current directory.
563 *--------------------------------------------------------------------*/
569 register char *cutoff
= NULL
; /* stifle -Wall */
576 * See if the file is accessible either through the current directory
577 * or through an absolute path.
580 if (access(name
, R_OK
) == 0) {
582 /*-------------------------------------------------------------*
583 * The file was accessible without any other work. But the current
584 * working directory might change on us, so if it was accessible
585 * through the cwd, then we should get it for later accesses.
586 *-------------------------------------------------------------*/
589 if (!absolute_pathname(name
)) {
590 #if defined(DOS) || defined(__WIN32__)
596 if (*(name
+ 1) == ':') {
597 if (*name
>= 'a' && *name
<= 'z')
598 drive
= (int) (*name
- 'a' + 1);
600 drive
= (int) (*name
- 'A' + 1);
602 *newrbuf
++ = *(name
+ 1);
603 *newrbuf
++ = DIR_SEPARATOR
;
607 *newrbuf
++ = DIR_SEPARATOR
;
609 if (getcwd(newrbuf
, drive
) == 0) { /* } */
611 if (getcwd(newrbuf
, 1024) == 0) { /* } */
615 if (getwd(imagedir
) == 0) { /* } */
616 #else /* !HAVE_GETWD */
617 if (getcwd(imagedir
, 1024) == 0) {
618 #endif /* !HAVE_GETWD */
620 fatal_error(FE_GETCWD
, 0);
622 for (; *t
; t
++) /* Set t to end of buffer */
624 if (*(t
- 1) == DIR_SEPARATOR
) /* leave slash if already
629 cutoff
= t
; /* otherwise put one in */
630 *t
++ = DIR_SEPARATOR
;
633 #if (!defined(__MAC__) && !defined(__DJGPP__) && !defined(__GO32__) && !defined(__WIN32__))
635 (*t
++ = DIR_SEPARATOR
);
638 /*-------------------------------------------------------------*
639 * Copy the rest of the string and set the cutoff if it was not
640 * already set. If the first character of name is a slash, cutoff
641 * is not presently set but will be on the first iteration of the
643 *-------------------------------------------------------------*/
645 for ((*name
== DIR_SEPARATOR
? (s
= name
+1) : (s
= name
));;) {
646 if (*s
== DIR_SEPARATOR
)
655 /*-------------------------------------------------------------*
656 * Get the path list from the environment. If the path list is
657 * inaccessible for any reason, leave with fatal error.
658 *-------------------------------------------------------------*/
661 if ((s
= getenv("Commands")) == (char *) 0)
663 if ((s
= getenv("PATH")) == (char *) 0)
665 fatal_error(FE_PATH
, 0);
668 * Copy path list into ebuf and set the source pointer to the
669 * beginning of this buffer.
677 while (*s
&& *s
!= PATH_SEPARATOR
)
679 if (t
> imagedir
&& *(t
- 1) == DIR_SEPARATOR
)
680 ; /* do nothing -- slash already is in place */
682 *t
++ = DIR_SEPARATOR
; /* put in the slash */
683 cutoff
= t
- 1; /* set cutoff */
685 if (access(imagedir
, R_OK
) == 0)
689 s
++; /* advance source pointer */
691 fatal_error(FE_INFND
, 0);
696 /*-------------------------------------------------------------*
697 | At this point the full pathname should exist in imagedir and
698 | cutoff should be set to the final slash. We must now determine
699 | whether the file name is a symbolic link or not and chase it down
700 | if it is. Note that we reuse ebuf for getting the link.
701 *-------------------------------------------------------------*/
704 while ((cc
= readlink(imagedir
, ebuf
, 512)) != -1) {
707 if (*s
== DIR_SEPARATOR
) {
714 if (*s
== DIR_SEPARATOR
)
715 cutoff
= t
; /* mark the last slash seen */
716 if (!(*t
++ = *s
++)) /* copy the character */
721 #endif /* HAVE_SYMLINK */
723 strcpy(imagename
, cutoff
+ 1); /* keep the image name */
724 *(cutoff
+ 1) = 0; /* chop off the filename part */
730 // Yield to other apps/messages and disable user input
731 bool wxSafeYield(wxWindow
*win
)
734 for ( node
= wxTopLevelWindows
.GetFirst();
736 node
= node
->GetNext() )
737 ((wxWindow
*)node
->GetData())->Enable(FALSE
);
739 // always enable ourselves
740 if(win
) win
->Enable(true);
743 for ( node
= wxTopLevelWindows
.GetFirst();
745 node
= node
->GetNext() )
746 ((wxWindow
*)node
->GetData())->Enable(TRUE
);
751 * N.B. these convenience functions must be separate from msgdlgg.cpp, textdlgg.cpp
752 * since otherwise the generic code may be pulled in unnecessarily.
755 int wxMessageBox(const wxString
& message
, const wxString
& caption
, long style
,
756 wxWindow
*parent
, int WXUNUSED(x
), int WXUNUSED(y
) )
758 wxMessageDialog
dialog(parent
, message
, caption
, style
);
760 int ans
= dialog
.ShowModal();
780 wxString
wxGetTextFromUser(const wxString
& message
, const wxString
& caption
,
781 const wxString
& defaultValue
, wxWindow
*parent
,
782 int x
, int y
, bool WXUNUSED(centre
) )
784 wxTextEntryDialog
dialog(parent
, message
, caption
, defaultValue
, wxOK
|wxCANCEL
, wxPoint(x
, y
));
785 if (dialog
.ShowModal() == wxID_OK
)
786 return dialog
.GetValue();
792 char *strdup(const char *s
)
794 return strcpy( (char*) malloc( strlen( s
) + 1 ) , s
) ;
799 return ( c
>= 0 && c
< 128 ) ;
803 // ----------------------------------------------------------------------------
804 // network and user id functions
805 // ----------------------------------------------------------------------------
807 // Get Full RFC822 style email address
808 bool wxGetEmailAddress(char *address
, int maxSize
)
810 wxString email
= wxGetEmailAddress();
814 strncpy(address
, email
, maxSize
- 1);
815 address
[maxSize
- 1] = '\0';
820 wxString
wxGetEmailAddress()
824 wxString host
= wxGetHostName();
827 wxString user
= wxGetUserId();
830 wxString
email(user
);
831 email
<< '@' << host
;
838 wxString
wxGetUserId()
840 static const int maxLoginLen
= 256; // FIXME arbitrary number
843 bool ok
= wxGetUserId(buf
.GetWriteBuf(maxLoginLen
), maxLoginLen
);
852 wxString
wxGetUserName()
854 static const int maxUserNameLen
= 1024; // FIXME arbitrary number
857 bool ok
= wxGetUserName(buf
.GetWriteBuf(maxUserNameLen
), maxUserNameLen
);
866 wxString
wxGetHostName()
868 static const size_t hostnameSize
= 257;
871 bool ok
= wxGetHostName(buf
.GetWriteBuf(hostnameSize
), hostnameSize
);