00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 module mango.io.Console;
00040
00041 private import mango.sys.OS;
00042
00043 private import mango.convert.Type;
00044
00045 private import mango.io.Buffer,
00046 mango.io.ConduitStyle,
00047 mango.io.DeviceConduit;
00048
00049
00050
00051
00052
00053
00054
00055
00056 version (Win32)
00057 {
00058 private extern (Windows)
00059 {
00060 HANDLE GetStdHandle (DWORD);
00061 DWORD GetConsoleMode (HANDLE, LPDWORD);
00062 BOOL WriteConsoleW (HANDLE, VOID*, DWORD, LPDWORD, LPVOID);
00063 int MultiByteToWideChar (UINT, DWORD, LPCSTR, int, LPWSTR, int);
00064 }
00065 }
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078 struct Console
00079 {
00080
00081
00082
00083
00084 class Input : Buffer
00085 {
00086 ConsoleConduit conduit;
00087
00088 this (FileDevice device)
00089 {
00090 super (1024);
00091 setConduit (conduit = new ConsoleConduit (device));
00092
00093 }
00094
00095 int opCall(inout char[] x)
00096 {
00097 get (0);
00098 x = cast(char[]) get (readable());
00099 return x.length;
00100 }
00101 }
00102
00103
00104
00105
00106
00107 class Output : Buffer
00108 {
00109 ConsoleConduit conduit;
00110
00111 this (FileDevice device)
00112 {
00113 super (1024 * 8);
00114 setConduit (conduit = new ConsoleConduit (device));
00115 }
00116
00117 int opCall (char[] x)
00118 {
00119 int ret = sink (x, Type.Utf8);
00120 flush();
00121 return ret;
00122 }
00123
00124 protected int sink (void[] x, int type)
00125 {
00126 return conduit.write (x);
00127 }
00128 }
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139 class ConsoleConduit : DeviceConduit
00140 {
00141
00142 alias DeviceConduit.copy copy;
00143 alias DeviceConduit.read read;
00144 alias DeviceConduit.write write;
00145
00146
00147 private bool transcode = false;
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158 package this (FileDevice device)
00159 {
00160 super (device);
00161
00162 version (Win32)
00163 {
00164 buffer = new wchar[128];
00165 setTranscode (true);
00166 }
00167 }
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180 public void setTranscode (bool enabled)
00181 {
00182 this.transcode = enabled;
00183 }
00184
00185
00186
00187
00188
00189
00190
00191 protected char[] getName()
00192 {
00193 return "console device";
00194 }
00195
00196
00197
00198
00199
00200
00201
00202 version(Win32)
00203 {
00204 private wchar[] buffer;
00205
00206 enum {FILE_TYPE_CHAR=2, CP_UTF8=65001};
00207
00208
00209
00210
00211
00212
00213
00214 protected override void reopen (FileDevice device)
00215 {
00216 static const DWORD[] id = [
00217 cast(DWORD) -10,
00218 cast(DWORD) -11,
00219 cast(DWORD) -12
00220 ];
00221 static const char[][] f = [
00222 "CONIN$\0",
00223 "CONOUT$\0",
00224 "CONOUT$\0"
00225 ];
00226
00227 assert (device.id < 3);
00228 handle = GetStdHandle (id[device.id]);
00229 if (! handle)
00230 handle = CreateFileA (f[device.id],
00231 GENERIC_READ | GENERIC_WRITE,
00232 FILE_SHARE_READ | FILE_SHARE_WRITE,
00233 null, OPEN_EXISTING, 0, null);
00234 if (! handle)
00235 error ();
00236
00237
00238 DWORD mode;
00239 if (! GetConsoleMode (handle, &mode))
00240 setTranscode (false);
00241 }
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251 override int write (void[] src)
00252 {
00253 void* p = src.ptr;
00254 DWORD len = src.length;
00255 const int Max = 1024 * 8;
00256
00257
00258 if (len is 0)
00259 return 0;
00260
00261
00262
00263
00264 do {
00265 DWORD i = len;
00266 if (i > Max)
00267 i = Max;
00268
00269 version (Win32SansUnicode)
00270 {
00271
00272 if (! WriteFile (handle, p, i, &i, null))
00273 error ();
00274 }
00275 else
00276 {
00277 if (transcode)
00278 {
00279
00280 if (buffer.length < i)
00281 buffer.length = i;
00282
00283
00284 uint j = MultiByteToWideChar (CP_UTF8, 0, cast(char*)p, i,
00285 buffer.ptr, buffer.length);
00286 if (j is 0)
00287 error();
00288
00289
00290 uint k;
00291 do {
00292 if (! WriteConsoleW (handle, &buffer[k], j, &k, null))
00293 error();
00294 } while (j -= k);
00295
00296 }
00297 else
00298
00299 if (! WriteFile (handle, p, i, &i, null))
00300 error ();
00301 }
00302
00303 len -= i;
00304 p += i;
00305 } while (len);
00306
00307 return src.length;
00308 }
00309 }
00310 }
00311 }
00312
00313
00314
00315
00316
00317 static Console.Input Cin;
00318 static Console.Output Cout,
00319 Cerr;
00320
00321 static this ()
00322 {
00323 Cin = new Console.Input (new FileDevice (0, ConduitStyle.Access.Read));
00324 Cout = new Console.Output (new FileDevice (1, ConduitStyle.Access.Write));
00325 Cerr = new Console.Output (new FileDevice (2, ConduitStyle.Access.Write));
00326 }