00001 /******************************************************************************* 00002 00003 @file Stdio.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 00027 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 00028 00029 00030 @version Initial version, March 2004 00031 @author Kris 00032 00033 00034 *******************************************************************************/ 00035 00036 module mango.io.Stdio; 00037 00038 private import mango.io.Buffer, 00039 mango.io.Reader, 00040 mango.io.Writer, 00041 mango.io.Tokenizer, 00042 mango.io.TextReader, 00043 mango.io.FileConduit, 00044 mango.io.ConduitStyle, 00045 mango.io.DisplayWriter; 00046 00047 00048 /******************************************************************************* 00049 00050 Standard IO readers and writers. These are at module scope so 00051 there's less typing involved to access them. 00052 00053 *******************************************************************************/ 00054 00055 static IReader Stdin; 00056 static IWriter Stdout, 00057 Stderr; 00058 00059 /******************************************************************************* 00060 00061 The ubiquitous console IO support. These are standard FileConduit 00062 instances, with Reader/Writer wrappers applied appropriately. Note 00063 that the outputs use FlushBuffer to automatically flush data as it 00064 is added to the buffer. The basic usage of this module is illustrated 00065 below: 00066 00067 @code 00068 char[] msg = "on the console"; 00069 00070 Stdout.put ("print ") 00071 .put (1) 00072 .put (' ') 00073 .put ("message ") 00074 .put (msg) 00075 .cr (); 00076 @endcode 00077 00078 An alternative is to use the C++ stream operator like so: 00079 00080 @code 00081 char[] msg = "on the console"; 00082 00083 Stdout << "print " 00084 << 1 00085 << ' ' 00086 << "message " 00087 << msg 00088 << Stdout.newline; 00089 @endcode 00090 00091 Since console idioms are based upon FileConduit, you can use them 00092 as direct targets for Conduit operations. For example, the code: 00093 00094 @code 00095 FileConduit fc = new FileConduit ("myfile.txt", FileStyle.Read); 00096 Stdio.stdout.copy (fc); 00097 @endcode 00098 00099 copies a text file directly to the console. Likewise, you can 00100 copy console input directly to a FileConduit or a SocketConduit. 00101 Input via Stdin is similar in nature, but uses the Token classes 00102 to isolate and parse each token on an input line: 00103 00104 @code 00105 int x; 00106 Stdout.put ("please input a number: "); 00107 Stdin.get(x); // or Stdin >> x; 00108 @endcode 00109 00110 Note that Stdin awaits a carriage-return before parsing the input 00111 into the targets. Note also that the Stdout and Stderr are not written 00112 to be thread-safe. As such you may find that output from two threads 00113 intersect across each other. If this is a problem you should wrap a 00114 synchronized block around the offending entity, like so: 00115 00116 @code 00117 synchronized (Stdout) 00118 Stdout << "this is " << "'atomic' " << " output".cr(); 00119 @endcode 00120 00121 Alternatively, please consider using the mango.log (Logger) package 00122 to provide detailed runtime diagnostics from your application. The 00123 functionality exposed there is likely sufficient for most application 00124 needs. 00125 00126 Redirecting the standard IO handles (via a shell) operates as one 00127 would expect. 00128 00129 *******************************************************************************/ 00130 00131 class Stdio 00132 { 00133 /*********************************************************************** 00134 00135 Standard IO conduits. These are inside the Stdio namespace 00136 since I prefer their usage to be explicit. 00137 00138 ***********************************************************************/ 00139 00140 static FileConduit stdin, 00141 stdout, 00142 stderr; 00143 00144 /*********************************************************************** 00145 00146 Create a set of Stdio conduits and readers/writers 00147 00148 ***********************************************************************/ 00149 00150 static this() 00151 { 00152 stdin = new FileConduit (new FileDevice(0, ConduitStyle.Access.Read)); 00153 stdout = new FileConduit (new FileDevice(1, ConduitStyle.Access.Write)); 00154 stderr = new FileConduit (new FileDevice(2, ConduitStyle.Access.Write)); 00155 00156 .Stdin = new TextReader (stdin.createBuffer(), new LineTokenizer()); 00157 .Stdout = new DisplayWriter (new FlushBuffer (1024 * 16, stdout)); 00158 .Stderr = new DisplayWriter (new FlushBuffer (1024 * 16, stderr)); 00159 } 00160 00161 /*********************************************************************** 00162 00163 Prohibit instantiation of this class 00164 00165 ***********************************************************************/ 00166 00167 private this() 00168 { 00169 } 00170 }