]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk/utilsgtk.cpp
1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
7 // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
13 //#pragma implementation "utils.h"
17 #include "wx/string.h"
23 #include <sys/types.h>
31 #include <sys/systeminfo.h>
34 //------------------------------------------------------------------------
36 //------------------------------------------------------------------------
43 //------------------------------------------------------------------------
44 // user and home routines
45 //------------------------------------------------------------------------
47 char* wxGetHomeDir( char *dest
)
49 wxString tmp
= wxGetUserHome( wxString() );
51 strcpy( wxBuffer
, "/" );
53 strcpy( wxBuffer
, tmp
);
54 if (dest
) strcpy( dest
, WXSTRINGCAST tmp
);
58 char *wxGetUserHome( const wxString
&user
)
60 struct passwd
*who
= NULL
;
62 if (user
.IsNull() || (user
== ""))
66 if ((ptr
= getenv("HOME")) != NULL
)
68 if ((ptr
= getenv("USER")) != NULL
69 || (ptr
= getenv("LOGNAME")) != NULL
) {
72 // We now make sure the the user exists!
74 who
= getpwuid(getuid());
77 who
= getpwnam (user
);
79 return who
? who
->pw_dir
: (char*)NULL
;
82 //------------------------------------------------------------------------
84 //------------------------------------------------------------------------
86 bool wxGetHostName(char *buf
, int sz
)
89 #if defined(__SVR4__) && !defined(__sgi)
90 return (sysinfo(SI_HOSTNAME
, buf
, sz
) != -1);
91 #else /* BSD Sockets */
95 if (gethostname(name
, sizeof(name
)/sizeof(char)-1) == -1)
97 // Get official full name of host
98 strncpy(buf
, (h
=gethostbyname(name
))!=NULL
? h
->h_name
: name
, sz
-1);
103 bool wxGetUserId(char *buf
, int sz
)
108 if ((who
= getpwuid(getuid ())) != NULL
) {
109 strncpy (buf
, who
->pw_name
, sz
-1);
115 bool wxGetUserName(char *buf
, int sz
)
120 if ((who
= getpwuid (getuid ())) != NULL
) {
121 strncpy (buf
, who
->pw_gecos
, sz
- 1);
127 //------------------------------------------------------------------------
128 // error and debug output routines
129 //------------------------------------------------------------------------
131 void wxDebugMsg( const char *format
, ... )
134 va_start( ap
, format
);
135 vfprintf( stderr
, format
, ap
);
140 void wxError( const wxString
&msg
, const wxString
&title
)
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" );
148 void wxFatalError( const wxString
&msg
, const wxString
&title
)
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" );
157 //------------------------------------------------------------------------
158 // directory routines
159 //------------------------------------------------------------------------
161 bool wxDirExists( const wxString
& dir
)
164 strcpy( buf
, WXSTRINGCAST(dir
) );
166 return ((stat(buf
, &sbuf
) != -1) && S_ISDIR(sbuf
.st_mode
) ? TRUE
: FALSE
);
169 //------------------------------------------------------------------------
170 // wild character routines
171 //------------------------------------------------------------------------
173 bool wxIsWild( const wxString
& pattern
)
175 wxString tmp
= pattern
;
176 char *pat
= WXSTRINGCAST(tmp
);
179 case '?': case '*': case '[': case '{':
190 bool wxMatchWild( const wxString
& pat
, const wxString
& text
, bool dot_special
)
193 char *pattern
= WXSTRINGCAST(tmp1
);
194 wxString tmp2
= text
;
195 char *str
= WXSTRINGCAST(tmp2
);
198 bool done
= FALSE
, ret_code
, ok
;
199 // Below is for vi fans
200 const char OB
= '{', CB
= '}';
202 // dot_special means '.' only matches '.'
203 if (dot_special
&& *str
== '.' && *pattern
!= *str
)
206 while ((*pattern
!= '\0') && (!done
)
207 && (((*str
=='\0')&&((*pattern
==OB
)||(*pattern
=='*')))||(*str
!='\0'))) {
211 if (*pattern
!= '\0')
218 && (!(ret_code
=wxMatchWild(pattern
, str
++, FALSE
))))
223 while (*pattern
!= '\0')
230 if ((*pattern
== '\0') || (*pattern
== ']')) {
234 if (*pattern
== '\\') {
236 if (*pattern
== '\0') {
241 if (*(pattern
+ 1) == '-') {
244 if (*pattern
== ']') {
248 if (*pattern
== '\\') {
250 if (*pattern
== '\0') {
255 if ((*str
< c
) || (*str
> *pattern
)) {
259 } else if (*pattern
!= *str
) {
264 while ((*pattern
!= ']') && (*pattern
!= '\0')) {
265 if ((*pattern
== '\\') && (*(pattern
+ 1) != '\0'))
269 if (*pattern
!= '\0') {
279 while ((*pattern
!= CB
) && (*pattern
!= '\0')) {
282 while (ok
&& (*cp
!= '\0') && (*pattern
!= '\0')
283 && (*pattern
!= ',') && (*pattern
!= CB
)) {
284 if (*pattern
== '\\')
286 ok
= (*pattern
++ == *cp
++);
288 if (*pattern
== '\0') {
294 while ((*pattern
!= CB
) && (*pattern
!= '\0')) {
295 if (*++pattern
== '\\') {
296 if (*++pattern
== CB
)
301 while (*pattern
!=CB
&& *pattern
!=',' && *pattern
!='\0') {
302 if (*++pattern
== '\\') {
303 if (*++pattern
== CB
|| *pattern
== ',')
308 if (*pattern
!= '\0')
313 if (*str
== *pattern
) {
320 while (*pattern
== '*')
322 return ((*str
== '\0') && (*pattern
== '\0'));
325 //------------------------------------------------------------------------
326 // subprocess routines
327 //------------------------------------------------------------------------
329 long wxExecute( char **argv
, bool Async
)
334 /* fork the process */
335 #if defined(sun) || defined(__ultrix) || defined(__bsdi__)
341 perror ("fork failed");
343 } else if (pid
== 0) {
346 execvp ((const char *)*argv
, (const char **)argv
);
348 execvp (*argv
, argv
);
351 wxError("command not found", *argv
);
354 wxError("could not execute", *argv
);
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!
366 wxSleep(2); // Give a little time
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
)
380 while (wait(&status
) != pid
)
384 wxSleep(3); // 3 sec?
390 long wxExecute( const wxString
& command
, bool Async
)
392 if (command
.IsNull() || command
== "") return FALSE
;
397 const char *IFS
= " \t\n";
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
)
404 return wxExecute(argv
, Async
);