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

Copy of UtfCodec.d

Go to the documentation of this file.
00001 /*******************************************************************************
00002 
00003         @file UtfCodec.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, Feb 2005      
00034         @author         Kris
00035 
00036 
00037 *******************************************************************************/
00038 
00039 module mango.io.UtfCodec;
00040 
00041 private import  mango.io.Buffer;
00042 
00043 private import  mango.convert.Utf;
00044 private import  mango.convert.Type;
00045 
00046 
00047 /*******************************************************************************
00048 
00049 *******************************************************************************/
00050 
00051 class UtfCodec(T) : RawCodec
00052 {
00053         //UtfCodec1!(T) unicode;
00054         //From!(T) from;
00055         //Into!(T) into;
00056 
00057         this (Buffer buffer)
00058         {
00059                 super (buffer);
00060                 //from = new From!(T);
00061                 //into = new Into!(T);
00062         }
00063 
00064         override uint type ()
00065         {
00066                 //return unicode.type();
00067         }
00068 
00069 
00070         private uint into (void[] dst, void* src, uint bytes, uint type, uint *ate)
00071         {
00072                 
00073                 switch (type)
00074                        {
00075                        static if (is (T == char))
00076                                  {
00077                                  case Type.Utf8:
00078                                       return cast(char[]) x;
00079                                  case Type.Utf16:
00080                                       return Utf.toUtf8 (cast(wchar[]) x, cast(char[]) tmp);
00081                                  case Type.Utf32:
00082                                       return Utf.toUtf8 (cast(dchar[]) x, cast(char[]) tmp);
00083                                  }
00084 
00085                        static if (is (T == wchar))
00086                                  {
00087                                  case Type.Utf8:
00088                                       return Utf.toUtf16 (cast(char[]) src[0..bytes], cast(wchar[]) dst, ate).length;
00089                                  case Type.Utf16:
00090                                       return super.importer (src, bytes, type);
00091                                  case Type.Utf32:
00092                                       return Utf.toUtf16 (cast(dchar[]) src[0..bytes], cast(wchar[]) dst, ate).length;
00093                                  }
00094 
00095                        static if (is (T == dchar))
00096                                  {
00097                                  case Type.Utf8:
00098                                       return update (Utf.toUtf32 (cast(char[]) x, cast(dchar[]) tmp));
00099                                  case Type.Utf16:
00100                                       return update (Utf.toUtf32 (cast(wchar[]) x, cast(dchar[]) tmp));
00101                                  case Type.Utf32:
00102                                       return cast(dchar[]) x;
00103                                  }
00104                                  default:
00105                                       break;
00106                         }
00107         }
00108 
00109 
00110         private override uint importer (void* src, uint bytes, uint type)
00111         {
00112                 uint ate;
00113                 uint eaten;
00114 
00115                 uint convert (void[] dst)
00116                 {
00117                         return into (dst, src, bytes, type, &ate);
00118                 }
00119 
00120 
00121                 buffer.write (&convert);
00122                 while ((eaten += ate) < bytes)
00123                       {
00124                       buffer.drain ();
00125                       buffer.write (&convert);
00126                       }
00127                 return eaten;
00128         }
00129 
00130 
00131         private uint from (void[] src, void* dst, uint bytes, uint type, uint *ate)
00132         {
00133                 switch (type)
00134                        {
00135                        static if (is (T == char))
00136                                  {
00137                                  case Type.Utf8:
00138                                       return src;
00139                                  case Type.Utf16:
00140                                       return update (Utf.toUtf16 (cast(char[]) src, cast(wchar[]) dst, ate));
00141                                  case Type.Utf32:
00142                                       return update (Utf.toUtf32 (cast(char[]) src, cast(dchar[]) dst, ate));
00143                        }
00144 
00145                        static if (is (T == wchar))
00146                                  {
00147                                  case Type.Utf8:
00148                                       return Utf.toUtf8 (cast(wchar[]) src, cast(char[]) dst[0..bytes], ate).length;
00149                                  case Type.Utf16:
00150                                        return super.exporter (dst, bytes, type);
00151                                  case Type.Utf32:
00152                                       return Utf.toUtf32 (cast(wchar[]) src, cast(dchar[]) dst[0..bytes], ate).length;
00153                                  }
00154 
00155                        static if (is (T == dchar))
00156                                  {
00157                                  case Type.Utf8:
00158                                       return update (Utf.toUtf8 (cast(dchar[]) src, cast(char[]) dst, ate));
00159                                  case Type.Utf16:
00160                                       return update (Utf.toUtf16 (cast(dchar[]) src, cast(wchar[]) dst, ate));
00161                                  case Type.Utf32:
00162                                       return src;
00163                                  }
00164                                  default:
00165                                       break;
00166                        }
00167         }
00168 
00169         private override uint exporter (void* dst, uint bytes, uint type)
00170         {
00171                 int written;
00172 
00173                 uint convert (void[] src)
00174                 {
00175                         uint ate;
00176                         written += from (src, dst, bytes, type, &ate);
00177                         return ate;
00178                 }
00179 
00180 
00181                 buffer.read (&convert);
00182                 while (written < bytes)
00183                       {
00184                       buffer.fill (buffer.getConduit);
00185                       buffer.read (&convert);
00186                       }
00187                 return written;
00188         }
00189 }

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