]>
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"
42 #if !defined(__WATCOMC__)
43 #if !(defined(_MSC_VER) && (_MSC_VER > 800))
48 #include <sys/types.h>
51 // Pattern matching code.
52 // Yes, this path is deliberate (for Borland compilation)
53 #ifdef wx_mac /* MATTHEW: [5] Mac doesn't like paths with "/" */
56 #include "../common/glob.inc"
63 #define _MAXPATHLEN 500
65 extern char *wxBuffer
;
68 // we have no strI functions under VMS, therefore I have implemented
69 // an inefficient but portable version: convert copies of strings to lowercase
70 // and then use the normal comparison
71 static void myLowerString(char *s
)
74 if(isalpha(*s
)) *s
= (char)tolower(*s
);
79 int strcasecmp(const char *str_1
, const char *str_2
)
81 char *temp1
= new char[strlen(str_1
)+1];
82 char *temp2
= new char[strlen(str_2
)+1];
88 int result
= strcmp(temp1
,temp2
);
95 int strncasecmp(const char *str_1
, const char *str_2
, size_t maxchar
)
97 char *temp1
= new char[strlen(str_1
)+1];
98 char *temp2
= new char[strlen(str_2
)+1];
101 myLowerString(temp1
);
102 myLowerString(temp2
);
104 int result
= strncmp(temp1
,temp2
,maxchar
);
115 #define strcasecmp stricmp
116 #define strncasecmp strnicmp
120 #pragma warning (disable : 4245)
124 #pragma warning (default : 4245)
128 // This declaration is missing in SunOS!
129 // (Yes, I know it is NOT ANSI-C but its in BSD libc)
130 #if defined(__xlC) || defined(__AIX__) || defined(__GNUG__)
133 int strcasecmp (const char *, const char *);
134 int strncasecmp (const char *, const char *, size_t);
137 #endif /* __WXMSW__ */
141 copystring (const char *s
)
143 if (s
== NULL
) s
= "";
144 size_t len
= strlen (s
) + 1;
146 char *news
= new char[len
];
147 memcpy (news
, s
, len
); // Should be the fastest
153 static long wxCurrentId
= 100;
158 return wxCurrentId
++;
162 wxGetCurrentId(void) { return wxCurrentId
; }
165 wxRegisterId (long id
)
167 if (id
>= wxCurrentId
)
168 wxCurrentId
= id
+ 1;
172 StringToFloat (char *s
, float *number
)
174 if (s
&& *s
&& number
)
175 *number
= (float) strtod (s
, (char **) NULL
);
179 StringToDouble (char *s
, double *number
)
181 if (s
&& *s
&& number
)
182 *number
= strtod (s
, (char **) NULL
);
186 FloatToString (float number
, const char *fmt
)
188 static char buf
[256];
190 // sprintf (buf, "%.2f", number);
191 sprintf (buf
, fmt
, number
);
196 DoubleToString (double number
, const char *fmt
)
198 static char buf
[256];
200 sprintf (buf
, fmt
, number
);
205 StringToInt (char *s
, int *number
)
207 if (s
&& *s
&& number
)
208 *number
= (int) strtol (s
, (char **) NULL
, 10);
212 StringToLong (char *s
, long *number
)
214 if (s
&& *s
&& number
)
215 *number
= strtol (s
, (char **) NULL
, 10);
219 IntToString (int number
)
223 sprintf (buf
, "%d", number
);
228 LongToString (long number
)
232 sprintf (buf
, "%ld", number
);
236 // Array used in DecToHex conversion routine.
237 static char hexArray
[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B',
238 'C', 'D', 'E', 'F' };
240 // Convert 2-digit hex number to decimal
241 int wxHexToDec(char *buf
)
243 int firstDigit
, secondDigit
;
246 firstDigit
= buf
[0] - 'A' + 10;
248 firstDigit
= buf
[0] - '0';
251 secondDigit
= buf
[1] - 'A' + 10;
253 secondDigit
= buf
[1] - '0';
255 return firstDigit
* 16 + secondDigit
;
258 // Convert decimal integer to 2-character hex string
259 void wxDecToHex(int dec
, char *buf
)
261 int firstDigit
= (int)(dec
/16.0);
262 int secondDigit
= (int)(dec
- (firstDigit
*16.0));
263 buf
[0] = hexArray
[firstDigit
];
264 buf
[1] = hexArray
[secondDigit
];
268 // Match a string INDEPENDENT OF CASE
270 StringMatch (char *str1
, char *str2
, bool subString
, bool exact
)
272 if (str1
== NULL
|| str2
== NULL
)
279 int len1
= strlen (str1
);
280 int len2
= strlen (str2
);
283 // Search for str1 in str2
284 // Slow .... but acceptable for short strings
285 for (i
= 0; i
<= len2
- len1
; i
++)
287 if (strncasecmp (str1
, str2
+ i
, len1
) == 0)
293 if (strcasecmp (str1
, str2
) == 0)
298 int len1
= strlen (str1
);
299 int len2
= strlen (str2
);
301 if (strncasecmp (str1
, str2
, wxMin (len1
, len2
)) == 0)
308 // Return the current date/time
310 wxString
wxNow( void )
312 time_t now
= time((time_t *) NULL
);
313 char *date
= ctime(&now
);
315 return wxString(date
);
318 /* Get Full RFC822 style email address */
320 wxGetEmailAddress (char *address
, int maxSize
)
325 if (wxGetHostName(host
, 64) == FALSE
)
327 if (wxGetUserId(user
, 64) == FALSE
)
335 strncpy(address
, tmp
, maxSize
- 1);
336 address
[maxSize
-1] = '\0';
341 * Strip out any menu codes
344 char *wxStripMenuCodes (char *in
, char *out
)
347 return (char *) NULL
;
350 out
= copystring(in
);
358 // Check && -> &, &x -> x
362 else if (*in
== '\t')
364 // Remove all stuff after \t in X mode, and let the stuff as is
366 // Accelerators are handled in wx_item.cc for Motif, and are not
367 // YET supported in XView
381 * Window search functions
386 * If parent is non-NULL, look through children for a label or title
387 * matching the specified string. If NULL, look through all top-level windows.
391 static wxWindow
*wxFindWindowByLabel1 (const wxString
& title
, wxWindow
* parent
);
394 wxFindWindowByLabel (const wxString
& title
, wxWindow
* parent
)
398 return wxFindWindowByLabel1 (title
, parent
);
402 for (wxNode
* node
= wxTopLevelWindows
.First (); node
; node
= node
->Next ())
404 wxWindow
*win
= (wxWindow
*) node
->Data ();
405 wxWindow
*retwin
= wxFindWindowByLabel1 (title
, win
);
411 return (wxWindow
*) NULL
;
416 wxFindWindowByLabel1 (const wxString
& title
, wxWindow
* parent
)
420 if (parent
->GetLabel() == title
)
426 for (wxNode
* node
= parent
->GetChildren()->First (); node
; node
= node
->Next ())
428 wxWindow
*win
= (wxWindow
*) node
->Data ();
429 wxWindow
*retwin
= wxFindWindowByLabel1 (title
, win
);
436 return (wxWindow
*) NULL
; // Not found
441 * If parent is non-NULL, look through children for a name
442 * matching the specified string. If NULL, look through all top-level windows.
446 static wxWindow
*wxFindWindowByName1 (const wxString
& title
, wxWindow
* parent
);
449 wxFindWindowByName (const wxString
& title
, wxWindow
* parent
)
453 return wxFindWindowByName1 (title
, parent
);
457 for (wxNode
* node
= wxTopLevelWindows
.First (); node
; node
= node
->Next ())
459 wxWindow
*win
= (wxWindow
*) node
->Data ();
460 wxWindow
*retwin
= wxFindWindowByName1 (title
, win
);
466 // Failed? Try by label instead.
467 return wxFindWindowByLabel(title
, parent
);
472 wxFindWindowByName1 (const wxString
& title
, wxWindow
* parent
)
476 if ( parent
->GetName() == title
)
482 for (wxNode
* node
= parent
->GetChildren()->First (); node
; node
= node
->Next ())
484 wxWindow
*win
= (wxWindow
*) node
->Data ();
485 wxWindow
*retwin
= wxFindWindowByName1 (title
, win
);
492 return (wxWindow
*) NULL
; // Not found
496 // Returns menu item id or -1 if none.
498 wxFindMenuItemId (wxFrame
* frame
, const wxString
& menuString
, const wxString
& itemString
)
500 wxMenuBar
*menuBar
= frame
->GetMenuBar ();
503 return menuBar
->FindMenuItem (menuString
, itemString
);
509 #if !defined(_WINDLL)
511 wxDebugStreamBuf::wxDebugStreamBuf(void)
513 if (allocate()) setp(base(),ebuf());
516 int wxDebugStreamBuf::overflow(int WXUNUSED(i
))
518 int len
= pptr() - pbase();
519 char *txt
= new char[len
+1];
520 strncpy(txt
, pbase(), len
);
523 OutputDebugString((LPCSTR
)txt
);
525 fprintf(stderr
, txt
);
527 setp(pbase(), epptr());
532 int wxDebugStreamBuf::sync(void)
534 int len
= pptr() - pbase();
535 char *txt
= new char[len
+1];
536 strncpy(txt
, pbase(), len
);
539 OutputDebugString((LPCSTR
)txt
);
541 fprintf(stderr
, txt
);
543 setp(pbase(), epptr());
551 On Fri, 21 Jul 1995, Paul Craven wrote:
553 > Is there a way to find the path of running program's executable? I can get
554 > my home directory, and the current directory, but I don't know how to get the
555 > executable directory.
558 The code below (warty as it is), does what you want on most Unix,
559 DOS, and Mac platforms (it's from the ALS Prolog main).
561 || Ken Bowen Applied Logic Systems, Inc. PO Box 180,
562 ||==== Voice: +1 (617)965-9191 Newton Centre,
563 || FAX: +1 (617)965-1636 MA 02159 USA
564 Email: ken@als.com WWW: http://www.als.com
565 ------------------------------------------------------------------------
568 // This code is commented out but it may be integrated with wxWin at
569 // a later date, after testing. Thanks Ken!
572 /*--------------------------------------------------------------------*
573 | whereami is given a filename f in the form: whereami(argv[0])
574 | It returns the directory in which the executable file (containing
575 | this code [main.c] ) may be found. A dot will be returned to indicate
576 | the current directory.
577 *--------------------------------------------------------------------*/
583 register char *cutoff
= NULL
; /* stifle -Wall */
590 * See if the file is accessible either through the current directory
591 * or through an absolute path.
594 if (access(name
, R_OK
) == 0) {
596 /*-------------------------------------------------------------*
597 * The file was accessible without any other work. But the current
598 * working directory might change on us, so if it was accessible
599 * through the cwd, then we should get it for later accesses.
600 *-------------------------------------------------------------*/
603 if (!absolute_pathname(name
)) {
604 #if defined(DOS) || defined(__WIN32__)
610 if (*(name
+ 1) == ':') {
611 if (*name
>= 'a' && *name
<= 'z')
612 drive
= (int) (*name
- 'a' + 1);
614 drive
= (int) (*name
- 'A' + 1);
616 *newrbuf
++ = *(name
+ 1);
617 *newrbuf
++ = DIR_SEPARATOR
;
621 *newrbuf
++ = DIR_SEPARATOR
;
623 if (getcwd(newrbuf
, drive
) == 0) { /* } */
625 if (getcwd(newrbuf
, 1024) == 0) { /* } */
629 if (getwd(imagedir
) == 0) { /* } */
630 #else /* !HAVE_GETWD */
631 if (getcwd(imagedir
, 1024) == 0) {
632 #endif /* !HAVE_GETWD */
634 fatal_error(FE_GETCWD
, 0);
636 for (; *t
; t
++) /* Set t to end of buffer */
638 if (*(t
- 1) == DIR_SEPARATOR
) /* leave slash if already
643 cutoff
= t
; /* otherwise put one in */
644 *t
++ = DIR_SEPARATOR
;
647 #if (!defined(__MAC__) && !defined(__DJGPP__) && !defined(__GO32__) && !defined(__WIN32__))
649 (*t
++ = DIR_SEPARATOR
);
652 /*-------------------------------------------------------------*
653 * Copy the rest of the string and set the cutoff if it was not
654 * already set. If the first character of name is a slash, cutoff
655 * is not presently set but will be on the first iteration of the
657 *-------------------------------------------------------------*/
659 for ((*name
== DIR_SEPARATOR
? (s
= name
+1) : (s
= name
));;) {
660 if (*s
== DIR_SEPARATOR
)
669 /*-------------------------------------------------------------*
670 * Get the path list from the environment. If the path list is
671 * inaccessible for any reason, leave with fatal error.
672 *-------------------------------------------------------------*/
675 if ((s
= getenv("Commands")) == (char *) 0)
677 if ((s
= getenv("PATH")) == (char *) 0)
679 fatal_error(FE_PATH
, 0);
682 * Copy path list into ebuf and set the source pointer to the
683 * beginning of this buffer.
691 while (*s
&& *s
!= PATH_SEPARATOR
)
693 if (t
> imagedir
&& *(t
- 1) == DIR_SEPARATOR
)
694 ; /* do nothing -- slash already is in place */
696 *t
++ = DIR_SEPARATOR
; /* put in the slash */
697 cutoff
= t
- 1; /* set cutoff */
699 if (access(imagedir
, R_OK
) == 0)
703 s
++; /* advance source pointer */
705 fatal_error(FE_INFND
, 0);
710 /*-------------------------------------------------------------*
711 | At this point the full pathname should exist in imagedir and
712 | cutoff should be set to the final slash. We must now determine
713 | whether the file name is a symbolic link or not and chase it down
714 | if it is. Note that we reuse ebuf for getting the link.
715 *-------------------------------------------------------------*/
718 while ((cc
= readlink(imagedir
, ebuf
, 512)) != -1) {
721 if (*s
== DIR_SEPARATOR
) {
728 if (*s
== DIR_SEPARATOR
)
729 cutoff
= t
; /* mark the last slash seen */
730 if (!(*t
++ = *s
++)) /* copy the character */
735 #endif /* HAVE_SYMLINK */
737 strcpy(imagename
, cutoff
+ 1); /* keep the image name */
738 *(cutoff
+ 1) = 0; /* chop off the filename part */