00001 /******************************************************************************* 00002 00003 @file Stdout.d 00004 00005 Copyright (c) 2004 Kris Bell 00006 00007 This software is provided 'as-is', without any express or implied 00008 warranty. In no event will the authors be held liable for damages 00009 of any kind arising from the use of this software. 00010 00011 Permission is hereby granted to anyone to use this software for any 00012 purpose, including commercial applications, and to alter it and/or 00013 redistribute it freely, subject to the following restrictions: 00014 00015 1. The origin of this software must not be misrepresented; you must 00016 not claim that you wrote the original software. If you use this 00017 software in a product, an acknowledgment within documentation of 00018 said product would be appreciated but is not required. 00019 00020 2. Altered source versions must be plainly marked as such, and must 00021 not be misrepresented as being the original software. 00022 00023 3. This notice may not be removed or altered from any distribution 00024 of the source. 00025 00026 4. Derivative works are permitted, but they must carry this notice 00027 in full and credit the original source. 00028 00029 00030 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 00031 00032 00033 @version Initial version, Feb 2005 00034 @author Kris 00035 00036 00037 *******************************************************************************/ 00038 00039 module mango.io.Stdout; 00040 00041 private import mango.io.FileConduit, 00042 mango.io.ConduitStyle; 00043 00044 // for CR 00045 public import mango.io.DisplayWriter; 00046 00047 00048 /******************************************************************************* 00049 00050 The ubiquitous console IO support. These are standard FileConduit 00051 instances, with Reader/Writer wrappers applied appropriately. Note 00052 that the outputs use FlushBuffer to automatically flush data as it 00053 is added to the buffer. The basic usage of this module is illustrated 00054 below: 00055 00056 @code 00057 char[] msg = "on the console"; 00058 00059 Stdout ("print ") (1) (' ') ("message ") (msg) (CR); 00060 @endcode 00061 00062 An alternative is to use put() notation like so: 00063 00064 @code 00065 char[] msg = "on the console"; 00066 00067 Stdout.put ("print ") 00068 .put (1) 00069 .put (' ') 00070 .put ("message ") 00071 .put (msg) 00072 .put (CR); 00073 @endcode 00074 00075 Another alternative is to use the C++ iostream operators like so: 00076 00077 @code 00078 char[] msg = "on the console"; 00079 00080 Stdout << "print " 00081 << 1 00082 << ' ' 00083 << "message " 00084 << msg 00085 << CR; 00086 @endcode 00087 00088 Since console idioms are based upon FileConduit, you can use them 00089 as direct targets for Conduit operations. For example, the code: 00090 00091 @code 00092 FileConduit fc = new FileConduit ("myfile.txt"); 00093 Stdout.conduit.copy (fc); 00094 @endcode 00095 00096 copies a text file directly to the console. Likewise, you can 00097 copy console input directly to a FileConduit or a SocketConduit. 00098 Input via Stdin is similar in nature, but uses the Token classes 00099 to isolate and parse each token on an input line: 00100 00101 @code 00102 Stdout ("please input a number: ") (); 00103 int x; 00104 Stdin (x); 00105 @endcode 00106 00107 @code 00108 Stdout ("please enter your name: ") (); 00109 char[] you; 00110 Stdin (you); 00111 Stdout ("Hello ") (you) (CR); 00112 @endcode 00113 00114 Stdout automatically flushes the output when it sees a CR, so you 00115 may need to flush the output manually where a CR is not desired. 00116 This is the case in the above example, so we use the empty () to 00117 request a flush (which is actually an alias for the flush method). 00118 00119 Note that Stdin awaits a carriage-return before parsing the input 00120 into the targets. Note also that the Stdout and Stderr are not written 00121 to be thread-safe. As such you may find that output from two threads 00122 intersect across each other. If this is a problem you should wrap a 00123 synchronized block around the offending entity, like so: 00124 00125 @code 00126 synchronized (Stdout) 00127 Stdout ("this is ") ("'atomic' ") (" output") (CR); 00128 @endcode 00129 00130 Alternatively, please consider using the mango.log (Logger) package 00131 to provide detailed runtime diagnostics from your application. The 00132 functionality exposed there is likely sufficient for most application 00133 needs. 00134 00135 Redirecting the standard IO handles (via a shell) operates as one 00136 would expect. 00137 00138 *******************************************************************************/ 00139 00140 class ConsoleWriter : FlushWriter 00141 { 00142 /*********************************************************************** 00143 00144 Standard output conduits. 00145 00146 ***********************************************************************/ 00147 00148 FileConduit conduit; 00149 00150 /*********************************************************************** 00151 00152 Prohibit instantiation of this class 00153 00154 ***********************************************************************/ 00155 00156 private this (int device) 00157 { 00158 conduit = new FileConduit (new FileDevice (device, ConduitStyle.Access.Write)); 00159 super (conduit); 00160 } 00161 } 00162 00163 /******************************************************************************* 00164 00165 Standard IO writers. These are exposed at global scope 00166 00167 *******************************************************************************/ 00168 00169 static ConsoleWriter Stdout, 00170 Stderr; 00171 00172 static this () 00173 { 00174 Stdout = new ConsoleWriter (1); 00175 Stderr = new ConsoleWriter (2); 00176 }