00001 /******************************************************************************* 00002 00003 @file TextLayout.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.TextLayout; 00037 00038 private import mango.io.Exception; 00039 00040 /******************************************************************************* 00041 00042 Arranges text strings in order, using indices to specify where 00043 each particular argument should be positioned within the text. 00044 This is handy for collating I18N components. 00045 00046 @code 00047 // create a TextLayout instance 00048 TextLayout tl = new TextLayout (256); 00049 00050 // write text to Stdout 00051 char[] arg1 = "one"; 00052 char[] arg2 = "two"; 00053 Stdout << tl.format ("%2 %1", arg1, arg2) << Stdout.newline; 00054 00055 // or access the content, post processing ... 00056 char[] text = tl.content; 00057 @endcode 00058 00059 The index numbers range from one through nine. Note also that 00060 this class is stateful, and is therefore not shareable across 00061 multiple threads. 00062 00063 *******************************************************************************/ 00064 00065 class TextLayout (Type) 00066 { 00067 private int count; 00068 private Type[] output; 00069 00070 /*********************************************************************** 00071 00072 Set the maximum buffer length 00073 00074 ***********************************************************************/ 00075 00076 this (int limit) 00077 { 00078 output = new Type [limit]; 00079 } 00080 00081 /*********************************************************************** 00082 00083 Address formatted output as a char[] 00084 00085 ***********************************************************************/ 00086 00087 Type[] content () 00088 { 00089 return output [0..count]; 00090 } 00091 00092 /*********************************************************************** 00093 00094 layout a set of arguments 00095 00096 ***********************************************************************/ 00097 00098 Type[] format (Type[] layout, ...) 00099 { 00100 return format (layout, _arguments, _argptr); 00101 } 00102 00103 /*********************************************************************** 00104 00105 layout a set of arguments 00106 00107 ***********************************************************************/ 00108 00109 private Type[] format (Type[] layout, TypeInfo[] arguments, void *argptr) 00110 { 00111 int pos, 00112 args; 00113 bool state; 00114 00115 args = arguments.length; 00116 foreach (Type c; layout) 00117 { 00118 if (state) 00119 { 00120 state = false; 00121 if (c >= '1' || c <= '9') 00122 { 00123 uint index = c - '1'; 00124 if (index < args && arguments[index] == typeid(Type[])) 00125 { 00126 Type[] x = *cast(Type[] *) (index + cast(void[] *) argptr); 00127 00128 int limit = pos + x.length; 00129 if (limit < output.length) 00130 { 00131 output[pos..limit] = x; 00132 pos = limit; 00133 continue; 00134 } 00135 else 00136 throw new Exception ("TextLayout limit exceeded"); 00137 } 00138 else 00139 throw new Exception ("invalid layout argument"); 00140 } 00141 } 00142 else 00143 if (c == '%') 00144 { 00145 state = true; 00146 continue; 00147 } 00148 00149 if (pos < output.length) 00150 { 00151 output[pos] = c; 00152 ++pos; 00153 } 00154 else 00155 throw new Exception ("layout buffer too small"); 00156 } 00157 00158 count = pos; 00159 return content; 00160 } 00161 }