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