Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | 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                         Anders F Bjorklund (Darwin patches)
00036 
00037 
00038 *******************************************************************************/
00039 
00040 module mango.io.TextLayout;
00041 
00042 /*******************************************************************************
00043 
00044         Arranges text strings in order, using indices to specify where
00045         each particular argument should be positioned within the text. 
00046         This is handy for collating I18N components.
00047 
00048         @code
00049         // write ordered text to Stdout
00050         char[64] dst;
00051 
00052         Stdout (TextLayout (dst, "%2 %1", "one", "two"));
00053         @endcode
00054 
00055         The index numbers range from one through nine. TextLayout defaults
00056         to char[], but you can instantiate the template for any other type.
00057         
00058 *******************************************************************************/
00059 
00060 struct TextLayoutTemplate(T)
00061 {       
00062         /**********************************************************************
00063               
00064         **********************************************************************/
00065 
00066         static T[] opCall (T[] output, T[][] layout ...)
00067         {
00068                 int     pos,
00069                         args;
00070                 bool    state;
00071 
00072                 args = layout.length - 1;
00073                 foreach (T c; layout[0])
00074                         {
00075                         if (state)
00076                            {
00077                            state = false;
00078                            if (c >= '1' || c <= '9')
00079                               {
00080                               uint index = c - '0';
00081                               if (index <= args)
00082                                  {
00083                                  T[] x = layout[index];
00084 
00085                                  int limit = pos + x.length;
00086                                  if (limit < output.length)
00087                                     {
00088                                     output[pos..limit] = x;
00089                                     pos = limit;
00090                                     continue;
00091                                     } 
00092                                  else
00093                                     error ("TextLayout : output buffer too small");
00094                                  }
00095                               else
00096                                  error ("TextLayout : invalid argument");
00097                               }
00098                            }
00099                         else
00100                            if (c == '%')
00101                               {
00102                               state = true;
00103                               continue;
00104                               }
00105                 
00106                         if (pos < output.length)
00107                            {
00108                            output[pos] = c;
00109                            ++pos;
00110                            }
00111                         else     
00112                            error ("TextLayout : output buffer too small");
00113                         }
00114 
00115                 return output [0..pos];
00116         }
00117 
00118         /**********************************************************************
00119               
00120         **********************************************************************/
00121 
00122         private static void error (char[] msg)
00123         {
00124                 throw new Exception (msg);
00125         }
00126 }
00127 
00128 
00129 alias TextLayoutTemplate!(char) TextLayout;
00130 

Generated on Sat Dec 24 17:28:34 2005 for Mango by  doxygen 1.4.0