X-Git-Url: http://git.kpe.io/?p=ctsim.git;a=blobdiff_plain;f=libctsupport%2Ffnetorderstream.cpp;fp=libctsupport%2Ffnetorderstream.cpp;h=7fff7a3623966e9a32553e0cc776b7bf409607ff;hp=0000000000000000000000000000000000000000;hb=44ba9ce559d2d52cbd7bbea6bcd76242840fd3eb;hpb=595e63c804284d460ce4d032c3848b75bc57186d diff --git a/libctsupport/fnetorderstream.cpp b/libctsupport/fnetorderstream.cpp new file mode 100644 index 0000000..7fff7a3 --- /dev/null +++ b/libctsupport/fnetorderstream.cpp @@ -0,0 +1,180 @@ +#if HAVE_CONFIG_H +#include "config.h" +#endif + +#if HAVE_UNISTD_H +#include +#endif + +#include "ctsupport.h" +#include "fnetorderstream.h" + + +void +ConvertNetworkOrder (void* buffer, size_t bytes) +{ +#ifndef WORDS_BIGENDIAN + if (bytes < 2) + return; + + char* start = static_cast(buffer); + char* end = start + bytes - 1; // last byte + size_t nSwap = bytes / 2; + + while (nSwap-- > 0) { + unsigned char c = *start; + *start++ = *end; + *end-- = c; + } +#endif +} + +void +ConvertReverseNetworkOrder (void* buffer, size_t bytes) +{ +#ifdef WORDS_BIGENDIAN + if (bytes < 2) + return; + + char* start = static_cast(buffer); + char* end = start + bytes - 1; // last byte + size_t nSwap = bytes / 2; + + while (nSwap-- > 0) { + unsigned char c = *start; + *start++ = *end; + *end-- = c; + } +#endif +} + +fnetorderstream& fnetorderstream::writeInt16 (kuint16 n) { +#ifndef WORDS_BIGENDIAN + SwapBytes2 (&n); +#endif + write (&n, 2); + return (*this); +} + +fnetorderstream& fnetorderstream::writeInt32 (kuint32 n) { +#ifndef WORDS_BIGENDIAN + SwapBytes4(&n); +#endif + write (&n, 4); + return (*this); +} + +fnetorderstream& fnetorderstream::writeFloat32 (kfloat32 n) { +#ifndef WORDS_BIGENDIAN + SwapBytes4 (&n); +#endif + write (&n, 4); + return (*this); +} + +fnetorderstream& fnetorderstream::writeFloat64 (kfloat64 n) { +#ifndef WORDS_BIGENDIAN + SwapBytes8 (&n); +#endif + write (&n, 8); + return (*this); +} + +fnetorderstream& fnetorderstream::readInt16 (kuint16& n) { + read (&n, 2); +#ifndef WORDS_BIGENDIAN + SwapBytes2 (&n); +#endif + return (*this); +} + +fnetorderstream& fnetorderstream::readInt32 (kuint32& n) { + read (&n, 4); +#ifndef WORDS_BIGENDIAN + SwapBytes4 (&n); +#endif + return (*this); +} + +fnetorderstream& fnetorderstream::readFloat32 (kfloat32& n) { + read (&n, 4); +#ifndef WORDS_BIGENDIAN + SwapBytes4 (&n); +#endif + return (*this); +} + +fnetorderstream& fnetorderstream::readFloat64 (kfloat64& n) { + read (&n, 8); +#ifndef WORDS_BIGENDIAN + SwapBytes8 (&n); +#endif + return (*this); +} + + + +frnetorderstream& frnetorderstream::writeInt16 (kuint16 n) { +#ifdef WORDS_BIGENDIAN + SwapBytes2 (&n); +#endif + write (&n, 2); + return (*this); +} + +frnetorderstream& frnetorderstream::writeInt32 (kuint32 n) { +#ifdef WORDS_BIGENDIAN + SwapBytes4(&n); +#endif + write (&n, 4); + return (*this); +} + +frnetorderstream& frnetorderstream::writeFloat32 (kfloat32 n) { +#ifdef WORDS_BIGENDIAN + SwapBytes4 (&n); +#endif + write (&n, 4); + return (*this); +} + +frnetorderstream& frnetorderstream::writeFloat64 (kfloat64 n) { +#ifdef WORDS_BIGENDIAN + SwapBytes8 (&n); +#endif + write (&n, 8); + return (*this); +} + +frnetorderstream& frnetorderstream::readInt16 (kuint16& n) { + read (&n, 2); +#ifdef WORDS_BIGENDIAN + SwapBytes2 (&n); +#endif + return (*this); +} + +frnetorderstream& frnetorderstream::readInt32 (kuint32& n) { + read (&n, 4); +#ifdef WORDS_BIGENDIAN + SwapBytes4 (&n); +#endif + return (*this); +} + +frnetorderstream& frnetorderstream::readFloat32 (kfloat32& n) { + read (&n, 4); +#ifdef WORDS_BIGENDIAN + SwapBytes4 (&n); +#endif + return (*this); +} + +frnetorderstream& frnetorderstream::readFloat64 (kfloat64& n) { + read (&n, 8); +#ifdef WORDS_BIGENDIAN + SwapBytes8 (&n); +#endif + return (*this); +} +