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 
00027                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00028 
00029 
00030         @version        Initial version, March 2004      
00031         @author         Kris
00032 
00033 
00034 *******************************************************************************/
00035 
00036 module mango.io.TextWriter;
00037 
00038 public import mango.io.DisplayWriter;
00039 
00040 /*******************************************************************************
00041 
00042         Print readable output to an IWriter seperated by delimiters. Note
00043         that the delimiter should be chosen such that it doesn't conflict
00044         with any other characters being written. For example, by choosing 
00045         a comma-delimiter, you should ensure a comma is not output within
00046         a text string (so the output can be easily tokenized when read).
00047 
00048 *******************************************************************************/
00049 
00050 class TextWriter : DisplayWriter
00051 {
00052         private byte    ignore;
00053         private char[]  delimiter;
00054 
00055         alias DisplayWriter.put put;
00056 
00057         /***********************************************************************
00058         
00059                 Construct a TextWriter using the provided buffer. Output 
00060                 is seperated with the given delimiter string.
00061 
00062         ***********************************************************************/
00063 
00064         this (IBuffer buffer, char[] delimiter)
00065         {
00066                 super (buffer);
00067                 this.delimiter = delimiter;
00068         }
00069      
00070         /***********************************************************************
00071         
00072         ***********************************************************************/
00073         
00074         override char[] toString()
00075         {
00076                 return "text writer";
00077         }
00078 
00079         /***********************************************************************
00080         
00081                 Reset this writer, so it won't emit the specified series 
00082                 of subsequent delimeters
00083 
00084         ***********************************************************************/
00085 
00086         void suppress (byte count)
00087         {    
00088                 ignore += count;
00089         }
00090      
00091         /***********************************************************************
00092         
00093                 Intercept the IWritable method to catch newlines
00094 
00095         ***********************************************************************/
00096 
00097         override IWriter put (IWritable x)
00098         {      
00099                 // don't delimit writable if it's to be ignored
00100                 if (cast(IPhantomWriter) x)
00101                     suppress (1);
00102 
00103                // have superclass print the IWritable
00104                 return super.put (x);
00105         }
00106      
00107         /***********************************************************************
00108         
00109                 Intercept the output so we can append a delimiter.
00110 
00111                 @todo - add the equivalent intercepts for wchar[] 
00112                         and dchar[] methods. I think it's perhaps
00113                         OK to restrict delimiters to char[] only.
00114 
00115         ***********************************************************************/
00116 
00117         override IWriter put (char[] x)
00118         {    
00119                 string.char8 (x, x.length);
00120                 delimit ();
00121                 return this;
00122         }
00123 
00124         /***********************************************************************
00125         
00126                 write a delimiter after each token
00127 
00128         ***********************************************************************/
00129 
00130         private final void delimit ()
00131         {    
00132                 // output the delimiter?
00133                 if (ignore)
00134                     --ignore;
00135                 else
00136                    string.char8 (delimiter, delimiter.length);
00137         }
00138 }

Generated on Sun Nov 7 19:06:53 2004 for Mango by doxygen 1.3.6