Reading UTF32 files

Hasan Aljudy hasan.aljudy at gmail.com
Thu Aug 3 21:14:08 PDT 2006



Tim Locke wrote:
> How do I read an UTF32 file? Stream only seems to support UTF8 with
> readLine and UTF16 with readLineW.
> 
> Thanks

I use mango to convert any file into UTF32

I haven't actually tested it very much .. but I think it should work:


---------
static import std.file;
import mango = mango.convert.UnicodeBom;
version( build ) //TEMP until build learns renamed import syntax
{
     pragma( include, mango.convert.UnicodeBom )
}

dchar[] readFile( char[] fileName )
{
     if( std.file.exists( fileName ) )
         return toUtf32( std.file.read( fileName ) );
     else
         throw new Exception("File: " ~ fileName ~ " doesn't exist");
}

private
{
     alias mango.UnicodeBomTemplate!(dchar) Utf32Decoder;

     ///read BOM and decode/convert to utf-32
     dchar[] toUtf32(void[] content)
     {
         auto decoder = new Utf32Decoder(mango.Unicode.Unknown);
         return decoder.decode(content);
     }
}
---------



More information about the Digitalmars-d-learn mailing list