Handling endianness in Tango's FileConduit

Nick Sabalausky a at a.a
Sat Oct 4 13:07:53 PDT 2008

"Steven Schveighoffer" <schveiguy at yahoo.com> wrote in message 
news:gc8ape$2i2r$1 at digitalmars.com...
> "Alexander Pánek" wrote
>> Nick Sabalausky wrote:
>>> I've been looking at the Tango docs, but I'm unclear on how IProtocol 
>>> works, specifically in relation to endianness.
>>> I see stuff about protocols and endianness in the docs, so I assume 
>>> Tango provides a way to say "The file I'm reading/writing through this 
>>> conduit uses X type of endieanness, so Tango, make sure that any shorts, 
>>> ints, etc, that I read/write are adjusted to match the system's native 
>>> endianness."  If so, how would I adjust the following to tell Tango 
>>> "This file uses little-endian"?:
>>> auto file = new FileConduit(infilename);
>>> scope(exit) file.close();
>>> auto input = new Reader(file.input);
>>> // Load data through 'input'
>> tango.io.protocol.EndianProtocol should do that:
>> auto file = new FileConduit(infilename);
>> scope(exit) file.close;
>> auto protocol = new EndianProtocol(file);
>> auto input = new DerivedReader(protocol);
>> Note: it seems like this is always assuming the other endianess as your 
>> current system runs on.
> Try PickleProtocol.  This always assumes the data being read/written is 
> big endian.  I don't think there's an equivalent one if the file is in 
> little endian form, but you could always just copy what PickleProtocol 
> does (make an alias depending on native endianness).  In fact, 
> PickleProtocol should probably be changed to allow you to specify the 
> endianness you want.
> -Steve

So, if I understand everything right:

- What EndianProtocol does: endianness is always switched.
- What PickleProtocol does: file's endianness is always assumed to be big 
endian and converted appropriately.

(I don't see any description of those in the docs, it should probably 
mention what they do and how they're used.)

If that's all what they do, then seems to me it should work more like this:

1. Rename "EndianProtocol" to "EndianSwitchProtocol" to make it's behavior 
more clear. Possibly make it internal to Tango.
2. Create a new "EndianProtocol" that takes an "Endianness" enum of 
"LittleEndian" or "BigEndian" (and maybe also "SwitchEndian" and 
"NativeEndian") as a ctor parameter, checks the native endianness, and 
appropriately adds or doesn't add the "EndianSwitchProtocol" (or just does 
the byte-order swapping itself instead of using "EndianSwitchProtocol").
3. Ditch the PickleProtocol (unless there's something else it also does that 
I'm not aware of.)

In any case, it looks like step 2 is something I'd currently have to 
implement myself. Does Tango provide a way to detect the system's 

More information about the Digitalmars-d-learn mailing list