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