00001 /******************************************************************************* 00002 00003 @file TextFormat.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.TextFormat; 00040 00041 private import mango.format.Format; 00042 00043 /******************************************************************************* 00044 00045 Constructs printf-style output. This is an adapter for "old" printf 00046 formatting, and writes it's output into a lookaside buffer. Content 00047 from the buffer can easily be passed to an IWriter instance. 00048 00049 @code 00050 // create a TextFormat instance 00051 TextFormat format = new TextFormat (256); 00052 00053 // write text to Stdout 00054 Stdout (format ("%d green bottles, sitting on a wall\n", 10)); 00055 00056 // or access the content, post formatting ... 00057 char[] text = format.content; 00058 @endcode 00059 00060 This can be really handy when you wish to format text in conjunction 00061 with a Logger. Please note that the class is stateful, and therefore 00062 is not shareable across multiple threads. 00063 00064 *******************************************************************************/ 00065 00066 class TextFormat 00067 { 00068 alias format opCall; 00069 00070 private int count; 00071 private char[] output; 00072 private Format.Sprintf sprintf; 00073 00074 /*********************************************************************** 00075 00076 Set the maximum buffer length 00077 00078 ***********************************************************************/ 00079 00080 this (int limit, char[] workspace = null, Format.DblFormat df = null) 00081 { 00082 output = new char [limit]; 00083 sprintf (output, workspace, df); 00084 } 00085 00086 /*********************************************************************** 00087 00088 format a set of arguments 00089 00090 ***********************************************************************/ 00091 00092 char[] format (char[] fmt, ...) 00093 { 00094 count = sprintf.format (fmt, _arguments, _argptr); 00095 return content; 00096 } 00097 00098 /*********************************************************************** 00099 00100 Address formatted output as a char[] 00101 00102 ***********************************************************************/ 00103 00104 char[] content () 00105 { 00106 return output [0..count]; 00107 } 00108 }