]>
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>
63 // Pattern matching code.
64 // Yes, this path is deliberate (for Borland compilation)
65 #ifdef wx_mac /* MATTHEW: [5] Mac doesn't like paths with "/" */
68 #include "../common/glob.inc"
75 #define _MAXPATHLEN 500
77 extern char *wxBuffer
;
80 int strcasecmp(const char *str_1
, const char *str_2
)
84 c1
= tolower(*str_1
++);
85 c2
= tolower(*str_2
++);
86 } while ( c1
&& (c1
== c2
) );
91 int strncasecmp(const char *str_1
, const char *str_2
, size_t maxchar
)
97 c1
= tolower(*str_1
++);
98 c2
= tolower(*str_2
++);
110 // we have no strI functions under VMS, therefore I have implemented
111 // an inefficient but portable version: convert copies of strings to lowercase
112 // and then use the normal comparison
113 static void myLowerString(char *s
)
116 if(isalpha(*s
)) *s
= (char)tolower(*s
);
121 int strcasecmp(const char *str_1
, const char *str_2
)
123 char *temp1
= new char[strlen(str_1
)+1];
124 char *temp2
= new char[strlen(str_2
)+1];
127 myLowerString(temp1
);
128 myLowerString(temp2
);
130 int result
= strcmp(temp1
,temp2
);
137 int strncasecmp(const char *str_1
, const char *str_2
, size_t maxchar
)
139 char *temp1
= new char[strlen(str_1
)+1];
140 char *temp2
= new char[strlen(str_2
)+1];
143 myLowerString(temp1
);
144 myLowerString(temp2
);
146 int result
= strncmp(temp1
,temp2
,maxchar
);
158 #define strcasecmp stricmp
159 #define strncasecmp strnicmp
161 #define strcasecmp _stricmp
162 #define strncasecmp _strnicmp
167 #pragma warning (disable : 4245)
171 #pragma warning (default : 4245)
175 // This declaration is missing in SunOS!
176 // (Yes, I know it is NOT ANSI-C but its in BSD libc)
177 #if defined(__xlC) || defined(__AIX__) || defined(__GNUG__)
180 int strcasecmp (const char *, const char *);
181 int strncasecmp (const char *, const char *, size_t);
184 #endif /* __WXMSW__ */
188 copystring (const char *s
)
190 if (s
== NULL
) s
= "";
191 size_t len
= strlen (s
) + 1;
193 char *news
= new char[len
];
194 memcpy (news
, s
, len
); // Should be the fastest
200 static long wxCurrentId
= 100;
205 return wxCurrentId
++;
209 wxGetCurrentId(void) { return wxCurrentId
; }
212 wxRegisterId (long id
)
214 if (id
>= wxCurrentId
)
215 wxCurrentId
= id
+ 1;
219 StringToFloat (char *s
, float *number
)
221 if (s
&& *s
&& number
)
222 *number
= (float) strtod (s
, (char **) NULL
);
226 StringToDouble (char *s
, double *number
)
228 if (s
&& *s
&& number
)
229 *number
= strtod (s
, (char **) NULL
);
233 FloatToString (float number
, const char *fmt
)
235 static char buf
[256];
237 // sprintf (buf, "%.2f", number);
238 sprintf (buf
, fmt
, number
);
243 DoubleToString (double number
, const char *fmt
)
245 static char buf
[256];
247 sprintf (buf
, fmt
, number
);
252 StringToInt (char *s
, int *number
)
254 if (s
&& *s
&& number
)
255 *number
= (int) strtol (s
, (char **) NULL
, 10);
259 StringToLong (char *s
, long *number
)
261 if (s
&& *s
&& number
)
262 *number
= strtol (s
, (char **) NULL
, 10);
266 IntToString (int number
)
270 sprintf (buf
, "%d", number
);
275 LongToString (long number
)
279 sprintf (buf
, "%ld", number
);
283 // Array used in DecToHex conversion routine.
284 static char hexArray
[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B',
285 'C', 'D', 'E', 'F' };
287 // Convert 2-digit hex number to decimal
288 int wxHexToDec(const wxString
& buf
)
290 int firstDigit
, secondDigit
;
292 if (buf
.GetChar(0) >= 'A')
293 firstDigit
= buf
.GetChar(0) - 'A' + 10;
295 firstDigit
= buf
.GetChar(0) - '0';
297 if (buf
.GetChar(1) >= 'A')
298 secondDigit
= buf
.GetChar(1) - 'A' + 10;
300 secondDigit
= buf
.GetChar(1) - '0';
302 return firstDigit
* 16 + secondDigit
;
305 // Convert decimal integer to 2-character hex string
306 void wxDecToHex(int dec
, char *buf
)
308 int firstDigit
= (int)(dec
/16.0);
309 int secondDigit
= (int)(dec
- (firstDigit
*16.0));
310 buf
[0] = hexArray
[firstDigit
];
311 buf
[1] = hexArray
[secondDigit
];
315 // Convert decimal integer to 2-character hex string
316 wxString
wxDecToHex(int dec
)
319 wxDecToHex(dec
, buf
);
320 return wxString(buf
);
323 // Match a string INDEPENDENT OF CASE
325 StringMatch (char *str1
, char *str2
, bool subString
, bool exact
)
327 if (str1
== NULL
|| str2
== NULL
)
334 int len1
= strlen (str1
);
335 int len2
= strlen (str2
);
338 // Search for str1 in str2
339 // Slow .... but acceptable for short strings
340 for (i
= 0; i
<= len2
- len1
; i
++)
342 if (strncasecmp (str1
, str2
+ i
, len1
) == 0)
348 if (strcasecmp (str1
, str2
) == 0)
353 int len1
= strlen (str1
);
354 int len2
= strlen (str2
);
356 if (strncasecmp (str1
, str2
, wxMin (len1
, len2
)) == 0)
363 // Return the current date/time
365 wxString
wxNow( void )
367 time_t now
= time((time_t *) NULL
);
368 char *date
= ctime(&now
);
370 return wxString(date
);
373 /* Get Full RFC822 style email address */
375 wxGetEmailAddress (char *address
, int maxSize
)
380 if (wxGetHostName(host
, 64) == FALSE
)
382 if (wxGetUserId(user
, 64) == FALSE
)
390 strncpy(address
, tmp
, maxSize
- 1);
391 address
[maxSize
-1] = '\0';
396 * Strip out any menu codes
399 char *wxStripMenuCodes (char *in
, char *out
)
402 return (char *) NULL
;
405 out
= copystring(in
);
413 // Check && -> &, &x -> x
417 else if (*in
== '\t')
419 // Remove all stuff after \t in X mode, and let the stuff as is
421 // Accelerators are handled in wx_item.cc for Motif, and are not
422 // YET supported in XView
434 wxString
wxStripMenuCodes(const wxString
& str
)
436 char *buf
= new char[str
.Length() + 1];
437 wxStripMenuCodes((char*) (const char*) str
, buf
);
444 * Window search functions
449 * If parent is non-NULL, look through children for a label or title
450 * matching the specified string. If NULL, look through all top-level windows.
454 static wxWindow
*wxFindWindowByLabel1 (const wxString
& title
, wxWindow
* parent
);
457 wxFindWindowByLabel (const wxString
& title
, wxWindow
* parent
)
461 return wxFindWindowByLabel1 (title
, parent
);
465 for (wxNode
* node
= wxTopLevelWindows
.First (); node
; node
= node
->Next ())
467 wxWindow
*win
= (wxWindow
*) node
->Data ();
468 wxWindow
*retwin
= wxFindWindowByLabel1 (title
, win
);
474 return (wxWindow
*) NULL
;
479 wxFindWindowByLabel1 (const wxString
& title
, wxWindow
* parent
)
483 if (parent
->GetLabel() == title
)
489 for (wxNode
* node
= parent
->GetChildren().First (); node
; node
= node
->Next ())
491 wxWindow
*win
= (wxWindow
*) node
->Data ();
492 wxWindow
*retwin
= wxFindWindowByLabel1 (title
, win
);
499 return (wxWindow
*) NULL
; // Not found
504 * If parent is non-NULL, look through children for a name
505 * matching the specified string. If NULL, look through all top-level windows.
509 static wxWindow
*wxFindWindowByName1 (const wxString
& title
, wxWindow
* parent
);
512 wxFindWindowByName (const wxString
& title
, wxWindow
* parent
)
516 return wxFindWindowByName1 (title
, parent
);
520 for (wxNode
* node
= wxTopLevelWindows
.First (); node
; node
= node
->Next ())
522 wxWindow
*win
= (wxWindow
*) node
->Data ();
523 wxWindow
*retwin
= wxFindWindowByName1 (title
, win
);
529 // Failed? Try by label instead.
530 return wxFindWindowByLabel(title
, parent
);
535 wxFindWindowByName1 (const wxString
& title
, wxWindow
* parent
)
539 if ( parent
->GetName() == title
)
545 for (wxNode
* node
= parent
->GetChildren().First (); node
; node
= node
->Next ())
547 wxWindow
*win
= (wxWindow
*) node
->Data ();
548 wxWindow
*retwin
= wxFindWindowByName1 (title
, win
);
555 return (wxWindow
*) NULL
; // Not found
559 // Returns menu item id or -1 if none.
561 wxFindMenuItemId (wxFrame
* frame
, const wxString
& menuString
, const wxString
& itemString
)
563 wxMenuBar
*menuBar
= frame
->GetMenuBar ();
566 return menuBar
->FindMenuItem (menuString
, itemString
);
570 On Fri, 21 Jul 1995, Paul Craven wrote:
572 > Is there a way to find the path of running program's executable? I can get
573 > my home directory, and the current directory, but I don't know how to get the
574 > executable directory.
577 The code below (warty as it is), does what you want on most Unix,
578 DOS, and Mac platforms (it's from the ALS Prolog main).
580 || Ken Bowen Applied Logic Systems, Inc. PO Box 180,
581 ||==== Voice: +1 (617)965-9191 Newton Centre,
582 || FAX: +1 (617)965-1636 MA 02159 USA
583 Email: ken@als.com WWW: http://www.als.com
584 ------------------------------------------------------------------------
587 // This code is commented out but it may be integrated with wxWin at
588 // a later date, after testing. Thanks Ken!
591 /*--------------------------------------------------------------------*
592 | whereami is given a filename f in the form: whereami(argv[0])
593 | It returns the directory in which the executable file (containing
594 | this code [main.c] ) may be found. A dot will be returned to indicate
595 | the current directory.
596 *--------------------------------------------------------------------*/
602 register char *cutoff
= NULL
; /* stifle -Wall */
609 * See if the file is accessible either through the current directory
610 * or through an absolute path.
613 if (access(name
, R_OK
) == 0) {
615 /*-------------------------------------------------------------*
616 * The file was accessible without any other work. But the current
617 * working directory might change on us, so if it was accessible
618 * through the cwd, then we should get it for later accesses.
619 *-------------------------------------------------------------*/
622 if (!absolute_pathname(name
)) {
623 #if defined(DOS) || defined(__WIN32__)
629 if (*(name
+ 1) == ':') {
630 if (*name
>= 'a' && *name
<= 'z')
631 drive
= (int) (*name
- 'a' + 1);
633 drive
= (int) (*name
- 'A' + 1);
635 *newrbuf
++ = *(name
+ 1);
636 *newrbuf
++ = DIR_SEPARATOR
;
640 *newrbuf
++ = DIR_SEPARATOR
;
642 if (getcwd(newrbuf
, drive
) == 0) { /* } */
644 if (getcwd(newrbuf
, 1024) == 0) { /* } */
648 if (getwd(imagedir
) == 0) { /* } */
649 #else /* !HAVE_GETWD */
650 if (getcwd(imagedir
, 1024) == 0) {
651 #endif /* !HAVE_GETWD */
653 fatal_error(FE_GETCWD
, 0);
655 for (; *t
; t
++) /* Set t to end of buffer */
657 if (*(t
- 1) == DIR_SEPARATOR
) /* leave slash if already
662 cutoff
= t
; /* otherwise put one in */
663 *t
++ = DIR_SEPARATOR
;
666 #if (!defined(__MAC__) && !defined(__DJGPP__) && !defined(__GO32__) && !defined(__WIN32__))
668 (*t
++ = DIR_SEPARATOR
);
671 /*-------------------------------------------------------------*
672 * Copy the rest of the string and set the cutoff if it was not
673 * already set. If the first character of name is a slash, cutoff
674 * is not presently set but will be on the first iteration of the
676 *-------------------------------------------------------------*/
678 for ((*name
== DIR_SEPARATOR
? (s
= name
+1) : (s
= name
));;) {
679 if (*s
== DIR_SEPARATOR
)
688 /*-------------------------------------------------------------*
689 * Get the path list from the environment. If the path list is
690 * inaccessible for any reason, leave with fatal error.
691 *-------------------------------------------------------------*/
694 if ((s
= getenv("Commands")) == (char *) 0)
696 if ((s
= getenv("PATH")) == (char *) 0)
698 fatal_error(FE_PATH
, 0);
701 * Copy path list into ebuf and set the source pointer to the
702 * beginning of this buffer.
710 while (*s
&& *s
!= PATH_SEPARATOR
)
712 if (t
> imagedir
&& *(t
- 1) == DIR_SEPARATOR
)
713 ; /* do nothing -- slash already is in place */
715 *t
++ = DIR_SEPARATOR
; /* put in the slash */
716 cutoff
= t
- 1; /* set cutoff */
718 if (access(imagedir
, R_OK
) == 0)
722 s
++; /* advance source pointer */
724 fatal_error(FE_INFND
, 0);
729 /*-------------------------------------------------------------*
730 | At this point the full pathname should exist in imagedir and
731 | cutoff should be set to the final slash. We must now determine
732 | whether the file name is a symbolic link or not and chase it down
733 | if it is. Note that we reuse ebuf for getting the link.
734 *-------------------------------------------------------------*/
737 while ((cc
= readlink(imagedir
, ebuf
, 512)) != -1) {
740 if (*s
== DIR_SEPARATOR
) {
747 if (*s
== DIR_SEPARATOR
)
748 cutoff
= t
; /* mark the last slash seen */
749 if (!(*t
++ = *s
++)) /* copy the character */
754 #endif /* HAVE_SYMLINK */
756 strcpy(imagename
, cutoff
+ 1); /* keep the image name */
757 *(cutoff
+ 1) = 0; /* chop off the filename part */
763 * N.B. these convenience functions must be separate from msgdlgg.cpp, textdlgg.cpp
764 * since otherwise the generic code may be pulled in unnecessarily.
767 int wxMessageBox(const wxString
& message
, const wxString
& caption
, long style
,
768 wxWindow
*parent
, int WXUNUSED(x
), int WXUNUSED(y
) )
770 wxMessageDialog
dialog(parent
, message
, caption
, style
);
772 int ans
= dialog
.ShowModal();
792 wxString
wxGetTextFromUser(const wxString
& message
, const wxString
& caption
,
793 const wxString
& defaultValue
, wxWindow
*parent
,
794 int x
, int y
, bool WXUNUSED(centre
) )
796 wxTextEntryDialog
dialog(parent
, message
, caption
, defaultValue
, wxOK
|wxCANCEL
, wxPoint(x
, y
));
797 if (dialog
.ShowModal() == wxID_OK
)
798 return dialog
.GetValue();
804 char *strdup(const char *s
)
806 return strcpy( (char*) malloc( strlen( s
) + 1 ) , s
) ;
811 return ( c
>= 0 && c
< 128 ) ;