]>
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
);
362 /* Get Full RFC822 style email address */
364 wxGetEmailAddress (char *address
, int maxSize
)
369 if (wxGetHostName(host
, 64) == FALSE
)
371 if (wxGetUserId(user
, 64) == FALSE
)
379 strncpy(address
, tmp
, maxSize
- 1);
380 address
[maxSize
-1] = '\0';
385 * Strip out any menu codes
388 char *wxStripMenuCodes (char *in
, char *out
)
391 return (char *) NULL
;
394 out
= copystring(in
);
402 // Check && -> &, &x -> x
406 else if (*in
== '\t')
408 // Remove all stuff after \t in X mode, and let the stuff as is
410 // Accelerators are handled in wx_item.cc for Motif, and are not
411 // YET supported in XView
423 wxString
wxStripMenuCodes(const wxString
& str
)
425 char *buf
= new char[str
.Length() + 1];
426 wxStripMenuCodes((char*) (const char*) str
, buf
);
433 * Window search functions
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.
443 static wxWindow
*wxFindWindowByLabel1 (const wxString
& title
, wxWindow
* parent
);
446 wxFindWindowByLabel (const wxString
& title
, wxWindow
* parent
)
450 return wxFindWindowByLabel1 (title
, parent
);
454 for (wxNode
* node
= wxTopLevelWindows
.First (); node
; node
= node
->Next ())
456 wxWindow
*win
= (wxWindow
*) node
->Data ();
457 wxWindow
*retwin
= wxFindWindowByLabel1 (title
, win
);
463 return (wxWindow
*) NULL
;
468 wxFindWindowByLabel1 (const wxString
& title
, wxWindow
* parent
)
472 if (parent
->GetLabel() == title
)
478 for (wxNode
* node
= parent
->GetChildren().First (); node
; node
= node
->Next ())
480 wxWindow
*win
= (wxWindow
*) node
->Data ();
481 wxWindow
*retwin
= wxFindWindowByLabel1 (title
, win
);
488 return (wxWindow
*) NULL
; // Not found
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.
498 static wxWindow
*wxFindWindowByName1 (const wxString
& title
, wxWindow
* parent
);
501 wxFindWindowByName (const wxString
& title
, wxWindow
* parent
)
505 return wxFindWindowByName1 (title
, parent
);
509 for (wxNode
* node
= wxTopLevelWindows
.First (); node
; node
= node
->Next ())
511 wxWindow
*win
= (wxWindow
*) node
->Data ();
512 wxWindow
*retwin
= wxFindWindowByName1 (title
, win
);
518 // Failed? Try by label instead.
519 return wxFindWindowByLabel(title
, parent
);
524 wxFindWindowByName1 (const wxString
& title
, wxWindow
* parent
)
528 if ( parent
->GetName() == title
)
534 for (wxNode
* node
= parent
->GetChildren().First (); node
; node
= node
->Next ())
536 wxWindow
*win
= (wxWindow
*) node
->Data ();
537 wxWindow
*retwin
= wxFindWindowByName1 (title
, win
);
544 return (wxWindow
*) NULL
; // Not found
548 // Returns menu item id or -1 if none.
550 wxFindMenuItemId (wxFrame
* frame
, const wxString
& menuString
, const wxString
& itemString
)
552 wxMenuBar
*menuBar
= frame
->GetMenuBar ();
555 return menuBar
->FindMenuItem (menuString
, itemString
);
559 On Fri, 21 Jul 1995, Paul Craven wrote:
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.
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).
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 ------------------------------------------------------------------------
576 // This code is commented out but it may be integrated with wxWin at
577 // a later date, after testing. Thanks Ken!
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 *--------------------------------------------------------------------*/
591 register char *cutoff
= NULL
; /* stifle -Wall */
598 * See if the file is accessible either through the current directory
599 * or through an absolute path.
602 if (access(name
, R_OK
) == 0) {
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 *-------------------------------------------------------------*/
611 if (!absolute_pathname(name
)) {
612 #if defined(DOS) || defined(__WIN32__)
618 if (*(name
+ 1) == ':') {
619 if (*name
>= 'a' && *name
<= 'z')
620 drive
= (int) (*name
- 'a' + 1);
622 drive
= (int) (*name
- 'A' + 1);
624 *newrbuf
++ = *(name
+ 1);
625 *newrbuf
++ = DIR_SEPARATOR
;
629 *newrbuf
++ = DIR_SEPARATOR
;
631 if (getcwd(newrbuf
, drive
) == 0) { /* } */
633 if (getcwd(newrbuf
, 1024) == 0) { /* } */
637 if (getwd(imagedir
) == 0) { /* } */
638 #else /* !HAVE_GETWD */
639 if (getcwd(imagedir
, 1024) == 0) {
640 #endif /* !HAVE_GETWD */
642 fatal_error(FE_GETCWD
, 0);
644 for (; *t
; t
++) /* Set t to end of buffer */
646 if (*(t
- 1) == DIR_SEPARATOR
) /* leave slash if already
651 cutoff
= t
; /* otherwise put one in */
652 *t
++ = DIR_SEPARATOR
;
655 #if (!defined(__MAC__) && !defined(__DJGPP__) && !defined(__GO32__) && !defined(__WIN32__))
657 (*t
++ = DIR_SEPARATOR
);
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
665 *-------------------------------------------------------------*/
667 for ((*name
== DIR_SEPARATOR
? (s
= name
+1) : (s
= name
));;) {
668 if (*s
== DIR_SEPARATOR
)
677 /*-------------------------------------------------------------*
678 * Get the path list from the environment. If the path list is
679 * inaccessible for any reason, leave with fatal error.
680 *-------------------------------------------------------------*/
683 if ((s
= getenv("Commands")) == (char *) 0)
685 if ((s
= getenv("PATH")) == (char *) 0)
687 fatal_error(FE_PATH
, 0);
690 * Copy path list into ebuf and set the source pointer to the
691 * beginning of this buffer.
699 while (*s
&& *s
!= PATH_SEPARATOR
)
701 if (t
> imagedir
&& *(t
- 1) == DIR_SEPARATOR
)
702 ; /* do nothing -- slash already is in place */
704 *t
++ = DIR_SEPARATOR
; /* put in the slash */
705 cutoff
= t
- 1; /* set cutoff */
707 if (access(imagedir
, R_OK
) == 0)
711 s
++; /* advance source pointer */
713 fatal_error(FE_INFND
, 0);
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 *-------------------------------------------------------------*/
726 while ((cc
= readlink(imagedir
, ebuf
, 512)) != -1) {
729 if (*s
== DIR_SEPARATOR
) {
736 if (*s
== DIR_SEPARATOR
)
737 cutoff
= t
; /* mark the last slash seen */
738 if (!(*t
++ = *s
++)) /* copy the character */
743 #endif /* HAVE_SYMLINK */
745 strcpy(imagename
, cutoff
+ 1); /* keep the image name */
746 *(cutoff
+ 1) = 0; /* chop off the filename part */
752 // Yield to other apps/messages and disable user input
753 bool wxSafeYield(wxWindow
*win
)
756 for ( node
= wxTopLevelWindows
.GetFirst();
758 node
= node
->GetNext() )
759 ((wxWindow
*)node
->GetData())->Enable(FALSE
);
761 // always enable ourselves
762 if(win
) win
->Enable(true);
765 for ( node
= wxTopLevelWindows
.GetFirst();
767 node
= node
->GetNext() )
768 ((wxWindow
*)node
->GetData())->Enable(TRUE
);
773 * N.B. these convenience functions must be separate from msgdlgg.cpp, textdlgg.cpp
774 * since otherwise the generic code may be pulled in unnecessarily.
777 int wxMessageBox(const wxString
& message
, const wxString
& caption
, long style
,
778 wxWindow
*parent
, int WXUNUSED(x
), int WXUNUSED(y
) )
780 wxMessageDialog
dialog(parent
, message
, caption
, style
);
782 int ans
= dialog
.ShowModal();
802 wxString
wxGetTextFromUser(const wxString
& message
, const wxString
& caption
,
803 const wxString
& defaultValue
, wxWindow
*parent
,
804 int x
, int y
, bool WXUNUSED(centre
) )
806 wxTextEntryDialog
dialog(parent
, message
, caption
, defaultValue
, wxOK
|wxCANCEL
, wxPoint(x
, y
));
807 if (dialog
.ShowModal() == wxID_OK
)
808 return dialog
.GetValue();
814 char *strdup(const char *s
)
816 return strcpy( (char*) malloc( strlen( s
) + 1 ) , s
) ;
821 return ( c
>= 0 && c
< 128 ) ;
825 bool wxGetUserId(wxString
& buf
)
827 bool success
= wxGetUserId(wxBuffer
, 500);
837 bool wxGetUserName(wxString
& buf
)
839 bool success
= wxGetUserName(wxBuffer
, 500);
849 bool wxGetHostName(wxString
& buf
)
851 static const size_t hostnameSize
= 257;
852 bool ok
= wxGetHostName(buf
.GetWriteBuf(hostnameSize
), hostnameSize
);