]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/utilsgtk.cpp
GTK wxBitmapButton added
[wxWidgets.git] / src / gtk / utilsgtk.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: utils.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Created: 01/02/97
6 // Id:
7 // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11
12 //#ifdef __GNUG__
13 //#pragma implementation "utils.h"
14 //#endif
15
16 #include "wx/utils.h"
17 #include "wx/string.h"
18
19 #include <stdarg.h>
20 #include <dirent.h>
21 #include <string.h>
22 #include <sys/stat.h>
23 #include <sys/types.h>
24 #include <unistd.h>
25 #include <sys/wait.h>
26 #include <pwd.h>
27 #include <errno.h>
28 #include <netdb.h>
29
30 #ifdef __SVR4__
31 #include <sys/systeminfo.h>
32 #endif
33
34 //------------------------------------------------------------------------
35 // misc.
36 //------------------------------------------------------------------------
37
38 void wxBell(void)
39 {
40 gdk_beep();
41 };
42
43 //------------------------------------------------------------------------
44 // user and home routines
45 //------------------------------------------------------------------------
46
47 char* wxGetHomeDir( char *dest )
48 {
49 wxString tmp = wxGetUserHome( wxString() );
50 if (tmp.IsNull())
51 strcpy( wxBuffer, "/" );
52 else
53 strcpy( wxBuffer, tmp );
54 if (dest) strcpy( dest, WXSTRINGCAST tmp );
55 return wxBuffer;
56 };
57
58 char *wxGetUserHome( const wxString &user )
59 {
60 struct passwd *who = NULL;
61
62 if (user.IsNull() || (user== ""))
63 {
64 register char *ptr;
65
66 if ((ptr = getenv("HOME")) != NULL)
67 return ptr;
68 if ((ptr = getenv("USER")) != NULL
69 || (ptr = getenv("LOGNAME")) != NULL) {
70 who = getpwnam(ptr);
71 }
72 // We now make sure the the user exists!
73 if (who == NULL)
74 who = getpwuid(getuid());
75 }
76 else
77 who = getpwnam (user);
78
79 return who ? who->pw_dir : (char*)NULL;
80 };
81
82 //------------------------------------------------------------------------
83 // id routines
84 //------------------------------------------------------------------------
85
86 bool wxGetHostName(char *buf, int sz)
87 {
88 *buf = '\0';
89 #if defined(__SVR4__) && !defined(__sgi)
90 return (sysinfo(SI_HOSTNAME, buf, sz) != -1);
91 #else /* BSD Sockets */
92 char name[255];
93 struct hostent *h;
94 // Get hostname
95 if (gethostname(name, sizeof(name)/sizeof(char)-1) == -1)
96 return FALSE;
97 // Get official full name of host
98 strncpy(buf, (h=gethostbyname(name))!=NULL ? h->h_name : name, sz-1);
99 return TRUE;
100 #endif
101 }
102
103 bool wxGetUserId(char *buf, int sz)
104 {
105 struct passwd *who;
106
107 *buf = '\0';
108 if ((who = getpwuid(getuid ())) != NULL) {
109 strncpy (buf, who->pw_name, sz-1);
110 return TRUE;
111 }
112 return FALSE;
113 }
114
115 bool wxGetUserName(char *buf, int sz)
116 {
117 struct passwd *who;
118
119 *buf = '\0';
120 if ((who = getpwuid (getuid ())) != NULL) {
121 strncpy (buf, who->pw_gecos, sz - 1);
122 return TRUE;
123 }
124 return FALSE;
125 }
126
127 //------------------------------------------------------------------------
128 // error and debug output routines
129 //------------------------------------------------------------------------
130
131 void wxDebugMsg( const char *format, ... )
132 {
133 va_list ap;
134 va_start( ap, format );
135 vfprintf( stderr, format, ap );
136 fflush( stderr );
137 va_end(ap);
138 };
139
140 void wxError( const wxString &msg, const wxString &title )
141 {
142 fprintf( stderr, "Error " );
143 if (!title.IsNull()) fprintf( stderr, "%s ", WXSTRINGCAST(title) );
144 if (!msg.IsNull()) fprintf( stderr, ": %s", WXSTRINGCAST(msg) );
145 fprintf( stderr, ".\n" );
146 };
147
148 void wxFatalError( const wxString &msg, const wxString &title )
149 {
150 fprintf( stderr, "Error " );
151 if (!title.IsNull()) fprintf( stderr, "%s ", WXSTRINGCAST(title) );
152 if (!msg.IsNull()) fprintf( stderr, ": %s", WXSTRINGCAST(msg) );
153 fprintf( stderr, ".\n" );
154 exit(1);
155 };
156
157 //------------------------------------------------------------------------
158 // directory routines
159 //------------------------------------------------------------------------
160
161 bool wxDirExists( const wxString& dir )
162 {
163 char buf[500];
164 strcpy( buf, WXSTRINGCAST(dir) );
165 struct stat sbuf;
166 return ((stat(buf, &sbuf) != -1) && S_ISDIR(sbuf.st_mode) ? TRUE : FALSE);
167 };
168
169 //------------------------------------------------------------------------
170 // wild character routines
171 //------------------------------------------------------------------------
172
173 bool wxIsWild( const wxString& pattern )
174 {
175 wxString tmp = pattern;
176 char *pat = WXSTRINGCAST(tmp);
177 while (*pat) {
178 switch (*pat++) {
179 case '?': case '*': case '[': case '{':
180 return TRUE;
181 case '\\':
182 if (!*pat++)
183 return FALSE;
184 }
185 }
186 return FALSE;
187 };
188
189
190 bool wxMatchWild( const wxString& pat, const wxString& text, bool dot_special )
191 {
192 wxString tmp1 = pat;
193 char *pattern = WXSTRINGCAST(tmp1);
194 wxString tmp2 = text;
195 char *str = WXSTRINGCAST(tmp2);
196 char c;
197 char *cp;
198 bool done = FALSE, ret_code, ok;
199 // Below is for vi fans
200 const char OB = '{', CB = '}';
201
202 // dot_special means '.' only matches '.'
203 if (dot_special && *str == '.' && *pattern != *str)
204 return FALSE;
205
206 while ((*pattern != '\0') && (!done)
207 && (((*str=='\0')&&((*pattern==OB)||(*pattern=='*')))||(*str!='\0'))) {
208 switch (*pattern) {
209 case '\\':
210 pattern++;
211 if (*pattern != '\0')
212 pattern++;
213 break;
214 case '*':
215 pattern++;
216 ret_code = FALSE;
217 while ((*str!='\0')
218 && (!(ret_code=wxMatchWild(pattern, str++, FALSE))))
219 /*loop*/;
220 if (ret_code) {
221 while (*str != '\0')
222 str++;
223 while (*pattern != '\0')
224 pattern++;
225 }
226 break;
227 case '[':
228 pattern++;
229 repeat:
230 if ((*pattern == '\0') || (*pattern == ']')) {
231 done = TRUE;
232 break;
233 }
234 if (*pattern == '\\') {
235 pattern++;
236 if (*pattern == '\0') {
237 done = TRUE;
238 break;
239 }
240 }
241 if (*(pattern + 1) == '-') {
242 c = *pattern;
243 pattern += 2;
244 if (*pattern == ']') {
245 done = TRUE;
246 break;
247 }
248 if (*pattern == '\\') {
249 pattern++;
250 if (*pattern == '\0') {
251 done = TRUE;
252 break;
253 }
254 }
255 if ((*str < c) || (*str > *pattern)) {
256 pattern++;
257 goto repeat;
258 }
259 } else if (*pattern != *str) {
260 pattern++;
261 goto repeat;
262 }
263 pattern++;
264 while ((*pattern != ']') && (*pattern != '\0')) {
265 if ((*pattern == '\\') && (*(pattern + 1) != '\0'))
266 pattern++;
267 pattern++;
268 }
269 if (*pattern != '\0') {
270 pattern++, str++;
271 }
272 break;
273 case '?':
274 pattern++;
275 str++;
276 break;
277 case OB:
278 pattern++;
279 while ((*pattern != CB) && (*pattern != '\0')) {
280 cp = str;
281 ok = TRUE;
282 while (ok && (*cp != '\0') && (*pattern != '\0')
283 && (*pattern != ',') && (*pattern != CB)) {
284 if (*pattern == '\\')
285 pattern++;
286 ok = (*pattern++ == *cp++);
287 }
288 if (*pattern == '\0') {
289 ok = FALSE;
290 done = TRUE;
291 break;
292 } else if (ok) {
293 str = cp;
294 while ((*pattern != CB) && (*pattern != '\0')) {
295 if (*++pattern == '\\') {
296 if (*++pattern == CB)
297 pattern++;
298 }
299 }
300 } else {
301 while (*pattern!=CB && *pattern!=',' && *pattern!='\0') {
302 if (*++pattern == '\\') {
303 if (*++pattern == CB || *pattern == ',')
304 pattern++;
305 }
306 }
307 }
308 if (*pattern != '\0')
309 pattern++;
310 }
311 break;
312 default:
313 if (*str == *pattern) {
314 str++, pattern++;
315 } else {
316 done = TRUE;
317 }
318 }
319 }
320 while (*pattern == '*')
321 pattern++;
322 return ((*str == '\0') && (*pattern == '\0'));
323 };
324
325 //------------------------------------------------------------------------
326 // subprocess routines
327 //------------------------------------------------------------------------
328
329 long wxExecute( char **argv, bool Async )
330 {
331 if (*argv == NULL)
332 return FALSE;
333
334 /* fork the process */
335 #if defined(sun) || defined(__ultrix) || defined(__bsdi__)
336 pid_t pid = vfork();
337 #else
338 pid_t pid = fork();
339 #endif
340 if (pid == -1) {
341 perror ("fork failed");
342 return FALSE;
343 } else if (pid == 0) {
344 /* child */
345 #ifdef _AIX
346 execvp ((const char *)*argv, (const char **)argv);
347 #else
348 execvp (*argv, argv);
349 #endif
350 if (errno == ENOENT)
351 wxError("command not found", *argv);
352 else
353 perror (*argv);
354 wxError("could not execute", *argv);
355 _exit (-1);
356 }
357
358 // Code below is NOT really acceptable!
359 // One should NEVER use wait under X
360 // Ideas? A Sleep idle callback?
361 // WARNING: WARNING: WARNING: WARNING:
362 // The CODE BELOW IS BAD BAD BAD BAD!
363 if (Async) {
364 int status;
365 /*
366 wxSleep(2); // Give a little time
367 */
368 #if !defined(DG) && \
369 !defined(__AIX__) && \
370 !defined(__xlC__) && \
371 !defined(__SVR4__) && \
372 !defined(__SUN__) && \
373 !defined(__ALPHA__) && \
374 !defined(__SGI__) && \
375 !defined(__HPUX__) && \
376 !defined(__SUNPRO_CC) && \
377 !defined(__FreeBSD__)
378 while (wait((union wait*)&status) != pid)
379 #else
380 while (wait(&status) != pid)
381 #endif
382 {};
383 /*
384 wxSleep(3); // 3 sec?
385 */
386 };
387 return TRUE;
388 };
389
390 long wxExecute( const wxString& command, bool Async )
391 {
392 if (command.IsNull() || command == "") return FALSE;
393
394 int argc = 0;
395 char *argv[127];
396 char tmp[1024];
397 const char *IFS = " \t\n";
398
399 strncpy (tmp, command, sizeof(tmp) / sizeof(char) - 1);
400 tmp[sizeof (tmp) / sizeof (char) - 1] = '\0';
401 argv[argc++] = strtok (tmp, IFS);
402 while ((argv[argc++] = strtok(NULL, IFS)) != NULL)
403 /* loop */ ;
404 return wxExecute(argv, Async);
405 };
406