]>
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 | ||
358 | // Return the current date/time | |
359 | // [volatile] | |
360 | wxString wxNow( void ) | |
361 | { | |
c67daf87 | 362 | time_t now = time((time_t *) NULL); |
3f4a0c5b | 363 | char *date = ctime(&now); |
c801d85f KB |
364 | date[24] = '\0'; |
365 | return wxString(date); | |
366 | } | |
367 | ||
c801d85f KB |
368 | /* |
369 | * Strip out any menu codes | |
370 | */ | |
371 | ||
0080691b | 372 | wxChar *wxStripMenuCodes (wxChar *in, wxChar *out) |
c801d85f KB |
373 | { |
374 | if (!in) | |
0080691b | 375 | return (wxChar *) NULL; |
3f4a0c5b | 376 | |
c801d85f KB |
377 | if (!out) |
378 | out = copystring(in); | |
379 | ||
0080691b | 380 | wxChar *tmpOut = out; |
3f4a0c5b | 381 | |
c801d85f KB |
382 | while (*in) |
383 | { | |
0080691b | 384 | if (*in == _T('&')) |
3f4a0c5b VZ |
385 | { |
386 | // Check && -> &, &x -> x | |
0080691b | 387 | if (*++in == _T('&')) |
3f4a0c5b VZ |
388 | *out++ = *in++; |
389 | } | |
0080691b | 390 | else if (*in == _T('\t')) |
3f4a0c5b | 391 | { |
c801d85f KB |
392 | // Remove all stuff after \t in X mode, and let the stuff as is |
393 | // in Windows mode. | |
394 | // Accelerators are handled in wx_item.cc for Motif, and are not | |
395 | // YET supported in XView | |
3f4a0c5b VZ |
396 | break; |
397 | } | |
c801d85f | 398 | else |
3f4a0c5b VZ |
399 | *out++ = *in++; |
400 | } // while | |
c801d85f | 401 | |
0080691b | 402 | *out = _T('\0'); |
c801d85f KB |
403 | |
404 | return tmpOut; | |
405 | } | |
406 | ||
47bc1060 JS |
407 | wxString wxStripMenuCodes(const wxString& str) |
408 | { | |
84fff0b3 OK |
409 | wxChar *buf = new wxChar[str.Length() + 1]; |
410 | wxStripMenuCodes(WXSTRINGCAST str, buf); | |
47bc1060 JS |
411 | wxString str1(buf); |
412 | delete[] buf; | |
413 | return str1; | |
414 | } | |
c801d85f KB |
415 | |
416 | /* | |
417 | * Window search functions | |
418 | * | |
419 | */ | |
420 | ||
421 | /* | |
422 | * If parent is non-NULL, look through children for a label or title | |
423 | * matching the specified string. If NULL, look through all top-level windows. | |
424 | * | |
425 | */ | |
426 | ||
c801d85f KB |
427 | wxWindow * |
428 | wxFindWindowByLabel (const wxString& title, wxWindow * parent) | |
429 | { | |
e146b8c8 | 430 | if (parent) |
c801d85f | 431 | { |
e146b8c8 | 432 | return wxFindWindowByLabel1(title, parent); |
c801d85f | 433 | } |
e146b8c8 | 434 | else |
c801d85f | 435 | { |
e146b8c8 VZ |
436 | for ( wxWindowList::Node * node = wxTopLevelWindows.GetFirst(); |
437 | node; | |
438 | node = node->GetNext() ) | |
3f4a0c5b | 439 | { |
e146b8c8 VZ |
440 | wxWindow *win = node->GetData(); |
441 | wxWindow *retwin = wxFindWindowByLabel1 (title, win); | |
442 | if (retwin) | |
443 | return retwin; | |
3f4a0c5b | 444 | } // for() |
c801d85f KB |
445 | |
446 | } | |
e146b8c8 | 447 | return (wxWindow *) NULL; |
c801d85f KB |
448 | } |
449 | ||
450 | // Recursive | |
451 | static wxWindow * | |
452 | wxFindWindowByLabel1 (const wxString& title, wxWindow * parent) | |
453 | { | |
e146b8c8 | 454 | if (parent) |
c801d85f | 455 | { |
e146b8c8 VZ |
456 | if (parent->GetLabel() == title) |
457 | return parent; | |
c801d85f KB |
458 | } |
459 | ||
e146b8c8 | 460 | if (parent) |
c801d85f | 461 | { |
e146b8c8 VZ |
462 | for ( wxNode * node = parent->GetChildren().GetFirst(); |
463 | node; | |
464 | node = node->GetNext() ) | |
3f4a0c5b | 465 | { |
e146b8c8 VZ |
466 | wxWindow *win = (wxWindow *)node->GetData(); |
467 | wxWindow *retwin = wxFindWindowByLabel1 (title, win); | |
468 | if (retwin) | |
469 | return retwin; | |
470 | } | |
c801d85f KB |
471 | |
472 | } | |
473 | ||
e146b8c8 | 474 | return (wxWindow *) NULL; // Not found |
c801d85f KB |
475 | } |
476 | ||
477 | /* | |
478 | * If parent is non-NULL, look through children for a name | |
479 | * matching the specified string. If NULL, look through all top-level windows. | |
480 | * | |
481 | */ | |
482 | ||
c801d85f KB |
483 | wxWindow * |
484 | wxFindWindowByName (const wxString& title, wxWindow * parent) | |
485 | { | |
e146b8c8 | 486 | if (parent) |
c801d85f | 487 | { |
e146b8c8 | 488 | return wxFindWindowByName1 (title, parent); |
c801d85f | 489 | } |
e146b8c8 | 490 | else |
c801d85f | 491 | { |
e146b8c8 VZ |
492 | for ( wxWindowList::Node * node = wxTopLevelWindows.GetFirst(); |
493 | node; | |
494 | node = node->GetNext() ) | |
3f4a0c5b | 495 | { |
e146b8c8 VZ |
496 | wxWindow *win = node->GetData(); |
497 | wxWindow *retwin = wxFindWindowByName1 (title, win); | |
498 | if (retwin) | |
499 | return retwin; | |
500 | } | |
c801d85f KB |
501 | |
502 | } | |
e146b8c8 VZ |
503 | |
504 | // Failed? Try by label instead. | |
505 | return wxFindWindowByLabel(title, parent); | |
c801d85f KB |
506 | } |
507 | ||
508 | // Recursive | |
509 | static wxWindow * | |
510 | wxFindWindowByName1 (const wxString& title, wxWindow * parent) | |
511 | { | |
512 | if (parent) | |
513 | { | |
3f4a0c5b VZ |
514 | if ( parent->GetName() == title ) |
515 | return parent; | |
c801d85f KB |
516 | } |
517 | ||
518 | if (parent) | |
519 | { | |
c0ed460c | 520 | for (wxNode * node = parent->GetChildren().First (); node; node = node->Next ()) |
3f4a0c5b VZ |
521 | { |
522 | wxWindow *win = (wxWindow *) node->Data (); | |
523 | wxWindow *retwin = wxFindWindowByName1 (title, win); | |
524 | if (retwin) | |
525 | return retwin; | |
526 | } // for() | |
c801d85f KB |
527 | |
528 | } | |
529 | ||
3f4a0c5b | 530 | return (wxWindow *) NULL; // Not found |
c801d85f KB |
531 | |
532 | } | |
533 | ||
534 | // Returns menu item id or -1 if none. | |
3f4a0c5b | 535 | int |
c801d85f KB |
536 | wxFindMenuItemId (wxFrame * frame, const wxString& menuString, const wxString& itemString) |
537 | { | |
538 | wxMenuBar *menuBar = frame->GetMenuBar (); | |
539 | if (!menuBar) | |
540 | return -1; | |
541 | return menuBar->FindMenuItem (menuString, itemString); | |
542 | } | |
543 | ||
c801d85f KB |
544 | /* |
545 | On Fri, 21 Jul 1995, Paul Craven wrote: | |
546 | ||
547 | > Is there a way to find the path of running program's executable? I can get | |
548 | > my home directory, and the current directory, but I don't know how to get the | |
549 | > executable directory. | |
3f4a0c5b | 550 | > |
c801d85f KB |
551 | |
552 | The code below (warty as it is), does what you want on most Unix, | |
553 | DOS, and Mac platforms (it's from the ALS Prolog main). | |
554 | ||
3f4a0c5b | 555 | || Ken Bowen Applied Logic Systems, Inc. PO Box 180, |
c801d85f KB |
556 | ||==== Voice: +1 (617)965-9191 Newton Centre, |
557 | || FAX: +1 (617)965-1636 MA 02159 USA | |
558 | Email: ken@als.com WWW: http://www.als.com | |
559 | ------------------------------------------------------------------------ | |
560 | */ | |
561 | ||
562 | // This code is commented out but it may be integrated with wxWin at | |
563 | // a later date, after testing. Thanks Ken! | |
564 | #if 0 | |
565 | ||
566 | /*--------------------------------------------------------------------* | |
567 | | whereami is given a filename f in the form: whereami(argv[0]) | |
3f4a0c5b VZ |
568 | | It returns the directory in which the executable file (containing |
569 | | this code [main.c] ) may be found. A dot will be returned to indicate | |
c801d85f KB |
570 | | the current directory. |
571 | *--------------------------------------------------------------------*/ | |
572 | ||
573 | static void | |
574 | whereami(name) | |
575 | char *name; | |
576 | { | |
3f4a0c5b | 577 | register char *cutoff = NULL; /* stifle -Wall */ |
c801d85f KB |
578 | register char *s; |
579 | register char *t; | |
580 | int cc; | |
581 | char ebuf[4096]; | |
582 | ||
583 | /* | |
584 | * See if the file is accessible either through the current directory | |
585 | * or through an absolute path. | |
586 | */ | |
587 | ||
588 | if (access(name, R_OK) == 0) { | |
589 | ||
3f4a0c5b VZ |
590 | /*-------------------------------------------------------------* |
591 | * The file was accessible without any other work. But the current | |
592 | * working directory might change on us, so if it was accessible | |
593 | * through the cwd, then we should get it for later accesses. | |
594 | *-------------------------------------------------------------*/ | |
c801d85f | 595 | |
3f4a0c5b VZ |
596 | t = imagedir; |
597 | if (!absolute_pathname(name)) { | |
c801d85f | 598 | #if defined(DOS) || defined(__WIN32__) |
3f4a0c5b VZ |
599 | int drive; |
600 | char *newrbuf; | |
c801d85f | 601 | |
3f4a0c5b | 602 | newrbuf = imagedir; |
c801d85f | 603 | #ifndef __DJGPP__ |
3f4a0c5b VZ |
604 | if (*(name + 1) == ':') { |
605 | if (*name >= 'a' && *name <= 'z') | |
606 | drive = (int) (*name - 'a' + 1); | |
607 | else | |
608 | drive = (int) (*name - 'A' + 1); | |
609 | *newrbuf++ = *name; | |
610 | *newrbuf++ = *(name + 1); | |
611 | *newrbuf++ = DIR_SEPARATOR; | |
612 | } | |
613 | else { | |
614 | drive = 0; | |
615 | *newrbuf++ = DIR_SEPARATOR; | |
616 | } | |
617 | if (getcwd(newrbuf, drive) == 0) { /* } */ | |
c801d85f | 618 | #else |
3f4a0c5b | 619 | if (getcwd(newrbuf, 1024) == 0) { /* } */ |
c801d85f KB |
620 | #endif |
621 | #else /* DOS */ | |
622 | #ifdef HAVE_GETWD | |
3f4a0c5b | 623 | if (getwd(imagedir) == 0) { /* } */ |
c801d85f | 624 | #else /* !HAVE_GETWD */ |
3f4a0c5b | 625 | if (getcwd(imagedir, 1024) == 0) { |
c801d85f KB |
626 | #endif /* !HAVE_GETWD */ |
627 | #endif /* DOS */ | |
3f4a0c5b VZ |
628 | fatal_error(FE_GETCWD, 0); |
629 | } | |
630 | for (; *t; t++) /* Set t to end of buffer */ | |
631 | ; | |
632 | if (*(t - 1) == DIR_SEPARATOR) /* leave slash if already | |
633 | * last char | |
634 | */ | |
635 | cutoff = t - 1; | |
636 | else { | |
637 | cutoff = t; /* otherwise put one in */ | |
638 | *t++ = DIR_SEPARATOR; | |
639 | } | |
640 | } | |
c801d85f | 641 | #if (!defined(__MAC__) && !defined(__DJGPP__) && !defined(__GO32__) && !defined(__WIN32__)) |
3f4a0c5b VZ |
642 | else |
643 | (*t++ = DIR_SEPARATOR); | |
c801d85f KB |
644 | #endif |
645 | ||
3f4a0c5b VZ |
646 | /*-------------------------------------------------------------* |
647 | * Copy the rest of the string and set the cutoff if it was not | |
648 | * already set. If the first character of name is a slash, cutoff | |
649 | * is not presently set but will be on the first iteration of the | |
650 | * loop below. | |
651 | *-------------------------------------------------------------*/ | |
c801d85f | 652 | |
3f4a0c5b VZ |
653 | for ((*name == DIR_SEPARATOR ? (s = name+1) : (s = name));;) { |
654 | if (*s == DIR_SEPARATOR) | |
655 | cutoff = t; | |
656 | if (!(*t++ = *s++)) | |
657 | break; | |
658 | } | |
c801d85f KB |
659 | |
660 | } | |
661 | else { | |
662 | ||
3f4a0c5b VZ |
663 | /*-------------------------------------------------------------* |
664 | * Get the path list from the environment. If the path list is | |
665 | * inaccessible for any reason, leave with fatal error. | |
666 | *-------------------------------------------------------------*/ | |
c801d85f KB |
667 | |
668 | #ifdef __MAC__ | |
3f4a0c5b | 669 | if ((s = getenv("Commands")) == (char *) 0) |
c801d85f | 670 | #else |
3f4a0c5b | 671 | if ((s = getenv("PATH")) == (char *) 0) |
c801d85f | 672 | #endif |
3f4a0c5b VZ |
673 | fatal_error(FE_PATH, 0); |
674 | ||
675 | /* | |
676 | * Copy path list into ebuf and set the source pointer to the | |
677 | * beginning of this buffer. | |
678 | */ | |
679 | ||
680 | strcpy(ebuf, s); | |
681 | s = ebuf; | |
682 | ||
683 | for (;;) { | |
684 | t = imagedir; | |
685 | while (*s && *s != PATH_SEPARATOR) | |
686 | *t++ = *s++; | |
687 | if (t > imagedir && *(t - 1) == DIR_SEPARATOR) | |
688 | ; /* do nothing -- slash already is in place */ | |
689 | else | |
690 | *t++ = DIR_SEPARATOR; /* put in the slash */ | |
691 | cutoff = t - 1; /* set cutoff */ | |
692 | strcpy(t, name); | |
693 | if (access(imagedir, R_OK) == 0) | |
694 | break; | |
695 | ||
696 | if (*s) | |
697 | s++; /* advance source pointer */ | |
698 | else | |
699 | fatal_error(FE_INFND, 0); | |
700 | } | |
c801d85f KB |
701 | |
702 | } | |
703 | ||
704 | /*-------------------------------------------------------------* | |
705 | | At this point the full pathname should exist in imagedir and | |
706 | | cutoff should be set to the final slash. We must now determine | |
707 | | whether the file name is a symbolic link or not and chase it down | |
708 | | if it is. Note that we reuse ebuf for getting the link. | |
709 | *-------------------------------------------------------------*/ | |
710 | ||
711 | #ifdef HAVE_SYMLINK | |
712 | while ((cc = readlink(imagedir, ebuf, 512)) != -1) { | |
3f4a0c5b VZ |
713 | ebuf[cc] = 0; |
714 | s = ebuf; | |
715 | if (*s == DIR_SEPARATOR) { | |
716 | t = imagedir; | |
717 | } | |
718 | else { | |
719 | t = cutoff + 1; | |
720 | } | |
721 | for (;;) { | |
722 | if (*s == DIR_SEPARATOR) | |
723 | cutoff = t; /* mark the last slash seen */ | |
724 | if (!(*t++ = *s++)) /* copy the character */ | |
725 | break; | |
726 | } | |
c801d85f KB |
727 | } |
728 | ||
729 | #endif /* HAVE_SYMLINK */ | |
730 | ||
3f4a0c5b VZ |
731 | strcpy(imagename, cutoff + 1); /* keep the image name */ |
732 | *(cutoff + 1) = 0; /* chop off the filename part */ | |
c801d85f KB |
733 | } |
734 | ||
735 | #endif | |
95dee651 KB |
736 | void wxEnableTopLevelWindows(bool enable) |
737 | { | |
738 | wxWindowList::Node *node; | |
739 | for ( node = wxTopLevelWindows.GetFirst(); node; node = node->GetNext() ) | |
740 | node->GetData()->Enable(enable); | |
741 | } | |
ead7ce10 KB |
742 | |
743 | // Yield to other apps/messages and disable user input | |
744 | bool wxSafeYield(wxWindow *win) | |
745 | { | |
a28d23bb | 746 | wxEnableTopLevelWindows(FALSE); |
95dee651 KB |
747 | // always enable ourselves |
748 | if ( win ) | |
749 | win->Enable(TRUE); | |
750 | bool rc = wxYield(); | |
a28d23bb | 751 | wxEnableTopLevelWindows(TRUE); |
95dee651 | 752 | return rc; |
ead7ce10 KB |
753 | } |
754 | ||
dfad0599 JS |
755 | /* |
756 | * N.B. these convenience functions must be separate from msgdlgg.cpp, textdlgg.cpp | |
757 | * since otherwise the generic code may be pulled in unnecessarily. | |
758 | */ | |
759 | ||
760 | int wxMessageBox(const wxString& message, const wxString& caption, long style, | |
761 | wxWindow *parent, int WXUNUSED(x), int WXUNUSED(y) ) | |
762 | { | |
763 | wxMessageDialog dialog(parent, message, caption, style); | |
764 | ||
765 | int ans = dialog.ShowModal(); | |
766 | switch ( ans ) | |
767 | { | |
768 | case wxID_OK: | |
769 | return wxOK; | |
770 | break; | |
771 | case wxID_YES: | |
772 | return wxYES; | |
773 | break; | |
774 | case wxID_NO: | |
775 | return wxNO; | |
776 | break; | |
777 | default: | |
778 | case wxID_CANCEL: | |
779 | return wxCANCEL; | |
780 | break; | |
781 | } | |
782 | return ans; | |
783 | } | |
784 | ||
785 | wxString wxGetTextFromUser(const wxString& message, const wxString& caption, | |
786 | const wxString& defaultValue, wxWindow *parent, | |
787 | int x, int y, bool WXUNUSED(centre) ) | |
788 | { | |
789 | wxTextEntryDialog dialog(parent, message, caption, defaultValue, wxOK|wxCANCEL, wxPoint(x, y)); | |
790 | if (dialog.ShowModal() == wxID_OK) | |
791 | return dialog.GetValue(); | |
792 | else | |
793 | return wxString(""); | |
794 | } | |
795 | ||
469e1e5c | 796 | #ifdef __MWERKS__ |
3f4a0c5b | 797 | char *strdup(const char *s) |
469e1e5c | 798 | { |
3f4a0c5b | 799 | return strcpy( (char*) malloc( strlen( s ) + 1 ) , s ) ; |
469e1e5c SC |
800 | } |
801 | ||
3f4a0c5b | 802 | int isascii( int c ) |
469e1e5c | 803 | { |
3f4a0c5b | 804 | return ( c >= 0 && c < 128 ) ; |
469e1e5c | 805 | } |
f60d0f94 | 806 | #endif |
e2a6f233 | 807 | |
0fb67cd1 VZ |
808 | // ---------------------------------------------------------------------------- |
809 | // network and user id functions | |
810 | // ---------------------------------------------------------------------------- | |
811 | ||
812 | // Get Full RFC822 style email address | |
84fff0b3 | 813 | bool wxGetEmailAddress(wxChar *address, int maxSize) |
e2a6f233 | 814 | { |
0fb67cd1 VZ |
815 | wxString email = wxGetEmailAddress(); |
816 | if ( !email ) | |
e2a6f233 | 817 | return FALSE; |
0fb67cd1 | 818 | |
84fff0b3 OK |
819 | wxStrncpy(address, email, maxSize - 1); |
820 | address[maxSize - 1] = _T('\0'); | |
0fb67cd1 VZ |
821 | |
822 | return TRUE; | |
e2a6f233 JS |
823 | } |
824 | ||
0fb67cd1 | 825 | wxString wxGetEmailAddress() |
e2a6f233 | 826 | { |
0fb67cd1 VZ |
827 | wxString email; |
828 | ||
829 | wxString host = wxGetHostName(); | |
830 | if ( !!host ) | |
e2a6f233 | 831 | { |
0fb67cd1 VZ |
832 | wxString user = wxGetUserId(); |
833 | if ( !!user ) | |
834 | { | |
835 | wxString email(user); | |
84fff0b3 | 836 | email << _T('@') << host; |
0fb67cd1 | 837 | } |
e2a6f233 | 838 | } |
0fb67cd1 VZ |
839 | |
840 | return email; | |
841 | } | |
842 | ||
843 | wxString wxGetUserId() | |
844 | { | |
845 | static const int maxLoginLen = 256; // FIXME arbitrary number | |
846 | ||
847 | wxString buf; | |
848 | bool ok = wxGetUserId(buf.GetWriteBuf(maxLoginLen), maxLoginLen); | |
849 | buf.UngetWriteBuf(); | |
850 | ||
851 | if ( !ok ) | |
852 | buf.Empty(); | |
853 | ||
854 | return buf; | |
855 | } | |
856 | ||
857 | wxString wxGetUserName() | |
858 | { | |
859 | static const int maxUserNameLen = 1024; // FIXME arbitrary number | |
860 | ||
861 | wxString buf; | |
862 | bool ok = wxGetUserName(buf.GetWriteBuf(maxUserNameLen), maxUserNameLen); | |
863 | buf.UngetWriteBuf(); | |
864 | ||
865 | if ( !ok ) | |
866 | buf.Empty(); | |
867 | ||
868 | return buf; | |
e2a6f233 JS |
869 | } |
870 | ||
0fb67cd1 | 871 | wxString wxGetHostName() |
518b5d2f VZ |
872 | { |
873 | static const size_t hostnameSize = 257; | |
0fb67cd1 VZ |
874 | |
875 | wxString buf; | |
518b5d2f VZ |
876 | bool ok = wxGetHostName(buf.GetWriteBuf(hostnameSize), hostnameSize); |
877 | ||
878 | buf.UngetWriteBuf(); | |
879 | ||
0fb67cd1 VZ |
880 | if ( !ok ) |
881 | buf.Empty(); | |
882 | ||
883 | return buf; | |
518b5d2f VZ |
884 | } |
885 |