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

TextReader.d

Go to the documentation of this file.
00001 /*******************************************************************************
00002 
00003         @file TextReader.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.TextReader;
00040 
00041 private import  mango.io.Token,
00042                 mango.io.Tokenizer,
00043                 mango.io.Exception,
00044                 mango.io.AbstractReader;
00045 
00046 /*******************************************************************************
00047 
00048         Grok readable input from a stream. All input is tokenized from the
00049         associated buffer, and converted as necessary into the destination 
00050         location. A SpaceTokenizer is used by default, but you can choose
00051         an alternative (such as a comma-delimiting tokenizer).
00052 
00053 *******************************************************************************/
00054 
00055 class TextReader : AbstractReader
00056 {
00057         alias opShr get;
00058         alias AbstractReader.opShr opShr;
00059 
00060         // an individual token
00061         private Token token;
00062 
00063         // internal tokenizer instance
00064         private ITokenizer tokenizer;
00065 
00066         /***********************************************************************
00067         
00068                 Construct a TextReader on the provided buffer, using the
00069                 specified Tokenizer instead of the default one.
00070 
00071         ***********************************************************************/
00072 
00073         this (IBuffer buffer, ITokenizer tokenizer = Tokenizers.line)
00074         {
00075                 super (buffer);
00076                 this.token = new Token;
00077                 this.tokenizer = tokenizer;
00078      
00079                 decode.int1 = &int1;
00080                 decode.int8 = 
00081                 decode.int8u = &int8;
00082                 decode.int16 =
00083                 decode.int16u = &int16;
00084                 decode.int32 = 
00085                 decode.int32u = &int32;
00086                 decode.int64 = 
00087                 decode.int64u = &int64;
00088 
00089                 decode.float32 = &float32;
00090                 decode.float64 = &float64;
00091                 decode.float80 = &float80;
00092         }
00093         
00094         /***********************************************************************
00095                 
00096                 Construct a TextReader upon the buffer associated with the
00097                 given conduit.
00098 
00099         ***********************************************************************/
00100 
00101         this (IConduit conduit, ITokenizer tokenizer = Tokenizers.line)
00102         {
00103                 this (conduit.createBuffer, tokenizer);
00104         }
00105 
00106         /***********************************************************************
00107         
00108                 Internal method to isolate the next token.
00109 
00110         ***********************************************************************/
00111 
00112         private final Token next ()
00113         {
00114                 if (tokenizer.next (buffer, token) && token.getLength > 0)
00115                     return token;    
00116                 throw new TokenException ("unexpected end of input");
00117         }
00118 
00119         /***********************************************************************
00120         
00121         ***********************************************************************/
00122 
00123         uint int1 (void* src, uint count)
00124         {     
00125                 for (uint i=count; i; --i)
00126                     {
00127                     * cast(bool*) src = (next.toString == "true");
00128                     ++src;
00129                     }
00130                 return count;
00131         }
00132 
00133         /***********************************************************************
00134         
00135         ***********************************************************************/
00136 
00137         uint int8 (void* src, uint count)
00138         {     
00139                 for (uint i=count; i; i -= byte.sizeof)
00140                     {
00141                     * cast(byte*) src = next.toInt();
00142                     src += byte.sizeof;
00143                     }
00144                 return count;
00145         }
00146 
00147         /***********************************************************************
00148         
00149         ***********************************************************************/
00150 
00151         uint int16 (void* src, uint count)
00152         {     
00153                 for (uint i=count; i; i -= short.sizeof)
00154                     {
00155                     * cast(short*) src = next.toInt();
00156                     src += short.sizeof;
00157                     }
00158                 return count;
00159         }
00160 
00161         /***********************************************************************
00162         
00163         ***********************************************************************/
00164 
00165         uint int32 (void* src, uint count)
00166         {     
00167                 for (uint i=count; i; i -= int.sizeof)
00168                     {
00169                     * cast(int*) src = next.toInt();
00170                     src += int.sizeof;
00171                     }
00172                 return count;
00173         }
00174 
00175         /***********************************************************************
00176         
00177         ***********************************************************************/
00178 
00179         uint int64 (void* src, uint count)
00180         {     
00181                 for (uint i=count; i; i -= long.sizeof)
00182                     {
00183                     * cast(long*) src = next.toLong();
00184                     src += long.sizeof;
00185                     }
00186                 return count;
00187         }
00188 
00189         /***********************************************************************
00190         
00191         ***********************************************************************/
00192 
00193         uint float32 (void* src, uint count)
00194         {     
00195                 for (uint i=count; i; i -= float.sizeof)
00196                     {
00197                     * cast(float*) src = next.toReal();
00198                     src += float.sizeof;
00199                     }
00200                 return count;
00201         }
00202 
00203         /***********************************************************************
00204         
00205         ***********************************************************************/
00206 
00207         uint float64 (void* src, uint count)
00208         {     
00209                 for (uint i=count; i; i -= double.sizeof)
00210                     {
00211                     * cast(double*) src = next.toReal();
00212                     src += double.sizeof;
00213                     }
00214                 return count;
00215         }
00216 
00217         /***********************************************************************
00218         
00219         ***********************************************************************/
00220 
00221         uint float80 (void* src, uint count)
00222         {     
00223                 for (uint i=count; i; i -= real.sizeof)
00224                     {
00225                     * cast(real*) src = next.toReal();
00226                     src += real.sizeof;
00227                     }
00228                 return count;
00229         }
00230 
00231         /***********************************************************************
00232         
00233         ***********************************************************************/
00234 
00235         override IReader opShr (inout char[] x) 
00236         { 
00237                 // dup the input string, to avoid surprises
00238                 x = next.toString (false);
00239                 return this;
00240         }
00241 
00242         /***********************************************************************
00243         
00244                 @todo - Tokenizer needs to handle wchar[] before this will
00245                         operate correctly
00246 
00247         ***********************************************************************/
00248 
00249         override IReader opShr (inout wchar[] x) 
00250         { 
00251                 assert (0);
00252                 return this;
00253         }
00254 
00255         /***********************************************************************
00256         
00257                 @todo - Tokenizer needs to handle dchar[] before this will
00258                         operate correctly
00259 
00260         ***********************************************************************/
00261 
00262         override IReader opShr (inout dchar[] x) 
00263         { 
00264                 assert (0);
00265                 return this;
00266         }
00267 }

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