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