Index: src/OFStream.h ================================================================== --- src/OFStream.h +++ src/OFStream.h @@ -115,10 +115,30 @@ * * \return A uint64_t from the stream in the native endianess */ - (uint64_t)readBigEndianInt64; +/** + * Reads a float from the stream which is encoded in big endian. + * + * WARNING: Only call this when you know that enough data is available! + * Otherwise you will get an exception! + * + * \return A float from the stream in the native endianess + */ +- (float)readBigEndianFloat; + +/** + * Reads a double from the stream which is encoded in big endian. + * + * WARNING: Only call this when you know that enough data is available! + * Otherwise you will get an exception! + * + * \return A double from the stream in the native endianess + */ +- (double)readBigEndianDouble; + /** * Reads a uint16_t from the stream which is encoded in little endian. * * WARNING: Only call this when you know that enough data is available! * Otherwise you will get an exception! @@ -145,10 +165,30 @@ * * \return A uint64_t from the stream in the native endianess */ - (uint64_t)readLittleEndianInt64; +/** + * Reads a float from the stream which is encoded in little endian. + * + * WARNING: Only call this when you know that enough data is available! + * Otherwise you will get an exception! + * + * \return A float from the stream in the native endianess + */ +- (float)readLittleEndianFloat; + +/** + * Reads a double from the stream which is encoded in little endian. + * + * WARNING: Only call this when you know that enough data is available! + * Otherwise you will get an exception! + * + * \return A double from the stream in the native endianess + */ +- (double)readLittleEndianDouble; + /** * Reads nItems items with an item size of 1 from the stream and returns them * in an OFDataArray. * * WARNING: Only call this when you know that enough data is available! @@ -270,10 +310,24 @@ * * \param int64 A uint64_t */ - (void)writeBigEndianInt64: (uint64_t)int64; +/** + * Writes a float into the stream, encoded in big endian. + * + * \param float_ A float + */ +- (void)writeBigEndianFloat: (float)float_; + +/** + * Writes a double into the stream, encoded in big endian. + * + * \param double_ A double + */ +- (void)writeBigEndianDouble: (double)double_; + /** * Writes a uint16_t into the stream, encoded in little endian. * * \param int16 A uint16_t */ @@ -291,10 +345,24 @@ * * \param int64 A uint64_t */ - (void)writeLittleEndianInt64: (uint64_t)int64; +/** + * Writes a float into the stream, encoded in little endian. + * + * \param float_ A float + */ +- (void)writeLittleEndianFloat: (float)float_; + +/** + * Writes a double into the stream, encoded in little endian. + * + * \param double_ A double + */ +- (void)writeLittleEndianDouble: (double)double_; + /** * Writes from an OFDataArray into the stream. * * \param dataArray The OFDataArray to write into the stream * \return The number of bytes written Index: src/OFStream.m ================================================================== --- src/OFStream.m +++ src/OFStream.m @@ -172,10 +172,30 @@ [self readExactlyNBytes: 8 intoBuffer: (char*)&ret]; return of_bswap64_if_le(ret); } + +- (float)readBigEndianFloat +{ + float ret; + + [self readExactlyNBytes: 4 + intoBuffer: (char*)&ret]; + + return of_bswap_float_if_le(ret); +} + +- (double)readBigEndianDouble +{ + double ret; + + [self readExactlyNBytes: 8 + intoBuffer: (char*)&ret]; + + return of_bswap_double_if_le(ret); +} - (uint16_t)readLittleEndianInt16 { uint16_t ret; @@ -202,10 +222,30 @@ [self readExactlyNBytes: 8 intoBuffer: (char*)&ret]; return of_bswap64_if_be(ret); } + +- (float)readLittleEndianFloat +{ + float ret; + + [self readExactlyNBytes: 4 + intoBuffer: (char*)&ret]; + + return of_bswap_float_if_be(ret); +} + +- (double)readLittleEndianDouble +{ + double ret; + + [self readExactlyNBytes: 8 + intoBuffer: (char*)&ret]; + + return of_bswap_double_if_be(ret); +} - (OFDataArray*)readDataArrayWithNItems: (size_t)nItems { return [self readDataArrayWithItemSize: 1 andNItems: nItems]; @@ -628,10 +668,26 @@ int64 = of_bswap64_if_le(int64); [self writeNBytes: 8 fromBuffer: (char*)&int64]; } + +- (void)writeBigEndianFloat: (float)float_ +{ + float_ = of_bswap_float_if_le(float_); + + [self writeNBytes: 4 + fromBuffer: (char*)&float_]; +} + +- (void)writeBigEndianDouble: (double)double_ +{ + double_ = of_bswap_double_if_le(double_); + + [self writeNBytes: 8 + fromBuffer: (char*)&double_]; +} - (void)writeLittleEndianInt16: (uint16_t)int16 { int16 = of_bswap16_if_be(int16); @@ -652,10 +708,26 @@ int64 = of_bswap64_if_be(int64); [self writeNBytes: 8 fromBuffer: (char*)&int64]; } + +- (void)writeLittleEndianFloat: (float)float_ +{ + float_ = of_bswap_float_if_be(float_); + + [self writeNBytes: 4 + fromBuffer: (char*)&float_]; +} + +- (void)writeLittleEndianDouble: (double)double_ +{ + double_ = of_bswap_double_if_be(double_); + + [self writeNBytes: 8 + fromBuffer: (char*)&double_]; +} - (size_t)writeDataArray: (OFDataArray*)dataArray { return [self writeNBytes: [dataArray count] * [dataArray itemSize] fromBuffer: [dataArray cArray]];