The Labs \ Source Viewer \ SSCLI \ Microsoft.JScript \ IMessageReceiver

  1. // ==++==
  2. //
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. //
  14. // ==--==
  15. namespace Microsoft.JScript
  16. {
  17.    
  18.     using System;
  19.     using System.Text;
  20.     using System.IO;
  21.    
  22.     //*************************************************************************************
  23.     // IRedirectOutput
  24.     //
  25.     // Implemented by VSAEngine; give a host a way to catch output that would otherwise
  26.     // go to console or nowhere when no console is up
  27.     //*************************************************************************************
  28.     [System.Runtime.InteropServices.GuidAttribute("5B807FA1-00CD-46ee-A493-FD80AC944715")]
  29.     [System.Runtime.InteropServices.ComVisible(true)]
  30.     public interface IRedirectOutput
  31.     {
  32.         void SetOutputStream(IMessageReceiver output);
  33.     }
  34.    
  35.     //*************************************************************************************
  36.     // IMessageReceiver
  37.     //
  38.     // Implemented by a host that wants to receive output from an engine. It is
  39.     // essentially a callback
  40.     //*************************************************************************************
  41.     [System.Runtime.InteropServices.GuidAttribute("F062C7FB-53BF-4f0d-B0F6-D66C5948E63F")]
  42.     [System.Runtime.InteropServices.ComVisible(true)]
  43.     public interface IMessageReceiver
  44.     {
  45.         void Message(string strValue);
  46.     }
  47.    
  48.     //*************************************************************************************
  49.     // COMCharStream
  50.     //
  51.     // Define a stream over an IMessageReceiver. The stream is then used to set
  52.     // the Out and Err static variable in ScriptStream
  53.     //*************************************************************************************
  54.     public class COMCharStream : Stream
  55.     {
  56.        
  57.         private IMessageReceiver messageReceiver;
  58.         private StringBuilder buffer;
  59.        
  60.         public COMCharStream(IMessageReceiver messageReceiver)
  61.         {
  62.             this.messageReceiver = messageReceiver;
  63.             this.buffer = new StringBuilder(128);
  64.         }
  65.        
  66.         public override bool CanWrite {
  67.             get { return true; }
  68.         }
  69.        
  70.         public override bool CanRead {
  71.             get { return false; }
  72.         }
  73.        
  74.         public override bool CanSeek {
  75.             get { return false; }
  76.         }
  77.        
  78.         public override long Length {
  79.             get { return this.buffer.Length; }
  80.         }
  81.        
  82.         public override long Position {
  83.             get { return this.buffer.Length; }
  84.             set { }
  85.         }
  86.        
  87.         public override void Close()
  88.         {
  89.             Flush();
  90.         }
  91.        
  92.         public override void Flush()
  93.         {
  94.             this.messageReceiver.Message(this.buffer.ToString());
  95.             this.buffer = new StringBuilder(128);
  96.         }
  97.        
  98.         public override int Read(byte[] buffer, int offset, int count)
  99.         {
  100.             throw new NotSupportedException();
  101.         }
  102.        
  103.         public override long Seek(long offset, SeekOrigin origin)
  104.         {
  105.             return 0;
  106.         }
  107.        
  108.         public override void SetLength(long value)
  109.         {
  110.             this.buffer.Length = (int)value;
  111.         }
  112.        
  113.         public override void Write(byte[] buffer, int offset, int count)
  114.         {
  115.             for (int i = count; i > 0; i--)
  116.                 this.buffer.Append((char)buffer[offset++]);
  117.         }
  118.        
  119.     }
  120. }

Developer Fusion