]>
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"
48 #if !defined(__WATCOMC__)
49 #if !(defined(_MSC_VER) && (_MSC_VER > 800))
55 #include <sys/types.h>
59 // Pattern matching code.
60 // Yes, this path is deliberate (for Borland compilation)
61 #ifdef wx_mac /* MATTHEW: [5] Mac doesn't like paths with "/" */
64 #include "../common/glob.inc"
71 #define _MAXPATHLEN 500
73 extern char *wxBuffer
;
76 int strcasecmp(const char *str_1
, const char *str_2
)
80 c1
= tolower(*str_1
++);
81 c2
= tolower(*str_2
++);
82 } while ( c1
&& (c1
== c2
) );
87 int strncasecmp(const char *str_1
, const char *str_2
, size_t maxchar
)
93 c1
= tolower(*str_1
++);
94 c2
= tolower(*str_2
++);
106 // we have no strI functions under VMS, therefore I have implemented
107 // an inefficient but portable version: convert copies of strings to lowercase
108 // and then use the normal comparison
109 static void myLowerString(char *s
)
112 if(isalpha(*s
)) *s
= (char)tolower(*s
);
117 int strcasecmp(const char *str_1
, const char *str_2
)
119 char *temp1
= new char[strlen(str_1
)+1];
120 char *temp2
= new char[strlen(str_2
)+1];
123 myLowerString(temp1
);
124 myLowerString(temp2
);
126 int result
= strcmp(temp1
,temp2
);
133 int strncasecmp(const char *str_1
, const char *str_2
, size_t maxchar
)
135 char *temp1
= new char[strlen(str_1
)+1];
136 char *temp2
= new char[strlen(str_2
)+1];
139 myLowerString(temp1
);
140 myLowerString(temp2
);
142 int result
= strncmp(temp1
,temp2
,maxchar
);
154 #define strcasecmp stricmp
155 #define strncasecmp strnicmp
157 #define strcasecmp _stricmp
158 #define strncasecmp _strnicmp
163 #pragma warning (disable : 4245)
167 #pragma warning (default : 4245)
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__)
176 int strcasecmp (const char *, const char *);
177 int strncasecmp (const char *, const char *, size_t);
180 #endif /* __WXMSW__ */
184 copystring (const char *s
)
186 if (s
== NULL
) s
= "";
187 size_t len
= strlen (s
) + 1;
189 char *news
= new char[len
];
190 memcpy (news
, s
, len
); // Should be the fastest
196 static long wxCurrentId
= 100;
201 return wxCurrentId
++;
205 wxGetCurrentId(void) { return wxCurrentId
; }
208 wxRegisterId (long id
)
210 if (id
>= wxCurrentId
)
211 wxCurrentId
= id
+ 1;
215 StringToFloat (char *s
, float *number
)
217 if (s
&& *s
&& number
)
218 *number
= (float) strtod (s
, (char **) NULL
);
222 StringToDouble (char *s
, double *number
)
224 if (s
&& *s
&& number
)
225 *number
= strtod (s
, (char **) NULL
);
229 FloatToString (float number
, const char *fmt
)
231 static char buf
[256];
233 // sprintf (buf, "%.2f", number);
234 sprintf (buf
, fmt
, number
);
239 DoubleToString (double number
, const char *fmt
)
241 static char buf
[256];
243 sprintf (buf
, fmt
, number
);
248 StringToInt (char *s
, int *number
)
250 if (s
&& *s
&& number
)
251 *number
= (int) strtol (s
, (char **) NULL
, 10);
255 StringToLong (char *s
, long *number
)
257 if (s
&& *s
&& number
)
258 *number
= strtol (s
, (char **) NULL
, 10);
262 IntToString (int number
)
266 sprintf (buf
, "%d", number
);
271 LongToString (long number
)
275 sprintf (buf
, "%ld", number
);
279 // Array used in DecToHex conversion routine.
280 static char hexArray
[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B',
281 'C', 'D', 'E', 'F' };
283 // Convert 2-digit hex number to decimal
284 int wxHexToDec(const wxString
& buf
)
286 int firstDigit
, secondDigit
;
288 if (buf
.GetChar(0) >= 'A')
289 firstDigit
= buf
.GetChar(0) - 'A' + 10;
291 firstDigit
= buf
.GetChar(0) - '0';
293 if (buf
.GetChar(1) >= 'A')
294 secondDigit
= buf
.GetChar(1) - 'A' + 10;
296 secondDigit
= buf
.GetChar(1) - '0';
298 return firstDigit
* 16 + secondDigit
;
301 // Convert decimal integer to 2-character hex string
302 void wxDecToHex(int dec
, char *buf
)
304 int firstDigit
= (int)(dec
/16.0);
305 int secondDigit
= (int)(dec
- (firstDigit
*16.0));
306 buf
[0] = hexArray
[firstDigit
];
307 buf
[1] = hexArray
[secondDigit
];
311 // Convert decimal integer to 2-character hex string
312 wxString
wxDecToHex(int dec
)
315 wxDecToHex(dec
, buf
);
316 return wxString(buf
);
319 // Match a string INDEPENDENT OF CASE
321 StringMatch (char *str1
, char *str2
, bool subString
, bool exact
)
323 if (str1
== NULL
|| str2
== NULL
)
330 int len1
= strlen (str1
);
331 int len2
= strlen (str2
);
334 // Search for str1 in str2
335 // Slow .... but acceptable for short strings
336 for (i
= 0; i
<= len2
- len1
; i
++)
338 if (strncasecmp (str1
, str2
+ i
, len1
) == 0)
344 if (strcasecmp (str1
, str2
) == 0)
349 int len1
= strlen (str1
);
350 int len2
= strlen (str2
);
352 if (strncasecmp (str1
, str2
, wxMin (len1
, len2
)) == 0)
359 // Return the current date/time
361 wxString
wxNow( void )
363 time_t now
= time((time_t *) NULL
);
364 char *date
= ctime(&now
);
366 return wxString(date
);
369 /* Get Full RFC822 style email address */
371 wxGetEmailAddress (char *address
, int maxSize
)
376 if (wxGetHostName(host
, 64) == FALSE
)
378 if (wxGetUserId(user
, 64) == FALSE
)
386 strncpy(address
, tmp
, maxSize
- 1);
387 address
[maxSize
-1] = '\0';
392 * Strip out any menu codes
395 char *wxStripMenuCodes (char *in
, char *out
)
398 return (char *) NULL
;
401 out
= copystring(in
);
409 // Check && -> &, &x -> x
413 else if (*in
== '\t')
415 // Remove all stuff after \t in X mode, and let the stuff as is
417 // Accelerators are handled in wx_item.cc for Motif, and are not
418 // YET supported in XView
430 wxString
wxStripMenuCodes(const wxString
& str
)
432 char *buf
= new char[str
.Length() + 1];
433 wxStripMenuCodes((char*) (const char*) str
, buf
);
440 * Window search functions
445 * If parent is non-NULL, look through children for a label or title
446 * matching the specified string. If NULL, look through all top-level windows.
450 static wxWindow
*wxFindWindowByLabel1 (const wxString
& title
, wxWindow
* parent
);
453 wxFindWindowByLabel (const wxString
& title
, wxWindow
* parent
)
457 return wxFindWindowByLabel1 (title
, parent
);
461 for (wxNode
* node
= wxTopLevelWindows
.First (); node
; node
= node
->Next ())
463 wxWindow
*win
= (wxWindow
*) node
->Data ();
464 wxWindow
*retwin
= wxFindWindowByLabel1 (title
, win
);
470 return (wxWindow
*) NULL
;
475 wxFindWindowByLabel1 (const wxString
& title
, wxWindow
* parent
)
479 if (parent
->GetLabel() == title
)
485 for (wxNode
* node
= parent
->GetChildren().First (); node
; node
= node
->Next ())
487 wxWindow
*win
= (wxWindow
*) node
->Data ();
488 wxWindow
*retwin
= wxFindWindowByLabel1 (title
, win
);
495 return (wxWindow
*) NULL
; // Not found
500 * If parent is non-NULL, look through children for a name
501 * matching the specified string. If NULL, look through all top-level windows.
505 static wxWindow
*wxFindWindowByName1 (const wxString
& title
, wxWindow
* parent
);
508 wxFindWindowByName (const wxString
& title
, wxWindow
* parent
)
512 return wxFindWindowByName1 (title
, parent
);
516 for (wxNode
* node
= wxTopLevelWindows
.First (); node
; node
= node
->Next ())
518 wxWindow
*win
= (wxWindow
*) node
->Data ();
519 wxWindow
*retwin
= wxFindWindowByName1 (title
, win
);
525 // Failed? Try by label instead.
526 return wxFindWindowByLabel(title
, parent
);
531 wxFindWindowByName1 (const wxString
& title
, wxWindow
* parent
)
535 if ( parent
->GetName() == title
)
541 for (wxNode
* node
= parent
->GetChildren().First (); node
; node
= node
->Next ())
543 wxWindow
*win
= (wxWindow
*) node
->Data ();
544 wxWindow
*retwin
= wxFindWindowByName1 (title
, win
);
551 return (wxWindow
*) NULL
; // Not found
555 // Returns menu item id or -1 if none.
557 wxFindMenuItemId (wxFrame
* frame
, const wxString
& menuString
, const wxString
& itemString
)
559 wxMenuBar
*menuBar
= frame
->GetMenuBar ();
562 return menuBar
->FindMenuItem (menuString
, itemString
);
568 #if !defined(_WINDLL)
570 wxDebugStreamBuf::wxDebugStreamBuf(void)
572 // <iostream> usage doesn't need this, and i have no idea how to simulate it.
579 int wxDebugStreamBuf::overflow(int WXUNUSED(i
))
581 int len
= pptr() - pbase();
582 char *txt
= new char[len
+1];
583 strncpy(txt
, pbase(), len
);
586 OutputDebugString((LPCSTR
)txt
);
588 fprintf(stderr
, txt
);
590 setp(pbase(), epptr());
595 int wxDebugStreamBuf::sync(void)
597 int len
= pptr() - pbase();
598 char *txt
= new char[len
+1];
599 strncpy(txt
, pbase(), len
);
602 OutputDebugString((LPCSTR
)txt
);
604 fprintf(stderr
, txt
);
606 setp(pbase(), epptr());
614 On Fri, 21 Jul 1995, Paul Craven wrote:
616 > Is there a way to find the path of running program's executable? I can get
617 > my home directory, and the current directory, but I don't know how to get the
618 > executable directory.
621 The code below (warty as it is), does what you want on most Unix,
622 DOS, and Mac platforms (it's from the ALS Prolog main).
624 || Ken Bowen Applied Logic Systems, Inc. PO Box 180,
625 ||==== Voice: +1 (617)965-9191 Newton Centre,
626 || FAX: +1 (617)965-1636 MA 02159 USA
627 Email: ken@als.com WWW: http://www.als.com
628 ------------------------------------------------------------------------
631 // This code is commented out but it may be integrated with wxWin at
632 // a later date, after testing. Thanks Ken!
635 /*--------------------------------------------------------------------*
636 | whereami is given a filename f in the form: whereami(argv[0])
637 | It returns the directory in which the executable file (containing
638 | this code [main.c] ) may be found. A dot will be returned to indicate
639 | the current directory.
640 *--------------------------------------------------------------------*/
646 register char *cutoff
= NULL
; /* stifle -Wall */
653 * See if the file is accessible either through the current directory
654 * or through an absolute path.
657 if (access(name
, R_OK
) == 0) {
659 /*-------------------------------------------------------------*
660 * The file was accessible without any other work. But the current
661 * working directory might change on us, so if it was accessible
662 * through the cwd, then we should get it for later accesses.
663 *-------------------------------------------------------------*/
666 if (!absolute_pathname(name
)) {
667 #if defined(DOS) || defined(__WIN32__)
673 if (*(name
+ 1) == ':') {
674 if (*name
>= 'a' && *name
<= 'z')
675 drive
= (int) (*name
- 'a' + 1);
677 drive
= (int) (*name
- 'A' + 1);
679 *newrbuf
++ = *(name
+ 1);
680 *newrbuf
++ = DIR_SEPARATOR
;
684 *newrbuf
++ = DIR_SEPARATOR
;
686 if (getcwd(newrbuf
, drive
) == 0) { /* } */
688 if (getcwd(newrbuf
, 1024) == 0) { /* } */
692 if (getwd(imagedir
) == 0) { /* } */
693 #else /* !HAVE_GETWD */
694 if (getcwd(imagedir
, 1024) == 0) {
695 #endif /* !HAVE_GETWD */
697 fatal_error(FE_GETCWD
, 0);
699 for (; *t
; t
++) /* Set t to end of buffer */
701 if (*(t
- 1) == DIR_SEPARATOR
) /* leave slash if already
706 cutoff
= t
; /* otherwise put one in */
707 *t
++ = DIR_SEPARATOR
;
710 #if (!defined(__MAC__) && !defined(__DJGPP__) && !defined(__GO32__) && !defined(__WIN32__))
712 (*t
++ = DIR_SEPARATOR
);
715 /*-------------------------------------------------------------*
716 * Copy the rest of the string and set the cutoff if it was not
717 * already set. If the first character of name is a slash, cutoff
718 * is not presently set but will be on the first iteration of the
720 *-------------------------------------------------------------*/
722 for ((*name
== DIR_SEPARATOR
? (s
= name
+1) : (s
= name
));;) {
723 if (*s
== DIR_SEPARATOR
)
732 /*-------------------------------------------------------------*
733 * Get the path list from the environment. If the path list is
734 * inaccessible for any reason, leave with fatal error.
735 *-------------------------------------------------------------*/
738 if ((s
= getenv("Commands")) == (char *) 0)
740 if ((s
= getenv("PATH")) == (char *) 0)
742 fatal_error(FE_PATH
, 0);
745 * Copy path list into ebuf and set the source pointer to the
746 * beginning of this buffer.
754 while (*s
&& *s
!= PATH_SEPARATOR
)
756 if (t
> imagedir
&& *(t
- 1) == DIR_SEPARATOR
)
757 ; /* do nothing -- slash already is in place */
759 *t
++ = DIR_SEPARATOR
; /* put in the slash */
760 cutoff
= t
- 1; /* set cutoff */
762 if (access(imagedir
, R_OK
) == 0)
766 s
++; /* advance source pointer */
768 fatal_error(FE_INFND
, 0);
773 /*-------------------------------------------------------------*
774 | At this point the full pathname should exist in imagedir and
775 | cutoff should be set to the final slash. We must now determine
776 | whether the file name is a symbolic link or not and chase it down
777 | if it is. Note that we reuse ebuf for getting the link.
778 *-------------------------------------------------------------*/
781 while ((cc
= readlink(imagedir
, ebuf
, 512)) != -1) {
784 if (*s
== DIR_SEPARATOR
) {
791 if (*s
== DIR_SEPARATOR
)
792 cutoff
= t
; /* mark the last slash seen */
793 if (!(*t
++ = *s
++)) /* copy the character */
798 #endif /* HAVE_SYMLINK */
800 strcpy(imagename
, cutoff
+ 1); /* keep the image name */
801 *(cutoff
+ 1) = 0; /* chop off the filename part */
807 * N.B. these convenience functions must be separate from msgdlgg.cpp, textdlgg.cpp
808 * since otherwise the generic code may be pulled in unnecessarily.
811 int wxMessageBox(const wxString
& message
, const wxString
& caption
, long style
,
812 wxWindow
*parent
, int WXUNUSED(x
), int WXUNUSED(y
) )
814 wxMessageDialog
dialog(parent
, message
, caption
, style
);
816 int ans
= dialog
.ShowModal();
836 wxString
wxGetTextFromUser(const wxString
& message
, const wxString
& caption
,
837 const wxString
& defaultValue
, wxWindow
*parent
,
838 int x
, int y
, bool WXUNUSED(centre
) )
840 wxTextEntryDialog
dialog(parent
, message
, caption
, defaultValue
, wxOK
|wxCANCEL
, wxPoint(x
, y
));
841 if (dialog
.ShowModal() == wxID_OK
)
842 return dialog
.GetValue();
848 char *strdup(const char *s
)
850 return strcpy( (char*) malloc( strlen( s
) + 1 ) , s
) ;
855 return ( c
>= 0 && c
< 128 ) ;