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

DisplayWriter.d

Go to the documentation of this file.
00001 /*******************************************************************************
00002 
00003         @file DisplayWriter.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                         Rewritten to support format tags; March 2005
00035 
00036         @author         Kris
00037 
00038 
00039 *******************************************************************************/
00040 
00041 module mango.io.DisplayWriter;
00042 
00043 private import  std.stdarg;
00044 
00045 public  import  mango.io.Writer;
00046 
00047 private import  mango.convert.Type,
00048                 mango.convert.Format,
00049                 mango.convert.Double;
00050 
00051 /*******************************************************************************
00052 
00053         Format output suitable for presentation. DisplayWriter provide 
00054         the means to append formatted  data to an IBuffer, and exposes 
00055         a convenient method of handling a variety of data types. 
00056         
00057         DisplayWriter supports the usual printf() format specifiers & flags, 
00058         and extends the notion to operate with one dimensional arrays. For
00059         instance, this code
00060 
00061         @code
00062         static int x = [1, 2, 3, 4, 5, 6, 7, 8];
00063 
00064         Stdout.print ("%@04b, ", x);
00065         @endcode
00066 
00067         results in the following output: 
00068 
00069         @code
00070         0001, 0010, 0011, 0100, 0101, 0110, 0111, 1000,
00071         @endcode
00072 
00073         Note that DisplayWriter itself is a wrapper around the mango.convert 
00074         package, which can be used directly as desired (Integer, Double, 
00075         DGDouble, etc). The latter classes are home to a set of static 
00076         formatting-methods, making them convenient for ad-hoc application.
00077 
00078         Mango.convert also has Format and Sprint classes for working directly
00079         with text arrays.
00080          
00081 *******************************************************************************/
00082 
00083 class DisplayWriter : Writer
00084 {
00085         alias FormatStructTemplate!(char) Format;
00086 
00087         private Format          format;
00088         private char[128]       workspace;
00089 
00090         /***********************************************************************
00091         
00092                 Construct a DisplayWriter upon the specified IBuffer. 
00093                 One can override the default floating-point formatting
00094                 by providing an appropriate handler to this constructor.
00095                 For example, one might configure the DGDouble.format()
00096                 function instead.
00097 
00098         ***********************************************************************/
00099 
00100         this (IBuffer buffer, char[] workspace = null, Format.DblFormat df = &Double.format)
00101         {
00102                 super (buffer);
00103                 
00104                 if (workspace == null)
00105                     workspace = this.workspace;
00106 
00107                 // configure output-handler, workspace, and the
00108                 // floating-point converter
00109                 format.ctor (&emit, null, workspace, df);
00110         }
00111      
00112         /***********************************************************************
00113         
00114                 Construct a DisplayWriter upon the specified IConduit
00115 
00116         ***********************************************************************/
00117 
00118         this (IConduit conduit)
00119         {
00120                 this (new Buffer(conduit));
00121         }
00122 
00123         /***********************************************************************
00124         
00125                 Format a set of arguments a la printf(). Please see module
00126                 mango.convert.Format for details
00127 
00128         ***********************************************************************/
00129 
00130         int print (char[] s, TypeInfo[] ti, va_list args)
00131         {       
00132                 return format (s, ti, args);
00133         }
00134 
00135         /***********************************************************************
00136         
00137                 Format a set of arguments a la printf(). Please see module
00138                 mango.convert.Format for details
00139 
00140         ***********************************************************************/
00141 
00142         DisplayWriter print (char[] s, ...)
00143         {       
00144                 print (s, _arguments, _argptr);
00145                 return this;
00146         }
00147 
00148         /***********************************************************************
00149         
00150                 Format a set of arguments a la printf(). Please see module
00151                 mango.convert.Format for details
00152 
00153         ***********************************************************************/
00154 
00155         DisplayWriter println (char[] s, ...)
00156         {       
00157                 print (s, _arguments, _argptr);
00158                 put (CR);
00159                 return this;
00160         }
00161 
00162         /***********************************************************************
00163         
00164                 Is this Writer text oriented?
00165 
00166         ***********************************************************************/
00167 
00168         bool isTextBased()
00169         {
00170                 return true;
00171         }
00172 
00173         /***********************************************************************
00174         
00175                 Intercept discrete output and convert it to printable form
00176 
00177         ***********************************************************************/
00178 
00179         protected override IWriter write (void* src, uint bytes, int type)
00180         {
00181                 format.emit (src, bytes, type);
00182                 return this;
00183         }
00184 
00185         /***********************************************************************
00186         
00187                 formatting handler
00188 
00189         ***********************************************************************/
00190 
00191         private uint emit (void[] x, uint type)
00192         {
00193                 encode (x, x.length, type);
00194                 return x.length;
00195         }
00196 }

Generated on Sat Dec 24 17:28:32 2005 for Mango by  doxygen 1.4.0