GME  13
GMEConsole.cs
Go to the documentation of this file.
00001 using System;
00002 using System.Collections.Generic;
00003 using System.Linq;
00004 using System.Text;
00005 using System.IO;
00006 using GME;
00007 
00008 namespace GME.CSharp
00009 {
00014     public class GMEConsole
00015     {
00016 
00020         public IGMEOLEApp gme = null;
00021         private GMETextWriter error;
00022         private GMETextWriter warning;
00023         private GMETextWriter info;
00024         private GMETextWriter normal;
00025 
00026         public GMEConsole()
00027         {
00028             error = new GMETextWriter(msgtype_enum.MSG_ERROR, this);
00029             warning = new GMETextWriter(msgtype_enum.MSG_WARNING, this);
00030             info = new GMETextWriter(msgtype_enum.MSG_INFO, this);
00031             normal = new GMETextWriter(msgtype_enum.MSG_NORMAL, this);
00032         }
00033 
00041         public TextWriter Error
00042         {
00043             get { return error; }
00044         }
00045 
00051         public TextWriter Out
00052         {
00053             get { return normal; }
00054         }
00055 
00056 
00062         public TextWriter Warning
00063         {
00064             get { return warning; }
00065         }
00066 
00067 
00073         public TextWriter Info
00074         {
00075             get { return info; }
00076         }
00077 
00081         public void Clear()
00082         {
00083             if (gme != null)
00084                 gme.ConsoleClear();
00085             else
00086                 System.Console.Clear();
00087         }
00088 
00089 
00090         public static GMEConsole CreateFromProject(GME.MGA.MgaProject project)
00091         {
00092             GMEConsole console = new GMEConsole();
00093             try
00094             {
00095                 // Initializing console               
00096                 console.gme = (IGMEOLEApp)project.GetClientByName("GME.Application").OLEServer;
00097             }
00098             catch (System.Runtime.InteropServices.COMException ex)
00099             {
00100                 // if GME is not present, the interpreter is called from standalone test application
00101                 if (ex.ErrorCode != -2023423888) // HResult 0x87650070: "Search by name failed"
00102                 {
00103                     throw;
00104                 }
00105                 console.gme = null;
00106             }
00107             return console;
00108         }
00109     }
00110 
00111 
00112     public class GMETextWriter : System.IO.TextWriter
00113     {
00114         private msgtype_enum type;
00115         private GMEConsole console;
00116 
00117         public GMETextWriter(msgtype_enum type, GMEConsole console)
00118         {
00119             this.type = type;
00120             this.console = console;
00121         }
00122 
00123         override public Encoding Encoding
00124         {
00125             get { return Encoding.Unicode; }
00126         }
00127 
00128         override public void WriteLine(string str)
00129         {
00130             Write(str + Environment.NewLine);
00131         }
00132 
00133         override public void Write(string str)
00134         {
00135             if (console.gme == null)
00136             {
00137                 switch (type)
00138                 {
00139                     case msgtype_enum.MSG_NORMAL:
00140                         Console.Out.Write(str);
00141                         break;
00142                     case msgtype_enum.MSG_INFO:
00143                         Console.Out.Write("Information: " + str);
00144                         break;
00145                     case msgtype_enum.MSG_WARNING:
00146                         Console.Out.Write("Warning: " + str);
00147                         break;
00148                     case msgtype_enum.MSG_ERROR:
00149                         Console.Error.Write(str);
00150 #if(DEBUG)
00151                         System.Diagnostics.Debug.Write(str);
00152 #endif
00153                         break;
00154                 }
00155             }
00156             else
00157             {
00158                 console.gme.ConsoleMessage(str, type);
00159             }
00160         }
00161     }
00162 }