]>
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"
44 #if !defined(__WATCOMC__)
45 #if !(defined(_MSC_VER) && (_MSC_VER > 800))
50 #include <sys/types.h>
53 // Pattern matching code.
54 // Yes, this path is deliberate (for Borland compilation)
55 #ifdef wx_mac /* MATTHEW: [5] Mac doesn't like paths with "/" */
58 #include "../common/glob.inc"
65 #define _MAXPATHLEN 500
67 extern char *wxBuffer
;
70 // we have no strI functions under VMS, therefore I have implemented
71 // an inefficient but portable version: convert copies of strings to lowercase
72 // and then use the normal comparison
73 static void myLowerString(char *s
)
76 if(isalpha(*s
)) *s
= (char)tolower(*s
);
81 int strcasecmp(const char *str_1
, const char *str_2
)
83 char *temp1
= new char[strlen(str_1
)+1];
84 char *temp2
= new char[strlen(str_2
)+1];
90 int result
= strcmp(temp1
,temp2
);
97 int strncasecmp(const char *str_1
, const char *str_2
, size_t maxchar
)
99 char *temp1
= new char[strlen(str_1
)+1];
100 char *temp2
= new char[strlen(str_2
)+1];
103 myLowerString(temp1
);
104 myLowerString(temp2
);
106 int result
= strncmp(temp1
,temp2
,maxchar
);
117 #define strcasecmp stricmp
118 #define strncasecmp strnicmp
122 #pragma warning (disable : 4245)
126 #pragma warning (default : 4245)
130 // This declaration is missing in SunOS!
131 // (Yes, I know it is NOT ANSI-C but its in BSD libc)
132 #if defined(__xlC) || defined(__AIX__) || defined(__GNUG__)
135 int strcasecmp (const char *, const char *);
136 int strncasecmp (const char *, const char *, size_t);
139 #endif /* __WXMSW__ */
143 copystring (const char *s
)
145 if (s
== NULL
) s
= "";
146 size_t len
= strlen (s
) + 1;
148 char *news
= new char[len
];
149 memcpy (news
, s
, len
); // Should be the fastest
155 static long wxCurrentId
= 100;
160 return wxCurrentId
++;
164 wxGetCurrentId(void) { return wxCurrentId
; }
167 wxRegisterId (long id
)
169 if (id
>= wxCurrentId
)
170 wxCurrentId
= id
+ 1;
174 StringToFloat (char *s
, float *number
)
176 if (s
&& *s
&& number
)
177 *number
= (float) strtod (s
, (char **) NULL
);
181 StringToDouble (char *s
, double *number
)
183 if (s
&& *s
&& number
)
184 *number
= strtod (s
, (char **) NULL
);
188 FloatToString (float number
, const char *fmt
)
190 static char buf
[256];
192 // sprintf (buf, "%.2f", number);
193 sprintf (buf
, fmt
, number
);
198 DoubleToString (double number
, const char *fmt
)
200 static char buf
[256];
202 sprintf (buf
, fmt
, number
);
207 StringToInt (char *s
, int *number
)
209 if (s
&& *s
&& number
)
210 *number
= (int) strtol (s
, (char **) NULL
, 10);
214 StringToLong (char *s
, long *number
)
216 if (s
&& *s
&& number
)
217 *number
= strtol (s
, (char **) NULL
, 10);
221 IntToString (int number
)
225 sprintf (buf
, "%d", number
);
230 LongToString (long number
)
234 sprintf (buf
, "%ld", number
);
238 // Array used in DecToHex conversion routine.
239 static char hexArray
[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B',
240 'C', 'D', 'E', 'F' };
242 // Convert 2-digit hex number to decimal
243 int wxHexToDec(char *buf
)
245 int firstDigit
, secondDigit
;
248 firstDigit
= buf
[0] - 'A' + 10;
250 firstDigit
= buf
[0] - '0';
253 secondDigit
= buf
[1] - 'A' + 10;
255 secondDigit
= buf
[1] - '0';
257 return firstDigit
* 16 + secondDigit
;
260 // Convert decimal integer to 2-character hex string
261 void wxDecToHex(int dec
, char *buf
)
263 int firstDigit
= (int)(dec
/16.0);
264 int secondDigit
= (int)(dec
- (firstDigit
*16.0));
265 buf
[0] = hexArray
[firstDigit
];
266 buf
[1] = hexArray
[secondDigit
];
270 // Match a string INDEPENDENT OF CASE
272 StringMatch (char *str1
, char *str2
, bool subString
, bool exact
)
274 if (str1
== NULL
|| str2
== NULL
)
281 int len1
= strlen (str1
);
282 int len2
= strlen (str2
);
285 // Search for str1 in str2
286 // Slow .... but acceptable for short strings
287 for (i
= 0; i
<= len2
- len1
; i
++)
289 if (strncasecmp (str1
, str2
+ i
, len1
) == 0)
295 if (strcasecmp (str1
, str2
) == 0)
300 int len1
= strlen (str1
);
301 int len2
= strlen (str2
);
303 if (strncasecmp (str1
, str2
, wxMin (len1
, len2
)) == 0)
310 // Return the current date/time
312 wxString
wxNow( void )
314 time_t now
= time((time_t *) NULL
);
315 char *date
= ctime(&now
);
317 return wxString(date
);
320 /* Get Full RFC822 style email address */
322 wxGetEmailAddress (char *address
, int maxSize
)
327 if (wxGetHostName(host
, 64) == FALSE
)
329 if (wxGetUserId(user
, 64) == FALSE
)
337 strncpy(address
, tmp
, maxSize
- 1);
338 address
[maxSize
-1] = '\0';
343 * Strip out any menu codes
346 char *wxStripMenuCodes (char *in
, char *out
)
349 return (char *) NULL
;
352 out
= copystring(in
);
360 // Check && -> &, &x -> x
364 else if (*in
== '\t')
366 // Remove all stuff after \t in X mode, and let the stuff as is
368 // Accelerators are handled in wx_item.cc for Motif, and are not
369 // YET supported in XView
383 * Window search functions
388 * If parent is non-NULL, look through children for a label or title
389 * matching the specified string. If NULL, look through all top-level windows.
393 static wxWindow
*wxFindWindowByLabel1 (const wxString
& title
, wxWindow
* parent
);
396 wxFindWindowByLabel (const wxString
& title
, wxWindow
* parent
)
400 return wxFindWindowByLabel1 (title
, parent
);
404 for (wxNode
* node
= wxTopLevelWindows
.First (); node
; node
= node
->Next ())
406 wxWindow
*win
= (wxWindow
*) node
->Data ();
407 wxWindow
*retwin
= wxFindWindowByLabel1 (title
, win
);
413 return (wxWindow
*) NULL
;
418 wxFindWindowByLabel1 (const wxString
& title
, wxWindow
* parent
)
422 if (parent
->GetLabel() == title
)
428 for (wxNode
* node
= parent
->GetChildren()->First (); node
; node
= node
->Next ())
430 wxWindow
*win
= (wxWindow
*) node
->Data ();
431 wxWindow
*retwin
= wxFindWindowByLabel1 (title
, win
);
438 return (wxWindow
*) NULL
; // Not found
443 * If parent is non-NULL, look through children for a name
444 * matching the specified string. If NULL, look through all top-level windows.
448 static wxWindow
*wxFindWindowByName1 (const wxString
& title
, wxWindow
* parent
);
451 wxFindWindowByName (const wxString
& title
, wxWindow
* parent
)
455 return wxFindWindowByName1 (title
, parent
);
459 for (wxNode
* node
= wxTopLevelWindows
.First (); node
; node
= node
->Next ())
461 wxWindow
*win
= (wxWindow
*) node
->Data ();
462 wxWindow
*retwin
= wxFindWindowByName1 (title
, win
);
468 // Failed? Try by label instead.
469 return wxFindWindowByLabel(title
, parent
);
474 wxFindWindowByName1 (const wxString
& title
, wxWindow
* parent
)
478 if ( parent
->GetName() == title
)
484 for (wxNode
* node
= parent
->GetChildren()->First (); node
; node
= node
->Next ())
486 wxWindow
*win
= (wxWindow
*) node
->Data ();
487 wxWindow
*retwin
= wxFindWindowByName1 (title
, win
);
494 return (wxWindow
*) NULL
; // Not found
498 // Returns menu item id or -1 if none.
500 wxFindMenuItemId (wxFrame
* frame
, const wxString
& menuString
, const wxString
& itemString
)
502 wxMenuBar
*menuBar
= frame
->GetMenuBar ();
505 return menuBar
->FindMenuItem (menuString
, itemString
);
511 #if !defined(_WINDLL)
513 wxDebugStreamBuf::wxDebugStreamBuf(void)
515 if (allocate()) setp(base(),ebuf());
518 int wxDebugStreamBuf::overflow(int WXUNUSED(i
))
520 int len
= pptr() - pbase();
521 char *txt
= new char[len
+1];
522 strncpy(txt
, pbase(), len
);
525 OutputDebugString((LPCSTR
)txt
);
527 fprintf(stderr
, txt
);
529 setp(pbase(), epptr());
534 int wxDebugStreamBuf::sync(void)
536 int len
= pptr() - pbase();
537 char *txt
= new char[len
+1];
538 strncpy(txt
, pbase(), len
);
541 OutputDebugString((LPCSTR
)txt
);
543 fprintf(stderr
, txt
);
545 setp(pbase(), epptr());
553 On Fri, 21 Jul 1995, Paul Craven wrote:
555 > Is there a way to find the path of running program's executable? I can get
556 > my home directory, and the current directory, but I don't know how to get the
557 > executable directory.
560 The code below (warty as it is), does what you want on most Unix,
561 DOS, and Mac platforms (it's from the ALS Prolog main).
563 || Ken Bowen Applied Logic Systems, Inc. PO Box 180,
564 ||==== Voice: +1 (617)965-9191 Newton Centre,
565 || FAX: +1 (617)965-1636 MA 02159 USA
566 Email: ken@als.com WWW: http://www.als.com
567 ------------------------------------------------------------------------
570 // This code is commented out but it may be integrated with wxWin at
571 // a later date, after testing. Thanks Ken!
574 /*--------------------------------------------------------------------*
575 | whereami is given a filename f in the form: whereami(argv[0])
576 | It returns the directory in which the executable file (containing
577 | this code [main.c] ) may be found. A dot will be returned to indicate
578 | the current directory.
579 *--------------------------------------------------------------------*/
585 register char *cutoff
= NULL
; /* stifle -Wall */
592 * See if the file is accessible either through the current directory
593 * or through an absolute path.
596 if (access(name
, R_OK
) == 0) {
598 /*-------------------------------------------------------------*
599 * The file was accessible without any other work. But the current
600 * working directory might change on us, so if it was accessible
601 * through the cwd, then we should get it for later accesses.
602 *-------------------------------------------------------------*/
605 if (!absolute_pathname(name
)) {
606 #if defined(DOS) || defined(__WIN32__)
612 if (*(name
+ 1) == ':') {
613 if (*name
>= 'a' && *name
<= 'z')
614 drive
= (int) (*name
- 'a' + 1);
616 drive
= (int) (*name
- 'A' + 1);
618 *newrbuf
++ = *(name
+ 1);
619 *newrbuf
++ = DIR_SEPARATOR
;
623 *newrbuf
++ = DIR_SEPARATOR
;
625 if (getcwd(newrbuf
, drive
) == 0) { /* } */
627 if (getcwd(newrbuf
, 1024) == 0) { /* } */
631 if (getwd(imagedir
) == 0) { /* } */
632 #else /* !HAVE_GETWD */
633 if (getcwd(imagedir
, 1024) == 0) {
634 #endif /* !HAVE_GETWD */
636 fatal_error(FE_GETCWD
, 0);
638 for (; *t
; t
++) /* Set t to end of buffer */
640 if (*(t
- 1) == DIR_SEPARATOR
) /* leave slash if already
645 cutoff
= t
; /* otherwise put one in */
646 *t
++ = DIR_SEPARATOR
;
649 #if (!defined(__MAC__) && !defined(__DJGPP__) && !defined(__GO32__) && !defined(__WIN32__))
651 (*t
++ = DIR_SEPARATOR
);
654 /*-------------------------------------------------------------*
655 * Copy the rest of the string and set the cutoff if it was not
656 * already set. If the first character of name is a slash, cutoff
657 * is not presently set but will be on the first iteration of the
659 *-------------------------------------------------------------*/
661 for ((*name
== DIR_SEPARATOR
? (s
= name
+1) : (s
= name
));;) {
662 if (*s
== DIR_SEPARATOR
)
671 /*-------------------------------------------------------------*
672 * Get the path list from the environment. If the path list is
673 * inaccessible for any reason, leave with fatal error.
674 *-------------------------------------------------------------*/
677 if ((s
= getenv("Commands")) == (char *) 0)
679 if ((s
= getenv("PATH")) == (char *) 0)
681 fatal_error(FE_PATH
, 0);
684 * Copy path list into ebuf and set the source pointer to the
685 * beginning of this buffer.
693 while (*s
&& *s
!= PATH_SEPARATOR
)
695 if (t
> imagedir
&& *(t
- 1) == DIR_SEPARATOR
)
696 ; /* do nothing -- slash already is in place */
698 *t
++ = DIR_SEPARATOR
; /* put in the slash */
699 cutoff
= t
- 1; /* set cutoff */
701 if (access(imagedir
, R_OK
) == 0)
705 s
++; /* advance source pointer */
707 fatal_error(FE_INFND
, 0);
712 /*-------------------------------------------------------------*
713 | At this point the full pathname should exist in imagedir and
714 | cutoff should be set to the final slash. We must now determine
715 | whether the file name is a symbolic link or not and chase it down
716 | if it is. Note that we reuse ebuf for getting the link.
717 *-------------------------------------------------------------*/
720 while ((cc
= readlink(imagedir
, ebuf
, 512)) != -1) {
723 if (*s
== DIR_SEPARATOR
) {
730 if (*s
== DIR_SEPARATOR
)
731 cutoff
= t
; /* mark the last slash seen */
732 if (!(*t
++ = *s
++)) /* copy the character */
737 #endif /* HAVE_SYMLINK */
739 strcpy(imagename
, cutoff
+ 1); /* keep the image name */
740 *(cutoff
+ 1) = 0; /* chop off the filename part */
746 * N.B. these convenience functions must be separate from msgdlgg.cpp, textdlgg.cpp
747 * since otherwise the generic code may be pulled in unnecessarily.
750 int wxMessageBox(const wxString
& message
, const wxString
& caption
, long style
,
751 wxWindow
*parent
, int WXUNUSED(x
), int WXUNUSED(y
) )
753 wxMessageDialog
dialog(parent
, message
, caption
, style
);
755 int ans
= dialog
.ShowModal();
775 wxString
wxGetTextFromUser(const wxString
& message
, const wxString
& caption
,
776 const wxString
& defaultValue
, wxWindow
*parent
,
777 int x
, int y
, bool WXUNUSED(centre
) )
779 wxTextEntryDialog
dialog(parent
, message
, caption
, defaultValue
, wxOK
|wxCANCEL
, wxPoint(x
, y
));
780 if (dialog
.ShowModal() == wxID_OK
)
781 return dialog
.GetValue();