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