Merge pull request #2698 from esdrubal/iosxmlarray
[mono.git] / mcs / class / corlib / System / Console.cs
index 8b68f4f505f373c581a8e8ddf4c69336f78a4b4c..f75547ab13d55a425298a88f431b885aa0462a08 100644 (file)
@@ -7,7 +7,7 @@
 //
 // (C) Ximian, Inc.  http://www.ximian.com
 // (C) 2004,2005 Novell, Inc. (http://www.novell.com)
-//
+// Copyright 2013 Xamarin Inc. (http://www.xamarin.com)
 
 // Permission is hereby granted, free of charge, to any person obtaining
 // a copy of this software and associated documentation files (the
@@ -39,7 +39,7 @@ using System.Text;
 
 namespace System
 {
-       public static class Console
+       public static partial class Console
        {
 #if !NET_2_1
                private class WindowsConsole
@@ -89,17 +89,13 @@ namespace System
                        }
                }
 #endif
+
                internal static TextWriter stdout;
                private static TextWriter stderr;
                private static TextReader stdin;
 
                static Console ()
                {
-#if NET_2_1
-                       Encoding inputEncoding;
-                       Encoding outputEncoding;
-#endif
-
                        if (Environment.IsRunningOnWindows) {
                                //
                                // On Windows, follow the Windows tradition
@@ -124,35 +120,45 @@ namespace System
                                // UTF-8 ZWNBSP (zero-width non-breaking space).
                                //
                                int code_page = 0;
-                               Encoding.InternalCodePage (ref code_page);
+                               EncodingHelper.InternalCodePage (ref code_page);
 
                                if (code_page != -1 && ((code_page & 0x0fffffff) == 3 // UTF8Encoding.UTF8_CODE_PAGE
                                        || ((code_page & 0x10000000) != 0)))
-                                       inputEncoding = outputEncoding = Encoding.UTF8Unmarked;
+                                       inputEncoding = outputEncoding = EncodingHelper.UTF8Unmarked;
                                else
                                        inputEncoding = outputEncoding = Encoding.Default;
                        }
 
-                       stderr = new UnexceptionalStreamWriter (OpenStandardError (0), outputEncoding); 
-                       ((StreamWriter)stderr).AutoFlush = true;
-                       stderr = TextWriter.Synchronized (stderr, true);
+                       SetupStreams (inputEncoding, outputEncoding);
+               }
 
+               static void SetupStreams (Encoding inputEncoding, Encoding outputEncoding)
+               {
 #if !NET_2_1
                        if (!Environment.IsRunningOnWindows && ConsoleDriver.IsConsole) {
-                               StreamWriter w = new CStreamWriter (OpenStandardOutput (0), outputEncoding);
-                               w.AutoFlush = true;
-                               stdout = TextWriter.Synchronized (w, true);
                                stdin = new CStreamReader (OpenStandardInput (0), inputEncoding);
-                       } else {
+                               stdout = TextWriter.Synchronized (new CStreamWriter (OpenStandardOutput (0), outputEncoding, true) { AutoFlush = true });
+                               stderr = TextWriter.Synchronized (new CStreamWriter (OpenStandardError (0), outputEncoding, true) { AutoFlush = true });
+                       } else
 #endif
-                               stdout = new UnexceptionalStreamWriter (OpenStandardOutput (0), outputEncoding);
-                               ((StreamWriter)stdout).AutoFlush = true;
-                               stdout = TextWriter.Synchronized (stdout, true);
-                               stdin = new UnexceptionalStreamReader (OpenStandardInput (0), inputEncoding);
-                               stdin = TextReader.Synchronized (stdin);
-#if !NET_2_1
+                       {
+                               stdin = TextReader.Synchronized (new UnexceptionalStreamReader (OpenStandardInput (0), inputEncoding));
+
+#if MONOTOUCH
+                               stdout = new NSLogWriter ();
+                               stderr = new NSLogWriter ();
+#else
+                               stdout = TextWriter.Synchronized (new UnexceptionalStreamWriter (OpenStandardOutput (0), outputEncoding) { AutoFlush = true });
+                               stderr = TextWriter.Synchronized (new UnexceptionalStreamWriter (OpenStandardError (0), outputEncoding) { AutoFlush = true });
+
+#if MONODROID
+                               if (LogcatTextWriter.IsRunningOnAndroid ()) {
+                                       stdout = TextWriter.Synchronized (new LogcatTextWriter ("mono-stdout", stdout));
+                                       stderr = TextWriter.Synchronized (new LogcatTextWriter ("mono-stderr", stderr));
+                               }
+#endif // MONODROID
+#endif // MONOTOUCH
                        }
-#endif
 
                        GC.SuppressFinalize (stdout);
                        GC.SuppressFinalize (stderr);
@@ -179,14 +185,11 @@ namespace System
 
                private static Stream Open (IntPtr handle, FileAccess access, int bufferSize)
                {
-#if MOONLIGHT
-                       if (SecurityManager.SecurityEnabled && !Debugger.IsAttached && Environment.GetEnvironmentVariable ("MOONLIGHT_ENABLE_CONSOLE") == null)
-                               return new NullStream ();
-#endif
                        try {
-                               return new FileStream (handle, access, false, bufferSize, false, bufferSize == 0);
+                               // TODO: Should use __ConsoleStream from reference sources
+                               return new FileStream (handle, access, false, bufferSize, false, true);
                        } catch (IOException) {
-                               return new NullStream ();
+                               return Stream.Null;
                        }
                }
 
@@ -516,21 +519,27 @@ namespace System
 
 #endif
 
-#if !NET_2_1
                // FIXME: Console should use these encodings when changed
                static Encoding inputEncoding;
                static Encoding outputEncoding;
 
                public static Encoding InputEncoding {
                        get { return inputEncoding; }
-                       set { inputEncoding = value; }
+                       set {
+                               inputEncoding = value;
+                               SetupStreams (inputEncoding, outputEncoding);
+                       }
                }
 
                public static Encoding OutputEncoding {
                        get { return outputEncoding; }
-                       set { outputEncoding = value; }
+                       set {
+                               outputEncoding = value;
+                               SetupStreams (inputEncoding, outputEncoding);
+                       }
                }
 
+#if !NET_2_1
                public static ConsoleColor BackgroundColor {
                        get { return ConsoleDriver.BackgroundColor; }
                        set { ConsoleDriver.BackgroundColor = value; }
@@ -629,6 +638,24 @@ namespace System
                        set { ConsoleDriver.WindowWidth = value; }
                }
 
+               public static bool IsErrorRedirected {
+                       get {
+                               return ConsoleDriver.IsErrorRedirected;
+                       }
+               }
+
+               public static bool IsOutputRedirected {
+                       get {
+                               return ConsoleDriver.IsOutputRedirected;
+                       }
+               }
+
+               public static bool IsInputRedirected {
+                       get {
+                               return ConsoleDriver.IsInputRedirected;
+                       }
+               }
+
                public static void Beep ()
                {
                        Beep (1000, 500);