]> git.saurik.com Git - wxWidgets.git/blame - src/common/utilscmn.cpp
static wxFile::Access() added
[wxWidgets.git] / src / common / utilscmn.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: utilscmn.cpp
3// Purpose: Miscellaneous utility functions and classes
4// Author: Julian Smart
5// Modified by:
6// Created: 29/01/98
7// RCS-ID: $Id$
8// Copyright: (c) 1998 Julian Smart
9// Licence: wxWindows license
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
7007fcfc 13#pragma implementation "utils.h"
c801d85f
KB
14#endif
15
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
18
19#ifdef __BORLANDC__
20#pragma hdrstop
21#endif
22
23#ifndef WX_PRECOMP
24#include "wx/defs.h"
25#include "wx/utils.h"
26#include "wx/window.h"
27#include "wx/menu.h"
28#include "wx/frame.h"
29#endif
30
31#if USE_IOSTREAMH
32#include <iostream.h>
33#else
34#include <iostream>
35#endif
36
37#include <fstream.h>
38#include <ctype.h>
39#include <stdio.h>
40#include <stdlib.h>
41#include <string.h>
42#if !defined(__WATCOMC__)
43#if !(defined(_MSC_VER) && (_MSC_VER > 800))
44#include <errno.h>
45#endif
46#endif
47#include <time.h>
48#include <sys/types.h>
49#include <sys/stat.h>
50
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 "/" */
54#include "glob.inc"
55#else
56#include "../common/glob.inc"
57#endif
58
2049ba38 59#ifdef __WXMSW__
c801d85f
KB
60#include "windows.h"
61#endif
62
63#define _MAXPATHLEN 500
64
65extern char *wxBuffer;
66
67#ifdef __VMS__
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
71static void myLowerString(char *s)
72{
73 while(*s){
74 if(isalpha(*s)) *s = (char)tolower(*s);
75 s++;
76 }
77}
78
79int strcasecmp(const char *str_1, const char *str_2)
80{
81 char *temp1 = new char[strlen(str_1)+1];
82 char *temp2 = new char[strlen(str_2)+1];
83 strcpy(temp1,str_1);
84 strcpy(temp2,str_2);
85 myLowerString(temp1);
86 myLowerString(temp2);
87
88 int result = strcmp(temp1,temp2);
89 delete[] temp1;
90 delete[] temp2;
91
92 return(result);
93}
94
95int strncasecmp(const char *str_1, const char *str_2, size_t maxchar)
96{
97 char *temp1 = new char[strlen(str_1)+1];
98 char *temp2 = new char[strlen(str_2)+1];
99 strcpy(temp1,str_1);
100 strcpy(temp2,str_2);
101 myLowerString(temp1);
102 myLowerString(temp2);
103
104 int result = strncmp(temp1,temp2,maxchar);
105 delete[] temp1;
106 delete[] temp2;
107
108 return(result);
109}
110#endif
111
2049ba38 112#ifdef __WXMSW__
c801d85f
KB
113
114#ifndef __GNUWIN32__
115#define strcasecmp stricmp
116#define strncasecmp strnicmp
117#endif
118
119#ifdef _MSC_VER
120#pragma warning (disable : 4245)
121#endif
122
123#ifdef _MSC_VER
124#pragma warning (default : 4245)
125#endif
126
127#else
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__)
131extern "C"
132{
133 int strcasecmp (const char *, const char *);
134 int strncasecmp (const char *, const char *, size_t);
135}
136#endif
2049ba38 137#endif /* __WXMSW__ */
c801d85f
KB
138
139
140char *
141copystring (const char *s)
142{
143 if (s == NULL) s = "";
144 size_t len = strlen (s) + 1;
145
146 char *news = new char[len];
147 memcpy (news, s, len); // Should be the fastest
148
149 return news;
150}
151
152// Id generation
153static long wxCurrentId = 100;
154
155long
156wxNewId (void)
157{
158 return wxCurrentId++;
159}
160
161long
162wxGetCurrentId(void) { return wxCurrentId; }
163
164void
165wxRegisterId (long id)
166{
167 if (id >= wxCurrentId)
168 wxCurrentId = id + 1;
169}
170
171void
172StringToFloat (char *s, float *number)
173{
174 if (s && *s && number)
175 *number = (float) strtod (s, NULL);
176}
177
178void
179StringToDouble (char *s, double *number)
180{
181 if (s && *s && number)
182 *number = strtod (s, NULL);
183}
184
185char *
186FloatToString (float number, const char *fmt)
187{
188 static char buf[256];
189
190// sprintf (buf, "%.2f", number);
191 sprintf (buf, fmt, number);
192 return buf;
193}
194
195char *
196DoubleToString (double number, const char *fmt)
197{
198 static char buf[256];
199
200 sprintf (buf, fmt, number);
201 return buf;
202}
203
204void
205StringToInt (char *s, int *number)
206{
207 if (s && *s && number)
208 *number = (int) strtol (s, NULL, 10);
209}
210
211void
212StringToLong (char *s, long *number)
213{
214 if (s && *s && number)
215 *number = strtol (s, NULL, 10);
216}
217
218char *
219IntToString (int number)
220{
221 static char buf[20];
222
223 sprintf (buf, "%d", number);
224 return buf;
225}
226
227char *
228LongToString (long number)
229{
230 static char buf[20];
231
232 sprintf (buf, "%ld", number);
233 return buf;
234}
235
236// Array used in DecToHex conversion routine.
237static char hexArray[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B',
238 'C', 'D', 'E', 'F' };
239
240// Convert 2-digit hex number to decimal
241int wxHexToDec(char *buf)
242{
243 int firstDigit, secondDigit;
244
245 if (buf[0] >= 'A')
246 firstDigit = buf[0] - 'A' + 10;
247 else
248 firstDigit = buf[0] - '0';
249
250 if (buf[1] >= 'A')
251 secondDigit = buf[1] - 'A' + 10;
252 else
253 secondDigit = buf[1] - '0';
254
255 return firstDigit * 16 + secondDigit;
256}
257
258// Convert decimal integer to 2-character hex string
259void wxDecToHex(int dec, char *buf)
260{
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];
265 buf[2] = 0;
266}
267
268// Match a string INDEPENDENT OF CASE
269bool
270StringMatch (char *str1, char *str2, bool subString, bool exact)
271{
272 if (str1 == NULL || str2 == NULL)
273 return FALSE;
274 if (str1 == str2)
275 return TRUE;
276
277 if (subString)
278 {
279 int len1 = strlen (str1);
280 int len2 = strlen (str2);
281 int i;
282
283 // Search for str1 in str2
284 // Slow .... but acceptable for short strings
285 for (i = 0; i <= len2 - len1; i++)
286 {
287 if (strncasecmp (str1, str2 + i, len1) == 0)
288 return TRUE;
289 }
290 }
291 else if (exact)
292 {
293 if (strcasecmp (str1, str2) == 0)
294 return TRUE;
295 }
296 else
297 {
298 int len1 = strlen (str1);
299 int len2 = strlen (str2);
300
301 if (strncasecmp (str1, str2, wxMin (len1, len2)) == 0)
302 return TRUE;
303 }
304
305 return FALSE;
306}
307
308// Return the current date/time
309// [volatile]
310wxString wxNow( void )
311{
312 time_t now = time(NULL);
313 char *date = ctime(&now);
314 date[24] = '\0';
315 return wxString(date);
316}
317
318/* Get Full RFC822 style email address */
319bool
320wxGetEmailAddress (char *address, int maxSize)
321{
322 char host[65];
323 char user[65];
324
325 if (wxGetHostName(host, 64) == FALSE)
326 return FALSE;
327 if (wxGetUserId(user, 64) == FALSE)
328 return FALSE;
329
330 char tmp[130];
331 strcpy(tmp, user);
332 strcat(tmp, "@");
333 strcat(tmp, host);
334
335 strncpy(address, tmp, maxSize - 1);
336 address[maxSize-1] = '\0';
337 return TRUE;
338}
339
340/*
341 * Strip out any menu codes
342 */
343
344char *wxStripMenuCodes (char *in, char *out)
345{
346 if (!in)
347 return NULL;
348
349 if (!out)
350 out = copystring(in);
351
352 char *tmpOut = out;
353
354 while (*in)
355 {
356 if (*in == '&')
357 {
358 // Check && -> &, &x -> x
359 if (*++in == '&')
360 *out++ = *in++;
361 }
362 else if (*in == '\t')
363 {
364 // Remove all stuff after \t in X mode, and let the stuff as is
365 // in Windows mode.
366 // Accelerators are handled in wx_item.cc for Motif, and are not
367 // YET supported in XView
368 break;
369 }
370 else
371 *out++ = *in++;
372 } // while
373
374 *out = '\0';
375
376 return tmpOut;
377}
378
379
380/*
381 * Window search functions
382 *
383 */
384
385/*
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.
388 *
389 */
390
391static wxWindow *wxFindWindowByLabel1 (const wxString& title, wxWindow * parent);
392
393wxWindow *
394wxFindWindowByLabel (const wxString& title, wxWindow * parent)
395{
396 if (parent)
397 {
398 return wxFindWindowByLabel1 (title, parent);
399 }
400 else
401 {
402 for (wxNode * node = wxTopLevelWindows.First (); node; node = node->Next ())
403 {
404 wxWindow *win = (wxWindow *) node->Data ();
405 wxWindow *retwin = wxFindWindowByLabel1 (title, win);
406 if (retwin)
407 return retwin;
408 } // for()
409
410 }
411 return NULL;
412}
413
414// Recursive
415static wxWindow *
416wxFindWindowByLabel1 (const wxString& title, wxWindow * parent)
417{
418 if (parent)
419 {
420 if (parent->GetLabel() == title)
421 return parent;
422 }
423
424 if (parent)
425 {
426 for (wxNode * node = parent->GetChildren()->First (); node; node = node->Next ())
427 {
428 wxWindow *win = (wxWindow *) node->Data ();
429 wxWindow *retwin = wxFindWindowByLabel1 (title, win);
430 if (retwin)
431 return retwin;
432 } // for()
433
434 }
435
436 return NULL; // Not found
437
438}
439
440/*
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.
443 *
444 */
445
446static wxWindow *wxFindWindowByName1 (const wxString& title, wxWindow * parent);
447
448wxWindow *
449wxFindWindowByName (const wxString& title, wxWindow * parent)
450{
451 if (parent)
452 {
453 return wxFindWindowByName1 (title, parent);
454 }
455 else
456 {
457 for (wxNode * node = wxTopLevelWindows.First (); node; node = node->Next ())
458 {
459 wxWindow *win = (wxWindow *) node->Data ();
460 wxWindow *retwin = wxFindWindowByName1 (title, win);
461 if (retwin)
462 return retwin;
463 } // for()
464
465 }
466 // Failed? Try by label instead.
467 return wxFindWindowByLabel(title, parent);
468}
469
470// Recursive
471static wxWindow *
472wxFindWindowByName1 (const wxString& title, wxWindow * parent)
473{
474 if (parent)
475 {
476 if ( parent->GetName() == title )
477 return parent;
478 }
479
480 if (parent)
481 {
482 for (wxNode * node = parent->GetChildren()->First (); node; node = node->Next ())
483 {
484 wxWindow *win = (wxWindow *) node->Data ();
485 wxWindow *retwin = wxFindWindowByName1 (title, win);
486 if (retwin)
487 return retwin;
488 } // for()
489
490 }
491
492 return NULL; // Not found
493
494}
495
496// Returns menu item id or -1 if none.
497int
498wxFindMenuItemId (wxFrame * frame, const wxString& menuString, const wxString& itemString)
499{
500 wxMenuBar *menuBar = frame->GetMenuBar ();
501 if (!menuBar)
502 return -1;
503 return menuBar->FindMenuItem (menuString, itemString);
504}
505
506/*
507 * wxDebugStreamBuf
508 */
509#if !defined(_WINDLL)
510
511wxDebugStreamBuf::wxDebugStreamBuf(void)
512{
513 if (allocate()) setp(base(),ebuf());
514}
515
516int wxDebugStreamBuf::overflow(int WXUNUSED(i))
517{
518 int len = pptr() - pbase();
519 char *txt = new char[len+1];
520 strncpy(txt, pbase(), len);
521 txt[len] = '\0';
2049ba38 522#ifdef __WXMSW__
c801d85f
KB
523 OutputDebugString((LPCSTR)txt);
524#else
525 fprintf(stderr, txt);
526#endif
527 setp(pbase(), epptr());
528 delete[] txt;
529 return EOF;
530}
531
532int wxDebugStreamBuf::sync(void)
533{
534 int len = pptr() - pbase();
535 char *txt = new char[len+1];
536 strncpy(txt, pbase(), len);
537 txt[len] = '\0';
2049ba38 538#ifdef __WXMSW__
c801d85f
KB
539 OutputDebugString((LPCSTR)txt);
540#else
541 fprintf(stderr, txt);
542#endif
543 setp(pbase(), epptr());
544 delete[] txt;
545 return 0;
546}
547
548#endif
549
550/*
551On Fri, 21 Jul 1995, Paul Craven wrote:
552
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.
556>
557
558The code below (warty as it is), does what you want on most Unix,
559DOS, and Mac platforms (it's from the ALS Prolog main).
560
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------------------------------------------------------------------------
566*/
567
568// This code is commented out but it may be integrated with wxWin at
569// a later date, after testing. Thanks Ken!
570#if 0
571
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 *--------------------------------------------------------------------*/
578
579static void
580whereami(name)
581 char *name;
582{
583 register char *cutoff = NULL; /* stifle -Wall */
584 register char *s;
585 register char *t;
586 int cc;
587 char ebuf[4096];
588
589 /*
590 * See if the file is accessible either through the current directory
591 * or through an absolute path.
592 */
593
594 if (access(name, R_OK) == 0) {
595
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 *-------------------------------------------------------------*/
601
602 t = imagedir;
603 if (!absolute_pathname(name)) {
604#if defined(DOS) || defined(__WIN32__)
605 int drive;
606 char *newrbuf;
607
608 newrbuf = imagedir;
609#ifndef __DJGPP__
610 if (*(name + 1) == ':') {
611 if (*name >= 'a' && *name <= 'z')
612 drive = (int) (*name - 'a' + 1);
613 else
614 drive = (int) (*name - 'A' + 1);
615 *newrbuf++ = *name;
616 *newrbuf++ = *(name + 1);
617 *newrbuf++ = DIR_SEPARATOR;
618 }
619 else {
620 drive = 0;
621 *newrbuf++ = DIR_SEPARATOR;
622 }
623 if (getcwd(newrbuf, drive) == 0) { /* } */
624#else
625 if (getcwd(newrbuf, 1024) == 0) { /* } */
626#endif
627#else /* DOS */
628#ifdef HAVE_GETWD
629 if (getwd(imagedir) == 0) { /* } */
630#else /* !HAVE_GETWD */
631 if (getcwd(imagedir, 1024) == 0) {
632#endif /* !HAVE_GETWD */
633#endif /* DOS */
634 fatal_error(FE_GETCWD, 0);
635 }
636 for (; *t; t++) /* Set t to end of buffer */
637 ;
638 if (*(t - 1) == DIR_SEPARATOR) /* leave slash if already
639 * last char
640 */
641 cutoff = t - 1;
642 else {
643 cutoff = t; /* otherwise put one in */
644 *t++ = DIR_SEPARATOR;
645 }
646 }
647#if (!defined(__MAC__) && !defined(__DJGPP__) && !defined(__GO32__) && !defined(__WIN32__))
648 else
649 (*t++ = DIR_SEPARATOR);
650#endif
651
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
656 * loop below.
657 *-------------------------------------------------------------*/
658
659 for ((*name == DIR_SEPARATOR ? (s = name+1) : (s = name));;) {
660 if (*s == DIR_SEPARATOR)
661 cutoff = t;
662 if (!(*t++ = *s++))
663 break;
664 }
665
666 }
667 else {
668
669 /*-------------------------------------------------------------*
670 * Get the path list from the environment. If the path list is
671 * inaccessible for any reason, leave with fatal error.
672 *-------------------------------------------------------------*/
673
674#ifdef __MAC__
675 if ((s = getenv("Commands")) == (char *) 0)
676#else
677 if ((s = getenv("PATH")) == (char *) 0)
678#endif
679 fatal_error(FE_PATH, 0);
680
681 /*
682 * Copy path list into ebuf and set the source pointer to the
683 * beginning of this buffer.
684 */
685
686 strcpy(ebuf, s);
687 s = ebuf;
688
689 for (;;) {
690 t = imagedir;
691 while (*s && *s != PATH_SEPARATOR)
692 *t++ = *s++;
693 if (t > imagedir && *(t - 1) == DIR_SEPARATOR)
694 ; /* do nothing -- slash already is in place */
695 else
696 *t++ = DIR_SEPARATOR; /* put in the slash */
697 cutoff = t - 1; /* set cutoff */
698 strcpy(t, name);
699 if (access(imagedir, R_OK) == 0)
700 break;
701
702 if (*s)
703 s++; /* advance source pointer */
704 else
705 fatal_error(FE_INFND, 0);
706 }
707
708 }
709
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 *-------------------------------------------------------------*/
716
717#ifdef HAVE_SYMLINK
718 while ((cc = readlink(imagedir, ebuf, 512)) != -1) {
719 ebuf[cc] = 0;
720 s = ebuf;
721 if (*s == DIR_SEPARATOR) {
722 t = imagedir;
723 }
724 else {
725 t = cutoff + 1;
726 }
727 for (;;) {
728 if (*s == DIR_SEPARATOR)
729 cutoff = t; /* mark the last slash seen */
730 if (!(*t++ = *s++)) /* copy the character */
731 break;
732 }
733 }
734
735#endif /* HAVE_SYMLINK */
736
737 strcpy(imagename, cutoff + 1); /* keep the image name */
738 *(cutoff + 1) = 0; /* chop off the filename part */
739}
740
741#endif
742