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 format = new TextFormat (256); 00056 00057 // write text to Stdout 00058 Stdout (format ("%d green bottles, sitting on a wall\n", 10)); 00059 00060 // or access the content, post formatting ... 00061 char[] text = format.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 alias format opCall; 00073 00074 private int count; 00075 private char[] output; 00076 00077 /*********************************************************************** 00078 00079 Set the maximum buffer length 00080 00081 ***********************************************************************/ 00082 00083 this (int limit) 00084 { 00085 output = new char [limit]; 00086 } 00087 00088 /*********************************************************************** 00089 00090 format a set of arguments 00091 00092 ***********************************************************************/ 00093 00094 char[] format (char[] fmt, ...) 00095 { 00096 va_list args; 00097 00098 args = cast(va_list) &fmt; 00099 args += fmt.sizeof; 00100 return format (fmt, args); 00101 } 00102 00103 /*********************************************************************** 00104 00105 format a va_list of arguments 00106 00107 ***********************************************************************/ 00108 00109 char[] format (char[] fmt, va_list args) 00110 { 00111 version (Win32) 00112 { 00113 count = _vsnprintf (output, output.length, fmt, args); 00114 } 00115 version (Posix) 00116 { 00117 count = vsnprintf (output, output.length, fmt, args); 00118 } 00119 00120 if (count < 0) 00121 throw new IOException ("TextFormat limit exceeded"); 00122 00123 return content (); 00124 } 00125 00126 /*********************************************************************** 00127 00128 Address formatted output as a char[] 00129 00130 ***********************************************************************/ 00131 00132 char[] content () 00133 { 00134 return output [0..count]; 00135 } 00136 }