Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages

TextLayout.d

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

Generated on Tue Jan 25 21:18:23 2005 for Mango by doxygen 1.3.6