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 00027 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 00028 00029 00030 @version Initial version, March 2004 00031 @author Kris 00032 00033 00034 *******************************************************************************/ 00035 00036 module mango.io.DisplayWriter; 00037 00038 private import std.c.stdio; 00039 00040 public import mango.io.Writer; 00041 00042 /******************************************************************************* 00043 00044 Format output suitable for presentation. There is no corresponding 00045 reader, since there are no explicit delimiters emitted to isolate 00046 each formatted item. One can use TextReader to read input from the 00047 console (or from some other input buffer). 00048 00049 *******************************************************************************/ 00050 00051 class DisplayWriter : Writer 00052 { 00053 // temporary buffer space for encoding 00054 private char[40] encode; 00055 00056 // number of floating-point digits to write 00057 private uint digits = 2; 00058 00059 version (Win32) 00060 alias _vsnprintf vsnprintf; 00061 00062 /*********************************************************************** 00063 00064 Construct a DisplayWriter upon the specified IBuffer 00065 00066 ***********************************************************************/ 00067 00068 this (IBuffer buffer) 00069 { 00070 super (buffer); 00071 00072 // disable array length output (for strings and so on) 00073 enableArrayPrefix (false); 00074 00075 numeric.int1 = &int1; 00076 numeric.int8 = &int8; 00077 numeric.int8u = &int8u; 00078 numeric.int16 = &int16; 00079 numeric.int16u = &int16u; 00080 numeric.int32 = &int32; 00081 numeric.int32u = &int32u; 00082 numeric.int64 = &int64; 00083 numeric.int64u = &int64u; 00084 00085 numeric.float32 = &float32; 00086 numeric.float64 = &float64; 00087 numeric.float80 = &float80; 00088 } 00089 00090 /*********************************************************************** 00091 00092 Construct a DisplayWriter upon the specified IConduit 00093 00094 ***********************************************************************/ 00095 00096 this (IConduit conduit) 00097 { 00098 this (conduit.createBuffer); 00099 } 00100 00101 /*********************************************************************** 00102 00103 ***********************************************************************/ 00104 00105 override char[] toString() 00106 { 00107 return "display writer"; 00108 } 00109 00110 /*********************************************************************** 00111 00112 Set the number of floating point digits 00113 00114 ***********************************************************************/ 00115 00116 void setPrecision (uint digits) 00117 { 00118 this.digits = digits; 00119 } 00120 00121 /*********************************************************************** 00122 00123 Convert to a string and send back through the framework 00124 such that it may be intercepted by a subclass. 00125 00126 ***********************************************************************/ 00127 00128 final void format (char[] fmt, void* src) 00129 { 00130 put (encode[0..vsnprintf (encode, encode.length, fmt, src)]); 00131 } 00132 00133 /*********************************************************************** 00134 00135 ***********************************************************************/ 00136 00137 final void int8f (ubyte* src, uint count, char[] fmt) 00138 { 00139 while (count) 00140 { 00141 uint i = *src; 00142 format (fmt, &i); 00143 00144 ++src; 00145 --count; 00146 } 00147 } 00148 00149 /*********************************************************************** 00150 00151 ***********************************************************************/ 00152 00153 final void int16f (ushort* src, uint count, char[] fmt) 00154 { 00155 while (count) 00156 { 00157 uint i = *src; 00158 format (fmt, &i); 00159 00160 ++src; 00161 count -= 2; 00162 } 00163 } 00164 00165 /*********************************************************************** 00166 00167 ***********************************************************************/ 00168 00169 final void format (void* src, uint count, uint width, char[] fmt) 00170 { 00171 while (count) 00172 { 00173 format (fmt, src); 00174 00175 src += width; 00176 count -= width; 00177 } 00178 } 00179 00180 /*********************************************************************** 00181 00182 ***********************************************************************/ 00183 00184 final void int1 (void* src, uint count) 00185 { 00186 while (count) 00187 { 00188 if (*cast(ubyte *) src) 00189 string.char8 (cast(char*) "true", 4); 00190 else 00191 string.char8 (cast(char*) "false", 5); 00192 00193 ++src; 00194 --count; 00195 } 00196 } 00197 00198 /*********************************************************************** 00199 00200 ***********************************************************************/ 00201 00202 final void int8 (void* src, uint count) 00203 { 00204 int8f (cast(ubyte*) src, count, "%d"); 00205 } 00206 00207 /*********************************************************************** 00208 00209 ***********************************************************************/ 00210 00211 final void int8u (void* src, uint count) 00212 { 00213 int8f (cast(ubyte*) src, count, "%u"); 00214 } 00215 00216 /*********************************************************************** 00217 00218 ***********************************************************************/ 00219 00220 final void int16 (void* src, uint count) 00221 { 00222 int16f (cast(ushort*) src, count, "%d"); 00223 } 00224 00225 /*********************************************************************** 00226 00227 ***********************************************************************/ 00228 00229 final void int16u (void* src, uint count) 00230 { 00231 int16f (cast(ushort*) src, count, "%u"); 00232 } 00233 00234 /*********************************************************************** 00235 00236 ***********************************************************************/ 00237 00238 final void int32 (void* src, uint count) 00239 { 00240 format (src, count, uint.sizeof, "%d"); 00241 } 00242 00243 /*********************************************************************** 00244 00245 ***********************************************************************/ 00246 00247 final void int32u (void* src, uint count) 00248 { 00249 format (src, count, uint.sizeof, "%u"); 00250 } 00251 00252 /*********************************************************************** 00253 00254 ***********************************************************************/ 00255 00256 final void int64 (void* src, uint count) 00257 { 00258 format (src, count, long.sizeof, "%lld"); 00259 } 00260 00261 /*********************************************************************** 00262 00263 ***********************************************************************/ 00264 00265 final void int64u (void* src, uint count) 00266 { 00267 format (src, count, long.sizeof, "%llu"); 00268 } 00269 00270 /*********************************************************************** 00271 00272 ***********************************************************************/ 00273 00274 final void float32 (void* src, uint count) 00275 { 00276 format (src, count, float.sizeof, "%.2f"); 00277 } 00278 00279 /*********************************************************************** 00280 00281 ***********************************************************************/ 00282 00283 final void float64 (void* src, uint count) 00284 { 00285 format (src, count, double.sizeof, "%.2lf"); 00286 } 00287 00288 /*********************************************************************** 00289 00290 ***********************************************************************/ 00291 00292 final void float80 (void* src, uint count) 00293 { 00294 format (src, count, real.sizeof, "%.2Lf"); 00295 } 00296 } 00297 00298 00299