Overwrites dequeued ref/value with default. Fixes 18421.
[mono.git] / mcs / class / corlib / System / Console.iOS.cs
1 //
2 // Helper for Console to allow indirect access to `stdout` using NSLog
3 //
4 // Authors:
5 //      Sebastien Pouliot  <sebastien@xamarin.com>
6 //
7 // Copyright 2012-2013 Xamarin Inc. All rights reserved.
8 //
9
10 #if FULL_AOT_RUNTIME
11
12 using System;
13 using System.IO;
14 using System.Runtime.InteropServices;
15 using System.Text;
16
17 namespace System {
18
19         public static partial class Console {
20
21                 class NSLogWriter : TextWriter {
22                         [DllImport ("__Internal", CharSet=CharSet.Unicode)]
23                         extern static void monotouch_log (string s);
24
25                         [DllImport ("/usr/lib/libSystem.dylib")]
26                         extern static int write (int fd, byte [] buffer, int n);
27                         
28                         StringBuilder sb;
29                         
30                         public NSLogWriter ()
31                         {
32                                 sb = new StringBuilder ();
33                         }
34                         
35                         public override System.Text.Encoding Encoding {
36                                 get { return System.Text.Encoding.UTF8; }
37                         }
38
39                         static void direct_write_to_stdout (string s)
40                         {
41                                 byte [] b = Encoding.Default.GetBytes (s);
42                                 while (write (1, b, b.Length) == -1 && Marshal.GetLastWin32Error () == /* EINTR*/ 4)
43                                         ;
44                         }
45                         
46                         public override void Flush ()
47                         {
48                                 string s = sb.ToString ();
49                                 try {
50                                         monotouch_log (s);
51                                 }
52                                 catch (Exception) {
53                                         try {
54                                                 direct_write_to_stdout (s);
55                                                 direct_write_to_stdout (Environment.NewLine);
56                                         } catch (Exception){}
57                                 }
58                                 sb.Length = 0;
59                         }
60                         
61                         // minimum to override - see http://msdn.microsoft.com/en-us/library/system.io.textwriter.aspx
62                         public override void Write (char value)
63                         {
64                                 try {
65                                         sb.Append (value);
66                                 }
67                                 catch (Exception) {
68                                 }
69                         }
70                         
71                         // optimization (to avoid concatening chars)
72                         public override void Write (string value)
73                         {
74                                 try {
75                                         sb.Append (value);
76                                         if (value != null && value.Length >= CoreNewLine.Length && EndsWithNewLine (value))
77                                                 Flush ();
78                                 }
79                                 catch (Exception) {
80                                 }
81                         }
82                         
83                         bool EndsWithNewLine (string value)
84                         {
85                                 for (int i = 0, v = value.Length - CoreNewLine.Length; i < CoreNewLine.Length; ++i, ++v) {
86                                         if (value [v] != CoreNewLine [i])
87                                                 return false;
88                                 }
89                                 
90                                 return true;
91                         }
92                         
93                         public override void WriteLine ()
94                         {
95                                 try {
96                                         Flush ();
97                                 }
98                                 catch (Exception) {
99                                 }
100                         }
101                 }
102         }
103 }
104
105 #endif