]>
git.saurik.com Git - wxWidgets.git/blob - src/common/utilscmn.cpp
55fbb4463026c0079f1442e79afbcddcbf0326f3
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))
54 #include <sys/types.h>
57 // Pattern matching code.
58 // Yes, this path is deliberate (for Borland compilation)
59 #ifdef wx_mac /* MATTHEW: [5] Mac doesn't like paths with "/" */
62 #include "../common/glob.inc"
69 #define _MAXPATHLEN 500
71 extern char *wxBuffer
;
74 // we have no strI functions under VMS, therefore I have implemented
75 // an inefficient but portable version: convert copies of strings to lowercase
76 // and then use the normal comparison
77 static void myLowerString(char *s
)
80 if(isalpha(*s
)) *s
= (char)tolower(*s
);
85 int strcasecmp(const char *str_1
, const char *str_2
)
87 char *temp1
= new char[strlen(str_1
)+1];
88 char *temp2
= new char[strlen(str_2
)+1];
94 int result
= strcmp(temp1
,temp2
);
101 int strncasecmp(const char *str_1
, const char *str_2
, size_t maxchar
)
103 char *temp1
= new char[strlen(str_1
)+1];
104 char *temp2
= new char[strlen(str_2
)+1];
107 myLowerString(temp1
);
108 myLowerString(temp2
);
110 int result
= strncmp(temp1
,temp2
,maxchar
);
121 #define strcasecmp stricmp
122 #define strncasecmp strnicmp
126 #pragma warning (disable : 4245)
130 #pragma warning (default : 4245)
134 // This declaration is missing in SunOS!
135 // (Yes, I know it is NOT ANSI-C but its in BSD libc)
136 #if defined(__xlC) || defined(__AIX__) || defined(__GNUG__)
139 int strcasecmp (const char *, const char *);
140 int strncasecmp (const char *, const char *, size_t);
143 #endif /* __WXMSW__ */
147 copystring (const char *s
)
149 if (s
== NULL
) s
= "";
150 size_t len
= strlen (s
) + 1;
152 char *news
= new char[len
];
153 memcpy (news
, s
, len
); // Should be the fastest
159 static long wxCurrentId
= 100;
164 return wxCurrentId
++;
168 wxGetCurrentId(void) { return wxCurrentId
; }
171 wxRegisterId (long id
)
173 if (id
>= wxCurrentId
)
174 wxCurrentId
= id
+ 1;
178 StringToFloat (char *s
, float *number
)
180 if (s
&& *s
&& number
)
181 *number
= (float) strtod (s
, (char **) NULL
);
185 StringToDouble (char *s
, double *number
)
187 if (s
&& *s
&& number
)
188 *number
= strtod (s
, (char **) NULL
);
192 FloatToString (float number
, const char *fmt
)
194 static char buf
[256];
196 // sprintf (buf, "%.2f", number);
197 sprintf (buf
, fmt
, number
);
202 DoubleToString (double number
, const char *fmt
)
204 static char buf
[256];
206 sprintf (buf
, fmt
, number
);
211 StringToInt (char *s
, int *number
)
213 if (s
&& *s
&& number
)
214 *number
= (int) strtol (s
, (char **) NULL
, 10);
218 StringToLong (char *s
, long *number
)
220 if (s
&& *s
&& number
)
221 *number
= strtol (s
, (char **) NULL
, 10);
225 IntToString (int number
)
229 sprintf (buf
, "%d", number
);
234 LongToString (long number
)
238 sprintf (buf
, "%ld", number
);
242 // Array used in DecToHex conversion routine.
243 static char hexArray
[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B',
244 'C', 'D', 'E', 'F' };
246 // Convert 2-digit hex number to decimal
247 int wxHexToDec(const wxString
& buf
)
249 int firstDigit
, secondDigit
;
251 if (buf
.GetChar(0) >= 'A')
252 firstDigit
= buf
.GetChar(0) - 'A' + 10;
254 firstDigit
= buf
.GetChar(0) - '0';
256 if (buf
.GetChar(1) >= 'A')
257 secondDigit
= buf
.GetChar(1) - 'A' + 10;
259 secondDigit
= buf
.GetChar(1) - '0';
261 return firstDigit
* 16 + secondDigit
;
264 // Convert decimal integer to 2-character hex string
265 void wxDecToHex(int dec
, char *buf
)
267 int firstDigit
= (int)(dec
/16.0);
268 int secondDigit
= (int)(dec
- (firstDigit
*16.0));
269 buf
[0] = hexArray
[firstDigit
];
270 buf
[1] = hexArray
[secondDigit
];
274 // Convert decimal integer to 2-character hex string
275 wxString
wxDecToHex(int dec
)
278 wxDecToHex(dec
, buf
);
279 return wxString(buf
);
282 // Match a string INDEPENDENT OF CASE
284 StringMatch (char *str1
, char *str2
, bool subString
, bool exact
)
286 if (str1
== NULL
|| str2
== NULL
)
293 int len1
= strlen (str1
);
294 int len2
= strlen (str2
);
297 // Search for str1 in str2
298 // Slow .... but acceptable for short strings
299 for (i
= 0; i
<= len2
- len1
; i
++)
301 if (strncasecmp (str1
, str2
+ i
, len1
) == 0)
307 if (strcasecmp (str1
, str2
) == 0)
312 int len1
= strlen (str1
);
313 int len2
= strlen (str2
);
315 if (strncasecmp (str1
, str2
, wxMin (len1
, len2
)) == 0)
322 // Return the current date/time
324 wxString
wxNow( void )
326 time_t now
= time((time_t *) NULL
);
327 char *date
= ctime(&now
);
329 return wxString(date
);
332 /* Get Full RFC822 style email address */
334 wxGetEmailAddress (char *address
, int maxSize
)
339 if (wxGetHostName(host
, 64) == FALSE
)
341 if (wxGetUserId(user
, 64) == FALSE
)
349 strncpy(address
, tmp
, maxSize
- 1);
350 address
[maxSize
-1] = '\0';
355 * Strip out any menu codes
358 char *wxStripMenuCodes (char *in
, char *out
)
361 return (char *) NULL
;
364 out
= copystring(in
);
372 // Check && -> &, &x -> x
376 else if (*in
== '\t')
378 // Remove all stuff after \t in X mode, and let the stuff as is
380 // Accelerators are handled in wx_item.cc for Motif, and are not
381 // YET supported in XView
395 * Window search functions
400 * If parent is non-NULL, look through children for a label or title
401 * matching the specified string. If NULL, look through all top-level windows.
405 static wxWindow
*wxFindWindowByLabel1 (const wxString
& title
, wxWindow
* parent
);
408 wxFindWindowByLabel (const wxString
& title
, wxWindow
* parent
)
412 return wxFindWindowByLabel1 (title
, parent
);
416 for (wxNode
* node
= wxTopLevelWindows
.First (); node
; node
= node
->Next ())
418 wxWindow
*win
= (wxWindow
*) node
->Data ();
419 wxWindow
*retwin
= wxFindWindowByLabel1 (title
, win
);
425 return (wxWindow
*) NULL
;
430 wxFindWindowByLabel1 (const wxString
& title
, wxWindow
* parent
)
434 if (parent
->GetLabel() == title
)
440 for (wxNode
* node
= parent
->GetChildren()->First (); node
; node
= node
->Next ())
442 wxWindow
*win
= (wxWindow
*) node
->Data ();
443 wxWindow
*retwin
= wxFindWindowByLabel1 (title
, win
);
450 return (wxWindow
*) NULL
; // Not found
455 * If parent is non-NULL, look through children for a name
456 * matching the specified string. If NULL, look through all top-level windows.
460 static wxWindow
*wxFindWindowByName1 (const wxString
& title
, wxWindow
* parent
);
463 wxFindWindowByName (const wxString
& title
, wxWindow
* parent
)
467 return wxFindWindowByName1 (title
, parent
);
471 for (wxNode
* node
= wxTopLevelWindows
.First (); node
; node
= node
->Next ())
473 wxWindow
*win
= (wxWindow
*) node
->Data ();
474 wxWindow
*retwin
= wxFindWindowByName1 (title
, win
);
480 // Failed? Try by label instead.
481 return wxFindWindowByLabel(title
, parent
);
486 wxFindWindowByName1 (const wxString
& title
, wxWindow
* parent
)
490 if ( parent
->GetName() == title
)
496 for (wxNode
* node
= parent
->GetChildren()->First (); node
; node
= node
->Next ())
498 wxWindow
*win
= (wxWindow
*) node
->Data ();
499 wxWindow
*retwin
= wxFindWindowByName1 (title
, win
);
506 return (wxWindow
*) NULL
; // Not found
510 // Returns menu item id or -1 if none.
512 wxFindMenuItemId (wxFrame
* frame
, const wxString
& menuString
, const wxString
& itemString
)
514 wxMenuBar
*menuBar
= frame
->GetMenuBar ();
517 return menuBar
->FindMenuItem (menuString
, itemString
);
523 #if !defined(_WINDLL)
525 wxDebugStreamBuf::wxDebugStreamBuf(void)
527 // <iostream> usage doesn't need this, and i have no idea how to simulate it.
534 int wxDebugStreamBuf::overflow(int WXUNUSED(i
))
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());
550 int wxDebugStreamBuf::sync(void)
552 int len
= pptr() - pbase();
553 char *txt
= new char[len
+1];
554 strncpy(txt
, pbase(), len
);
557 OutputDebugString((LPCSTR
)txt
);
559 fprintf(stderr
, txt
);
561 setp(pbase(), epptr());
569 On Fri, 21 Jul 1995, Paul Craven wrote:
571 > Is there a way to find the path of running program's executable? I can get
572 > my home directory, and the current directory, but I don't know how to get the
573 > executable directory.
576 The code below (warty as it is), does what you want on most Unix,
577 DOS, and Mac platforms (it's from the ALS Prolog main).
579 || Ken Bowen Applied Logic Systems, Inc. PO Box 180,
580 ||==== Voice: +1 (617)965-9191 Newton Centre,
581 || FAX: +1 (617)965-1636 MA 02159 USA
582 Email: ken@als.com WWW: http://www.als.com
583 ------------------------------------------------------------------------
586 // This code is commented out but it may be integrated with wxWin at
587 // a later date, after testing. Thanks Ken!
590 /*--------------------------------------------------------------------*
591 | whereami is given a filename f in the form: whereami(argv[0])
592 | It returns the directory in which the executable file (containing
593 | this code [main.c] ) may be found. A dot will be returned to indicate
594 | the current directory.
595 *--------------------------------------------------------------------*/
601 register char *cutoff
= NULL
; /* stifle -Wall */
608 * See if the file is accessible either through the current directory
609 * or through an absolute path.
612 if (access(name
, R_OK
) == 0) {
614 /*-------------------------------------------------------------*
615 * The file was accessible without any other work. But the current
616 * working directory might change on us, so if it was accessible
617 * through the cwd, then we should get it for later accesses.
618 *-------------------------------------------------------------*/
621 if (!absolute_pathname(name
)) {
622 #if defined(DOS) || defined(__WIN32__)
628 if (*(name
+ 1) == ':') {
629 if (*name
>= 'a' && *name
<= 'z')
630 drive
= (int) (*name
- 'a' + 1);
632 drive
= (int) (*name
- 'A' + 1);
634 *newrbuf
++ = *(name
+ 1);
635 *newrbuf
++ = DIR_SEPARATOR
;
639 *newrbuf
++ = DIR_SEPARATOR
;
641 if (getcwd(newrbuf
, drive
) == 0) { /* } */
643 if (getcwd(newrbuf
, 1024) == 0) { /* } */
647 if (getwd(imagedir
) == 0) { /* } */
648 #else /* !HAVE_GETWD */
649 if (getcwd(imagedir
, 1024) == 0) {
650 #endif /* !HAVE_GETWD */
652 fatal_error(FE_GETCWD
, 0);
654 for (; *t
; t
++) /* Set t to end of buffer */
656 if (*(t
- 1) == DIR_SEPARATOR
) /* leave slash if already
661 cutoff
= t
; /* otherwise put one in */
662 *t
++ = DIR_SEPARATOR
;
665 #if (!defined(__MAC__) && !defined(__DJGPP__) && !defined(__GO32__) && !defined(__WIN32__))
667 (*t
++ = DIR_SEPARATOR
);
670 /*-------------------------------------------------------------*
671 * Copy the rest of the string and set the cutoff if it was not
672 * already set. If the first character of name is a slash, cutoff
673 * is not presently set but will be on the first iteration of the
675 *-------------------------------------------------------------*/
677 for ((*name
== DIR_SEPARATOR
? (s
= name
+1) : (s
= name
));;) {
678 if (*s
== DIR_SEPARATOR
)
687 /*-------------------------------------------------------------*
688 * Get the path list from the environment. If the path list is
689 * inaccessible for any reason, leave with fatal error.
690 *-------------------------------------------------------------*/
693 if ((s
= getenv("Commands")) == (char *) 0)
695 if ((s
= getenv("PATH")) == (char *) 0)
697 fatal_error(FE_PATH
, 0);
700 * Copy path list into ebuf and set the source pointer to the
701 * beginning of this buffer.
709 while (*s
&& *s
!= PATH_SEPARATOR
)
711 if (t
> imagedir
&& *(t
- 1) == DIR_SEPARATOR
)
712 ; /* do nothing -- slash already is in place */
714 *t
++ = DIR_SEPARATOR
; /* put in the slash */
715 cutoff
= t
- 1; /* set cutoff */
717 if (access(imagedir
, R_OK
) == 0)
721 s
++; /* advance source pointer */
723 fatal_error(FE_INFND
, 0);
728 /*-------------------------------------------------------------*
729 | At this point the full pathname should exist in imagedir and
730 | cutoff should be set to the final slash. We must now determine
731 | whether the file name is a symbolic link or not and chase it down
732 | if it is. Note that we reuse ebuf for getting the link.
733 *-------------------------------------------------------------*/
736 while ((cc
= readlink(imagedir
, ebuf
, 512)) != -1) {
739 if (*s
== DIR_SEPARATOR
) {
746 if (*s
== DIR_SEPARATOR
)
747 cutoff
= t
; /* mark the last slash seen */
748 if (!(*t
++ = *s
++)) /* copy the character */
753 #endif /* HAVE_SYMLINK */
755 strcpy(imagename
, cutoff
+ 1); /* keep the image name */
756 *(cutoff
+ 1) = 0; /* chop off the filename part */
762 * N.B. these convenience functions must be separate from msgdlgg.cpp, textdlgg.cpp
763 * since otherwise the generic code may be pulled in unnecessarily.
766 int wxMessageBox(const wxString
& message
, const wxString
& caption
, long style
,
767 wxWindow
*parent
, int WXUNUSED(x
), int WXUNUSED(y
) )
769 wxMessageDialog
dialog(parent
, message
, caption
, style
);
771 int ans
= dialog
.ShowModal();
791 wxString
wxGetTextFromUser(const wxString
& message
, const wxString
& caption
,
792 const wxString
& defaultValue
, wxWindow
*parent
,
793 int x
, int y
, bool WXUNUSED(centre
) )
795 wxTextEntryDialog
dialog(parent
, message
, caption
, defaultValue
, wxOK
|wxCANCEL
, wxPoint(x
, y
));
796 if (dialog
.ShowModal() == wxID_OK
)
797 return dialog
.GetValue();