]> git.saurik.com Git - wxWidgets.git/blob - src/os2/utils.cpp
Hack to support iso8859 and other wrongly formated
[wxWidgets.git] / src / os2 / utils.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/os2/utils.cpp
3 // Purpose: Various utilities
4 // Author: David Webster
5 // Modified by:
6 // Created: 09/17/99
7 // RCS-ID: $Id$
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #include "wx/utils.h"
16
17 #ifndef WX_PRECOMP
18 #include "wx/intl.h"
19 #include "wx/log.h"
20 #endif //WX_PRECOMP
21
22 #include "wx/os2/private.h"
23 #include "wx/apptrait.h"
24 #include "wx/filename.h"
25
26 #include <ctype.h>
27 #ifdef __EMX__
28 #include <dirent.h>
29 #endif
30
31
32 #include <io.h>
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <errno.h>
38 #include <stdarg.h>
39
40 #define PURE_32
41
42 #if defined(__WATCOMC__)
43 extern "C"
44 {
45 #include <upm.h>
46 }
47 #elif !defined(__EMX__)
48 #include <upm.h>
49 #include <netcons.h>
50 #include <netbios.h>
51 #endif
52
53 static const wxChar WX_SECTION[] = _T("wxWidgets");
54 static const wxChar eHOSTNAME[] = _T("HostName");
55
56 // For the following functions we SHOULD fill in support
57 // for Windows-NT (which I don't know) as I assume it begin
58 // a POSIX Unix (so claims MS) that it has some special
59 // functions beyond those provided by WinSock
60
61 // Get full hostname (eg. DoDo.BSn-Germany.crg.de)
62 bool wxGetHostName( wxChar* zBuf, int nMaxSize )
63 {
64 if (!zBuf) return false;
65
66 #if defined(wxUSE_NET_API) && wxUSE_NET_API
67 char zServer[256];
68 char zComputer[256];
69 unsigned long ulLevel = 0;
70 unsigned char* zBuffer = NULL;
71 unsigned long ulBuffer = 256;
72 unsigned long* pulTotalAvail = NULL;
73
74 NetBios32GetInfo( (const unsigned char*)zServer
75 ,(const unsigned char*)zComputer
76 ,ulLevel
77 ,zBuffer
78 ,ulBuffer
79 ,pulTotalAvail
80 );
81 strcpy(zBuf, zServer);
82 #else
83 wxChar* zSysname;
84 const wxChar* zDefaultHost = _T("noname");
85
86 if ((zSysname = wxGetenv(_T("SYSTEM_NAME"))) == NULL &&
87 (zSysname = wxGetenv(_T("HOSTNAME"))) == NULL)
88 {
89 ::PrfQueryProfileString( HINI_PROFILE
90 ,(PSZ)WX_SECTION
91 ,(PSZ)eHOSTNAME
92 ,(PSZ)zDefaultHost
93 ,(void*)zBuf
94 ,(ULONG)nMaxSize - 1
95 );
96 }
97 else
98 {
99 wxStrncpy(zBuf, zSysname, nMaxSize - 1);
100 }
101
102 zBuf[nMaxSize] = _T('\0');
103 #endif
104
105 return *zBuf ? true : false;
106 }
107
108 // Get user ID e.g. jacs
109 bool wxGetUserId(wxChar* zBuf, int nType)
110 {
111 #if defined(__VISAGECPP__) || defined(__WATCOMC__)
112 // UPM procs return 0 on success
113 long lrc = U32ELOCU((PUCHAR)zBuf, (PULONG)&nType);
114 if (lrc == 0) return true;
115 #endif
116 return false;
117 }
118
119 bool wxGetUserName( wxChar* zBuf, int nMaxSize )
120 {
121 #ifdef USE_NET_API
122 wxGetUserId( zBuf, nMaxSize );
123 #else
124 wxStrncpy(zBuf, _T("Unknown User"), nMaxSize);
125 #endif
126 return true;
127 }
128
129 int wxKill(long lPid,
130 wxSignal WXUNUSED(eSig),
131 wxKillError* WXUNUSED(peError),
132 int WXUNUSED(flags))
133 {
134 return((int)::DosKillProcess(0, (PID)lPid));
135 }
136
137 //
138 // Execute a program in an Interactive Shell
139 //
140 bool wxShell( const wxString& rCommand )
141 {
142 wxChar* zShell = _T("CMD.EXE");
143 wxString sInputs;
144 STARTDATA SData = {0};
145 PSZ PgmTitle = "Command Shell";
146 APIRET rc;
147 PID vPid = 0;
148 ULONG ulSessID = 0;
149 UCHAR achObjBuf[256] = {0}; //error data if DosStart fails
150 RESULTCODES vResult;
151
152 SData.Length = sizeof(STARTDATA);
153 SData.Related = SSF_RELATED_INDEPENDENT;
154 SData.FgBg = SSF_FGBG_FORE;
155 SData.TraceOpt = SSF_TRACEOPT_NONE;
156 SData.PgmTitle = PgmTitle;
157 SData.PgmName = (char*)zShell;
158
159 sInputs = _T("/C ") + rCommand;
160 SData.PgmInputs = (BYTE*)sInputs.c_str();
161 SData.TermQ = 0;
162 SData.Environment = 0;
163 SData.InheritOpt = SSF_INHERTOPT_SHELL;
164 SData.SessionType = SSF_TYPE_WINDOWABLEVIO;
165 SData.IconFile = 0;
166 SData.PgmHandle = 0;
167 SData.PgmControl = SSF_CONTROL_VISIBLE | SSF_CONTROL_MAXIMIZE;
168 SData.InitXPos = 30;
169 SData.InitYPos = 40;
170 SData.InitXSize = 200;
171 SData.InitYSize = 140;
172 SData.Reserved = 0;
173 SData.ObjectBuffer = (char*)achObjBuf;
174 SData.ObjectBuffLen = (ULONG)sizeof(achObjBuf);
175
176 rc = ::DosStartSession(&SData, &ulSessID, &vPid);
177 if (rc == 0 || rc == 457) // NO_ERROR or SMG_START_IN_BACKGROUND
178 {
179 PTIB ptib;
180 PPIB ppib;
181
182 ::DosGetInfoBlocks(&ptib, &ppib);
183
184 ::DosWaitChild( DCWA_PROCESS
185 ,DCWW_WAIT
186 ,&vResult
187 ,&ppib->pib_ulpid
188 ,vPid
189 );
190 }
191 return (rc != 0);
192 }
193
194 // Shutdown or reboot the PC
195 bool wxShutdown(wxShutdownFlags WXUNUSED(wFlags))
196 {
197 // TODO
198 return false;
199 }
200
201 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
202 wxMemorySize wxGetFreeMemory()
203 {
204 void* pMemptr = NULL;
205 LONG lSize;
206 ULONG lMemFlags;
207 APIRET rc;
208
209 lMemFlags = PAG_FREE;
210 rc = ::DosQueryMem(pMemptr, (PULONG)&lSize, &lMemFlags);
211 if (rc != 0)
212 lSize = -1L;
213 return (wxMemorySize)lSize;
214 }
215
216 // Get Process ID
217 unsigned long wxGetProcessId()
218 {
219 return (unsigned long)getpid();
220 }
221
222 // ----------------------------------------------------------------------------
223 // env vars
224 // ----------------------------------------------------------------------------
225
226 bool wxGetEnv(const wxString& var, wxString *value)
227 {
228 // wxGetenv is defined as getenv()
229 wxChar *p = wxGetenv(var);
230 if ( !p )
231 return false;
232
233 if ( value )
234 {
235 *value = p;
236 }
237
238 return true;
239 }
240
241 bool wxSetEnv(const wxString& variable, const wxChar *value)
242 {
243 #if defined(HAVE_SETENV)
244 return setenv(variable.mb_str(), value ? wxString(value).mb_str().data()
245 : NULL, 1 /* overwrite */) == 0;
246 #elif defined(HAVE_PUTENV)
247 wxString s = variable;
248 if ( value )
249 s << _T('=') << value;
250
251 // transform to ANSI
252 const char *p = s.mb_str();
253
254 // the string will be free()d by libc
255 char *buf = (char *)malloc(strlen(p) + 1);
256 strcpy(buf, p);
257
258 return putenv(buf) == 0;
259 #else // no way to set an env var
260 wxUnusedVar(variable);
261 wxUnusedVar(value);
262 return false;
263 #endif
264 }
265
266 void wxMilliSleep(
267 unsigned long ulMilliseconds
268 )
269 {
270 ::DosSleep(ulMilliseconds);
271 }
272
273 void wxMicroSleep(
274 unsigned long ulMicroseconds
275 )
276 {
277 ::DosSleep(ulMicroseconds/1000);
278 }
279
280 void wxSleep(
281 int nSecs
282 )
283 {
284 ::DosSleep(1000 * nSecs);
285 }
286
287 // Consume all events until no more left
288 void wxFlushEvents()
289 {
290 // wxYield();
291 }
292
293 // Emit a beeeeeep
294 void wxBell()
295 {
296 DosBeep(1000,1000); // 1kHz during 1 sec.
297 }
298
299 wxString wxGetOsDescription()
300 {
301 wxString strVer(_T("OS/2"));
302 ULONG ulSysInfo = 0;
303
304 if (::DosQuerySysInfo( QSV_VERSION_MINOR,
305 QSV_VERSION_MINOR,
306 (PVOID)&ulSysInfo,
307 sizeof(ULONG)
308 ) == 0L )
309 {
310 wxString ver;
311 ver.Printf( _T(" ver. %d.%d"),
312 int(ulSysInfo / 10),
313 int(ulSysInfo % 10)
314 );
315 strVer += ver;
316 }
317
318 return strVer;
319 }
320
321 bool wxIsPlatform64Bit()
322 {
323 // FIXME: No idea how to test for 64 bit processor
324 // (Probably irrelevant anyhow, though).
325 return false;
326 }
327
328 void wxAppTraits::InitializeGui(unsigned long &WXUNUSED(ulHab))
329 {
330 }
331
332 void wxAppTraits::TerminateGui(unsigned long WXUNUSED(ulHab))
333 {
334 }
335
336 wxOperatingSystemId wxGetOsVersion(int *verMaj, int *verMin)
337 {
338 ULONG ulSysInfo = 0;
339 APIRET ulrc;
340
341 ulrc = ::DosQuerySysInfo( QSV_VERSION_MINOR,
342 QSV_VERSION_MINOR,
343 (PVOID)&ulSysInfo,
344 sizeof(ULONG)
345 );
346
347 if (ulrc == 0L)
348 {
349 if ( verMaj )
350 *verMaj = ulSysInfo / 10;
351 if ( verMin )
352 *verMin = ulSysInfo % 10;
353 }
354
355 return wxOS_OS2;
356 }
357
358
359 // ---------------------------------------------------------------------------
360 const wxChar* wxGetHomeDir(
361 wxString* pStr
362 )
363 {
364 wxString& rStrDir = *pStr;
365
366 // OS/2 has no idea about home,
367 // so use the working directory instead.
368 // However, we might have a valid HOME directory,
369 // as is used on many machines that have unix utilities
370 // on them, so we should use that, if available.
371
372 // 256 was taken from os2def.h
373 #ifndef MAX_PATH
374 # define MAX_PATH 256
375 #endif
376
377 const wxChar *szHome = wxGetenv((wxChar*)"HOME");
378 if ( szHome == NULL ) {
379 // we're homeless, use current directory.
380 rStrDir = wxT(".");
381 }
382 else
383 rStrDir = szHome;
384
385 return rStrDir.c_str();
386 }
387
388 // Hack for OS/2
389 #if wxUSE_UNICODE
390 const wxMB2WXbuf wxGetUserHome( const wxString &rUser )
391 #else // just for binary compatibility -- there is no 'const' here
392 wxChar* wxGetUserHome ( const wxString &rUser )
393 #endif
394 {
395 wxChar* zHome;
396 wxString sUser1(rUser);
397
398 wxChar *wxBuffer = new wxChar[256];
399 #ifndef __EMX__
400 if (!sUser1.empty())
401 {
402 wxChar zTmp[64];
403
404 if (wxGetUserId( zTmp
405 ,sizeof(zTmp)/sizeof(char)
406 ))
407 {
408 // Guests belong in the temp dir
409 if (wxStricmp(zTmp, _T("annonymous")) == 0)
410 {
411 if ((zHome = wxGetenv(_T("TMP"))) != NULL ||
412 (zHome = wxGetenv(_T("TMPDIR"))) != NULL ||
413 (zHome = wxGetenv(_T("TEMP"))) != NULL)
414 delete[] wxBuffer;
415 return *zHome ? zHome : (wxChar*)_T("\\");
416 }
417 if (wxStricmp(zTmp, WXSTRINGCAST sUser1) == 0)
418 sUser1 = wxEmptyString;
419 }
420 }
421 #endif
422 if (sUser1.empty())
423 {
424 if ((zHome = wxGetenv(_T("HOME"))) != NULL)
425 {
426 wxStrcpy(wxBuffer, zHome);
427 wxUnix2DosFilename(wxBuffer);
428 #if wxUSE_UNICODE
429 wxWCharBuffer retBuffer (wxBuffer);
430 delete[] wxBuffer;
431 return retBuffer;
432 #else
433 wxStrcpy(zHome, wxBuffer);
434 delete[] wxBuffer;
435 return zHome;
436 #endif
437 }
438 }
439 delete[] wxBuffer;
440 return (wxChar*)wxEmptyString; // No home known!
441 }
442
443 bool wxGetDiskSpace(const wxString& path,
444 wxDiskspaceSize_t *pTotal,
445 wxDiskspaceSize_t *pFree)
446 {
447 if (path.empty())
448 return false;
449
450 wxFileName fn(path);
451 FSALLOCATE fsaBuf = {0};
452 APIRET rc = NO_ERROR;
453 ULONG disknum = 0;
454
455 fn.MakeAbsolute();
456
457 if (wxDirExists(fn.GetFullPath()) == false)
458 return false;
459
460 disknum = 1 + wxToupper(fn.GetVolume().GetChar(0)) - _T('A');
461
462 rc = ::DosQueryFSInfo(disknum, // 1 = A, 2 = B, 3 = C, ...
463 FSIL_ALLOC, // allocation info
464 (PVOID)&fsaBuf,
465 sizeof(FSALLOCATE));
466
467 if (rc != NO_ERROR)
468 return false;
469 else
470 {
471 if(pTotal)
472 {
473 // to try to avoid 32-bit overflow, let's not multiply right away
474 // (num of alloc units)
475 *pTotal = fsaBuf.cUnit;
476 // * (num of sectors per alloc unit) * (num of bytes per sector)
477 (*pTotal) *= fsaBuf.cSectorUnit * fsaBuf.cbSector;
478 }
479 if(pFree)
480 {
481 *pFree = fsaBuf.cUnitAvail;
482 (*pFree) *= fsaBuf.cSectorUnit * fsaBuf.cbSector;
483 }
484 return true;
485 }
486 }
487
488 wxString wxPMErrorToStr(ERRORID vError)
489 {
490 wxString sError;
491
492 //
493 // Remove the high order byte -- it is useless
494 //
495 vError &= 0x0000ffff;
496 switch(vError)
497 {
498 case PMERR_INVALID_HWND:
499 sError = wxT("Invalid window handle specified");
500 break;
501
502 case PMERR_INVALID_FLAG:
503 sError = wxT("Invalid flag bit set");
504 break;
505
506 case PMERR_NO_MSG_QUEUE:
507 sError = wxT("No message queue available");
508 break;
509
510 case PMERR_INVALID_PARM:
511 sError = wxT("Parameter contained invalid data");
512 break;
513
514 case PMERR_INVALID_PARAMETERS:
515 sError = wxT("Parameter value is out of range");
516 break;
517
518 case PMERR_PARAMETER_OUT_OF_RANGE:
519 sError = wxT("Parameter value is out of range");
520 break;
521
522 case PMERR_INVALID_INTEGER_ATOM:
523 sError = wxT("Not a valid atom");
524 break;
525
526 case PMERR_INVALID_HATOMTBL:
527 sError = wxT("Atom table handle is invalid");
528 break;
529
530 case PMERR_INVALID_ATOM_NAME:
531 sError = wxT("Not a valid atom name");
532 break;
533
534 case PMERR_ATOM_NAME_NOT_FOUND:
535 sError = wxT("Valid name format, but cannot find name in atom table");
536 break;
537
538 case PMERR_INV_HPS:
539 sError = wxT("PMERR_INV_HPS");
540 break;
541
542 case PMERR_PS_BUSY:
543 sError = wxT("PMERR_PS_BUSY");
544 break;
545
546 case PMERR_INV_PRIMITIVE_TYPE:
547 sError = wxT("PMERR_INV_PRIMITIVE_TYPE");
548 break;
549
550 case PMERR_UNSUPPORTED_ATTR:
551 sError = wxT("PMERR_UNSUPPORTED_ATTR");
552 break;
553
554 case PMERR_INV_COLOR_ATTR:
555 sError = wxT("PMERR_INV_COLOR_ATTR");
556 break;
557
558 case PMERR_INV_BACKGROUND_COL_ATTR:
559 sError = wxT("PMERR_INV_BACKGROUND_COL_ATTR");
560 break;
561
562 case PMERR_INV_MIX_ATTR:
563 sError = wxT("PMERR_INV_MIX_ATTR");
564 break;
565
566 case PMERR_INV_LINE_WIDTH_ATTR:
567 sError = wxT("PMERR_INV_LINE_WIDTH_ATTR");
568 break;
569
570 case PMERR_INV_GEOM_LINE_WIDTH_ATTR:
571 sError = wxT("PMERR_INV_GEOM_LINE_WIDTH_ATTR");
572 break;
573
574 case PMERR_INV_LINE_TYPE_ATTR:
575 sError = wxT("PMERR_INV_LINE_TYPE_ATTR");
576 break;
577
578 case PMERR_INV_LINE_END_ATTR:
579 sError = wxT("PMERR_INV_LINE_END_ATTR");
580 break;
581
582 case PMERR_INV_LINE_JOIN_ATTR:
583 sError = wxT("PMERR_INV_LINE_JOIN_ATTR");
584 break;
585
586 case PMERR_INV_CHAR_SET_ATTR:
587 sError = wxT("PMERR_INV_CHAR_SET_ATTR");
588 break;
589
590 case PMERR_INV_CHAR_MODE_ATTR:
591 sError = wxT("PMERR_INV_CHAR_MODE_ATTR");
592 break;
593
594 case PMERR_INV_CHAR_DIRECTION_ATTR:
595 sError = wxT("PMERR_INV_CHAR_DIRECTION_ATTR");
596 break;
597
598 case PMERR_INV_CHAR_SHEAR_ATTR:
599 sError = wxT("PMERR_INV_CHAR_SHEAR_ATTR");
600 break;
601
602 case PMERR_INV_CHAR_ANGLE_ATTR:
603 sError = wxT("PMERR_INV_CHAR_ANGLE_ATTR");
604 break;
605
606 case PMERR_INV_MARKER_SET_ATTR:
607 sError = wxT("PMERR_INV_MARKER_SET_ATTR");
608 break;
609
610 case PMERR_INV_MARKER_SYMBOL_ATTR:
611 sError = wxT("PMERR_INV_MARKER_SYMBOL_ATTR");
612 break;
613
614 case PMERR_INV_PATTERN_SET_ATTR:
615 sError = wxT("PMERR_INV_PATTERN_SET_ATTR");
616 break;
617
618 case PMERR_INV_PATTERN_ATTR:
619 sError = wxT("PMERR_INV_PATTERN_ATTR");
620 break;
621
622 case PMERR_INV_COORDINATE:
623 sError = wxT("PMERR_INV_COORDINATE");
624 break;
625
626 case PMERR_UNSUPPORTED_ATTR_VALUE:
627 sError = wxT("PMERR_UNSUPPORTED_ATTR_VALUE");
628 break;
629
630 case PMERR_INV_PATTERN_SET_FONT:
631 sError = wxT("PMERR_INV_PATTERN_SET_FONT");
632 break;
633
634 case PMERR_HUGE_FONTS_NOT_SUPPORTED:
635 sError = wxT("PMERR_HUGE_FONTS_NOT_SUPPORTED");
636 break;
637
638 default:
639 sError = wxT("Unknown error");
640 }
641 return sError;
642 } // end of wxPMErrorToStr
643
644 // replacement for implementation in unix/utilsunx.cpp,
645 // to be used by all X11 based ports.
646 struct wxEndProcessData;
647
648 void wxHandleProcessTermination(wxEndProcessData *WXUNUSED(proc_data))
649 {
650 // For now, just do nothing. To be filled in as needed.
651 }