Reading UTF32 files

kris foo at bar.com
Thu Aug 3 21:42:07 PDT 2006


It's perhaps easier to use UnicodeFile instead:

# import mango.io.UnicodeFile;
#
# auto file = new UnicodeFileT!(dchar)("myfile", Unicode.Unknown);
# auto content = file.read;


Please note that Mango leverages a different IO model than Phobos, so 
you'll have to compile this along with a few other mango.io modules.

Mango typically requires the use of Build to pull in relevant modules, 
because the combination of D, libraries, and templates just doesn't work 
reliably at this time. If the compiler front-end were to handle 
recursive imports natively (like a very simple Build), it would be 
great! The changes to do so (for DMD) are minimal ;)



Hasan Aljudy wrote:
> 
> 
> 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