r115: *** empty log message ***
[ctsim.git] / libctsupport / fnetorderstream.cpp
diff --git a/libctsupport/fnetorderstream.cpp b/libctsupport/fnetorderstream.cpp
new file mode 100644 (file)
index 0000000..7fff7a3
--- /dev/null
@@ -0,0 +1,180 @@
+#if HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#if HAVE_UNISTD_H
+#include <unistd.h>
+#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<char*>(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<char*>(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);
+}
+