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