00001 /******************************************************************************* 00002 00003 @file Stdin.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.Stdin; 00040 00041 private import mango.io.Console, 00042 mango.io.TextReader, 00043 mango.io.BufferCodec; 00044 00045 private import mango.text.QuotedToken; 00046 00047 /******************************************************************************* 00048 00049 The ubiquitous console IO support. These are standard Conduit 00050 instances, with Reader/Writer wrappers applied appropriately. Note 00051 that the outputs use FlushBuffer to automatically flush data as it 00052 is added to the buffer. The basic usage of this module is illustrated 00053 below: 00054 00055 @code 00056 char[] msg = "on the console"; 00057 00058 Stdout ("print ") (1) (' ') ("message ") (msg) (CR); 00059 @endcode 00060 00061 An alternative is to use put() notation like so: 00062 00063 @code 00064 char[] msg = "on the console"; 00065 00066 Stdout.put ("print ") 00067 .put (1) 00068 .put (' ') 00069 .put ("message ") 00070 .put (msg) 00071 .put (CR); 00072 @endcode 00073 00074 Another alternative is to use the C++ iostream operators like so: 00075 00076 @code 00077 char[] msg = "on the console"; 00078 00079 Stdout << "print " 00080 << 1 00081 << ' ' 00082 << "message " 00083 << msg 00084 << CR; 00085 @endcode 00086 00087 Since console idioms are based upon Conduit, you can use them as 00088 direct targets for stream-oriented operations. For example, the 00089 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 enter your name: ") (); 00103 char[] you; 00104 Stdin (you); 00105 Stdout ("Hello ") (you) (CR); 00106 @endcode 00107 00108 Note that Stdin awaits a carriage-return before parsing the input 00109 into the targets. Note also that the Stdout and Stderr are not written 00110 to be thread-safe. As such you may find that output from two threads 00111 intersect across each other. If this is a problem you should wrap a 00112 synchronized block around the offending entity, like so: 00113 00114 @code 00115 synchronized (Stdout) 00116 Stdout ("this is ") ("'atomic' ") (" output") (CR); 00117 @endcode 00118 00119 Alternatively, please consider using the mango.log (Logger) package 00120 to provide detailed runtime diagnostics from your application. The 00121 functionality exposed there is likely sufficient for most application 00122 needs. 00123 00124 Redirecting the standard IO handles (via a shell) operates as one 00125 would expect. 00126 00127 *******************************************************************************/ 00128 00129 static TextReader Stdin; 00130 00131 static this () 00132 { 00133 Stdin = new TextReader (Cin, new QuotedToken (",\t \n")); 00134 Stdin.setDecoder (new UnicodeExporter!(char) (Cin)); 00135 } 00136