]> git.saurik.com Git - wxWidgets.git/blame - src/os2/utils.cpp
return the index of the inserted/appended item
[wxWidgets.git] / src / os2 / utils.cpp
CommitLineData
0e320a79
DW
1/////////////////////////////////////////////////////////////////////////////
2// Name: utils.cpp
3// Purpose: Various utilities
45fcbf3b 4// Author: David Webster
0e320a79 5// Modified by:
45fcbf3b 6// Created: 09/17/99
0e320a79 7// RCS-ID: $Id$
45fcbf3b 8// Copyright: (c) David Webster
6aa89a22 9// Licence: wxWindows licence
0e320a79
DW
10/////////////////////////////////////////////////////////////////////////////
11
45fcbf3b
DW
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
15#ifndef WX_PRECOMP
16 #include "wx/setup.h"
17 #include "wx/utils.h"
18 #include "wx/app.h"
45fcbf3b
DW
19#endif //WX_PRECOMP
20
21#include "wx/os2/private.h"
22#include "wx/timer.h"
23#include "wx/intl.h"
cd478a95 24#include "wx/apptrait.h"
45fcbf3b 25
0e320a79 26#include <ctype.h>
06298235
SN
27#ifdef __EMX__
28#include <dirent.h>
06298235 29#endif
45fcbf3b
DW
30
31#include "wx/log.h"
32
33#include <io.h>
0e320a79
DW
34
35#include <stdio.h>
36#include <stdlib.h>
37#include <string.h>
45fcbf3b 38#include <errno.h>
0e320a79
DW
39#include <stdarg.h>
40
d90895ac 41#define PURE_32
468e327a 42
06298235 43#ifndef __EMX__
7cdc2f1e
DW
44#include <upm.h>
45#include <netcons.h>
46#include <netbios.h>
06298235 47#endif
45fcbf3b 48
86de7616
DW
49static const wxChar WX_SECTION[] = _T("wxWindows");
50static const wxChar eHOSTNAME[] = _T("HostName");
51static const wxChar eUSERID[] = _T("UserId");
52static const wxChar eUSERNAME[] = _T("UserName");
45fcbf3b
DW
53
54// For the following functions we SHOULD fill in support
55// for Windows-NT (which I don't know) as I assume it begin
56// a POSIX Unix (so claims MS) that it has some special
57// functions beyond those provided by WinSock
58
0e320a79 59// Get full hostname (eg. DoDo.BSn-Germany.crg.de)
10e5b930
DW
60bool wxGetHostName(
61 wxChar* zBuf
62, int nMaxSize
63)
0e320a79 64{
d90895ac 65#if wxUSE_NET_API
10e5b930
DW
66 char zServer[256];
67 char zComputer[256];
909b4f08 68 unsigned long ulLevel = 0;
9dea36ef
DW
69 unsigned char* zBuffer = NULL;
70 unsigned long ulBuffer = 256;
71 unsigned long* pulTotalAvail = NULL;
10e5b930 72
dde11e60
DW
73 NetBios32GetInfo( (const unsigned char*)zServer
74 ,(const unsigned char*)zComputer
909b4f08 75 ,ulLevel
dde11e60 76 ,zBuffer
909b4f08
DW
77 ,ulBuffer
78 ,pulTotalAvail
dde11e60 79 );
10e5b930 80 strcpy(zBuf, zServer);
45fcbf3b 81#else
10e5b930
DW
82 wxChar* zSysname;
83 const wxChar* zDefaultHost = _T("noname");
45fcbf3b 84
10e5b930
DW
85 if ((zSysname = wxGetenv(_T("SYSTEM_NAME"))) == NULL)
86 {
87 ULONG n = ::PrfQueryProfileString( HINI_PROFILE
88 ,(PSZ)WX_SECTION
89 ,(PSZ)eHOSTNAME
90 ,(PSZ)zDefaultHost
91 ,(void*)zBuf
92 ,(ULONG)nMaxSize - 1
93 );
94 }
95 else
96 wxStrncpy(zBuf, zSysname, nMaxSize - 1);
97 zBuf[nMaxSize] = _T('\0');
45fcbf3b 98#endif
10e5b930 99 return *zBuf ? TRUE : FALSE;
0e320a79
DW
100}
101
102// Get user ID e.g. jacs
10e5b930
DW
103bool wxGetUserId(
104 wxChar* zBuf
2bd5bbc9 105, int nType
10e5b930 106)
0e320a79 107{
19193a2c 108#if defined(__VISAGECPP__)
2bd5bbc9
DW
109 long lrc;
110 // UPM procs return 0 on success
111 lrc = U32ELOCU((unsigned char*)zBuf, (unsigned long *)&nType);
112 if (lrc == 0) return TRUE;
06298235 113#endif
2bd5bbc9 114 return FALSE;
0e320a79
DW
115}
116
10e5b930
DW
117bool wxGetUserName(
118 wxChar* zBuf
119, int nMaxSize
120)
0e320a79 121{
45fcbf3b 122#ifdef USE_NET_API
10e5b930
DW
123 wxGetUserId( zBuf
124 ,nMaxSize
125 );
45fcbf3b 126#else
10e5b930 127 wxStrncpy(zBuf, _T("Unknown User"), nMaxSize);
45fcbf3b 128#endif
10e5b930 129 return TRUE;
0e320a79
DW
130}
131
10e5b930
DW
132int wxKill(
133 long lPid
2004cad1
DW
134, wxSignal eSig
135, wxKillError* peError
10e5b930 136)
0e320a79 137{
10e5b930 138 return((int)::DosKillProcess(0, (PID)lPid));
0e320a79
DW
139}
140
141//
142// Execute a program in an Interactive Shell
143//
10e5b930
DW
144bool wxShell(
145 const wxString& rCommand
146)
0e320a79 147{
c5fb56c0
DW
148 wxChar* zShell = _T("CMD.EXE");
149 wxString sInputs;
10e5b930 150 wxChar zTmp[255];
c5fb56c0
DW
151 STARTDATA SData = {0};
152 PSZ PgmTitle = "Command Shell";
153 APIRET rc;
154 PID vPid = 0;
155 ULONG ulSessID = 0;
156 UCHAR achObjBuf[256] = {0}; //error data if DosStart fails
157 RESULTCODES vResult;
158
159 SData.Length = sizeof(STARTDATA);
160 SData.Related = SSF_RELATED_INDEPENDENT;
161 SData.FgBg = SSF_FGBG_FORE;
162 SData.TraceOpt = SSF_TRACEOPT_NONE;
163 SData.PgmTitle = PgmTitle;
164 SData.PgmName = zShell;
165
909b4f08 166 sInputs = "/C " + rCommand;
9ac6ff7b 167 SData.PgmInputs = (BYTE*)sInputs.c_str();
c5fb56c0
DW
168 SData.TermQ = 0;
169 SData.Environment = 0;
170 SData.InheritOpt = SSF_INHERTOPT_SHELL;
171 SData.SessionType = SSF_TYPE_WINDOWABLEVIO;
172 SData.IconFile = 0;
173 SData.PgmHandle = 0;
174 SData.PgmControl = SSF_CONTROL_VISIBLE | SSF_CONTROL_MAXIMIZE;
175 SData.InitXPos = 30;
176 SData.InitYPos = 40;
177 SData.InitXSize = 200;
178 SData.InitYSize = 140;
179 SData.Reserved = 0;
180 SData.ObjectBuffer = (char*)achObjBuf;
181 SData.ObjectBuffLen = (ULONG)sizeof(achObjBuf);
182
183 rc = ::DosStartSession(&SData, &ulSessID, &vPid);
9ac6ff7b 184 if (rc == 0 || rc == 457) // NO_ERROR or SMG_START_IN_BACKGROUND
c5fb56c0
DW
185 {
186 PTIB ptib;
187 PPIB ppib;
10e5b930 188
c5fb56c0 189 ::DosGetInfoBlocks(&ptib, &ppib);
10e5b930 190
c5fb56c0
DW
191 ::DosWaitChild( DCWA_PROCESS
192 ,DCWW_WAIT
193 ,&vResult
194 ,&ppib->pib_ulpid
195 ,vPid
196 );
197 }
198 return (rc != 0);
0e320a79
DW
199}
200
a23692f0 201// Shutdown or reboot the PC
f6ba47d9
VZ
202bool wxShutdown(wxShutdownFlags wFlags)
203{
204 // TODO
205 return FALSE;
206}
207
0e320a79 208// Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
5d4b632b 209long wxGetFreeMemory()
0e320a79 210{
9dea36ef 211 void* pMemptr = NULL;
78d50441
DW
212 ULONG lSize;
213 ULONG lMemFlags;
214 APIRET rc;
215
216 lMemFlags = PAG_FREE;
10e5b930 217 rc = ::DosQueryMem(pMemptr, &lSize, &lMemFlags);
78d50441
DW
218 if (rc != 0)
219 return -1L;
220 return (long)lSize;
45fcbf3b
DW
221}
222
8269a903
SN
223// ----------------------------------------------------------------------------
224// env vars
225// ----------------------------------------------------------------------------
226
227bool wxGetEnv(const wxString& var, wxString *value)
228{
229 // wxGetenv is defined as getenv()
230 wxChar *p = wxGetenv(var);
231 if ( !p )
232 return FALSE;
233
234 if ( value )
235 {
236 *value = p;
237 }
238
239 return TRUE;
240}
241
242bool wxSetEnv(const wxString& variable, const wxChar *value)
243{
244#if defined(HAVE_SETENV)
245 return setenv(variable.mb_str(), value ? wxString(value).mb_str().data()
246 : NULL, 1 /* overwrite */) == 0;
247#elif defined(HAVE_PUTENV)
248 wxString s = variable;
249 if ( value )
250 s << _T('=') << value;
251
252 // transform to ANSI
253 const char *p = s.mb_str();
254
255 // the string will be free()d by libc
256 char *buf = (char *)malloc(strlen(p) + 1);
257 strcpy(buf, p);
258
259 return putenv(buf) == 0;
260#else // no way to set an env var
261 return FALSE;
262#endif
263}
264
10e5b930
DW
265void wxUsleep(
266 unsigned long ulMilliseconds
267)
45fcbf3b 268{
f53a0d32 269 ::DosSleep(ulMilliseconds);
0e320a79
DW
270}
271
10e5b930
DW
272void wxSleep(
273 int nSecs
274)
0e320a79 275{
10e5b930 276 ::DosSleep(1000 * nSecs);
0e320a79
DW
277}
278
279// Consume all events until no more left
280void wxFlushEvents()
281{
45fcbf3b 282// wxYield();
0e320a79
DW
283}
284
73deed44
VZ
285#if WXWIN_COMPATIBILITY_2_2
286
45fcbf3b 287// Output a debug mess., in a system dependent fashion.
10e5b930
DW
288void wxDebugMsg(
289 const wxChar* zFmt ...
290)
291{
292 va_list vAp;
293 static wxChar zBuffer[512];
294
295 if (!wxTheApp->GetWantDebugOutput())
296 return ;
297 va_start(vAp, zFmt);
298 sprintf(zBuffer, zFmt, vAp) ;
299 va_end(vAp);
0e320a79
DW
300}
301
302// Non-fatal error: pop up message box and (possibly) continue
10e5b930
DW
303void wxError(
304 const wxString& rMsg
305, const wxString& rTitle
306)
307{
13a4ea8d 308 wxBuffer = new wxChar[256];
10e5b930
DW
309 wxSprintf(wxBuffer, "%s\nContinue?", WXSTRINGCAST rMsg);
310 if (::WinMessageBox( HWND_DESKTOP
311 ,NULL
312 ,(PSZ)wxBuffer
313 ,(PSZ)WXSTRINGCAST rTitle
314 ,0
315 ,MB_ICONEXCLAMATION | MB_YESNO
316 ) == MBID_YES)
13a4ea8d 317 delete[] wxBuffer;
0e320a79
DW
318 wxExit();
319}
320
321// Fatal error: pop up message box and abort
10e5b930
DW
322void wxFatalError(
323 const wxString& rMsg
324, const wxString& rTitle
325)
326{
327 unsigned long ulRc;
328
329 ulRc = ::WinMessageBox( HWND_DESKTOP
330 ,NULL
331 ,WXSTRINGCAST rMsg
332 ,WXSTRINGCAST rTitle
333 ,0
334 ,MB_NOICON | MB_OK
335 );
336 DosExit(EXIT_PROCESS, ulRc);
0e320a79
DW
337}
338
73deed44
VZ
339#endif // WXWIN_COMPATIBILITY_2_2
340
0e320a79
DW
341// Emit a beeeeeep
342void wxBell()
343{
45fcbf3b 344 DosBeep(1000,1000); // 1kHz during 1 sec.
0e320a79 345}
bd3b171d 346#if 0
cd478a95 347int wxGUIAppTraits::GetOSVersion(
10e5b930
DW
348 int* pMajorVsn
349, int* pMinorVsn
350)
351{
352 ULONG ulSysInfo[QSV_MAX] = {0};
5d4b632b
DW
353 APIRET ulrc;
354
355 ulrc = ::DosQuerySysInfo( 1L
356 ,QSV_MAX
357 ,(PVOID)ulSysInfo
358 ,sizeof(ULONG) * QSV_MAX
359 );
360 if (ulrc == 0L)
10e5b930
DW
361 {
362 *pMajorVsn = ulSysInfo[QSV_VERSION_MAJOR];
5d4b632b 363 *pMajorVsn = *pMajorVsn/10;
10e5b930
DW
364 *pMinorVsn = ulSysInfo[QSV_VERSION_MINOR];
365 return wxWINDOWS_OS2;
366 }
367 return wxWINDOWS; // error if we get here, return generic value
0e320a79 368}
bd3b171d 369#endif
45fcbf3b 370// ---------------------------------------------------------------------------
1be7f92a
DW
371const wxChar* wxGetHomeDir(
372 wxString* pStr
373)
45fcbf3b 374{
1be7f92a 375 wxString& rStrDir = *pStr;
0e320a79 376
1be7f92a
DW
377 // OS/2 has no idea about home,
378 // so use the working directory instead?
45fcbf3b 379
1be7f92a 380 // 256 was taken from os2def.h
45fcbf3b
DW
381#ifndef MAX_PATH
382# define MAX_PATH 256
383#endif
384
1be7f92a
DW
385 char zDirName[256];
386 ULONG ulDirLen;
45fcbf3b 387
1be7f92a
DW
388 ::DosQueryCurrentDir(0, zDirName, &ulDirLen);
389 rStrDir = zDirName;
390 return rStrDir.c_str();
45fcbf3b
DW
391}
392
10e5b930 393// Hack for OS/2
1be7f92a
DW
394wxChar* wxGetUserHome (
395 const wxString& rUser
396)
397{
398 wxChar* zHome;
399 wxString sUser1(rUser);
400
13a4ea8d 401 wxBuffer = new wxChar[256];
06298235 402#ifndef __EMX__
1be7f92a 403 if (sUser1 != _T(""))
45fcbf3b 404 {
1be7f92a
DW
405 wxChar zTmp[64];
406
407 if (wxGetUserId( zTmp
408 ,sizeof(zTmp)/sizeof(char)
409 ))
410 {
411 // Guests belong in the temp dir
412 if (wxStricmp(zTmp, _T("annonymous")) == 0)
413 {
414 if ((zHome = wxGetenv(_T("TMP"))) != NULL ||
415 (zHome = wxGetenv(_T("TMPDIR"))) != NULL ||
416 (zHome = wxGetenv(_T("TEMP"))) != NULL)
13a4ea8d 417 delete[] wxBuffer;
1be7f92a
DW
418 return *zHome ? zHome : (wxChar*)_T("\\");
419 }
420 if (wxStricmp(zTmp, WXSTRINGCAST sUser1) == 0)
421 sUser1 = _T("");
422 }
45fcbf3b 423 }
06298235 424#endif
1be7f92a 425 if (sUser1 == _T(""))
13a4ea8d 426 {
1be7f92a
DW
427 if ((zHome = wxGetenv(_T("HOME"))) != NULL)
428 {
429 wxStrcpy(wxBuffer, zHome);
2b5f62a0 430 wxUnix2DosFilename(wxBuffer);
13a4ea8d
DW
431 wxStrcpy(zHome, wxBuffer);
432 delete[] wxBuffer;
433 return zHome;
1be7f92a 434 }
13a4ea8d
DW
435 }
436 delete[] wxBuffer;
437 return NULL; // No home known!
0e320a79
DW
438}
439
1be7f92a
DW
440bool wxDirExists(
441 const wxString& rDir
442)
45fcbf3b 443{
1be7f92a 444 return (::DosSetCurrentDir(WXSTRINGCAST rDir));
45fcbf3b
DW
445}
446
914589c2
DW
447wxString WXDLLEXPORT wxPMErrorToStr(
448 ERRORID vError
449)
450{
451 wxString sError;
452
453 //
454 // Remove the high order byte -- it is useless
455 //
456 vError &= 0x0000ffff;
457 switch(vError)
458 {
459 case PMERR_INVALID_HWND:
460 sError = wxT("Invalid window handle specified");
461 break;
462
463 case PMERR_INVALID_FLAG:
464 sError = wxT("Invalid flag bit set");
465 break;
466
467 case PMERR_NO_MSG_QUEUE:
468 sError = wxT("No message queue available");
469 break;
470
471 case PMERR_INVALID_PARM:
472 sError = wxT("Parameter contained invalid data");
473 break;
474
475 case PMERR_INVALID_PARAMETERS:
476 sError = wxT("Parameter value is out of range");
477 break;
478
479 case PMERR_PARAMETER_OUT_OF_RANGE:
480 sError = wxT("Parameter value is out of range");
481 break;
482
483 case PMERR_INVALID_INTEGER_ATOM:
484 sError = wxT("Not a valid atom");
485 break;
486
487 case PMERR_INVALID_HATOMTBL:
488 sError = wxT("Atom table handle is invalid");
489 break;
490
491 case PMERR_INVALID_ATOM_NAME:
492 sError = wxT("Not a valid atom name");
493 break;
494
495 case PMERR_ATOM_NAME_NOT_FOUND:
496 sError = wxT("Valid name format, but cannot find name in atom table");
497 break;
498
499 default:
500 sError = wxT("Unknown error");
501 }
502 return(sError);
503} // end of wxPMErrorToStr