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 std.stdarg; 00042 00043 private import std.c.stdio; 00044 00045 private import mango.io.Exception; 00046 00047 /******************************************************************************* 00048 00049 Constructs printf-style output. This is an adapter for "old" printf 00050 formatting, and writes it's output into a lookaside buffer. Content 00051 from the buffer can easily be passed to an IWriter instance. 00052 00053 @code 00054 // create a TextFormat instance 00055 TextFormat p = new TextFormat (256); 00056 00057 // write text to Stdout 00058 Stdout << p.format ("%d green bottles, sitting on a wall\n", 10); 00059 00060 // or access the content, post formatting ... 00061 char[] text = p.content; 00062 @endcode 00063 00064 This can be really handy when you wish to format text in conjunction 00065 with a Logger. Please note that the class is stateful, and therefore 00066 is not shareable across multiple threads. 00067 00068 *******************************************************************************/ 00069 00070 class TextFormat 00071 { 00072 private int count; 00073 private char[] output; 00074 00075 /*********************************************************************** 00076 00077 Set the maximum buffer length 00078 00079 ***********************************************************************/ 00080 00081 this (int limit) 00082 { 00083 output = new char [limit]; 00084 } 00085 00086 /*********************************************************************** 00087 00088 format a set of arguments 00089 00090 ***********************************************************************/ 00091 00092 char[] format (char[] fmt, ...) 00093 { 00094 va_list args; 00095 00096 args = cast(va_list) &fmt; 00097 args += fmt.sizeof; 00098 return format (fmt, args); 00099 } 00100 00101 /*********************************************************************** 00102 00103 format a va_list of arguments 00104 00105 ***********************************************************************/ 00106 00107 char[] format (char[] fmt, va_list args) 00108 { 00109 version (Win32) 00110 { 00111 count = _vsnprintf (output, output.length, fmt, args); 00112 } 00113 version (Posix) 00114 { 00115 count = vsnprintf (output, output.length, fmt, args); 00116 } 00117 00118 if (count < 0) 00119 throw new IOException ("TextFormat limit exceeded"); 00120 00121 return content (); 00122 } 00123 00124 /*********************************************************************** 00125 00126 Address formatted output as a char[] 00127 00128 ***********************************************************************/ 00129 00130 char[] content () 00131 { 00132 return output [0..count]; 00133 } 00134 }