r241: *** empty log message ***
[ctsim.git] / include / fnetorderstream.h
1 #ifndef NETORDER_H
2 #define NETORDER_H
3
4 #ifdef HAVE_CONFIG_H
5 #include "config.h"
6 #endif
7
8 #include <fstream>
9 #include <iostream>
10 #include <string>
11 using namespace std;
12
13
14 inline bool NativeBigEndian (void)
15 {
16 #ifdef WORDS_BIGENDIAN
17     return true;
18 #else
19     return false;
20 #endif
21 }
22
23 inline void
24 SwapBytes2 (void* buffer)
25 {
26   unsigned char* p = static_cast<unsigned char*>(buffer);
27   unsigned char temp = p[0];
28   p[0] = p[1];
29   p[1] = temp;
30 }
31
32 // 0<->3  1<->2 = 0123 -> 3210
33 inline void
34 SwapBytes4 (void* buffer)
35 {
36   unsigned char* p = static_cast<unsigned char*>(buffer);
37   unsigned char temp = p[0];
38   p[0] = p[3];
39   p[3] = temp;
40   temp = p[1];
41   p[1] = p[2];
42   p[2] = temp;
43 }
44
45 // 0<->7 1<->6 2<->5 3<->4 = 01234567 -> 76543210
46 inline void
47 SwapBytes8 (void* buffer)
48 {
49   unsigned char* p = static_cast<unsigned char*>(buffer);
50   unsigned char temp = p[0];
51   p[0] = p[7];
52   p[7] = temp;
53   temp = p[1];
54   p[1] = p[6];
55   p[6] = temp;
56   temp = p[2];
57   p[2] = p[5];
58   p[5] = temp;
59   temp = p[3];
60   p[3] = p[4];
61   p[4] = temp;
62 }
63
64
65 void ConvertNetworkOrder (void* buffer, size_t bytes);
66 void ConvertReverseNetworkOrder (void* buffer, size_t bytes);
67
68
69 class fnetorderstream : public fstream {
70  public:
71   fnetorderstream (const char* filename, int mode)
72     : fstream (filename, mode) {}
73
74   ~fnetorderstream (void)
75       {}
76
77   virtual  writeInt16 (kuint16 n);
78   virtual  writeInt32 (kuint32 n);
79   virtual  writeFloat32 (kfloat32 n);
80   virtual  writeFloat64 (kfloat64 n);
81   
82   virtual  readInt16 (kuint16& n);
83   virtual  readInt32 (kuint32& n);
84   virtual  readFloat32 (kfloat32& n);
85   virtual  readFloat64 (kfloat64& n);
86 };
87
88
89 class frnetorderstream : public fnetorderstream {
90  public:
91   frnetorderstream (const char* filename, int mode)
92     : fnetorderstream (filename, mode) {}
93
94   writeInt16 (kuint16 n);
95   writeInt32 (kuint32 n);
96   writeFloat32 (kfloat32 n);
97   writeFloat64 (kfloat64 n);
98   
99   readInt16 (kuint16& n);
100   readInt32 (kuint32& n);
101   readFloat32 (kfloat32& n);
102   readFloat64 (kfloat64& n);
103 };
104
105 #endif