]>
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 // we have no strI functions under VMS, therefore I have implemented
77 // an inefficient but portable version: convert copies of strings to lowercase
78 // and then use the normal comparison
79 static void myLowerString(char *s
)
82 if(isalpha(*s
)) *s
= (char)tolower(*s
);
87 int strcasecmp(const char *str_1
, const char *str_2
)
89 char *temp1
= new char[strlen(str_1
)+1];
90 char *temp2
= new char[strlen(str_2
)+1];
96 int result
= strcmp(temp1
,temp2
);
103 int strncasecmp(const char *str_1
, const char *str_2
, size_t maxchar
)
105 char *temp1
= new char[strlen(str_1
)+1];
106 char *temp2
= new char[strlen(str_2
)+1];
109 myLowerString(temp1
);
110 myLowerString(temp2
);
112 int result
= strncmp(temp1
,temp2
,maxchar
);
124 #define strcasecmp stricmp
125 #define strncasecmp strnicmp
127 #define strcasecmp _stricmp
128 #define strncasecmp _strnicmp
133 #pragma warning (disable : 4245)
137 #pragma warning (default : 4245)
141 // This declaration is missing in SunOS!
142 // (Yes, I know it is NOT ANSI-C but its in BSD libc)
143 #if defined(__xlC) || defined(__AIX__) || defined(__GNUG__)
146 int strcasecmp (const char *, const char *);
147 int strncasecmp (const char *, const char *, size_t);
150 #endif /* __WXMSW__ */
154 copystring (const char *s
)
156 if (s
== NULL
) s
= "";
157 size_t len
= strlen (s
) + 1;
159 char *news
= new char[len
];
160 memcpy (news
, s
, len
); // Should be the fastest
166 static long wxCurrentId
= 100;
171 return wxCurrentId
++;
175 wxGetCurrentId(void) { return wxCurrentId
; }
178 wxRegisterId (long id
)
180 if (id
>= wxCurrentId
)
181 wxCurrentId
= id
+ 1;
185 StringToFloat (char *s
, float *number
)
187 if (s
&& *s
&& number
)
188 *number
= (float) strtod (s
, (char **) NULL
);
192 StringToDouble (char *s
, double *number
)
194 if (s
&& *s
&& number
)
195 *number
= strtod (s
, (char **) NULL
);
199 FloatToString (float number
, const char *fmt
)
201 static char buf
[256];
203 // sprintf (buf, "%.2f", number);
204 sprintf (buf
, fmt
, number
);
209 DoubleToString (double number
, const char *fmt
)
211 static char buf
[256];
213 sprintf (buf
, fmt
, number
);
218 StringToInt (char *s
, int *number
)
220 if (s
&& *s
&& number
)
221 *number
= (int) strtol (s
, (char **) NULL
, 10);
225 StringToLong (char *s
, long *number
)
227 if (s
&& *s
&& number
)
228 *number
= strtol (s
, (char **) NULL
, 10);
232 IntToString (int number
)
236 sprintf (buf
, "%d", number
);
241 LongToString (long number
)
245 sprintf (buf
, "%ld", number
);
249 // Array used in DecToHex conversion routine.
250 static char hexArray
[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B',
251 'C', 'D', 'E', 'F' };
253 // Convert 2-digit hex number to decimal
254 int wxHexToDec(const wxString
& buf
)
256 int firstDigit
, secondDigit
;
258 if (buf
.GetChar(0) >= 'A')
259 firstDigit
= buf
.GetChar(0) - 'A' + 10;
261 firstDigit
= buf
.GetChar(0) - '0';
263 if (buf
.GetChar(1) >= 'A')
264 secondDigit
= buf
.GetChar(1) - 'A' + 10;
266 secondDigit
= buf
.GetChar(1) - '0';
268 return firstDigit
* 16 + secondDigit
;
271 // Convert decimal integer to 2-character hex string
272 void wxDecToHex(int dec
, char *buf
)
274 int firstDigit
= (int)(dec
/16.0);
275 int secondDigit
= (int)(dec
- (firstDigit
*16.0));
276 buf
[0] = hexArray
[firstDigit
];
277 buf
[1] = hexArray
[secondDigit
];
281 // Convert decimal integer to 2-character hex string
282 wxString
wxDecToHex(int dec
)
285 wxDecToHex(dec
, buf
);
286 return wxString(buf
);
289 // Match a string INDEPENDENT OF CASE
291 StringMatch (char *str1
, char *str2
, bool subString
, bool exact
)
293 if (str1
== NULL
|| str2
== NULL
)
300 int len1
= strlen (str1
);
301 int len2
= strlen (str2
);
304 // Search for str1 in str2
305 // Slow .... but acceptable for short strings
306 for (i
= 0; i
<= len2
- len1
; i
++)
308 if (strncasecmp (str1
, str2
+ i
, len1
) == 0)
314 if (strcasecmp (str1
, str2
) == 0)
319 int len1
= strlen (str1
);
320 int len2
= strlen (str2
);
322 if (strncasecmp (str1
, str2
, wxMin (len1
, len2
)) == 0)
329 // Return the current date/time
331 wxString
wxNow( void )
333 time_t now
= time((time_t *) NULL
);
334 char *date
= ctime(&now
);
336 return wxString(date
);
339 /* Get Full RFC822 style email address */
341 wxGetEmailAddress (char *address
, int maxSize
)
346 if (wxGetHostName(host
, 64) == FALSE
)
348 if (wxGetUserId(user
, 64) == FALSE
)
356 strncpy(address
, tmp
, maxSize
- 1);
357 address
[maxSize
-1] = '\0';
362 * Strip out any menu codes
365 char *wxStripMenuCodes (char *in
, char *out
)
368 return (char *) NULL
;
371 out
= copystring(in
);
379 // Check && -> &, &x -> x
383 else if (*in
== '\t')
385 // Remove all stuff after \t in X mode, and let the stuff as is
387 // Accelerators are handled in wx_item.cc for Motif, and are not
388 // YET supported in XView
400 wxString
wxStripMenuCodes(const wxString
& str
)
402 char *buf
= new char[str
.Length() + 1];
403 wxStripMenuCodes((char*) (const char*) str
, buf
);
410 * Window search functions
415 * If parent is non-NULL, look through children for a label or title
416 * matching the specified string. If NULL, look through all top-level windows.
420 static wxWindow
*wxFindWindowByLabel1 (const wxString
& title
, wxWindow
* parent
);
423 wxFindWindowByLabel (const wxString
& title
, wxWindow
* parent
)
427 return wxFindWindowByLabel1 (title
, parent
);
431 for (wxNode
* node
= wxTopLevelWindows
.First (); node
; node
= node
->Next ())
433 wxWindow
*win
= (wxWindow
*) node
->Data ();
434 wxWindow
*retwin
= wxFindWindowByLabel1 (title
, win
);
440 return (wxWindow
*) NULL
;
445 wxFindWindowByLabel1 (const wxString
& title
, wxWindow
* parent
)
449 if (parent
->GetLabel() == title
)
455 for (wxNode
* node
= parent
->GetChildren().First (); node
; node
= node
->Next ())
457 wxWindow
*win
= (wxWindow
*) node
->Data ();
458 wxWindow
*retwin
= wxFindWindowByLabel1 (title
, win
);
465 return (wxWindow
*) NULL
; // Not found
470 * If parent is non-NULL, look through children for a name
471 * matching the specified string. If NULL, look through all top-level windows.
475 static wxWindow
*wxFindWindowByName1 (const wxString
& title
, wxWindow
* parent
);
478 wxFindWindowByName (const wxString
& title
, wxWindow
* parent
)
482 return wxFindWindowByName1 (title
, parent
);
486 for (wxNode
* node
= wxTopLevelWindows
.First (); node
; node
= node
->Next ())
488 wxWindow
*win
= (wxWindow
*) node
->Data ();
489 wxWindow
*retwin
= wxFindWindowByName1 (title
, win
);
495 // Failed? Try by label instead.
496 return wxFindWindowByLabel(title
, parent
);
501 wxFindWindowByName1 (const wxString
& title
, wxWindow
* parent
)
505 if ( parent
->GetName() == title
)
511 for (wxNode
* node
= parent
->GetChildren().First (); node
; node
= node
->Next ())
513 wxWindow
*win
= (wxWindow
*) node
->Data ();
514 wxWindow
*retwin
= wxFindWindowByName1 (title
, win
);
521 return (wxWindow
*) NULL
; // Not found
525 // Returns menu item id or -1 if none.
527 wxFindMenuItemId (wxFrame
* frame
, const wxString
& menuString
, const wxString
& itemString
)
529 wxMenuBar
*menuBar
= frame
->GetMenuBar ();
532 return menuBar
->FindMenuItem (menuString
, itemString
);
538 #if !defined(_WINDLL)
540 wxDebugStreamBuf::wxDebugStreamBuf(void)
542 // <iostream> usage doesn't need this, and i have no idea how to simulate it.
549 int wxDebugStreamBuf::overflow(int WXUNUSED(i
))
551 int len
= pptr() - pbase();
552 char *txt
= new char[len
+1];
553 strncpy(txt
, pbase(), len
);
556 OutputDebugString((LPCSTR
)txt
);
558 fprintf(stderr
, txt
);
560 setp(pbase(), epptr());
565 int wxDebugStreamBuf::sync(void)
567 int len
= pptr() - pbase();
568 char *txt
= new char[len
+1];
569 strncpy(txt
, pbase(), len
);
572 OutputDebugString((LPCSTR
)txt
);
574 fprintf(stderr
, txt
);
576 setp(pbase(), epptr());
584 On Fri, 21 Jul 1995, Paul Craven wrote:
586 > Is there a way to find the path of running program's executable? I can get
587 > my home directory, and the current directory, but I don't know how to get the
588 > executable directory.
591 The code below (warty as it is), does what you want on most Unix,
592 DOS, and Mac platforms (it's from the ALS Prolog main).
594 || Ken Bowen Applied Logic Systems, Inc. PO Box 180,
595 ||==== Voice: +1 (617)965-9191 Newton Centre,
596 || FAX: +1 (617)965-1636 MA 02159 USA
597 Email: ken@als.com WWW: http://www.als.com
598 ------------------------------------------------------------------------
601 // This code is commented out but it may be integrated with wxWin at
602 // a later date, after testing. Thanks Ken!
605 /*--------------------------------------------------------------------*
606 | whereami is given a filename f in the form: whereami(argv[0])
607 | It returns the directory in which the executable file (containing
608 | this code [main.c] ) may be found. A dot will be returned to indicate
609 | the current directory.
610 *--------------------------------------------------------------------*/
616 register char *cutoff
= NULL
; /* stifle -Wall */
623 * See if the file is accessible either through the current directory
624 * or through an absolute path.
627 if (access(name
, R_OK
) == 0) {
629 /*-------------------------------------------------------------*
630 * The file was accessible without any other work. But the current
631 * working directory might change on us, so if it was accessible
632 * through the cwd, then we should get it for later accesses.
633 *-------------------------------------------------------------*/
636 if (!absolute_pathname(name
)) {
637 #if defined(DOS) || defined(__WIN32__)
643 if (*(name
+ 1) == ':') {
644 if (*name
>= 'a' && *name
<= 'z')
645 drive
= (int) (*name
- 'a' + 1);
647 drive
= (int) (*name
- 'A' + 1);
649 *newrbuf
++ = *(name
+ 1);
650 *newrbuf
++ = DIR_SEPARATOR
;
654 *newrbuf
++ = DIR_SEPARATOR
;
656 if (getcwd(newrbuf
, drive
) == 0) { /* } */
658 if (getcwd(newrbuf
, 1024) == 0) { /* } */
662 if (getwd(imagedir
) == 0) { /* } */
663 #else /* !HAVE_GETWD */
664 if (getcwd(imagedir
, 1024) == 0) {
665 #endif /* !HAVE_GETWD */
667 fatal_error(FE_GETCWD
, 0);
669 for (; *t
; t
++) /* Set t to end of buffer */
671 if (*(t
- 1) == DIR_SEPARATOR
) /* leave slash if already
676 cutoff
= t
; /* otherwise put one in */
677 *t
++ = DIR_SEPARATOR
;
680 #if (!defined(__MAC__) && !defined(__DJGPP__) && !defined(__GO32__) && !defined(__WIN32__))
682 (*t
++ = DIR_SEPARATOR
);
685 /*-------------------------------------------------------------*
686 * Copy the rest of the string and set the cutoff if it was not
687 * already set. If the first character of name is a slash, cutoff
688 * is not presently set but will be on the first iteration of the
690 *-------------------------------------------------------------*/
692 for ((*name
== DIR_SEPARATOR
? (s
= name
+1) : (s
= name
));;) {
693 if (*s
== DIR_SEPARATOR
)
702 /*-------------------------------------------------------------*
703 * Get the path list from the environment. If the path list is
704 * inaccessible for any reason, leave with fatal error.
705 *-------------------------------------------------------------*/
708 if ((s
= getenv("Commands")) == (char *) 0)
710 if ((s
= getenv("PATH")) == (char *) 0)
712 fatal_error(FE_PATH
, 0);
715 * Copy path list into ebuf and set the source pointer to the
716 * beginning of this buffer.
724 while (*s
&& *s
!= PATH_SEPARATOR
)
726 if (t
> imagedir
&& *(t
- 1) == DIR_SEPARATOR
)
727 ; /* do nothing -- slash already is in place */
729 *t
++ = DIR_SEPARATOR
; /* put in the slash */
730 cutoff
= t
- 1; /* set cutoff */
732 if (access(imagedir
, R_OK
) == 0)
736 s
++; /* advance source pointer */
738 fatal_error(FE_INFND
, 0);
743 /*-------------------------------------------------------------*
744 | At this point the full pathname should exist in imagedir and
745 | cutoff should be set to the final slash. We must now determine
746 | whether the file name is a symbolic link or not and chase it down
747 | if it is. Note that we reuse ebuf for getting the link.
748 *-------------------------------------------------------------*/
751 while ((cc
= readlink(imagedir
, ebuf
, 512)) != -1) {
754 if (*s
== DIR_SEPARATOR
) {
761 if (*s
== DIR_SEPARATOR
)
762 cutoff
= t
; /* mark the last slash seen */
763 if (!(*t
++ = *s
++)) /* copy the character */
768 #endif /* HAVE_SYMLINK */
770 strcpy(imagename
, cutoff
+ 1); /* keep the image name */
771 *(cutoff
+ 1) = 0; /* chop off the filename part */
777 * N.B. these convenience functions must be separate from msgdlgg.cpp, textdlgg.cpp
778 * since otherwise the generic code may be pulled in unnecessarily.
781 int wxMessageBox(const wxString
& message
, const wxString
& caption
, long style
,
782 wxWindow
*parent
, int WXUNUSED(x
), int WXUNUSED(y
) )
784 wxMessageDialog
dialog(parent
, message
, caption
, style
);
786 int ans
= dialog
.ShowModal();
806 wxString
wxGetTextFromUser(const wxString
& message
, const wxString
& caption
,
807 const wxString
& defaultValue
, wxWindow
*parent
,
808 int x
, int y
, bool WXUNUSED(centre
) )
810 wxTextEntryDialog
dialog(parent
, message
, caption
, defaultValue
, wxOK
|wxCANCEL
, wxPoint(x
, y
));
811 if (dialog
.ShowModal() == wxID_OK
)
812 return dialog
.GetValue();
818 char *strdup(const char *s
)
820 return strcpy( (char*) malloc( strlen( s
) + 1 ) , s
) ;
825 return ( c
>= 0 && c
< 128 ) ;