Main Page | Class Hierarchy | Alphabetical List | Class List | 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.DisplayWriter;
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 : DisplayWriter
00054 {
00055         private byte    ignore;
00056         private char[]  delimiter;
00057 
00058         alias opShl put;
00059         alias DisplayWriter.opShl opShl;
00060 
00061         /***********************************************************************
00062         
00063                 Construct a TextWriter using the provided buffer. Output 
00064                 is seperated with the given delimiter string.
00065 
00066         ***********************************************************************/
00067 
00068         this (IBuffer buffer, char[] delimiter)
00069         {
00070                 super (buffer);
00071                 this.delimiter = delimiter;
00072         }
00073      
00074         /***********************************************************************
00075         
00076                 Reset this writer, so it won't emit the specified series 
00077                 of subsequent delimeters
00078 
00079         ***********************************************************************/
00080 
00081         void suppress (byte count)
00082         {    
00083                 ignore += count;
00084         }
00085      
00086         /***********************************************************************
00087         
00088                 Intercept the IWritable method to catch newlines
00089 
00090         ***********************************************************************/
00091 
00092         override IWriter opShl (IWritable x)
00093         {      
00094                 // don't delimit writable if it's to be ignored
00095                 if (cast(IPhantomWriter) x)
00096                     suppress (1);
00097 
00098                // have superclass print the IWritable
00099                 return super.opShl (x);
00100         }
00101      
00102         /***********************************************************************
00103         
00104                 Intercept the output so we can append a delimiter.
00105 
00106         ***********************************************************************/
00107 
00108         override IWriter opShl (char[] x)
00109         {    
00110                 super.opShl (x);
00111                 return delimit ();
00112         }
00113 
00114         /***********************************************************************
00115         
00116                 Intercept the output so we can append a delimiter.
00117 
00118         ***********************************************************************/
00119 
00120         override IWriter opShlw (wchar[] x)
00121         {    
00122                 super.opShlw (x);
00123                 return delimit ();
00124         }
00125 
00126         /***********************************************************************
00127         
00128                 Intercept the output so we can append a delimiter.
00129 
00130         ***********************************************************************/
00131 
00132         override IWriter opShld (dchar[] x)
00133         {    
00134                 super.opShld (x);
00135                 return delimit ();
00136         }
00137 
00138         /***********************************************************************
00139         
00140                 write a delimiter after each token
00141 
00142         ***********************************************************************/
00143 
00144         private final IWriter delimit ()
00145         {    
00146                 // output the delimiter?
00147                 if (ignore)
00148                     --ignore;
00149                 else
00150                    encode.char8 (delimiter, delimiter.length);
00151                 return this;
00152         }
00153 }

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