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