]> git.saurik.com Git - wxWidgets.git/blob - src/qt/utilsgtk.cpp
no message
[wxWidgets.git] / src / qt / 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 #include <signal.h>
30
31 #ifdef __SVR4__
32 #include <sys/systeminfo.h>
33 #endif
34
35 //------------------------------------------------------------------------
36 // misc.
37 //------------------------------------------------------------------------
38
39 void wxBell(void)
40 {
41 gdk_beep();
42 };
43
44 void wxSleep(int nSecs)
45 {
46 sleep(nSecs);
47 };
48
49 int wxKill(long pid, int sig)
50 {
51 return kill(pid, sig);
52 };
53
54 void wxDisplaySize( int *width, int *height )
55 {
56 if (width) *width = gdk_screen_width();
57 if (height) *height = gdk_screen_height();
58 }
59
60 //------------------------------------------------------------------------
61 // user and home routines
62 //------------------------------------------------------------------------
63
64 char* wxGetHomeDir( char *dest )
65 {
66 wxString tmp = wxGetUserHome( wxString() );
67 if (tmp.IsNull())
68 strcpy( wxBuffer, "/" );
69 else
70 strcpy( wxBuffer, tmp );
71 if (dest) strcpy( dest, WXSTRINGCAST tmp );
72 return wxBuffer;
73 };
74
75 char *wxGetUserHome( const wxString &user )
76 {
77 struct passwd *who = NULL;
78
79 if (user.IsNull() || (user== ""))
80 {
81 register char *ptr;
82
83 if ((ptr = getenv("HOME")) != NULL)
84 return ptr;
85 if ((ptr = getenv("USER")) != NULL
86 || (ptr = getenv("LOGNAME")) != NULL) {
87 who = getpwnam(ptr);
88 }
89 // We now make sure the the user exists!
90 if (who == NULL)
91 who = getpwuid(getuid());
92 }
93 else
94 who = getpwnam (user);
95
96 return who ? who->pw_dir : (char*)NULL;
97 };
98
99 //------------------------------------------------------------------------
100 // id routines
101 //------------------------------------------------------------------------
102
103 bool wxGetHostName(char *buf, int sz)
104 {
105 *buf = '\0';
106 #if defined(__SVR4__) && !defined(__sgi)
107 return (sysinfo(SI_HOSTNAME, buf, sz) != -1);
108 #else /* BSD Sockets */
109 char name[255];
110 struct hostent *h;
111 // Get hostname
112 if (gethostname(name, sizeof(name)/sizeof(char)-1) == -1)
113 return FALSE;
114 // Get official full name of host
115 strncpy(buf, (h=gethostbyname(name))!=NULL ? h->h_name : name, sz-1);
116 return TRUE;
117 #endif
118 }
119
120 bool wxGetUserId(char *buf, int sz)
121 {
122 struct passwd *who;
123
124 *buf = '\0';
125 if ((who = getpwuid(getuid ())) != NULL) {
126 strncpy (buf, who->pw_name, sz-1);
127 return TRUE;
128 }
129 return FALSE;
130 }
131
132 bool wxGetUserName(char *buf, int sz)
133 {
134 struct passwd *who;
135
136 *buf = '\0';
137 if ((who = getpwuid (getuid ())) != NULL) {
138 strncpy (buf, who->pw_gecos, sz - 1);
139 return TRUE;
140 }
141 return FALSE;
142 }
143
144 //------------------------------------------------------------------------
145 // error and debug output routines
146 //------------------------------------------------------------------------
147
148 void wxDebugMsg( const char *format, ... )
149 {
150 va_list ap;
151 va_start( ap, format );
152 vfprintf( stderr, format, ap );
153 fflush( stderr );
154 va_end(ap);
155 };
156
157 void wxError( const wxString &msg, const wxString &title )
158 {
159 fprintf( stderr, "Error " );
160 if (!title.IsNull()) fprintf( stderr, "%s ", WXSTRINGCAST(title) );
161 if (!msg.IsNull()) fprintf( stderr, ": %s", WXSTRINGCAST(msg) );
162 fprintf( stderr, ".\n" );
163 };
164
165 void wxFatalError( const wxString &msg, const wxString &title )
166 {
167 fprintf( stderr, "Error " );
168 if (!title.IsNull()) fprintf( stderr, "%s ", WXSTRINGCAST(title) );
169 if (!msg.IsNull()) fprintf( stderr, ": %s", WXSTRINGCAST(msg) );
170 fprintf( stderr, ".\n" );
171 exit(1);
172 };
173
174 //------------------------------------------------------------------------
175 // directory routines
176 //------------------------------------------------------------------------
177
178 bool wxDirExists( const wxString& dir )
179 {
180 char buf[500];
181 strcpy( buf, WXSTRINGCAST(dir) );
182 struct stat sbuf;
183 return ((stat(buf, &sbuf) != -1) && S_ISDIR(sbuf.st_mode) ? TRUE : FALSE);
184 };
185
186 //------------------------------------------------------------------------
187 // wild character routines
188 //------------------------------------------------------------------------
189
190 bool wxIsWild( const wxString& pattern )
191 {
192 wxString tmp = pattern;
193 char *pat = WXSTRINGCAST(tmp);
194 while (*pat) {
195 switch (*pat++) {
196 case '?': case '*': case '[': case '{':
197 return TRUE;
198 case '\\':
199 if (!*pat++)
200 return FALSE;
201 }
202 }
203 return FALSE;
204 };
205
206
207 bool wxMatchWild( const wxString& pat, const wxString& text, bool dot_special )
208 {
209 wxString tmp1 = pat;
210 char *pattern = WXSTRINGCAST(tmp1);
211 wxString tmp2 = text;
212 char *str = WXSTRINGCAST(tmp2);
213 char c;
214 char *cp;
215 bool done = FALSE, ret_code, ok;
216 // Below is for vi fans
217 const char OB = '{', CB = '}';
218
219 // dot_special means '.' only matches '.'
220 if (dot_special && *str == '.' && *pattern != *str)
221 return FALSE;
222
223 while ((*pattern != '\0') && (!done)
224 && (((*str=='\0')&&((*pattern==OB)||(*pattern=='*')))||(*str!='\0'))) {
225 switch (*pattern) {
226 case '\\':
227 pattern++;
228 if (*pattern != '\0')
229 pattern++;
230 break;
231 case '*':
232 pattern++;
233 ret_code = FALSE;
234 while ((*str!='\0')
235 && (!(ret_code=wxMatchWild(pattern, str++, FALSE))))
236 /*loop*/;
237 if (ret_code) {
238 while (*str != '\0')
239 str++;
240 while (*pattern != '\0')
241 pattern++;
242 }
243 break;
244 case '[':
245 pattern++;
246 repeat:
247 if ((*pattern == '\0') || (*pattern == ']')) {
248 done = TRUE;
249 break;
250 }
251 if (*pattern == '\\') {
252 pattern++;
253 if (*pattern == '\0') {
254 done = TRUE;
255 break;
256 }
257 }
258 if (*(pattern + 1) == '-') {
259 c = *pattern;
260 pattern += 2;
261 if (*pattern == ']') {
262 done = TRUE;
263 break;
264 }
265 if (*pattern == '\\') {
266 pattern++;
267 if (*pattern == '\0') {
268 done = TRUE;
269 break;
270 }
271 }
272 if ((*str < c) || (*str > *pattern)) {
273 pattern++;
274 goto repeat;
275 }
276 } else if (*pattern != *str) {
277 pattern++;
278 goto repeat;
279 }
280 pattern++;
281 while ((*pattern != ']') && (*pattern != '\0')) {
282 if ((*pattern == '\\') && (*(pattern + 1) != '\0'))
283 pattern++;
284 pattern++;
285 }
286 if (*pattern != '\0') {
287 pattern++, str++;
288 }
289 break;
290 case '?':
291 pattern++;
292 str++;
293 break;
294 case OB:
295 pattern++;
296 while ((*pattern != CB) && (*pattern != '\0')) {
297 cp = str;
298 ok = TRUE;
299 while (ok && (*cp != '\0') && (*pattern != '\0')
300 && (*pattern != ',') && (*pattern != CB)) {
301 if (*pattern == '\\')
302 pattern++;
303 ok = (*pattern++ == *cp++);
304 }
305 if (*pattern == '\0') {
306 ok = FALSE;
307 done = TRUE;
308 break;
309 } else if (ok) {
310 str = cp;
311 while ((*pattern != CB) && (*pattern != '\0')) {
312 if (*++pattern == '\\') {
313 if (*++pattern == CB)
314 pattern++;
315 }
316 }
317 } else {
318 while (*pattern!=CB && *pattern!=',' && *pattern!='\0') {
319 if (*++pattern == '\\') {
320 if (*++pattern == CB || *pattern == ',')
321 pattern++;
322 }
323 }
324 }
325 if (*pattern != '\0')
326 pattern++;
327 }
328 break;
329 default:
330 if (*str == *pattern) {
331 str++, pattern++;
332 } else {
333 done = TRUE;
334 }
335 }
336 }
337 while (*pattern == '*')
338 pattern++;
339 return ((*str == '\0') && (*pattern == '\0'));
340 };
341
342 //------------------------------------------------------------------------
343 // subprocess routines
344 //------------------------------------------------------------------------
345
346 long wxExecute( char **argv, bool sync, wxProcess *process )
347 {
348 };
349
350 long wxExecute( const wxString& command, bool sync, wxProcess *process )
351 {
352 if (command.IsNull() || command == "") return FALSE;
353
354 int argc = 0;
355 char *argv[127];
356 char tmp[1024];
357 const char *IFS = " \t\n";
358
359 strncpy (tmp, command, sizeof(tmp) / sizeof(char) - 1);
360 tmp[sizeof (tmp) / sizeof (char) - 1] = '\0';
361 argv[argc++] = strtok (tmp, IFS);
362 while ((argv[argc++] = strtok(NULL, IFS)) != NULL)
363 /* loop */ ;
364 return wxExecute(argv, sync, process);
365 };
366
367