41d1ead4e27c29e8f251a16b7f7429767dc600de
[ctsim.git] / libctsupport / fnetorderstream.cpp
1 #if HAVE_CONFIG_H
2 #include "config.h"
3 #endif
4
5 #if HAVE_UNISTD_H
6 #include <unistd.h>
7 #endif
8
9 #include "ctsupport.h"
10 #include "fnetorderstream.h"
11
12
13 void 
14 ConvertNetworkOrder (void* buffer, size_t bytes)
15 {
16 #ifndef WORDS_BIGENDIAN
17     if (bytes < 2)
18         return;
19
20     char* start = static_cast<char*>(buffer);
21     char* end = start + bytes - 1;   // last byte
22     size_t nSwap = bytes / 2;
23     
24     while (nSwap-- > 0) {
25         unsigned char c = *start;
26         *start++ = *end;
27         *end-- = c;
28     }
29 #endif    
30 }
31
32 void 
33 ConvertReverseNetworkOrder (void* buffer, size_t bytes)
34 {
35 #ifdef WORDS_BIGENDIAN
36     if (bytes < 2)
37         return;
38
39     char* start = static_cast<char*>(buffer);
40     char* end = start + bytes - 1;  // last byte 
41     size_t nSwap = bytes / 2;
42     
43     while (nSwap-- > 0) {
44         unsigned char c = *start;
45         *start++ = *end;
46         *end-- = c;
47     }
48 #endif    
49 }
50
51 void
52 fnetorderstream::writeInt16 (kuint16 n) {
53 #ifndef WORDS_BIGENDIAN
54   SwapBytes2 (&n);
55 #endif
56   write (reinterpret_cast<const char*>(&n), 2);
57 }
58
59 void
60 fnetorderstream::writeInt32 (kuint32 n) {
61 #ifndef WORDS_BIGENDIAN
62   SwapBytes4(&n);
63 #endif
64   write (reinterpret_cast<const char*>(&n), 4);
65 }
66
67 void
68 fnetorderstream::writeFloat32 (kfloat32 n) {
69 #ifndef WORDS_BIGENDIAN
70   SwapBytes4 (&n);
71 #endif
72   write (reinterpret_cast<const char*>(&n), 4);
73 }
74
75 void
76 fnetorderstream::writeFloat64 (kfloat64 n) {
77 #ifndef WORDS_BIGENDIAN
78   SwapBytes8 (&n);
79 #endif
80   write (reinterpret_cast<const char*>(&n), 8);
81 }
82
83 void
84 fnetorderstream::readInt16 (kuint16& n) {
85   read (reinterpret_cast<char*>(&n), 2);
86 #ifndef WORDS_BIGENDIAN
87   SwapBytes2 (&n);
88 #endif
89 }
90
91 void
92 fnetorderstream::readInt32 (kuint32& n) {
93   read (reinterpret_cast<char*>(&n), 4);
94 #ifndef WORDS_BIGENDIAN
95   SwapBytes4 (&n);
96 #endif
97 }
98
99 void
100 fnetorderstream::readFloat32 (kfloat32& n) {
101   read (reinterpret_cast<char*>(&n), 4);
102 #ifndef WORDS_BIGENDIAN
103   SwapBytes4 (&n);
104 #endif
105 }
106
107 void
108 fnetorderstream::readFloat64 (kfloat64& n) {
109   read (reinterpret_cast<char*>(&n), 8);
110 #ifndef WORDS_BIGENDIAN
111   SwapBytes8 (&n);
112 #endif
113 }
114
115
116
117 void
118 frnetorderstream::writeInt16 (kuint16 n) {
119 #ifdef WORDS_BIGENDIAN
120   SwapBytes2 (&n);
121 #endif
122   write (reinterpret_cast<char*>(&n), 2);
123 }
124
125 void
126 frnetorderstream::writeInt32 (kuint32 n) {
127 #ifdef WORDS_BIGENDIAN
128   SwapBytes4(&n);
129 #endif
130   write (reinterpret_cast<char*>(&n), 4);
131 }
132
133 void 
134 frnetorderstream::writeFloat32 (kfloat32 n) {
135 #ifdef WORDS_BIGENDIAN
136   SwapBytes4 (&n);
137 #endif
138   write (reinterpret_cast<char*>(&n), 4);
139 }
140
141 void 
142 frnetorderstream::writeFloat64 (kfloat64 n) {
143 #ifdef WORDS_BIGENDIAN
144   SwapBytes8 (&n);
145 #endif
146   write (reinterpret_cast<char*>(&n), 8);
147 }
148
149 void 
150 frnetorderstream::readInt16 (kuint16& n) {
151   read (reinterpret_cast<char*>(&n), 2);
152 #ifdef WORDS_BIGENDIAN
153   SwapBytes2 (&n);
154 #endif
155 }
156
157 void 
158 frnetorderstream::readInt32 (kuint32& n) {
159   read (reinterpret_cast<char*>(&n), 4);
160 #ifdef WORDS_BIGENDIAN
161   SwapBytes4 (&n);
162 #endif
163 }
164
165 void
166 frnetorderstream::readFloat32 (kfloat32& n) {
167   read (reinterpret_cast<char*>(&n), 4);
168 #ifdef WORDS_BIGENDIAN
169   SwapBytes4 (&n);
170 #endif
171 }
172
173 void
174 frnetorderstream::readFloat64 (kfloat64& n) {
175   read (reinterpret_cast<char*>(&n), 8);
176 #ifdef WORDS_BIGENDIAN
177   SwapBytes8 (&n);
178 #endif
179 }
180