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