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

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