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

TextWriter.d

Go to the documentation of this file.
00001 /*******************************************************************************
00002 
00003         @file TextWriter.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.TextWriter;
00040 
00041 public import mango.io.FlushWriter;
00042 
00043 /*******************************************************************************
00044 
00045         Print readable output to an IWriter seperated by delimiters. Note
00046         that the delimiter should be chosen such that it doesn't conflict
00047         with any other characters being written. For example, by choosing 
00048         a comma-delimiter, you should ensure a comma is not output within
00049         a text string (so the output can be easily tokenized when read).
00050 
00051 *******************************************************************************/
00052 
00053 class TextWriter : FlushWriter
00054 {
00055         alias FlushWriter.put put;
00056 
00057         private byte    ignore;
00058         private char[]  delimiter;
00059 
00060         /***********************************************************************
00061         
00062                 Construct a TextWriter using the provided buffer. Output 
00063                 is seperated with the given delimiter string.
00064 
00065         ***********************************************************************/
00066 
00067         this (IBuffer buffer, char[] delimiter)
00068         {
00069                 super (buffer);
00070                 this.delimiter = delimiter;
00071         }
00072      
00073         /***********************************************************************
00074         
00075                 Reset this writer, so it won't emit the specified series 
00076                 of subsequent delimeters
00077 
00078         ***********************************************************************/
00079 
00080         void suppress (byte count)
00081         {    
00082                 ignore += count;
00083         }
00084      
00085         /***********************************************************************
00086         
00087                 Intercept the IWritable method to catch newlines
00088 
00089         ***********************************************************************/
00090 
00091         override IWriter put (IWritable x)
00092         {      
00093                 // don't delimit writable if it's to be ignored
00094                 if (cast(IPhantomWriter) x)
00095                     suppress (1);
00096 
00097                // have superclass print the IWritable
00098                 return super.put (x);
00099         }
00100 
00101         /***********************************************************************
00102         
00103                 Intercept the output so we can append a delimiter.
00104 
00105         ***********************************************************************/
00106 
00107         override IWriter put (char[] x)
00108         {    
00109                 super.put (x);
00110                 return delimit ();
00111         }
00112 
00113         /***********************************************************************
00114         
00115                 Intercept the output so we can append a delimiter.
00116 
00117         ***********************************************************************/
00118 
00119         override IWriter putw (wchar[] x)
00120         {    
00121                 super.putw (x);
00122                 return delimit ();
00123         }
00124 
00125         /***********************************************************************
00126         
00127                 Intercept the output so we can append a delimiter.
00128 
00129         ***********************************************************************/
00130 
00131         override IWriter putd (dchar[] x)
00132         {    
00133                 super.putd (x);
00134                 return delimit ();
00135         }
00136 
00137         /***********************************************************************
00138         
00139                 write a delimiter after each token
00140 
00141         ***********************************************************************/
00142 
00143         private final IWriter delimit ()
00144         {    
00145                 // output the delimiter?
00146                 if (ignore)
00147                     --ignore;
00148                 else
00149                    encode.char8 (delimiter, delimiter.length, 10);
00150                 return this;
00151         }
00152 }

Generated on Fri Nov 11 18:44:22 2005 for Mango by  doxygen 1.4.0