r115: *** empty log message ***
[ctsim.git] / include / fnetorderstream.h
diff --git a/include/fnetorderstream.h b/include/fnetorderstream.h
new file mode 100644 (file)
index 0000000..d341763
--- /dev/null
@@ -0,0 +1,105 @@
+#ifndef NETORDER_H
+#define NETORDER_H
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <fstream>
+#include <iostream>
+#include <string>
+using namespace std;
+
+
+inline bool NativeBigEndian (void)
+{
+#ifdef WORDS_BIGENDIAN
+    return true;
+#else
+    return false;
+#endif
+}
+
+inline void
+SwapBytes2 (void* buffer)
+{
+  unsigned char* p = static_cast<unsigned char*>(buffer);
+  unsigned char temp = p[0];
+  p[0] = p[1];
+  p[1] = temp;
+}
+
+// 0<->3  1<->2 = 0123 -> 3210
+inline void
+SwapBytes4 (void* buffer)
+{
+  unsigned char* p = static_cast<unsigned char*>(buffer);
+  unsigned char temp = p[0];
+  p[0] = p[3];
+  p[3] = temp;
+  temp = p[1];
+  p[1] = p[2];
+  p[2] = temp;
+}
+
+// 0<->7 1<->6 2<->5 3<->4 = 01234567 -> 76543210
+inline void
+SwapBytes8 (void* buffer)
+{
+  unsigned char* p = static_cast<unsigned char*>(buffer);
+  unsigned char temp = p[0];
+  p[0] = p[7];
+  p[7] = temp;
+  temp = p[1];
+  p[1] = p[6];
+  p[6] = temp;
+  temp = p[2];
+  p[2] = p[5];
+  p[5] = temp;
+  temp = p[3];
+  p[3] = p[4];
+  p[4] = temp;
+}
+
+
+void ConvertNetworkOrder (void* buffer, size_t bytes);
+void ConvertReverseNetworkOrder (void* buffer, size_t bytes);
+
+
+class fnetorderstream : public fstream {
+ public:
+  fnetorderstream (const char* filename, int mode)
+    : fstream (filename, mode) {}
+
+  ~fnetorderstream (void)
+      {}
+
+  virtual fnetorderstream& writeInt16 (kuint16 n);
+  virtual fnetorderstream& writeInt32 (kuint32 n);
+  virtual fnetorderstream& writeFloat32 (kfloat32 n);
+  virtual fnetorderstream& writeFloat64 (kfloat64 n);
+  
+  virtual fnetorderstream& readInt16 (kuint16& n);
+  virtual fnetorderstream& readInt32 (kuint32& n);
+  virtual fnetorderstream& readFloat32 (kfloat32& n);
+  virtual fnetorderstream& readFloat64 (kfloat64& n);
+};
+
+
+class frnetorderstream : public fnetorderstream {
+ public:
+  frnetorderstream (const char* filename, int mode)
+    : fnetorderstream (filename, mode) {}
+
+  virtual frnetorderstream& writeInt16 (kuint16 n);
+  virtual frnetorderstream& writeInt32 (kuint32 n);
+  virtual frnetorderstream& writeFloat32 (kfloat32 n);
+  virtual frnetorderstream& writeFloat64 (kfloat64 n);
+  
+  virtual frnetorderstream& readInt16 (kuint16& n);
+  virtual frnetorderstream& readInt32 (kuint32& n);
+  virtual frnetorderstream& readFloat32 (kfloat32& n);
+  virtual frnetorderstream& readFloat64 (kfloat64& n);
+};
+
+#endif