]>
git.saurik.com Git - wxWidgets.git/blob - src/common/utilscmn.cpp
cb7890e7a8dd666d256acd63895bcd00fd4db1b9
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(const wxString
& buf
)
245 int firstDigit
, secondDigit
;
247 if (buf
.GetChar(0) >= 'A')
248 firstDigit
= buf
.GetChar(0) - 'A' + 10;
250 firstDigit
= buf
.GetChar(0) - '0';
252 if (buf
.GetChar(1) >= 'A')
253 secondDigit
= buf
.GetChar(1) - 'A' + 10;
255 secondDigit
= buf
.GetChar(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 // Convert decimal integer to 2-character hex string
271 wxString
wxDecToHex(int dec
)
274 wxDecToHex(dec
, buf
);
275 return wxString(buf
);
278 // Match a string INDEPENDENT OF CASE
280 StringMatch (char *str1
, char *str2
, bool subString
, bool exact
)
282 if (str1
== NULL
|| str2
== NULL
)
289 int len1
= strlen (str1
);
290 int len2
= strlen (str2
);
293 // Search for str1 in str2
294 // Slow .... but acceptable for short strings
295 for (i
= 0; i
<= len2
- len1
; i
++)
297 if (strncasecmp (str1
, str2
+ i
, len1
) == 0)
303 if (strcasecmp (str1
, str2
) == 0)
308 int len1
= strlen (str1
);
309 int len2
= strlen (str2
);
311 if (strncasecmp (str1
, str2
, wxMin (len1
, len2
)) == 0)
318 // Return the current date/time
320 wxString
wxNow( void )
322 time_t now
= time((time_t *) NULL
);
323 char *date
= ctime(&now
);
325 return wxString(date
);
328 /* Get Full RFC822 style email address */
330 wxGetEmailAddress (char *address
, int maxSize
)
335 if (wxGetHostName(host
, 64) == FALSE
)
337 if (wxGetUserId(user
, 64) == FALSE
)
345 strncpy(address
, tmp
, maxSize
- 1);
346 address
[maxSize
-1] = '\0';
351 * Strip out any menu codes
354 char *wxStripMenuCodes (char *in
, char *out
)
357 return (char *) NULL
;
360 out
= copystring(in
);
368 // Check && -> &, &x -> x
372 else if (*in
== '\t')
374 // Remove all stuff after \t in X mode, and let the stuff as is
376 // Accelerators are handled in wx_item.cc for Motif, and are not
377 // YET supported in XView
391 * Window search functions
396 * If parent is non-NULL, look through children for a label or title
397 * matching the specified string. If NULL, look through all top-level windows.
401 static wxWindow
*wxFindWindowByLabel1 (const wxString
& title
, wxWindow
* parent
);
404 wxFindWindowByLabel (const wxString
& title
, wxWindow
* parent
)
408 return wxFindWindowByLabel1 (title
, parent
);
412 for (wxNode
* node
= wxTopLevelWindows
.First (); node
; node
= node
->Next ())
414 wxWindow
*win
= (wxWindow
*) node
->Data ();
415 wxWindow
*retwin
= wxFindWindowByLabel1 (title
, win
);
421 return (wxWindow
*) NULL
;
426 wxFindWindowByLabel1 (const wxString
& title
, wxWindow
* parent
)
430 if (parent
->GetLabel() == title
)
436 for (wxNode
* node
= parent
->GetChildren()->First (); node
; node
= node
->Next ())
438 wxWindow
*win
= (wxWindow
*) node
->Data ();
439 wxWindow
*retwin
= wxFindWindowByLabel1 (title
, win
);
446 return (wxWindow
*) NULL
; // Not found
451 * If parent is non-NULL, look through children for a name
452 * matching the specified string. If NULL, look through all top-level windows.
456 static wxWindow
*wxFindWindowByName1 (const wxString
& title
, wxWindow
* parent
);
459 wxFindWindowByName (const wxString
& title
, wxWindow
* parent
)
463 return wxFindWindowByName1 (title
, parent
);
467 for (wxNode
* node
= wxTopLevelWindows
.First (); node
; node
= node
->Next ())
469 wxWindow
*win
= (wxWindow
*) node
->Data ();
470 wxWindow
*retwin
= wxFindWindowByName1 (title
, win
);
476 // Failed? Try by label instead.
477 return wxFindWindowByLabel(title
, parent
);
482 wxFindWindowByName1 (const wxString
& title
, wxWindow
* parent
)
486 if ( parent
->GetName() == title
)
492 for (wxNode
* node
= parent
->GetChildren()->First (); node
; node
= node
->Next ())
494 wxWindow
*win
= (wxWindow
*) node
->Data ();
495 wxWindow
*retwin
= wxFindWindowByName1 (title
, win
);
502 return (wxWindow
*) NULL
; // Not found
506 // Returns menu item id or -1 if none.
508 wxFindMenuItemId (wxFrame
* frame
, const wxString
& menuString
, const wxString
& itemString
)
510 wxMenuBar
*menuBar
= frame
->GetMenuBar ();
513 return menuBar
->FindMenuItem (menuString
, itemString
);
519 #if !defined(_WINDLL)
521 wxDebugStreamBuf::wxDebugStreamBuf(void)
523 if (allocate()) setp(base(),ebuf());
526 int wxDebugStreamBuf::overflow(int WXUNUSED(i
))
528 int len
= pptr() - pbase();
529 char *txt
= new char[len
+1];
530 strncpy(txt
, pbase(), len
);
533 OutputDebugString((LPCSTR
)txt
);
535 fprintf(stderr
, txt
);
537 setp(pbase(), epptr());
542 int wxDebugStreamBuf::sync(void)
544 int len
= pptr() - pbase();
545 char *txt
= new char[len
+1];
546 strncpy(txt
, pbase(), len
);
549 OutputDebugString((LPCSTR
)txt
);
551 fprintf(stderr
, txt
);
553 setp(pbase(), epptr());
561 On Fri, 21 Jul 1995, Paul Craven wrote:
563 > Is there a way to find the path of running program's executable? I can get
564 > my home directory, and the current directory, but I don't know how to get the
565 > executable directory.
568 The code below (warty as it is), does what you want on most Unix,
569 DOS, and Mac platforms (it's from the ALS Prolog main).
571 || Ken Bowen Applied Logic Systems, Inc. PO Box 180,
572 ||==== Voice: +1 (617)965-9191 Newton Centre,
573 || FAX: +1 (617)965-1636 MA 02159 USA
574 Email: ken@als.com WWW: http://www.als.com
575 ------------------------------------------------------------------------
578 // This code is commented out but it may be integrated with wxWin at
579 // a later date, after testing. Thanks Ken!
582 /*--------------------------------------------------------------------*
583 | whereami is given a filename f in the form: whereami(argv[0])
584 | It returns the directory in which the executable file (containing
585 | this code [main.c] ) may be found. A dot will be returned to indicate
586 | the current directory.
587 *--------------------------------------------------------------------*/
593 register char *cutoff
= NULL
; /* stifle -Wall */
600 * See if the file is accessible either through the current directory
601 * or through an absolute path.
604 if (access(name
, R_OK
) == 0) {
606 /*-------------------------------------------------------------*
607 * The file was accessible without any other work. But the current
608 * working directory might change on us, so if it was accessible
609 * through the cwd, then we should get it for later accesses.
610 *-------------------------------------------------------------*/
613 if (!absolute_pathname(name
)) {
614 #if defined(DOS) || defined(__WIN32__)
620 if (*(name
+ 1) == ':') {
621 if (*name
>= 'a' && *name
<= 'z')
622 drive
= (int) (*name
- 'a' + 1);
624 drive
= (int) (*name
- 'A' + 1);
626 *newrbuf
++ = *(name
+ 1);
627 *newrbuf
++ = DIR_SEPARATOR
;
631 *newrbuf
++ = DIR_SEPARATOR
;
633 if (getcwd(newrbuf
, drive
) == 0) { /* } */
635 if (getcwd(newrbuf
, 1024) == 0) { /* } */
639 if (getwd(imagedir
) == 0) { /* } */
640 #else /* !HAVE_GETWD */
641 if (getcwd(imagedir
, 1024) == 0) {
642 #endif /* !HAVE_GETWD */
644 fatal_error(FE_GETCWD
, 0);
646 for (; *t
; t
++) /* Set t to end of buffer */
648 if (*(t
- 1) == DIR_SEPARATOR
) /* leave slash if already
653 cutoff
= t
; /* otherwise put one in */
654 *t
++ = DIR_SEPARATOR
;
657 #if (!defined(__MAC__) && !defined(__DJGPP__) && !defined(__GO32__) && !defined(__WIN32__))
659 (*t
++ = DIR_SEPARATOR
);
662 /*-------------------------------------------------------------*
663 * Copy the rest of the string and set the cutoff if it was not
664 * already set. If the first character of name is a slash, cutoff
665 * is not presently set but will be on the first iteration of the
667 *-------------------------------------------------------------*/
669 for ((*name
== DIR_SEPARATOR
? (s
= name
+1) : (s
= name
));;) {
670 if (*s
== DIR_SEPARATOR
)
679 /*-------------------------------------------------------------*
680 * Get the path list from the environment. If the path list is
681 * inaccessible for any reason, leave with fatal error.
682 *-------------------------------------------------------------*/
685 if ((s
= getenv("Commands")) == (char *) 0)
687 if ((s
= getenv("PATH")) == (char *) 0)
689 fatal_error(FE_PATH
, 0);
692 * Copy path list into ebuf and set the source pointer to the
693 * beginning of this buffer.
701 while (*s
&& *s
!= PATH_SEPARATOR
)
703 if (t
> imagedir
&& *(t
- 1) == DIR_SEPARATOR
)
704 ; /* do nothing -- slash already is in place */
706 *t
++ = DIR_SEPARATOR
; /* put in the slash */
707 cutoff
= t
- 1; /* set cutoff */
709 if (access(imagedir
, R_OK
) == 0)
713 s
++; /* advance source pointer */
715 fatal_error(FE_INFND
, 0);
720 /*-------------------------------------------------------------*
721 | At this point the full pathname should exist in imagedir and
722 | cutoff should be set to the final slash. We must now determine
723 | whether the file name is a symbolic link or not and chase it down
724 | if it is. Note that we reuse ebuf for getting the link.
725 *-------------------------------------------------------------*/
728 while ((cc
= readlink(imagedir
, ebuf
, 512)) != -1) {
731 if (*s
== DIR_SEPARATOR
) {
738 if (*s
== DIR_SEPARATOR
)
739 cutoff
= t
; /* mark the last slash seen */
740 if (!(*t
++ = *s
++)) /* copy the character */
745 #endif /* HAVE_SYMLINK */
747 strcpy(imagename
, cutoff
+ 1); /* keep the image name */
748 *(cutoff
+ 1) = 0; /* chop off the filename part */
754 * N.B. these convenience functions must be separate from msgdlgg.cpp, textdlgg.cpp
755 * since otherwise the generic code may be pulled in unnecessarily.
758 int wxMessageBox(const wxString
& message
, const wxString
& caption
, long style
,
759 wxWindow
*parent
, int WXUNUSED(x
), int WXUNUSED(y
) )
761 wxMessageDialog
dialog(parent
, message
, caption
, style
);
763 int ans
= dialog
.ShowModal();
783 wxString
wxGetTextFromUser(const wxString
& message
, const wxString
& caption
,
784 const wxString
& defaultValue
, wxWindow
*parent
,
785 int x
, int y
, bool WXUNUSED(centre
) )
787 wxTextEntryDialog
dialog(parent
, message
, caption
, defaultValue
, wxOK
|wxCANCEL
, wxPoint(x
, y
));
788 if (dialog
.ShowModal() == wxID_OK
)
789 return dialog
.GetValue();