]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk1/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>
32 #include <sys/systeminfo.h>
35 //------------------------------------------------------------------------
37 //------------------------------------------------------------------------
44 void wxSleep(int nSecs
)
49 int wxKill(long pid
, int sig
)
51 return kill(pid
, sig
);
54 void wxDisplaySize( int *width
, int *height
)
56 if (width
) *width
= gdk_screen_width();
57 if (height
) *height
= gdk_screen_height();
60 void wxGetMousePosition( int* x
, int* y
)
62 wxFAIL_MSG( "GetMousePosition not yet implemented" );
67 bool wxColourDisplay(void)
69 wxFAIL_MSG( "wxColourDisplay always returns TRUE" );
73 int wxDisplayDepth(void)
75 wxFAIL_MSG( "wxDisplayDepth always returns 8" );
79 //------------------------------------------------------------------------
80 // user and home routines
81 //------------------------------------------------------------------------
83 const char* wxGetHomeDir( wxString
*home
)
85 *home
= wxGetUserHome( wxString() );
86 if (home
->IsNull()) *home
= "/";
90 char *wxGetUserHome( const wxString
&user
)
92 struct passwd
*who
= (struct passwd
*) NULL
;
94 if (user
.IsNull() || (user
== ""))
98 if ((ptr
= getenv("HOME")) != NULL
)
100 if ((ptr
= getenv("USER")) != NULL
101 || (ptr
= getenv("LOGNAME")) != NULL
) {
104 // We now make sure the the user exists!
106 who
= getpwuid(getuid());
109 who
= getpwnam (user
);
111 return who
? who
->pw_dir
: (char*)NULL
;
114 //------------------------------------------------------------------------
116 //------------------------------------------------------------------------
118 bool wxGetHostName(char *buf
, int sz
)
121 #if defined(__SVR4__) && !defined(__sgi)
122 return (sysinfo(SI_HOSTNAME
, buf
, sz
) != -1);
123 #else /* BSD Sockets */
127 if (gethostname(name
, sizeof(name
)/sizeof(char)-1) == -1)
129 // Get official full name of host
130 strncpy(buf
, (h
=gethostbyname(name
))!=NULL
? h
->h_name
: name
, sz
-1);
135 bool wxGetUserId(char *buf
, int sz
)
140 if ((who
= getpwuid(getuid ())) != NULL
) {
141 strncpy (buf
, who
->pw_name
, sz
-1);
147 bool wxGetUserName(char *buf
, int sz
)
152 if ((who
= getpwuid (getuid ())) != NULL
) {
153 strncpy (buf
, who
->pw_gecos
, sz
- 1);
159 //------------------------------------------------------------------------
160 // error and debug output routines
161 //------------------------------------------------------------------------
163 void wxDebugMsg( const char *format
, ... )
166 va_start( ap
, format
);
167 vfprintf( stderr
, format
, ap
);
172 void wxError( const wxString
&msg
, const wxString
&title
)
174 fprintf( stderr
, "Error " );
175 if (!title
.IsNull()) fprintf( stderr
, "%s ", WXSTRINGCAST(title
) );
176 if (!msg
.IsNull()) fprintf( stderr
, ": %s", WXSTRINGCAST(msg
) );
177 fprintf( stderr
, ".\n" );
180 void wxFatalError( const wxString
&msg
, const wxString
&title
)
182 fprintf( stderr
, "Error " );
183 if (!title
.IsNull()) fprintf( stderr
, "%s ", WXSTRINGCAST(title
) );
184 if (!msg
.IsNull()) fprintf( stderr
, ": %s", WXSTRINGCAST(msg
) );
185 fprintf( stderr
, ".\n" );
189 //------------------------------------------------------------------------
190 // directory routines
191 //------------------------------------------------------------------------
193 bool wxDirExists( const wxString
& dir
)
196 strcpy( buf
, WXSTRINGCAST(dir
) );
198 return ((stat(buf
, &sbuf
) != -1) && S_ISDIR(sbuf
.st_mode
) ? TRUE
: FALSE
);
201 //------------------------------------------------------------------------
202 // wild character routines
203 //------------------------------------------------------------------------
205 bool wxIsWild( const wxString
& pattern
)
207 wxString tmp
= pattern
;
208 char *pat
= WXSTRINGCAST(tmp
);
211 case '?': case '*': case '[': case '{':
222 bool wxMatchWild( const wxString
& pat
, const wxString
& text
, bool dot_special
)
225 char *pattern
= WXSTRINGCAST(tmp1
);
226 wxString tmp2
= text
;
227 char *str
= WXSTRINGCAST(tmp2
);
230 bool done
= FALSE
, ret_code
, ok
;
231 // Below is for vi fans
232 const char OB
= '{', CB
= '}';
234 // dot_special means '.' only matches '.'
235 if (dot_special
&& *str
== '.' && *pattern
!= *str
)
238 while ((*pattern
!= '\0') && (!done
)
239 && (((*str
=='\0')&&((*pattern
==OB
)||(*pattern
=='*')))||(*str
!='\0'))) {
243 if (*pattern
!= '\0')
250 && (!(ret_code
=wxMatchWild(pattern
, str
++, FALSE
))))
255 while (*pattern
!= '\0')
262 if ((*pattern
== '\0') || (*pattern
== ']')) {
266 if (*pattern
== '\\') {
268 if (*pattern
== '\0') {
273 if (*(pattern
+ 1) == '-') {
276 if (*pattern
== ']') {
280 if (*pattern
== '\\') {
282 if (*pattern
== '\0') {
287 if ((*str
< c
) || (*str
> *pattern
)) {
291 } else if (*pattern
!= *str
) {
296 while ((*pattern
!= ']') && (*pattern
!= '\0')) {
297 if ((*pattern
== '\\') && (*(pattern
+ 1) != '\0'))
301 if (*pattern
!= '\0') {
311 while ((*pattern
!= CB
) && (*pattern
!= '\0')) {
314 while (ok
&& (*cp
!= '\0') && (*pattern
!= '\0')
315 && (*pattern
!= ',') && (*pattern
!= CB
)) {
316 if (*pattern
== '\\')
318 ok
= (*pattern
++ == *cp
++);
320 if (*pattern
== '\0') {
326 while ((*pattern
!= CB
) && (*pattern
!= '\0')) {
327 if (*++pattern
== '\\') {
328 if (*++pattern
== CB
)
333 while (*pattern
!=CB
&& *pattern
!=',' && *pattern
!='\0') {
334 if (*++pattern
== '\\') {
335 if (*++pattern
== CB
|| *pattern
== ',')
340 if (*pattern
!= '\0')
345 if (*str
== *pattern
) {
352 while (*pattern
== '*')
354 return ((*str
== '\0') && (*pattern
== '\0'));
357 //------------------------------------------------------------------------
358 // subprocess routines
359 //------------------------------------------------------------------------
366 static void GTK_EndProcessDetector(gpointer data
, gint source
,
367 GdkInputCondition
WXUNUSED(condition
) )
369 wxEndProcessData
*proc_data
= (wxEndProcessData
*)data
;
372 pid
= (proc_data
->pid
> 0) ? proc_data
->pid
: -(proc_data
->pid
);
374 /* wait4 is not part of any standard, use at own risk
375 * not sure what wait4 does, but wait3 seems to be closest, whats a digit ;-)
376 * --- offer@sgi.com */
378 wait4(proc_data
->pid
, NULL
, 0, NULL
);
380 wait3((int *) NULL
, 0, (rusage
*) NULL
);
384 gdk_input_remove(proc_data
->tag
);
386 if (proc_data
->process
)
387 proc_data
->process
->OnTerminate(proc_data
->pid
);
389 if (proc_data
->pid
> 0)
395 long wxExecute( char **argv
, bool sync
, wxProcess
*process
)
397 wxEndProcessData
*data
= new wxEndProcessData
;
398 int end_proc_detect
[2];
404 if (pipe(end_proc_detect
) == -1) {
405 perror("pipe failed");
409 /* fork the process */
410 #if defined(sun) || defined(__ultrix) || defined(__bsdi__)
416 perror ("fork failed");
418 } else if (pid
== 0) {
419 /* Close fd not useful */
420 close(end_proc_detect
[0]); // close reading side
424 execvp ((const char *)*argv
, (const char **)argv
);
426 execvp (*argv
, argv
);
429 wxError("command not found", *argv
);
432 wxError("could not execute", *argv
);
436 close(end_proc_detect
[1]); // close writing side
437 data
->tag
= gdk_input_add(end_proc_detect
[0], GDK_INPUT_READ
,
438 GTK_EndProcessDetector
, (gpointer
)data
);
441 data
->process
= process
;
443 data
->process
= (wxProcess
*) NULL
;
444 data
->pid
= -(data
->pid
);
446 while (data
->pid
!= 0)
455 long wxExecute( const wxString
& command
, bool sync
, wxProcess
*process
)
457 if (command
.IsNull() || command
== "") return FALSE
;
462 const char *IFS
= " \t\n";
464 strncpy (tmp
, command
, sizeof(tmp
) / sizeof(char) - 1);
465 tmp
[sizeof (tmp
) / sizeof (char) - 1] = '\0';
466 argv
[argc
++] = strtok (tmp
, IFS
);
467 while ((argv
[argc
++] = strtok((char *) NULL
, IFS
)) != NULL
)
469 return wxExecute(argv
, sync
, process
);