]>
Commit | Line | Data |
---|---|---|
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 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | #ifdef __GNUG__ | |
21 | #pragma implementation "utils.h" | |
22 | #endif | |
23 | ||
24 | // For compilers that support precompilation, includes "wx.h". | |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #ifdef __BORLANDC__ | |
28 | #pragma hdrstop | |
29 | #endif | |
30 | ||
31 | #ifndef WX_PRECOMP | |
32 | #include "wx/defs.h" | |
33 | #include "wx/string.h" | |
34 | #include "wx/utils.h" | |
35 | #include "wx/intl.h" | |
36 | #include "wx/log.h" | |
37 | ||
38 | #if wxUSE_GUI | |
39 | #include "wx/window.h" | |
40 | #include "wx/menu.h" | |
41 | #include "wx/frame.h" | |
42 | #include "wx/msgdlg.h" | |
43 | #include "wx/textdlg.h" | |
44 | #if wxUSE_ACCEL | |
45 | #include "wx/menuitem.h" | |
46 | #include "wx/accel.h" | |
47 | #endif // wxUSE_ACCEL | |
48 | #endif // wxUSE_GUI | |
49 | #endif // WX_PRECOMP | |
50 | ||
51 | #include <ctype.h> | |
52 | #include <stdio.h> | |
53 | #include <stdlib.h> | |
54 | #include <string.h> | |
55 | ||
56 | #if !defined(__WATCOMC__) | |
57 | #if !(defined(_MSC_VER) && (_MSC_VER > 800)) | |
58 | #include <errno.h> | |
59 | #endif | |
60 | #endif | |
61 | ||
62 | #include <time.h> | |
63 | ||
64 | #ifndef __MWERKS__ | |
65 | #include <sys/types.h> | |
66 | #include <sys/stat.h> | |
67 | #endif | |
68 | ||
69 | #ifdef __SALFORDC__ | |
70 | #include <clib.h> | |
71 | #endif | |
72 | ||
73 | #ifdef __WXMSW__ | |
74 | #include "windows.h" | |
75 | #endif | |
76 | ||
77 | // ---------------------------------------------------------------------------- | |
78 | // function protoypes | |
79 | // ---------------------------------------------------------------------------- | |
80 | ||
81 | #if wxUSE_GUI | |
82 | static wxWindow *wxFindWindowByLabel1(const wxString& title, wxWindow *parent); | |
83 | static wxWindow *wxFindWindowByName1 (const wxString& title, wxWindow *parent); | |
84 | #endif // wxUSE_GUI | |
85 | ||
86 | // ============================================================================ | |
87 | // implementation | |
88 | // ============================================================================ | |
89 | ||
90 | // ---------------------------------------------------------------------------- | |
91 | // string functions | |
92 | // ---------------------------------------------------------------------------- | |
93 | ||
94 | #ifdef __WXMAC__ | |
95 | int strcasecmp(const char *str_1, const char *str_2) | |
96 | { | |
97 | register char c1, c2; | |
98 | do { | |
99 | c1 = tolower(*str_1++); | |
100 | c2 = tolower(*str_2++); | |
101 | } while ( c1 && (c1 == c2) ); | |
102 | ||
103 | return c1 - c2; | |
104 | } | |
105 | ||
106 | int strncasecmp(const char *str_1, const char *str_2, size_t maxchar) | |
107 | { | |
108 | ||
109 | register char c1, c2; | |
110 | while( maxchar--) | |
111 | { | |
112 | c1 = tolower(*str_1++); | |
113 | c2 = tolower(*str_2++); | |
114 | ||
115 | if ( !c1 || c1!=c2 ) | |
116 | return c1 - c2; | |
117 | ||
118 | } ; | |
119 | ||
120 | return 0 ; | |
121 | ||
122 | } | |
123 | #endif // wxMAC | |
124 | ||
125 | #if defined( __VMS__ ) && ( __VMS_VER < 70000000 ) | |
126 | // we have no strI functions under VMS, therefore I have implemented | |
127 | // an inefficient but portable version: convert copies of strings to lowercase | |
128 | // and then use the normal comparison | |
129 | static void myLowerString(char *s) | |
130 | { | |
131 | while(*s){ | |
132 | if(isalpha(*s)) *s = (char)tolower(*s); | |
133 | s++; | |
134 | } | |
135 | } | |
136 | ||
137 | int strcasecmp(const char *str_1, const char *str_2) | |
138 | { | |
139 | char *temp1 = new char[strlen(str_1)+1]; | |
140 | char *temp2 = new char[strlen(str_2)+1]; | |
141 | strcpy(temp1,str_1); | |
142 | strcpy(temp2,str_2); | |
143 | myLowerString(temp1); | |
144 | myLowerString(temp2); | |
145 | ||
146 | int result = wxStrcmp(temp1,temp2); | |
147 | delete[] temp1; | |
148 | delete[] temp2; | |
149 | ||
150 | return(result); | |
151 | } | |
152 | ||
153 | int strncasecmp(const char *str_1, const char *str_2, size_t maxchar) | |
154 | { | |
155 | char *temp1 = new char[strlen(str_1)+1]; | |
156 | char *temp2 = new char[strlen(str_2)+1]; | |
157 | strcpy(temp1,str_1); | |
158 | strcpy(temp2,str_2); | |
159 | myLowerString(temp1); | |
160 | myLowerString(temp2); | |
161 | ||
162 | int result = strncmp(temp1,temp2,maxchar); | |
163 | delete[] temp1; | |
164 | delete[] temp2; | |
165 | ||
166 | return(result); | |
167 | } | |
168 | #endif // __VMS__ | |
169 | ||
170 | #ifdef __WINDOWS__ | |
171 | ||
172 | #ifndef __GNUWIN32__ | |
173 | #ifndef __MWERKS__ | |
174 | #define strcasecmp stricmp | |
175 | #define strncasecmp strnicmp | |
176 | #else | |
177 | #define strcasecmp _stricmp | |
178 | #define strncasecmp _strnicmp | |
179 | #endif | |
180 | #endif | |
181 | ||
182 | #else | |
183 | ||
184 | #ifdef __EMX__ | |
185 | #define strcasecmp stricmp | |
186 | #define strncasecmp strnicmp | |
187 | #endif | |
188 | ||
189 | // This declaration is missing in SunOS! | |
190 | // (Yes, I know it is NOT ANSI-C but its in BSD libc) | |
191 | #if defined(__xlC) || defined(__AIX__) || defined(__GNUG__) | |
192 | extern "C" | |
193 | { | |
194 | int strcasecmp (const char *, const char *); | |
195 | int strncasecmp (const char *, const char *, size_t); | |
196 | } | |
197 | #endif | |
198 | #endif /* __WXMSW__ */ | |
199 | ||
200 | #ifdef __WXPM__ | |
201 | #define strcasecmp stricmp | |
202 | #define strncasecmp strnicmp | |
203 | #endif | |
204 | ||
205 | wxChar * | |
206 | copystring (const wxChar *s) | |
207 | { | |
208 | if (s == NULL) s = wxT(""); | |
209 | size_t len = wxStrlen (s) + 1; | |
210 | ||
211 | wxChar *news = new wxChar[len]; | |
212 | memcpy (news, s, len * sizeof(wxChar)); // Should be the fastest | |
213 | ||
214 | return news; | |
215 | } | |
216 | ||
217 | // Id generation | |
218 | static long wxCurrentId = 100; | |
219 | ||
220 | long | |
221 | wxNewId (void) | |
222 | { | |
223 | return wxCurrentId++; | |
224 | } | |
225 | ||
226 | long | |
227 | wxGetCurrentId(void) { return wxCurrentId; } | |
228 | ||
229 | void | |
230 | wxRegisterId (long id) | |
231 | { | |
232 | if (id >= wxCurrentId) | |
233 | wxCurrentId = id + 1; | |
234 | } | |
235 | ||
236 | void | |
237 | StringToFloat (wxChar *s, float *number) | |
238 | { | |
239 | if (s && *s && number) | |
240 | *number = (float) wxStrtod (s, (wxChar **) NULL); | |
241 | } | |
242 | ||
243 | void | |
244 | StringToDouble (wxChar *s, double *number) | |
245 | { | |
246 | if (s && *s && number) | |
247 | *number = wxStrtod (s, (wxChar **) NULL); | |
248 | } | |
249 | ||
250 | wxChar * | |
251 | FloatToString (float number, const wxChar *fmt) | |
252 | { | |
253 | static wxChar buf[256]; | |
254 | ||
255 | // sprintf (buf, "%.2f", number); | |
256 | wxSprintf (buf, fmt, number); | |
257 | return buf; | |
258 | } | |
259 | ||
260 | wxChar * | |
261 | DoubleToString (double number, const wxChar *fmt) | |
262 | { | |
263 | static wxChar buf[256]; | |
264 | ||
265 | wxSprintf (buf, fmt, number); | |
266 | return buf; | |
267 | } | |
268 | ||
269 | void | |
270 | StringToInt (wxChar *s, int *number) | |
271 | { | |
272 | if (s && *s && number) | |
273 | *number = (int) wxStrtol (s, (wxChar **) NULL, 10); | |
274 | } | |
275 | ||
276 | void | |
277 | StringToLong (wxChar *s, long *number) | |
278 | { | |
279 | if (s && *s && number) | |
280 | *number = wxStrtol (s, (wxChar **) NULL, 10); | |
281 | } | |
282 | ||
283 | wxChar * | |
284 | IntToString (int number) | |
285 | { | |
286 | static wxChar buf[20]; | |
287 | ||
288 | wxSprintf (buf, wxT("%d"), number); | |
289 | return buf; | |
290 | } | |
291 | ||
292 | wxChar * | |
293 | LongToString (long number) | |
294 | { | |
295 | static wxChar buf[20]; | |
296 | ||
297 | wxSprintf (buf, wxT("%ld"), number); | |
298 | return buf; | |
299 | } | |
300 | ||
301 | // Array used in DecToHex conversion routine. | |
302 | static wxChar hexArray[] = wxT("0123456789ABCDEF"); | |
303 | ||
304 | // Convert 2-digit hex number to decimal | |
305 | int wxHexToDec(const wxString& buf) | |
306 | { | |
307 | int firstDigit, secondDigit; | |
308 | ||
309 | if (buf.GetChar(0) >= wxT('A')) | |
310 | firstDigit = buf.GetChar(0) - wxT('A') + 10; | |
311 | else | |
312 | firstDigit = buf.GetChar(0) - wxT('0'); | |
313 | ||
314 | if (buf.GetChar(1) >= wxT('A')) | |
315 | secondDigit = buf.GetChar(1) - wxT('A') + 10; | |
316 | else | |
317 | secondDigit = buf.GetChar(1) - wxT('0'); | |
318 | ||
319 | return firstDigit * 16 + secondDigit; | |
320 | } | |
321 | ||
322 | // Convert decimal integer to 2-character hex string | |
323 | void wxDecToHex(int dec, wxChar *buf) | |
324 | { | |
325 | int firstDigit = (int)(dec/16.0); | |
326 | int secondDigit = (int)(dec - (firstDigit*16.0)); | |
327 | buf[0] = hexArray[firstDigit]; | |
328 | buf[1] = hexArray[secondDigit]; | |
329 | buf[2] = 0; | |
330 | } | |
331 | ||
332 | // Convert decimal integer to 2-character hex string | |
333 | wxString wxDecToHex(int dec) | |
334 | { | |
335 | wxChar buf[3]; | |
336 | wxDecToHex(dec, buf); | |
337 | return wxString(buf); | |
338 | } | |
339 | ||
340 | // Match a string INDEPENDENT OF CASE | |
341 | bool | |
342 | StringMatch (char *str1, char *str2, bool subString, bool exact) | |
343 | { | |
344 | if (str1 == NULL || str2 == NULL) | |
345 | return FALSE; | |
346 | if (str1 == str2) | |
347 | return TRUE; | |
348 | ||
349 | if (subString) | |
350 | { | |
351 | int len1 = strlen (str1); | |
352 | int len2 = strlen (str2); | |
353 | int i; | |
354 | ||
355 | // Search for str1 in str2 | |
356 | // Slow .... but acceptable for short strings | |
357 | for (i = 0; i <= len2 - len1; i++) | |
358 | { | |
359 | if (strncasecmp (str1, str2 + i, len1) == 0) | |
360 | return TRUE; | |
361 | } | |
362 | } | |
363 | else if (exact) | |
364 | { | |
365 | if (strcasecmp (str1, str2) == 0) | |
366 | return TRUE; | |
367 | } | |
368 | else | |
369 | { | |
370 | int len1 = strlen (str1); | |
371 | int len2 = strlen (str2); | |
372 | ||
373 | if (strncasecmp (str1, str2, wxMin (len1, len2)) == 0) | |
374 | return TRUE; | |
375 | } | |
376 | ||
377 | return FALSE; | |
378 | } | |
379 | ||
380 | // Return the current date/time | |
381 | // [volatile] | |
382 | wxString wxNow() | |
383 | { | |
384 | time_t now = time((time_t *) NULL); | |
385 | char *date = ctime(&now); | |
386 | date[24] = '\0'; | |
387 | return wxString(date); | |
388 | } | |
389 | ||
390 | #if wxUSE_GUI | |
391 | ||
392 | // ---------------------------------------------------------------------------- | |
393 | // Menu accelerators related functions | |
394 | // ---------------------------------------------------------------------------- | |
395 | ||
396 | wxChar *wxStripMenuCodes (wxChar *in, wxChar *out) | |
397 | { | |
398 | if (!in) | |
399 | return (wxChar *) NULL; | |
400 | ||
401 | if (!out) | |
402 | out = copystring(in); | |
403 | ||
404 | wxChar *tmpOut = out; | |
405 | ||
406 | while (*in) | |
407 | { | |
408 | if (*in == wxT('&')) | |
409 | { | |
410 | // Check && -> &, &x -> x | |
411 | if (*++in == wxT('&')) | |
412 | *out++ = *in++; | |
413 | } | |
414 | else if (*in == wxT('\t')) | |
415 | { | |
416 | // Remove all stuff after \t in X mode, and let the stuff as is | |
417 | // in Windows mode. | |
418 | // Accelerators are handled in wx_item.cc for Motif, and are not | |
419 | // YET supported in XView | |
420 | break; | |
421 | } | |
422 | else | |
423 | *out++ = *in++; | |
424 | } // while | |
425 | ||
426 | *out = wxT('\0'); | |
427 | ||
428 | return tmpOut; | |
429 | } | |
430 | ||
431 | wxString wxStripMenuCodes(const wxString& str) | |
432 | { | |
433 | wxChar *buf = new wxChar[str.Length() + 1]; | |
434 | wxStripMenuCodes(WXSTRINGCAST str, buf); | |
435 | wxString str1(buf); | |
436 | delete[] buf; | |
437 | return str1; | |
438 | } | |
439 | ||
440 | #if wxUSE_ACCEL | |
441 | ||
442 | // return wxAcceleratorEntry for the given menu string or NULL if none | |
443 | // specified | |
444 | wxAcceleratorEntry *wxGetAccelFromString(const wxString& label) | |
445 | { | |
446 | // check for accelerators: they are given after '\t' | |
447 | int posTab = label.Find(wxT('\t')); | |
448 | if ( posTab != wxNOT_FOUND ) { | |
449 | // parse the accelerator string | |
450 | int keyCode = 0; | |
451 | int accelFlags = wxACCEL_NORMAL; | |
452 | wxString current; | |
453 | for ( size_t n = (size_t)posTab + 1; n < label.Len(); n++ ) { | |
454 | if ( (label[n] == '+') || (label[n] == '-') ) { | |
455 | if ( current == _("ctrl") ) | |
456 | accelFlags |= wxACCEL_CTRL; | |
457 | else if ( current == _("alt") ) | |
458 | accelFlags |= wxACCEL_ALT; | |
459 | else if ( current == _("shift") ) | |
460 | accelFlags |= wxACCEL_SHIFT; | |
461 | else { | |
462 | wxLogDebug(wxT("Unknown accel modifier: '%s'"), | |
463 | current.c_str()); | |
464 | } | |
465 | ||
466 | current.Empty(); | |
467 | } | |
468 | else { | |
469 | current += wxTolower(label[n]); | |
470 | } | |
471 | } | |
472 | ||
473 | if ( current.IsEmpty() ) { | |
474 | wxLogDebug(wxT("No accel key found, accel string ignored.")); | |
475 | } | |
476 | else { | |
477 | if ( current.Len() == 1 ) { | |
478 | // it's a letter | |
479 | keyCode = wxToupper(current[0U]); | |
480 | } | |
481 | else { | |
482 | // is it a function key? | |
483 | if ( current[0U] == 'f' && isdigit(current[1U]) && | |
484 | (current.Len() == 2 || | |
485 | (current.Len() == 3 && isdigit(current[2U]))) ) { | |
486 | int n; | |
487 | wxSscanf(current.c_str() + 1, wxT("%d"), &n); | |
488 | ||
489 | keyCode = WXK_F1 + n - 1; | |
490 | } | |
491 | else { | |
492 | #if 0 // this is not supported by GTK+, apparently | |
493 | // several special cases | |
494 | current.MakeUpper(); | |
495 | if ( current == wxT("DEL") ) { | |
496 | keyCode = VK_DELETE; | |
497 | } | |
498 | else if ( current == wxT("PGUP") ) { | |
499 | keyCode = VK_PRIOR; | |
500 | } | |
501 | else if ( current == wxT("PGDN") ) { | |
502 | keyCode = VK_NEXT; | |
503 | } | |
504 | else | |
505 | #endif // 0 | |
506 | { | |
507 | wxLogDebug(wxT("Unrecognized accel key '%s', accel " | |
508 | "string ignored."), current.c_str()); | |
509 | } | |
510 | } | |
511 | } | |
512 | } | |
513 | ||
514 | if ( keyCode ) { | |
515 | // we do have something | |
516 | return new wxAcceleratorEntry(accelFlags, keyCode); | |
517 | } | |
518 | } | |
519 | ||
520 | return (wxAcceleratorEntry *)NULL; | |
521 | } | |
522 | ||
523 | #endif // wxUSE_ACCEL | |
524 | ||
525 | // ---------------------------------------------------------------------------- | |
526 | // Window search functions | |
527 | // ---------------------------------------------------------------------------- | |
528 | ||
529 | /* | |
530 | * If parent is non-NULL, look through children for a label or title | |
531 | * matching the specified string. If NULL, look through all top-level windows. | |
532 | * | |
533 | */ | |
534 | ||
535 | wxWindow * | |
536 | wxFindWindowByLabel (const wxString& title, wxWindow * parent) | |
537 | { | |
538 | if (parent) | |
539 | { | |
540 | return wxFindWindowByLabel1(title, parent); | |
541 | } | |
542 | else | |
543 | { | |
544 | for ( wxWindowList::Node * node = wxTopLevelWindows.GetFirst(); | |
545 | node; | |
546 | node = node->GetNext() ) | |
547 | { | |
548 | wxWindow *win = node->GetData(); | |
549 | wxWindow *retwin = wxFindWindowByLabel1 (title, win); | |
550 | if (retwin) | |
551 | return retwin; | |
552 | } // for() | |
553 | ||
554 | } | |
555 | return (wxWindow *) NULL; | |
556 | } | |
557 | ||
558 | // Recursive | |
559 | static wxWindow * | |
560 | wxFindWindowByLabel1 (const wxString& title, wxWindow * parent) | |
561 | { | |
562 | if (parent) | |
563 | { | |
564 | if (parent->GetLabel() == title) | |
565 | return parent; | |
566 | } | |
567 | ||
568 | if (parent) | |
569 | { | |
570 | for ( wxWindowList::Node * node = parent->GetChildren().GetFirst(); | |
571 | node; | |
572 | node = node->GetNext() ) | |
573 | { | |
574 | wxWindow *win = (wxWindow *)node->GetData(); | |
575 | wxWindow *retwin = wxFindWindowByLabel1 (title, win); | |
576 | if (retwin) | |
577 | return retwin; | |
578 | } | |
579 | ||
580 | } | |
581 | ||
582 | return (wxWindow *) NULL; // Not found | |
583 | } | |
584 | ||
585 | /* | |
586 | * If parent is non-NULL, look through children for a name | |
587 | * matching the specified string. If NULL, look through all top-level windows. | |
588 | * | |
589 | */ | |
590 | ||
591 | wxWindow * | |
592 | wxFindWindowByName (const wxString& title, wxWindow * parent) | |
593 | { | |
594 | if (parent) | |
595 | { | |
596 | return wxFindWindowByName1 (title, parent); | |
597 | } | |
598 | else | |
599 | { | |
600 | for ( wxWindowList::Node * node = wxTopLevelWindows.GetFirst(); | |
601 | node; | |
602 | node = node->GetNext() ) | |
603 | { | |
604 | wxWindow *win = node->GetData(); | |
605 | wxWindow *retwin = wxFindWindowByName1 (title, win); | |
606 | if (retwin) | |
607 | return retwin; | |
608 | } | |
609 | ||
610 | } | |
611 | ||
612 | // Failed? Try by label instead. | |
613 | return wxFindWindowByLabel(title, parent); | |
614 | } | |
615 | ||
616 | // Recursive | |
617 | static wxWindow * | |
618 | wxFindWindowByName1 (const wxString& title, wxWindow * parent) | |
619 | { | |
620 | if (parent) | |
621 | { | |
622 | if ( parent->GetName() == title ) | |
623 | return parent; | |
624 | } | |
625 | ||
626 | if (parent) | |
627 | { | |
628 | for (wxNode * node = parent->GetChildren().First (); node; node = node->Next ()) | |
629 | { | |
630 | wxWindow *win = (wxWindow *) node->Data (); | |
631 | wxWindow *retwin = wxFindWindowByName1 (title, win); | |
632 | if (retwin) | |
633 | return retwin; | |
634 | } // for() | |
635 | ||
636 | } | |
637 | ||
638 | return (wxWindow *) NULL; // Not found | |
639 | ||
640 | } | |
641 | ||
642 | // Returns menu item id or -1 if none. | |
643 | int | |
644 | wxFindMenuItemId (wxFrame * frame, const wxString& menuString, const wxString& itemString) | |
645 | { | |
646 | wxMenuBar *menuBar = frame->GetMenuBar (); | |
647 | if (!menuBar) | |
648 | return -1; | |
649 | return menuBar->FindMenuItem (menuString, itemString); | |
650 | } | |
651 | ||
652 | #endif // wxUSE_GUI | |
653 | ||
654 | /* | |
655 | On Fri, 21 Jul 1995, Paul Craven wrote: | |
656 | ||
657 | > Is there a way to find the path of running program's executable? I can get | |
658 | > my home directory, and the current directory, but I don't know how to get the | |
659 | > executable directory. | |
660 | > | |
661 | ||
662 | The code below (warty as it is), does what you want on most Unix, | |
663 | DOS, and Mac platforms (it's from the ALS Prolog main). | |
664 | ||
665 | || Ken Bowen Applied Logic Systems, Inc. PO Box 180, | |
666 | ||==== Voice: +1 (617)965-9191 Newton Centre, | |
667 | || FAX: +1 (617)965-1636 MA 02159 USA | |
668 | Email: ken@als.com WWW: http://www.als.com | |
669 | ------------------------------------------------------------------------ | |
670 | */ | |
671 | ||
672 | // This code is commented out but it may be integrated with wxWin at | |
673 | // a later date, after testing. Thanks Ken! | |
674 | #if 0 | |
675 | ||
676 | /*--------------------------------------------------------------------* | |
677 | | whereami is given a filename f in the form: whereami(argv[0]) | |
678 | | It returns the directory in which the executable file (containing | |
679 | | this code [main.c] ) may be found. A dot will be returned to indicate | |
680 | | the current directory. | |
681 | *--------------------------------------------------------------------*/ | |
682 | ||
683 | static void | |
684 | whereami(name) | |
685 | char *name; | |
686 | { | |
687 | register char *cutoff = NULL; /* stifle -Wall */ | |
688 | register char *s; | |
689 | register char *t; | |
690 | int cc; | |
691 | char ebuf[4096]; | |
692 | ||
693 | /* | |
694 | * See if the file is accessible either through the current directory | |
695 | * or through an absolute path. | |
696 | */ | |
697 | ||
698 | if (access(name, R_OK) == 0) { | |
699 | ||
700 | /*-------------------------------------------------------------* | |
701 | * The file was accessible without any other work. But the current | |
702 | * working directory might change on us, so if it was accessible | |
703 | * through the cwd, then we should get it for later accesses. | |
704 | *-------------------------------------------------------------*/ | |
705 | ||
706 | t = imagedir; | |
707 | if (!absolute_pathname(name)) { | |
708 | #if defined(DOS) || defined(__WIN32__) | |
709 | int drive; | |
710 | char *newrbuf; | |
711 | ||
712 | newrbuf = imagedir; | |
713 | #ifndef __DJGPP__ | |
714 | if (*(name + 1) == ':') { | |
715 | if (*name >= 'a' && *name <= 'z') | |
716 | drive = (int) (*name - 'a' + 1); | |
717 | else | |
718 | drive = (int) (*name - 'A' + 1); | |
719 | *newrbuf++ = *name; | |
720 | *newrbuf++ = *(name + 1); | |
721 | *newrbuf++ = DIR_SEPARATOR; | |
722 | } | |
723 | else { | |
724 | drive = 0; | |
725 | *newrbuf++ = DIR_SEPARATOR; | |
726 | } | |
727 | if (getcwd(newrbuf, drive) == 0) { /* } */ | |
728 | #else | |
729 | if (getcwd(newrbuf, 1024) == 0) { /* } */ | |
730 | #endif | |
731 | #else /* DOS */ | |
732 | #ifdef HAVE_GETWD | |
733 | if (getwd(imagedir) == 0) { /* } */ | |
734 | #else /* !HAVE_GETWD */ | |
735 | if (getcwd(imagedir, 1024) == 0) { | |
736 | #endif /* !HAVE_GETWD */ | |
737 | #endif /* DOS */ | |
738 | fatal_error(FE_GETCWD, 0); | |
739 | } | |
740 | for (; *t; t++) /* Set t to end of buffer */ | |
741 | ; | |
742 | if (*(t - 1) == DIR_SEPARATOR) /* leave slash if already | |
743 | * last char | |
744 | */ | |
745 | cutoff = t - 1; | |
746 | else { | |
747 | cutoff = t; /* otherwise put one in */ | |
748 | *t++ = DIR_SEPARATOR; | |
749 | } | |
750 | } | |
751 | #if (!defined(__MAC__) && !defined(__DJGPP__) && !defined(__GO32__) && !defined(__WIN32__)) | |
752 | else | |
753 | (*t++ = DIR_SEPARATOR); | |
754 | #endif | |
755 | ||
756 | /*-------------------------------------------------------------* | |
757 | * Copy the rest of the string and set the cutoff if it was not | |
758 | * already set. If the first character of name is a slash, cutoff | |
759 | * is not presently set but will be on the first iteration of the | |
760 | * loop below. | |
761 | *-------------------------------------------------------------*/ | |
762 | ||
763 | for ((*name == DIR_SEPARATOR ? (s = name+1) : (s = name));;) { | |
764 | if (*s == DIR_SEPARATOR) | |
765 | cutoff = t; | |
766 | if (!(*t++ = *s++)) | |
767 | break; | |
768 | } | |
769 | ||
770 | } | |
771 | else { | |
772 | ||
773 | /*-------------------------------------------------------------* | |
774 | * Get the path list from the environment. If the path list is | |
775 | * inaccessible for any reason, leave with fatal error. | |
776 | *-------------------------------------------------------------*/ | |
777 | ||
778 | #ifdef __MAC__ | |
779 | if ((s = getenv("Commands")) == (char *) 0) | |
780 | #else | |
781 | if ((s = getenv("PATH")) == (char *) 0) | |
782 | #endif | |
783 | fatal_error(FE_PATH, 0); | |
784 | ||
785 | /* | |
786 | * Copy path list into ebuf and set the source pointer to the | |
787 | * beginning of this buffer. | |
788 | */ | |
789 | ||
790 | strcpy(ebuf, s); | |
791 | s = ebuf; | |
792 | ||
793 | for (;;) { | |
794 | t = imagedir; | |
795 | while (*s && *s != PATH_SEPARATOR) | |
796 | *t++ = *s++; | |
797 | if (t > imagedir && *(t - 1) == DIR_SEPARATOR) | |
798 | ; /* do nothing -- slash already is in place */ | |
799 | else | |
800 | *t++ = DIR_SEPARATOR; /* put in the slash */ | |
801 | cutoff = t - 1; /* set cutoff */ | |
802 | strcpy(t, name); | |
803 | if (access(imagedir, R_OK) == 0) | |
804 | break; | |
805 | ||
806 | if (*s) | |
807 | s++; /* advance source pointer */ | |
808 | else | |
809 | fatal_error(FE_INFND, 0); | |
810 | } | |
811 | ||
812 | } | |
813 | ||
814 | /*-------------------------------------------------------------* | |
815 | | At this point the full pathname should exist in imagedir and | |
816 | | cutoff should be set to the final slash. We must now determine | |
817 | | whether the file name is a symbolic link or not and chase it down | |
818 | | if it is. Note that we reuse ebuf for getting the link. | |
819 | *-------------------------------------------------------------*/ | |
820 | ||
821 | #ifdef HAVE_SYMLINK | |
822 | while ((cc = readlink(imagedir, ebuf, 512)) != -1) { | |
823 | ebuf[cc] = 0; | |
824 | s = ebuf; | |
825 | if (*s == DIR_SEPARATOR) { | |
826 | t = imagedir; | |
827 | } | |
828 | else { | |
829 | t = cutoff + 1; | |
830 | } | |
831 | for (;;) { | |
832 | if (*s == DIR_SEPARATOR) | |
833 | cutoff = t; /* mark the last slash seen */ | |
834 | if (!(*t++ = *s++)) /* copy the character */ | |
835 | break; | |
836 | } | |
837 | } | |
838 | ||
839 | #endif /* HAVE_SYMLINK */ | |
840 | ||
841 | strcpy(imagename, cutoff + 1); /* keep the image name */ | |
842 | *(cutoff + 1) = 0; /* chop off the filename part */ | |
843 | } | |
844 | ||
845 | #endif | |
846 | ||
847 | #if wxUSE_GUI | |
848 | ||
849 | // ---------------------------------------------------------------------------- | |
850 | // GUI helpers | |
851 | // ---------------------------------------------------------------------------- | |
852 | ||
853 | /* | |
854 | * N.B. these convenience functions must be separate from msgdlgg.cpp, textdlgg.cpp | |
855 | * since otherwise the generic code may be pulled in unnecessarily. | |
856 | */ | |
857 | ||
858 | int wxMessageBox(const wxString& message, const wxString& caption, long style, | |
859 | wxWindow *parent, int WXUNUSED(x), int WXUNUSED(y) ) | |
860 | { | |
861 | wxMessageDialog dialog(parent, message, caption, style); | |
862 | ||
863 | int ans = dialog.ShowModal(); | |
864 | switch ( ans ) | |
865 | { | |
866 | case wxID_OK: | |
867 | return wxOK; | |
868 | case wxID_YES: | |
869 | return wxYES; | |
870 | case wxID_NO: | |
871 | return wxNO; | |
872 | default: | |
873 | case wxID_CANCEL: | |
874 | return wxCANCEL; | |
875 | } | |
876 | } | |
877 | ||
878 | #if wxUSE_TEXTDLG | |
879 | wxString wxGetTextFromUser(const wxString& message, const wxString& caption, | |
880 | const wxString& defaultValue, wxWindow *parent, | |
881 | int x, int y, bool WXUNUSED(centre) ) | |
882 | { | |
883 | wxString str; | |
884 | wxTextEntryDialog dialog(parent, message, caption, defaultValue, wxOK|wxCANCEL, wxPoint(x, y)); | |
885 | if (dialog.ShowModal() == wxID_OK) | |
886 | { | |
887 | str = dialog.GetValue(); | |
888 | } | |
889 | ||
890 | return str; | |
891 | } | |
892 | ||
893 | wxString wxGetPasswordFromUser(const wxString& message, | |
894 | const wxString& caption, | |
895 | const wxString& defaultValue, | |
896 | wxWindow *parent) | |
897 | { | |
898 | wxString str; | |
899 | wxTextEntryDialog dialog(parent, message, caption, defaultValue, | |
900 | wxOK | wxCANCEL | wxTE_PASSWORD); | |
901 | if ( dialog.ShowModal() == wxID_OK ) | |
902 | { | |
903 | str = dialog.GetValue(); | |
904 | } | |
905 | ||
906 | return str; | |
907 | } | |
908 | ||
909 | #endif // wxUSE_TEXTDLG | |
910 | ||
911 | #ifdef __MWERKS__ | |
912 | char *strdup(const char *s) | |
913 | { | |
914 | return strcpy( (char*) malloc( strlen( s ) + 1 ) , s ) ; | |
915 | } | |
916 | ||
917 | int isascii( int c ) | |
918 | { | |
919 | return ( c >= 0 && c < 128 ) ; | |
920 | } | |
921 | #endif // __MWERKS__ | |
922 | ||
923 | // ---------------------------------------------------------------------------- | |
924 | // misc functions | |
925 | // ---------------------------------------------------------------------------- | |
926 | ||
927 | void wxEnableTopLevelWindows(bool enable) | |
928 | { | |
929 | wxWindowList::Node *node; | |
930 | for ( node = wxTopLevelWindows.GetFirst(); node; node = node->GetNext() ) | |
931 | node->GetData()->Enable(enable); | |
932 | } | |
933 | ||
934 | // Yield to other apps/messages and disable user input | |
935 | bool wxSafeYield(wxWindow *win) | |
936 | { | |
937 | wxEnableTopLevelWindows(FALSE); | |
938 | // always enable ourselves | |
939 | if ( win ) | |
940 | win->Enable(TRUE); | |
941 | bool rc = wxYield(); | |
942 | wxEnableTopLevelWindows(TRUE); | |
943 | return rc; | |
944 | } | |
945 | ||
946 | // Don't synthesize KeyUp events holding down a key and producing KeyDown | |
947 | // events with autorepeat. On by default and always on in wxMSW. wxGTK version | |
948 | // in utilsgtk.cpp. | |
949 | #ifndef __WXGTK__ | |
950 | bool wxSetDetectableAutoRepeat( bool WXUNUSED(flag) ) | |
951 | { | |
952 | return TRUE; // detectable auto-repeat is the only mode MSW supports | |
953 | } | |
954 | #endif // !wxGTK | |
955 | ||
956 | #endif // wxUSE_GUI | |
957 | ||
958 | // ---------------------------------------------------------------------------- | |
959 | // network and user id functions | |
960 | // ---------------------------------------------------------------------------- | |
961 | ||
962 | // Get Full RFC822 style email address | |
963 | bool wxGetEmailAddress(wxChar *address, int maxSize) | |
964 | { | |
965 | wxString email = wxGetEmailAddress(); | |
966 | if ( !email ) | |
967 | return FALSE; | |
968 | ||
969 | wxStrncpy(address, email, maxSize - 1); | |
970 | address[maxSize - 1] = wxT('\0'); | |
971 | ||
972 | return TRUE; | |
973 | } | |
974 | ||
975 | wxString wxGetEmailAddress() | |
976 | { | |
977 | wxString email; | |
978 | ||
979 | wxString host = wxGetHostName(); | |
980 | if ( !!host ) | |
981 | { | |
982 | wxString user = wxGetUserId(); | |
983 | if ( !!user ) | |
984 | { | |
985 | wxString email(user); | |
986 | email << wxT('@') << host; | |
987 | } | |
988 | } | |
989 | ||
990 | return email; | |
991 | } | |
992 | ||
993 | wxString wxGetUserId() | |
994 | { | |
995 | static const int maxLoginLen = 256; // FIXME arbitrary number | |
996 | ||
997 | wxString buf; | |
998 | bool ok = wxGetUserId(buf.GetWriteBuf(maxLoginLen), maxLoginLen); | |
999 | buf.UngetWriteBuf(); | |
1000 | ||
1001 | if ( !ok ) | |
1002 | buf.Empty(); | |
1003 | ||
1004 | return buf; | |
1005 | } | |
1006 | ||
1007 | wxString wxGetUserName() | |
1008 | { | |
1009 | static const int maxUserNameLen = 1024; // FIXME arbitrary number | |
1010 | ||
1011 | wxString buf; | |
1012 | bool ok = wxGetUserName(buf.GetWriteBuf(maxUserNameLen), maxUserNameLen); | |
1013 | buf.UngetWriteBuf(); | |
1014 | ||
1015 | if ( !ok ) | |
1016 | buf.Empty(); | |
1017 | ||
1018 | return buf; | |
1019 | } | |
1020 | ||
1021 | wxString wxGetHostName() | |
1022 | { | |
1023 | static const size_t hostnameSize = 257; | |
1024 | ||
1025 | wxString buf; | |
1026 | bool ok = wxGetHostName(buf.GetWriteBuf(hostnameSize), hostnameSize); | |
1027 | ||
1028 | buf.UngetWriteBuf(); | |
1029 | ||
1030 | if ( !ok ) | |
1031 | buf.Empty(); | |
1032 | ||
1033 | return buf; | |
1034 | } | |
1035 | ||
1036 | wxString wxGetFullHostName() | |
1037 | { | |
1038 | static const size_t hostnameSize = 257; | |
1039 | ||
1040 | wxString buf; | |
1041 | bool ok = wxGetFullHostName(buf.GetWriteBuf(hostnameSize), hostnameSize); | |
1042 | ||
1043 | buf.UngetWriteBuf(); | |
1044 | ||
1045 | if ( !ok ) | |
1046 | buf.Empty(); | |
1047 | ||
1048 | return buf; | |
1049 | } | |
1050 | ||
1051 | wxString wxGetHomeDir() | |
1052 | { | |
1053 | wxString home; | |
1054 | wxGetHomeDir(&home); | |
1055 | ||
1056 | return home; | |
1057 | } | |
1058 | ||
1059 | #if 0 | |
1060 | ||
1061 | wxString wxGetCurrentDir() | |
1062 | { | |
1063 | wxString dir; | |
1064 | size_t len = 1024; | |
1065 | bool ok; | |
1066 | do | |
1067 | { | |
1068 | ok = getcwd(dir.GetWriteBuf(len + 1), len) != NULL; | |
1069 | dir.UngetWriteBuf(); | |
1070 | ||
1071 | if ( !ok ) | |
1072 | { | |
1073 | if ( errno != ERANGE ) | |
1074 | { | |
1075 | wxLogSysError(_T("Failed to get current directory")); | |
1076 | ||
1077 | return wxEmptyString; | |
1078 | } | |
1079 | else | |
1080 | { | |
1081 | // buffer was too small, retry with a larger one | |
1082 | len *= 2; | |
1083 | } | |
1084 | } | |
1085 | //else: ok | |
1086 | } while ( !ok ); | |
1087 | ||
1088 | return dir; | |
1089 | } | |
1090 | ||
1091 | #endif // 0 |