Index: src/OFStream.h ================================================================== --- src/OFStream.h +++ src/OFStream.h @@ -135,10 +135,80 @@ * * \return A double from the stream in the native endianess */ - (double)readBigEndianDouble; +/** + * Reads nInt16s uint16_ts from the stream which are encoded in big endian. + * + * WARNING: Only call this when you know that enough data is available! + * Otherwise you will get an exception! + * + * \param nInt16s The number of uint16_ts to read + * \param buffer A buffer of sufficient size to store the specified number of + * uint16_ts + * \return The number of bytes read + */ +- (size_t)readNBigEndianInt16s: (size_t)nInt16s + intoBuffer: (uint16_t*)buffer; + +/** + * Reads nInt32s uint32_ts from the stream which are encoded in big endian. + * + * WARNING: Only call this when you know that enough data is available! + * Otherwise you will get an exception! + * + * \param nInt32s The number of uint32_ts to read + * \param buffer A buffer of sufficient size to store the specified number of + * uint32_ts + * \return The number of bytes read + */ +- (size_t)readNBigEndianInt32s: (size_t)nInt32s + intoBuffer: (uint32_t*)buffer; + +/** + * Reads nInt64s uint64_ts from the stream which are encoded in big endian. + * + * WARNING: Only call this when you know that enough data is available! + * Otherwise you will get an exception! + * + * \param nInt64s The number of uint64_ts to read + * \param buffer A buffer of sufficient size to store the specified number of + * uint64_ts + * \return The number of bytes read + */ +- (size_t)readNBigEndianInt64s: (size_t)nInt64s + intoBuffer: (uint64_t*)buffer; + +/** + * Reads nFloats floats from the stream which are encoded in big endian. + * + * WARNING: Only call this when you know that enough data is available! + * Otherwise you will get an exception! + * + * \param nFloatss The number of floats to read + * \param buffer A buffer of sufficient size to store the specified number of + * floats + * \return The number of bytes read + */ +- (size_t)readNBigEndianFloats: (size_t)nFloats + intoBuffer: (float*)buffer; + +/** + * Reads nDoubles doubles from the stream which are encoded in big endian. + * + * WARNING: Only call this when you know that enough data is available! + * Otherwise you will get an exception! + * + * \param nDoubles The number of doubles to read + * \param buffer A buffer of sufficient size to store the specified number of + * doubles + * \return The number of bytes read + */ +- (size_t)readNBigEndianDoubles: (size_t)nDoubles + intoBuffer: (double*)buffer; + /** * 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! @@ -185,10 +255,80 @@ * * \return A double from the stream in the native endianess */ - (double)readLittleEndianDouble; +/** + * Reads nInt16s uint16_ts from the stream which are encoded in little endian. + * + * WARNING: Only call this when you know that enough data is available! + * Otherwise you will get an exception! + * + * \param nInt16s The number of uint16_ts to read + * \param buffer A buffer of sufficient size to store the specified number of + * uint16_ts + * \return The number of bytes read + */ +- (size_t)readNLittleEndianInt16s: (size_t)nInt16s + intoBuffer: (uint16_t*)buffer; + +/** + * Reads nInt32s uint32_ts from the stream which are encoded in little endian. + * + * WARNING: Only call this when you know that enough data is available! + * Otherwise you will get an exception! + * + * \param nInt32s The number of uint32_ts to read + * \param buffer A buffer of sufficient size to store the specified number of + * uint32_ts + * \return The number of bytes read + */ +- (size_t)readNLittleEndianInt32s: (size_t)nInt32s + intoBuffer: (uint32_t*)buffer; + +/** + * Reads nInt64s uint64_ts from the stream which are encoded in little endian. + * + * WARNING: Only call this when you know that enough data is available! + * Otherwise you will get an exception! + * + * \param nInt64s The number of uint64_ts to read + * \param buffer A buffer of sufficient size to store the specified number of + * uint64_ts + * \return The number of bytes read + */ +- (size_t)readNLittleEndianInt64s: (size_t)nInt64s + intoBuffer: (uint64_t*)buffer; + +/** + * Reads nFloats floats from the stream which are encoded in little endian. + * + * WARNING: Only call this when you know that enough data is available! + * Otherwise you will get an exception! + * + * \param nFloats The number of floats to read + * \param buffer A buffer of sufficient size to store the specified number of + * floats + * \return The number of bytes read + */ +- (size_t)readNLittleEndianFloats: (size_t)nFloats + intoBuffer: (float*)buffer; + +/** + * Reads nDoubles doubles from the stream which are encoded in little endian. + * + * WARNING: Only call this when you know that enough data is available! + * Otherwise you will get an exception! + * + * \param nDoubles The number of doubles to read + * \param buffer A buffer of sufficient size to store the specified number of + * doubles + * \return The number of bytes read + */ +- (size_t)readNLittleEndianDoubles: (size_t)nDoubles + intoBuffer: (double*)buffer; + /** * 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! @@ -323,10 +463,65 @@ * * \param double_ A double */ - (void)writeBigEndianDouble: (double)double_; +/** + * Writes nInt16s uint16_ts into the stream, encoded in big endian. + * + * \param nInt16 The number of uint16_ts to write + * \param buffer The buffer from which the data is written to the stream after + * it has been byte swapped if necessary + * \return The number of bytes written to the stream + */ +- (size_t)writeNBigEndianInt16s: (size_t)nInt16s + fromBuffer: (const uint16_t*)buffer; + +/** + * Writes nInt32s uint32_ts into the stream, encoded in big endian. + * + * \param nInt32 The number of uint32_ts to write + * \param buffer The buffer from which the data is written to the stream after + * it has been byte swapped if necessary + * \return The number of bytes written to the stream + */ +- (size_t)writeNBigEndianInt32s: (size_t)nInt32s + fromBuffer: (const uint32_t*)buffer; + +/** + * Writes nInt64s uint64_ts into the stream, encoded in big endian. + * + * \param nInt64 The number of uint64_ts to write + * \param buffer The buffer from which the data is written to the stream after + * it has been byte swapped if necessary + * \return The number of bytes written to the stream + */ +- (size_t)writeNBigEndianInt64s: (size_t)nInt64s + fromBuffer: (const uint64_t*)buffer; + +/** + * Writes nFloats floats into the stream, encoded in big endian. + * + * \param nFloats The number of floats to write + * \param buffer The buffer from which the data is written to the stream after + * it has been byte swapped if necessary + * \return The number of bytes written to the stream + */ +- (size_t)writeNBigEndianFloats: (size_t)nFloats + fromBuffer: (const float*)buffer; + +/** + * Writes nDoubles doubles into the stream, encoded in big endian. + * + * \param nDoubles The number of doubles to write + * \param buffer The buffer from which the data is written to the stream after + * it has been byte swapped if necessary + * \return The number of bytes written to the stream + */ +- (size_t)writeNBigEndianDoubles: (size_t)nDoubles + fromBuffer: (const double*)buffer; + /** * Writes a uint16_t into the stream, encoded in little endian. * * \param int16 A uint16_t */ @@ -358,10 +553,65 @@ * * \param double_ A double */ - (void)writeLittleEndianDouble: (double)double_; +/** + * Writes nInt16s uint16_ts into the stream, encoded in little endian. + * + * \param nInt16 The number of uint16_ts to write + * \param buffer The buffer from which the data is written to the stream after + * it has been byte swapped if necessary + * \return The number of bytes written to the stream + */ +- (size_t)writeNLittleEndianInt16s: (size_t)nInt16s + fromBuffer: (const uint16_t*)buffer; + +/** + * Writes nInt32s uint32_ts into the stream, encoded in little endian. + * + * \param nInt32 The number of uint32_ts to write + * \param buffer The buffer from which the data is written to the stream after + * it has been byte swapped if necessary + * \return The number of bytes written to the stream + */ +- (size_t)writeNLittleEndianInt32s: (size_t)nInt32s + fromBuffer: (const uint32_t*)buffer; + +/** + * Writes nInt64s uint64_ts into the stream, encoded in little endian. + * + * \param nInt64 The number of uint64_ts to write + * \param buffer The buffer from which the data is written to the stream after + * it has been byte swapped if necessary + * \return The number of bytes written to the stream + */ +- (size_t)writeNLittleEndianInt64s: (size_t)nInt64s + fromBuffer: (const uint64_t*)buffer; + +/** + * Writes nFloats floats into the stream, encoded in little endian. + * + * \param nFloats The number of floats to write + * \param buffer The buffer from which the data is written to the stream after + * it has been byte swapped if necessary + * \return The number of bytes written to the stream + */ +- (size_t)writeNLittleEndianFloats: (size_t)nFloats + fromBuffer: (const float*)buffer; + +/** + * Writes nDoubles doubles into the stream, encoded in little endian. + * + * \param nDoubles The number of doubles to write + * \param buffer The buffer from which the data is written to the stream after + * it has been byte swapped if necessary + * \return The number of bytes written to the stream + */ +- (size_t)writeNLittleEndianDoubles: (size_t)nDoubles + fromBuffer: (const double*)buffer; + /** * 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 @@ -192,10 +192,100 @@ [self readExactlyNBytes: 8 intoBuffer: (char*)&ret]; return of_bswap_double_if_le(ret); } + +- (size_t)readNBigEndianInt16s: (size_t)nInt16s + intoBuffer: (uint16_t*)buffer +{ + size_t size = nInt16s * sizeof(uint16_t); + + [self readExactlyNBytes: size + intoBuffer: buffer]; + +#ifndef OF_BIG_ENDIAN + size_t i; + + for (i = 0; i < nInt16s; i++) + buffer[i] = of_bswap16(buffer[i]); +#endif + + return size; +} + +- (size_t)readNBigEndianInt32s: (size_t)nInt32s + intoBuffer: (uint32_t*)buffer +{ + size_t size = nInt32s * sizeof(uint32_t); + + [self readExactlyNBytes: size + intoBuffer: buffer]; + +#ifndef OF_BIG_ENDIAN + size_t i; + + for (i = 0; i < nInt32s; i++) + buffer[i] = of_bswap32(buffer[i]); +#endif + + return size; +} + +- (size_t)readNBigEndianInt64s: (size_t)nInt64s + intoBuffer: (uint64_t*)buffer +{ + size_t size = nInt64s * sizeof(uint64_t); + + [self readExactlyNBytes: size + intoBuffer: buffer]; + +#ifndef OF_BIG_ENDIAN + size_t i; + + for (i = 0; i < nInt64s; i++) + buffer[i] = of_bswap64(buffer[i]); +#endif + + return size; +} + +- (size_t)readNBigEndianFloats: (size_t)nFloats + intoBuffer: (float*)buffer +{ + size_t size = nFloats * sizeof(float); + + [self readExactlyNBytes: size + intoBuffer: buffer]; + +#ifndef OF_FLOAT_BIG_ENDIAN + size_t i; + + for (i = 0; i < nFloats; i++) + buffer[i] = of_bswap_float(buffer[i]); +#endif + + return size; +} + +- (size_t)readNBigEndianDoubles: (size_t)nDoubles + intoBuffer: (double*)buffer +{ + size_t size = nDoubles * sizeof(double); + + [self readExactlyNBytes: size + intoBuffer: buffer]; + +#ifndef OF_FLOAT_BIG_ENDIAN + size_t i; + + for (i = 0; i < nDoubles; i++) + buffer[i] = of_bswap_double(buffer[i]); +#endif + + return size; +} - (uint16_t)readLittleEndianInt16 { uint16_t ret; @@ -242,10 +332,100 @@ [self readExactlyNBytes: 8 intoBuffer: (char*)&ret]; return of_bswap_double_if_be(ret); } + +- (size_t)readNLittleEndianInt16s: (size_t)nInt16s + intoBuffer: (uint16_t*)buffer +{ + size_t size = nInt16s * sizeof(uint16_t); + + [self readExactlyNBytes: size + intoBuffer: buffer]; + +#ifdef OF_BIG_ENDIAN + size_t i; + + for (i = 0; i < nInt16s; i++) + buffer[i] = of_bswap16(buffer[i]); +#endif + + return size; +} + +- (size_t)readNLittleEndianInt32s: (size_t)nInt32s + intoBuffer: (uint32_t*)buffer +{ + size_t size = nInt32s * sizeof(uint32_t); + + [self readExactlyNBytes: size + intoBuffer: buffer]; + +#ifdef OF_BIG_ENDIAN + size_t i; + + for (i = 0; i < nInt32s; i++) + buffer[i] = of_bswap32(buffer[i]); +#endif + + return size; +} + +- (size_t)readNLittleEndianInt64s: (size_t)nInt64s + intoBuffer: (uint64_t*)buffer +{ + size_t size = nInt64s * sizeof(uint64_t); + + [self readExactlyNBytes: size + intoBuffer: buffer]; + +#ifdef OF_BIG_ENDIAN + size_t i; + + for (i = 0; i < nInt64s; i++) + buffer[i] = of_bswap64(buffer[i]); +#endif + + return size; +} + +- (size_t)readNLittleEndianFloats: (size_t)nFloats + intoBuffer: (float*)buffer +{ + size_t size = nFloats * sizeof(float); + + [self readExactlyNBytes: size + intoBuffer: buffer]; + +#ifdef OF_FLOAT_BIG_ENDIAN + size_t i; + + for (i = 0; i < nFloats; i++) + buffer[i] = of_bswap_float(buffer[i]); +#endif + + return size; +} + +- (size_t)readNLittleEndianDoubles: (size_t)nDoubles + intoBuffer: (double*)buffer +{ + size_t size = nDoubles * sizeof(double); + + [self readExactlyNBytes: size + intoBuffer: buffer]; + +#ifdef OF_FLOAT_BIG_ENDIAN + size_t i; + + for (i = 0; i < nDoubles; i++) + buffer[i] = of_bswap_double(buffer[i]); +#endif + + return size; +} - (OFDataArray*)readDataArrayWithNItems: (size_t)nItems { return [self readDataArrayWithItemSize: 1 andNItems: nItems]; @@ -682,10 +862,160 @@ double_ = of_bswap_double_if_le(double_); [self writeNBytes: 8 fromBuffer: (char*)&double_]; } + +- (size_t)writeNBigEndianInt16s: (size_t)nInt16s + fromBuffer: (const uint16_t*)buffer +{ + size_t size = nInt16s * sizeof(uint16_t); + +#ifdef OF_BIG_ENDIAN + [self writeNBytes: size + fromBuffer: buffer]; +#else + uint16_t *tmp; + + tmp = [self allocMemoryForNItems: nInt16s + withSize: sizeof(uint16_t)]; + + @try { + size_t i; + + for (i = 0; i < nInt16s; i++) + tmp[i] = of_bswap16(buffer[i]); + + [self writeNBytes: size + fromBuffer: tmp]; + } @finally { + [self freeMemory: tmp]; + } +#endif + + return size; +} + +- (size_t)writeNBigEndianInt32s: (size_t)nInt32s + fromBuffer: (const uint32_t*)buffer +{ + size_t size = nInt32s * sizeof(uint32_t); + +#ifdef OF_BIG_ENDIAN + [self writeNBytes: size + fromBuffer: buffer]; +#else + uint32_t *tmp; + + tmp = [self allocMemoryForNItems: nInt32s + withSize: sizeof(uint32_t)]; + + @try { + size_t i; + + for (i = 0; i < nInt32s; i++) + tmp[i] = of_bswap32(buffer[i]); + + [self writeNBytes: size + fromBuffer: tmp]; + } @finally { + [self freeMemory: tmp]; + } +#endif + + return size; +} + +- (size_t)writeNBigEndianInt64s: (size_t)nInt64s + fromBuffer: (const uint64_t*)buffer +{ + size_t size = nInt64s * sizeof(uint64_t); + +#ifdef OF_BIG_ENDIAN + [self writeNBytes: size + fromBuffer: buffer]; +#else + uint64_t *tmp; + + tmp = [self allocMemoryForNItems: nInt64s + withSize: sizeof(uint64_t)]; + + @try { + size_t i; + + for (i = 0; i < nInt64s; i++) + tmp[i] = of_bswap64(buffer[i]); + + [self writeNBytes: size + fromBuffer: tmp]; + } @finally { + [self freeMemory: tmp]; + } +#endif + + return size; +} + +- (size_t)writeNBigEndianFloats: (size_t)nFloats + fromBuffer: (const float*)buffer +{ + size_t size = nFloats * sizeof(float); + +#ifdef OF_FLOAT_BIG_ENDIAN + [self writeNBytes: size + fromBuffer: buffer]; +#else + float *tmp; + + tmp = [self allocMemoryForNItems: nFloats + withSize: sizeof(float)]; + + @try { + size_t i; + + for (i = 0; i < nFloats; i++) + tmp[i] = of_bswap_float(buffer[i]); + + [self writeNBytes: size + fromBuffer: tmp]; + } @finally { + [self freeMemory: tmp]; + } +#endif + + return size; +} + +- (size_t)writeNBigEndianDoubles: (size_t)nDoubles + fromBuffer: (const double*)buffer +{ + size_t size = nDoubles * sizeof(double); + +#ifdef OF_FLOAT_BIG_ENDIAN + [self writeNBytes: size + fromBuffer: buffer]; +#else + double *tmp; + + tmp = [self allocMemoryForNItems: nDoubles + withSize: sizeof(double)]; + + @try { + size_t i; + + for (i = 0; i < nDoubles; i++) + tmp[i] = of_bswap_double(buffer[i]); + + [self writeNBytes: size + fromBuffer: tmp]; + } @finally { + [self freeMemory: tmp]; + } +#endif + + return size; +} - (void)writeLittleEndianInt16: (uint16_t)int16 { int16 = of_bswap16_if_be(int16); @@ -722,10 +1052,160 @@ double_ = of_bswap_double_if_be(double_); [self writeNBytes: 8 fromBuffer: (char*)&double_]; } + +- (size_t)writeNLittleEndianInt16s: (size_t)nInt16s + fromBuffer: (const uint16_t*)buffer +{ + size_t size = nInt16s * sizeof(uint16_t); + +#ifndef OF_BIG_ENDIAN + [self writeNBytes: size + fromBuffer: buffer]; +#else + uint16_t *tmp; + + tmp = [self allocMemoryForNItems: nInt16s + withSize: sizeof(uint16_t)]; + + @try { + size_t i; + + for (i = 0; i < nInt16s; i++) + tmp[i] = of_bswap16(buffer[i]); + + [self writeNBytes: size + fromBuffer: tmp]; + } @finally { + [self freeMemory: tmp]; + } +#endif + + return size; +} + +- (size_t)writeNLittleEndianInt32s: (size_t)nInt32s + fromBuffer: (const uint32_t*)buffer +{ + size_t size = nInt32s * sizeof(uint32_t); + +#ifndef OF_BIG_ENDIAN + [self writeNBytes: size + fromBuffer: buffer]; +#else + uint32_t *tmp; + + tmp = [self allocMemoryForNItems: nInt32s + withSize: sizeof(uint32_t)]; + + @try { + size_t i; + + for (i = 0; i < nInt32s; i++) + tmp[i] = of_bswap32(buffer[i]); + + [self writeNBytes: size + fromBuffer: tmp]; + } @finally { + [self freeMemory: tmp]; + } +#endif + + return size; +} + +- (size_t)writeNLittleEndianInt64s: (size_t)nInt64s + fromBuffer: (const uint64_t*)buffer +{ + size_t size = nInt64s * sizeof(uint64_t); + +#ifndef OF_BIG_ENDIAN + [self writeNBytes: size + fromBuffer: buffer]; +#else + uint64_t *tmp; + + tmp = [self allocMemoryForNItems: nInt64s + withSize: sizeof(uint64_t)]; + + @try { + size_t i; + + for (i = 0; i < nInt64s; i++) + tmp[i] = of_bswap64(buffer[i]); + + [self writeNBytes: size + fromBuffer: tmp]; + } @finally { + [self freeMemory: tmp]; + } +#endif + + return size; +} + +- (size_t)writeNLittleEndianFloats: (size_t)nFloats + fromBuffer: (const float*)buffer +{ + size_t size = nFloats * sizeof(float); + +#ifndef OF_FLOAT_BIG_ENDIAN + [self writeNBytes: size + fromBuffer: buffer]; +#else + float *tmp; + + tmp = [self allocMemoryForNItems: nFloats + withSize: sizeof(float)]; + + @try { + size_t i; + + for (i = 0; i < nFloats; i++) + tmp[i] = of_bswap_float(buffer[i]); + + [self writeNBytes: size + fromBuffer: tmp]; + } @finally { + [self freeMemory: tmp]; + } +#endif + + return size; +} + +- (size_t)writeNLittleEndianDoubles: (size_t)nDoubles + fromBuffer: (const double*)buffer +{ + size_t size = nDoubles * sizeof(double); + +#ifndef OF_FLOAT_BIG_ENDIAN + [self writeNBytes: size + fromBuffer: buffer]; +#else + double *tmp; + + tmp = [self allocMemoryForNItems: nDoubles + withSize: sizeof(double)]; + + @try { + size_t i; + + for (i = 0; i < nDoubles; i++) + tmp[i] = of_bswap_double(buffer[i]); + + [self writeNBytes: size + fromBuffer: tmp]; + } @finally { + [self freeMemory: tmp]; + } +#endif + + return size; +} - (size_t)writeDataArray: (OFDataArray*)dataArray { size_t length = [dataArray count] * [dataArray itemSize];