2005-01-31 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mcs / class / corlib / System / Console.cs
1 //
2 // System.Console.cs
3 //
4 // Author:
5 //      Dietmar Maurer (dietmar@ximian.com)
6 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 //
8 // (C) Ximian, Inc.  http://www.ximian.com
9 // (C) 2004,2005 Novell, Inc. (http://www.novell.com)
10 //
11
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 using System.IO;
33 using System.Text;
34
35 namespace System
36 {
37         public
38 #if NET_2_0
39         static
40 #else
41         sealed
42 #endif
43         class Console
44         {
45                 private static TextWriter stdout;
46                 private static TextWriter stderr;
47                 private static TextReader stdin;
48
49                 static Console ()
50                 {
51                         int code_page = 0;
52                         Encoding.InternalCodePage (ref code_page);
53                         Encoding encoding;
54
55                         if (((int) Environment.Platform) == 128){
56                                 //
57                                 // On Unix systems (128), do not output the
58                                 // UTF-8 ZWNBSP (zero-width non-breaking space).
59                                 //
60                                 if (code_page == UTF8Encoding.UTF8_CODE_PAGE || ((code_page & 0x10000000) != 0))
61                                         encoding = Encoding.UTF8Unmarked;
62                                 else
63                                         encoding = Encoding.Default;
64                         } else {
65                                 //
66                                 // On Windows, follow the Windows tradition
67                                 //
68                                 encoding = Encoding.Default;
69                         }
70
71                         stderr = new UnexceptionalStreamWriter (OpenStandardError (0), encoding); 
72                         ((StreamWriter)stderr).AutoFlush = true;
73                         stderr = TextWriter.Synchronized (stderr, true);
74
75                         stdout = new UnexceptionalStreamWriter (OpenStandardOutput (0), encoding);
76                         ((StreamWriter)stdout).AutoFlush = true;
77                         stdout = TextWriter.Synchronized (stdout, true);
78
79                         stdin  = new UnexceptionalStreamReader (OpenStandardInput (0), encoding);
80                         stdin = TextReader.Synchronized (stdin);
81                 }
82
83 #if !NET_2_0
84                 private Console ()
85                 {
86                 }
87 #endif
88
89                 public static TextWriter Error {
90                         get {
91                                 return stderr;
92                         }
93                 }
94
95                 public static TextWriter Out {
96                         get {
97                                 return stdout;
98                         }
99                 }
100
101                 public static TextReader In {
102                         get {
103                                 return stdin;
104                         }
105                 }
106
107                 public static Stream OpenStandardError ()
108                 {
109                         return OpenStandardError (0);
110                 }
111
112                 public static Stream OpenStandardError (int bufferSize)
113                 {
114                         try {
115                                 return new FileStream (MonoIO.ConsoleError, FileAccess.Write, false, bufferSize, false, bufferSize == 0);
116                         } catch (IOException) {
117                                 return new NullStream ();
118                         }
119                 }
120
121                 public static Stream OpenStandardInput ()
122                 {
123                         return OpenStandardInput (0);
124                 }
125
126                 public static Stream OpenStandardInput (int bufferSize)
127                 {
128                         try {
129                                 return new FileStream (MonoIO.ConsoleInput, FileAccess.Read, false, bufferSize, false, bufferSize == 0);
130                         } catch (IOException) {
131                                 return new NullStream ();
132                         }
133                 }
134
135                 public static Stream OpenStandardOutput ()
136                 {
137                         return OpenStandardOutput (0);
138                 }
139
140                 public static Stream OpenStandardOutput (int bufferSize)
141                 {
142                         try {
143                                 return new FileStream (MonoIO.ConsoleOutput, FileAccess.Write, false, bufferSize, false, bufferSize == 0);
144                         } catch (IOException) {
145                                 return new NullStream ();
146                         }
147                 }
148
149                 public static void SetError (TextWriter newError)
150                 {
151                         if (newError == null)
152                                 throw new ArgumentNullException ("newError");
153
154                         stderr = newError;
155                 }
156
157                 public static void SetIn (TextReader newIn)
158                 {
159                         if (newIn == null)
160                                 throw new ArgumentNullException ("newIn");
161
162                         stdin = newIn;
163                 }
164
165                 public static void SetOut (TextWriter newOut)
166                 {
167                         if (newOut == null)
168                                 throw new ArgumentNullException ("newOut");
169
170                         stdout = newOut;
171                 }
172
173                 public static void Write (bool value)
174                 {
175                         stdout.Write (value);
176                 }
177
178                 public static void Write (char value)
179                 {
180                         stdout.Write (value);
181                 }
182
183                 public static void Write (char[] value)
184                 {
185                         stdout.Write (value);
186                 }
187
188                 public static void Write (decimal value)
189                 {
190                         stdout.Write (value);
191                 }
192                 
193                 public static void Write (double value)
194                 {
195                         stdout.Write (value);
196                 }
197
198                 public static void Write (int value)
199                 {
200                         stdout.Write (value);
201                 }
202
203                 public static void Write (long value)
204                 {
205                         stdout.Write (value);
206                 }
207
208                 public static void Write (object value)
209                 {
210                         stdout.Write (value);
211                 }
212
213                 public static void Write (float value)
214                 {
215                         stdout.Write (value);
216                 }
217
218                 public static void Write (string value)
219                 {
220                         stdout.Write (value);
221                 }
222
223                 [CLSCompliant (false)]
224                 public static void Write (uint value)
225                 {
226                         stdout.Write (value);
227                 }
228
229                 [CLSCompliant (false)]
230                 public static void Write (ulong value)
231                 {
232                         stdout.Write (value);
233                 }
234
235                 public static void Write (string format, object arg0)
236                 {
237                         stdout.Write (format, arg0);
238                 }
239
240                 public static void Write (string format, params object[] arg)
241                 {
242                         stdout.Write (format, arg);
243                 }
244
245                 public static void Write (char[] buffer, int index, int count)
246                 {
247                         stdout.Write (buffer, index, count);
248                 }
249
250                 public static void Write (string format, object arg0, object arg1)
251                 {
252                         stdout.Write (format, arg0, arg1);
253                 }
254
255                 public static void Write (string format, object arg0, object arg1, object arg2 )
256                 {
257                         stdout.Write (format, arg0, arg1, arg2);
258                 }
259
260 #if ! BOOTSTRAP_WITH_OLDLIB
261                 [CLSCompliant (false)]
262                 public static void Write (string format, object arg0, object arg1, object arg2, object arg3, __arglist)
263                 {
264                         ArgIterator iter = new ArgIterator (__arglist);
265                         int argCount = iter.GetRemainingCount();
266
267                         object[] args = new object [argCount + 4];
268                         args [0] = arg0;
269                         args [1] = arg1;
270                         args [2] = arg2;
271                         args [3] = arg3;
272                         for (int i = 0; i < argCount; i++) {
273                                 TypedReference typedRef = iter.GetNextArg ();
274                                 args [i + 4] = TypedReference.ToObject (typedRef);
275                         }
276
277                         stdout.Write (String.Format (format, args));
278                 }
279 #endif
280
281                 public static void WriteLine ()
282                 {
283                         stdout.WriteLine ();
284                 }
285
286                 public static void WriteLine (bool value)
287                 {
288                         stdout.WriteLine (value);
289                 }
290
291                 public static void WriteLine (char value)
292                 {
293                         stdout.WriteLine (value);
294                 }
295
296                 public static void WriteLine (char[] value)
297                 {
298                         stdout.WriteLine (value);
299                 }
300
301                 public static void WriteLine (decimal value)
302                 {
303                         stdout.WriteLine (value);
304                 }
305
306                 public static void WriteLine (double value)
307                 {
308                         stdout.WriteLine (value);
309                 }
310
311                 public static void WriteLine (int value)
312                 {
313                         stdout.WriteLine (value);
314                 }
315
316                 public static void WriteLine (long value)
317                 {
318                         stdout.WriteLine (value);
319                 }
320
321                 public static void WriteLine (object value)
322                 {
323                         stdout.WriteLine (value);
324                 }
325
326                 public static void WriteLine (float value)
327                 {
328                         stdout.WriteLine (value);
329                 }
330
331                 public static void WriteLine (string value)
332                 {
333                         stdout.WriteLine (value);
334                 }
335
336                 [CLSCompliant (false)]
337                 public static void WriteLine (uint value)
338                 {
339                         stdout.WriteLine (value);
340                 }
341
342                 [CLSCompliant (false)]
343                 public static void WriteLine (ulong value)
344                 {
345                         stdout.WriteLine (value);
346                 }
347
348                 public static void WriteLine (string format, object arg0)
349                 {
350                         stdout.WriteLine (format, arg0);
351                 }
352
353                 public static void WriteLine (string format, params object[] arg)
354                 {
355                         stdout.WriteLine (format, arg);
356                 }
357
358                 public static void WriteLine (char[] buffer, int index, int count)
359                 {
360                         stdout.WriteLine (buffer, index, count);
361                 }
362
363                 public static void WriteLine (string format, object arg0, object arg1)
364                 {
365                         stdout.WriteLine (format, arg0, arg1);
366                 }
367
368                 public static void WriteLine (string format, object arg0, object arg1, object arg2)
369                 {
370                         stdout.WriteLine (format, arg0, arg1, arg2);
371                 }
372
373 #if ! BOOTSTRAP_WITH_OLDLIB
374                 [CLSCompliant (false)]
375                 public static void WriteLine (string format, object arg0, object arg1, object arg2, object arg3, __arglist)
376                 {
377                         ArgIterator iter = new ArgIterator (__arglist);
378                         int argCount = iter.GetRemainingCount();
379
380                         object[] args = new object [argCount + 4];
381                         args [0] = arg0;
382                         args [1] = arg1;
383                         args [2] = arg2;
384                         args [3] = arg3;
385                         for (int i = 0; i < argCount; i++) {
386                                 TypedReference typedRef = iter.GetNextArg ();
387                                 args [i + 4] = TypedReference.ToObject (typedRef);
388                         }
389
390                         stdout.WriteLine (String.Format (format, args));
391                 }
392 #endif
393
394                 public static int Read ()
395                 {
396                         return stdin.Read ();
397                 }
398
399                 public static string ReadLine ()
400                 {
401 #if NET_2_0
402                         bool prevEcho;
403                         if (ConsoleDriver.Initialized) {
404                                 prevEcho = ConsoleDriver.Echo;
405                                 ConsoleDriver.Echo = true;
406                         }
407 #endif
408                         return stdin.ReadLine ();
409 #if NET_2_0
410                         if (ConsoleDriver.Initialized)
411                                 ConsoleDriver.Echo = prevEcho;
412 #endif
413                 }
414
415 #if NET_2_0
416                 // On windows, for en-US the Default is Windows-1252, while input/output is IBM437
417                 // We might want to initialize these fields in the ConsoleDriver instead.
418                 static Encoding inputEncoding = Encoding.Default;
419                 static Encoding outputEncoding = Encoding.Default;
420
421                 public static Encoding InputEncoding {
422                         get { return inputEncoding; }
423                         set { inputEncoding = value; }
424                 }
425
426                 public static Encoding OutputEncoding {
427                         get { return outputEncoding; }
428                         set { outputEncoding = value; }
429                 }
430
431                 public static ConsoleColor BackgroundColor {
432                         get { return ConsoleDriver.BackgroundColor; }
433                         set { ConsoleDriver.BackgroundColor = value; }
434                 }
435
436                 public static int BufferHeight {
437                         get { return ConsoleDriver.BufferHeight; }
438                         set { ConsoleDriver.BufferHeight = value; }
439                 }
440
441                 public static int BufferWidth {
442                         get { return ConsoleDriver.BufferWidth; }
443                         set { ConsoleDriver.BufferWidth = value; }
444                 }
445
446                 public static int CursorLeft {
447                         get { return ConsoleDriver.CursorLeft; }
448                         set { ConsoleDriver.CursorLeft = value; }
449                 }
450
451                 public static int CursorTop {
452                         get { return ConsoleDriver.CursorTop; }
453                         set { ConsoleDriver.CursorTop = value; }
454                 }
455
456                 public static bool CursorVisible {
457                         get { return ConsoleDriver.CursorVisible; }
458                         set { ConsoleDriver.CursorVisible = value; }
459                 }
460
461                 public static ConsoleColor ForegroundColor {
462                         get { return ConsoleDriver.ForegroundColor; }
463                         set { ConsoleDriver.ForegroundColor = value; }
464                 }
465
466                 public static bool KeyAvailable {
467                         get { return ConsoleDriver.KeyAvailable; }
468                 }
469
470                 public static string Title {
471                         get { return ConsoleDriver.Title; }
472                         set { ConsoleDriver.Title = value; }
473                 }
474
475                 public static bool TreatControlCAsInput {
476                         get { return ConsoleDriver.TreatControlCAsInput; }
477                         set { ConsoleDriver.TreatControlCAsInput = value; }
478                 }
479
480                 public static int WindowHeight {
481                         get { return ConsoleDriver.WindowHeight; }
482                         set { ConsoleDriver.WindowHeight = value; }
483                 }
484
485                 public static int WindowLeft {
486                         get { return ConsoleDriver.WindowLeft; }
487                         set { ConsoleDriver.WindowLeft = value; }
488                 }
489
490                 public static int WindowTop {
491                         get { return ConsoleDriver.WindowTop; }
492                         set { ConsoleDriver.WindowTop = value; }
493                 }
494
495                 public static int WindowWidth {
496                         get { return ConsoleDriver.WindowWidth; }
497                         set { ConsoleDriver.WindowWidth = value; }
498                 }
499
500                 public static void Beep ()
501                 {
502                         Beep (1000, 500);
503                 }
504
505                 public static void Beep (int frequency, int duration)
506                 {
507                         if (frequency < 37 || frequency > 32767)
508                                 throw new ArgumentOutOfRangeException ("frequency");
509
510                         if (duration <= 0)
511                                 throw new ArgumentOutOfRangeException ("duration");
512
513                         ConsoleDriver.Beep (frequency, duration);
514                 }
515
516                 public static void Clear ()
517                 {
518                         ConsoleDriver.Clear ();
519                 }
520
521                 [MonoTODO]
522                 public static void MoveBufferArea (int sourceLeft, int sourceTop, int sourceWidth, int sourceHeight,
523                                                 int targetLeft, int targetTop)
524                 {
525                         throw new NotImplementedException ();
526                 }
527
528                 [MonoTODO]
529                 public static void MoveBufferArea (int sourceLeft, int sourceTop, int sourceWidth, int sourceHeight,
530                                                 int targetLeft, int targetTop, Char sourceChar,
531                                                 ConsoleColor sourceForeColor, ConsoleColor sourceBackColor)
532                 {
533                         throw new NotImplementedException ();
534                 }
535
536                 public static ConsoleKeyInfo ReadKey ()
537                 {
538                         return ReadKey (false);
539                 }
540
541                 public static ConsoleKeyInfo ReadKey (bool intercept)
542                 {
543                         return ConsoleDriver.ReadKey (intercept);
544                 }
545
546                 public static void ResetColor ()
547                 {
548                         ConsoleDriver.ResetColor ();
549                 }
550
551                 public static void SetBufferSize (int width, int height)
552                 {
553                         ConsoleDriver.SetBufferSize (width, height);
554                 }
555
556                 public static void SetCursorPosition (int left, int top)
557                 {
558                         ConsoleDriver.SetCursorPosition (left, top);
559                 }
560
561                 public static void SetWindowPosition (int left, int top)
562                 {
563                         ConsoleDriver.SetWindowPosition (left, top);
564                 }
565
566                 public static void SetWindowSize (int width, int height)
567                 {
568                         ConsoleDriver.SetWindowSize (width, height);
569                 }
570
571                 //FIXME
572                 //public event ConsoleCancelEventHandler CancelKeyPress;
573 #endif
574         }
575 }
576