00001 /******************************************************************************* 00002 00003 @file BufferFormat.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; December 2005 00034 00035 @author Kris 00036 00037 00038 *******************************************************************************/ 00039 00040 module mango.io.BufferFormat; 00041 00042 private import mango.convert.Format, 00043 mango.convert.Double; 00044 00045 private import mango.io.BufferCodec; 00046 00047 private import mango.io.model.IBuffer; 00048 00049 00050 /******************************************************************************* 00051 00052 A bridge between a Format instance and a Buffer. This is used for 00053 the Print & Error globals, but can be used for general-purpose 00054 buffer-formatting as desired. The Template type 'T' dictates the 00055 text arrangement within the target buffer ~ one of char, wchar or 00056 dchar (utf8, utf16, or utf32) 00057 00058 *******************************************************************************/ 00059 00060 class BufferFormatTemplate(T) : FormatClassTemplate!(T) 00061 { 00062 private T[] tail; 00063 private bool flush; 00064 private IBuffer buffer; 00065 private Importer importer; 00066 00067 /********************************************************************** 00068 00069 Construct a BufferFormat instance, tying the provided 00070 buffer to a formatter. Set option 'flush' to true if 00071 the result should be flushed when complete, and provide 00072 a 'tail' to append on the completed output (such as a 00073 newline). 00074 00075 **********************************************************************/ 00076 00077 this (IBuffer buffer, bool flush = false, T[] tail = null) 00078 { 00079 // configure the formatter 00080 super (&write, &render, &Double.format); 00081 00082 // hook up a unicode converter 00083 importer = new UnicodeImporter!(T)(buffer); 00084 00085 // save buffer and tail references 00086 this.buffer = buffer; 00087 this.flush = flush; 00088 this.tail = tail; 00089 } 00090 00091 /********************************************************************** 00092 00093 Callback from the Format instance to write an array 00094 00095 **********************************************************************/ 00096 00097 private uint write (void[] x, uint type) 00098 { 00099 return importer (x, x.length, type); 00100 } 00101 00102 /********************************************************************** 00103 00104 Callback from the Format instance to render the content 00105 00106 **********************************************************************/ 00107 00108 private void render () 00109 { 00110 if (tail.length) 00111 buffer.append (tail); 00112 00113 if (flush) 00114 buffer.flush (); 00115 } 00116 } 00117 00118 00119 // convenience alias 00120 alias BufferFormatTemplate!(char) BufferFormat;