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

UnicodeFilter.d

Go to the documentation of this file.
00001 /*******************************************************************************
00002 
00003         @file UnicodeFilter.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; November 2005
00034 
00035         @author         Kris
00036 
00037 
00038 *******************************************************************************/
00039 
00040 module mango.io.UnicodeFilter;
00041 
00042 private import  mango.io.Buffer,
00043                 mango.io.Conduit;
00044 
00045 private import  mango.convert.Unicode;
00046 
00047 class UnicodeFilter(Src, Dst) : ConduitFilter
00048 {
00049         private Buffer buffer;
00050 
00051         this (int size = 1024*8) 
00052         {
00053                 buffer = new Buffer (size);
00054         }
00055 
00056         static if (!is (Src == Dst))
00057         {
00058         uint reader (void[] dst)
00059         {
00060                 uint ate;
00061                 uint r = buffer.readable;
00062 
00063                 // put some new data into our buffer?
00064                 while (r == 0)
00065                       {
00066                       // clear old content
00067                       buffer.clear ();
00068 
00069                       // read next chunk
00070                       if (buffer.write (&next.reader) == Conduit.Eof)
00071                           return Conduit.Eof;
00072 
00073                       // should have some content now
00074                       r = buffer.readable;
00075                       }
00076 
00077 
00078                 // clip the input and output appropriately
00079                 dst = dst [0 .. (length & ~(Dst.sizeof-1))];
00080                 Src[] src = cast(Src[]) buffer.toString [0 .. (r & ~(Src.sizeof-1))];
00081 
00082                 void[] result;
00083 
00084                 // convert from local buffer into dst
00085                 static if (is (Dst == dchar))
00086                            result = Unicode.toUtf32 (src, cast(Dst[]) dst, &ate);
00087 
00088                 // convert from local buffer into dst
00089                 static if (is (Dst == wchar))
00090                            result = Unicode.toUtf16 (src, cast(Dst[]) dst, &ate);
00091 
00092                 // convert from local buffer into dst
00093                 static if (is (Dst == char))
00094                            result = Unicode.toUtf8 (src, cast(Dst[]) dst, &ate);
00095 
00096                 // throw processed stuff away
00097                 buffer.get (ate, true);
00098                 return result.length;
00099         }
00100 
00101         uint writer (void[] src)
00102         { 
00103                 return next.writer (src);
00104         }
00105         }
00106 }
00107 
00108 alias UnicodeFilter!(char, dchar) UnicodeFilter8to32;

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