Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages

Stdio.d

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

Generated on Tue Jan 25 21:18:23 2005 for Mango by doxygen 1.3.6