132
|
1 /*******************************************************************************
|
|
2
|
|
3 copyright: Copyright (c) 2004 Kris Bell. All rights reserved
|
|
4
|
|
5 license: BSD style: $(LICENSE)
|
|
6
|
|
7 version: Initial release: November 2005
|
|
8
|
|
9 author: Kris
|
|
10
|
|
11 *******************************************************************************/
|
|
12
|
|
13 module tango.sys.Common;
|
|
14
|
|
15 version (Win32)
|
|
16 {
|
|
17 public import tango.sys.win32.UserGdi;
|
|
18 }
|
|
19
|
|
20 version (linux)
|
|
21 {
|
|
22 public import tango.sys.linux.linux;
|
|
23 alias tango.sys.linux.linux posix;
|
|
24 }
|
|
25
|
|
26 version (darwin)
|
|
27 {
|
|
28 public import tango.sys.darwin.darwin;
|
|
29 alias tango.sys.darwin.darwin posix;
|
|
30 }
|
|
31
|
|
32
|
|
33 /*******************************************************************************
|
|
34
|
|
35 Stuff for sysErrorMsg(), kindly provided by Regan Heath.
|
|
36
|
|
37 *******************************************************************************/
|
|
38
|
|
39 version (Win32)
|
|
40 {
|
|
41 private const FORMAT_MESSAGE_ALLOCATE_BUFFER = 0x00000100;
|
|
42 private const FORMAT_MESSAGE_IGNORE_INSERTS = 0x00000200;
|
|
43 private const FORMAT_MESSAGE_FROM_STRING = 0x00000400;
|
|
44 private const FORMAT_MESSAGE_FROM_HMODULE = 0x00000800;
|
|
45 private const FORMAT_MESSAGE_FROM_SYSTEM = 0x00001000;
|
|
46 private const FORMAT_MESSAGE_ARGUMENT_ARRAY = 0x00002000;
|
|
47 private const FORMAT_MESSAGE_MAX_WIDTH_MASK = 0x000000FF;
|
|
48
|
|
49 private DWORD MAKELANGID(WORD p, WORD s) { return (((cast(WORD)s) << 10) | cast(WORD)p); }
|
|
50
|
|
51 private alias HGLOBAL HLOCAL;
|
|
52
|
|
53 private const LANG_NEUTRAL = 0x00;
|
|
54 private const SUBLANG_DEFAULT = 0x01;
|
|
55
|
|
56 private extern (Windows)
|
|
57 {
|
|
58 DWORD FormatMessageA (DWORD dwFlags,
|
|
59 LPCVOID lpSource,
|
|
60 DWORD dwMessageId,
|
|
61 DWORD dwLanguageId,
|
|
62 LPTSTR lpBuffer,
|
|
63 DWORD nSize,
|
|
64 LPCVOID args
|
|
65 );
|
|
66
|
|
67 HLOCAL LocalFree(HLOCAL hMem);
|
|
68 }
|
|
69 }
|
|
70 else
|
|
71 version (Posix)
|
|
72 {
|
|
73 private import tango.stdc.errno;
|
|
74 private import tango.stdc.string;
|
|
75 }
|
|
76 else
|
|
77 {
|
|
78 pragma (msg, "Unsupported environment; neither Win32 or Posix is declared");
|
|
79 static assert(0);
|
|
80 }
|
|
81
|
|
82
|
|
83 /*******************************************************************************
|
|
84
|
|
85 *******************************************************************************/
|
|
86
|
|
87 struct SysError
|
|
88 {
|
|
89 /***********************************************************************
|
|
90
|
|
91 ***********************************************************************/
|
|
92
|
|
93 static uint lastCode ()
|
|
94 {
|
|
95 version (Win32)
|
|
96 return GetLastError;
|
|
97 else
|
|
98 return errno;
|
|
99 }
|
|
100
|
|
101 /***********************************************************************
|
|
102
|
|
103 ***********************************************************************/
|
|
104
|
|
105 static char[] lastMsg ()
|
|
106 {
|
|
107 return lookup (lastCode);
|
|
108 }
|
|
109
|
|
110 /***********************************************************************
|
|
111
|
|
112 ***********************************************************************/
|
|
113
|
|
114 static char[] lookup (uint errcode)
|
|
115 {
|
|
116 char[] text;
|
|
117
|
|
118 version (Win32)
|
|
119 {
|
|
120 DWORD r;
|
|
121 LPVOID lpMsgBuf;
|
|
122
|
|
123 r = FormatMessageA (
|
|
124 FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
|
125 FORMAT_MESSAGE_FROM_SYSTEM |
|
|
126 FORMAT_MESSAGE_IGNORE_INSERTS,
|
|
127 null,
|
|
128 errcode,
|
|
129 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
|
|
130 cast(LPTSTR)&lpMsgBuf,
|
|
131 0,
|
|
132 null);
|
|
133
|
|
134 /* Remove \r\n from error string */
|
|
135 if (r >= 2) r-= 2;
|
|
136 text = (cast(char *)lpMsgBuf)[0..r].dup;
|
|
137 LocalFree(cast(HLOCAL)lpMsgBuf);
|
|
138 }
|
|
139 else
|
|
140 {
|
|
141 uint r;
|
|
142 char* pemsg;
|
|
143
|
|
144 pemsg = strerror(errcode);
|
|
145 r = strlen(pemsg);
|
|
146
|
|
147 /* Remove \r\n from error string */
|
|
148 if (pemsg[r-1] == '\n') r--;
|
|
149 if (pemsg[r-1] == '\r') r--;
|
|
150 text = pemsg[0..r].dup;
|
|
151 }
|
|
152
|
|
153 return text;
|
|
154 }
|
|
155 }
|