Applied initial patches for wx2.8 compatibility
[ctsim.git] / libctsupport / fnetorderstream.cpp
1 /*****************************************************************************
2 **  This is part of the CTSim program
3 **  Copyright (c) 1983-2001 Kevin Rosenberg
4 **
5 **  $Id$
6 **
7 **  This program is free software; you can redistribute it and/or modify
8 **  it under the terms of the GNU General Public License (version 2) as
9 **  published by the Free Software Foundation.
10 **
11 **  This program is distributed in the hope that it will be useful,
12 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 **  GNU General Public License for more details.
15 **
16 **  You should have received a copy of the GNU General Public License
17 **  along with this program; if not, write to the Free Software
18 **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 ******************************************************************************/
20
21 #if HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #if HAVE_UNISTD_H
26 #include <unistd.h>
27 #endif
28
29 #include "ctsupport.h"
30 #include "fnetorderstream.h"
31
32
33 void
34 ConvertNetworkOrder (void* buffer, size_t bytes)
35 {
36 #ifndef WORDS_BIGENDIAN
37     if (bytes < 2)
38         return;
39
40     char* start = static_cast<char*>(buffer);
41     char* end = start + bytes - 1;   // last byte
42     size_t nSwap = bytes / 2;
43
44     while (nSwap-- > 0) {
45         unsigned char c = *start;
46         *start++ = *end;
47         *end-- = c;
48     }
49 #endif
50 }
51
52 void
53 ConvertReverseNetworkOrder (void* buffer, size_t bytes)
54 {
55 #ifdef WORDS_BIGENDIAN
56     if (bytes < 2)
57         return;
58
59     char* start = static_cast<char*>(buffer);
60     char* end = start + bytes - 1;  // last byte
61     size_t nSwap = bytes / 2;
62
63     while (nSwap-- > 0) {
64         unsigned char c = *start;
65         *start++ = *end;
66         *end-- = c;
67     }
68 #endif
69 }
70
71 void
72 fnetorderstream::writeInt16 (kuint16 n) {
73 #ifndef WORDS_BIGENDIAN
74   SwapBytes2 (&n);
75 #endif
76   write (reinterpret_cast<const char*>(&n), 2);
77 }
78
79 void
80 fnetorderstream::writeInt32 (kuint32 n) {
81 #ifndef WORDS_BIGENDIAN
82   SwapBytes4(&n);
83 #endif
84   write (reinterpret_cast<const char*>(&n), 4);
85 }
86
87 void
88 fnetorderstream::writeFloat32 (kfloat32 n) {
89 #ifndef WORDS_BIGENDIAN
90   SwapBytes4 (&n);
91 #endif
92   write (reinterpret_cast<const char*>(&n), 4);
93 }
94
95 void
96 fnetorderstream::writeFloat64 (kfloat64 n) {
97 #ifndef WORDS_BIGENDIAN
98   SwapBytes8 (&n);
99 #endif
100   write (reinterpret_cast<const char*>(&n), 8);
101 }
102
103 void
104 fnetorderstream::readInt16 (kuint16& n) {
105   read (reinterpret_cast<char*>(&n), 2);
106 #ifndef WORDS_BIGENDIAN
107   SwapBytes2 (&n);
108 #endif
109 }
110
111 void
112 fnetorderstream::readInt32 (kuint32& n) {
113   read (reinterpret_cast<char*>(&n), 4);
114 #ifndef WORDS_BIGENDIAN
115   SwapBytes4 (&n);
116 #endif
117 }
118
119 void
120 fnetorderstream::readFloat32 (kfloat32& n) {
121   read (reinterpret_cast<char*>(&n), 4);
122 #ifndef WORDS_BIGENDIAN
123   SwapBytes4 (&n);
124 #endif
125 }
126
127 void
128 fnetorderstream::readFloat64 (kfloat64& n) {
129   read (reinterpret_cast<char*>(&n), 8);
130 #ifndef WORDS_BIGENDIAN
131   SwapBytes8 (&n);
132 #endif
133 }
134
135
136
137 void
138 frnetorderstream::writeInt16 (kuint16 n) {
139 #ifdef WORDS_BIGENDIAN
140   SwapBytes2 (&n);
141 #endif
142   write (reinterpret_cast<char*>(&n), 2);
143 }
144
145 void
146 frnetorderstream::writeInt32 (kuint32 n) {
147 #ifdef WORDS_BIGENDIAN
148   SwapBytes4(&n);
149 #endif
150   write (reinterpret_cast<char*>(&n), 4);
151 }
152
153 void
154 frnetorderstream::writeFloat32 (kfloat32 n) {
155 #ifdef WORDS_BIGENDIAN
156   SwapBytes4 (&n);
157 #endif
158   write (reinterpret_cast<char*>(&n), 4);
159 }
160
161 void
162 frnetorderstream::writeFloat64 (kfloat64 n) {
163 #ifdef WORDS_BIGENDIAN
164   SwapBytes8 (&n);
165 #endif
166   write (reinterpret_cast<char*>(&n), 8);
167 }
168
169 void
170 frnetorderstream::readInt16 (kuint16& n) {
171   read (reinterpret_cast<char*>(&n), 2);
172 #ifdef WORDS_BIGENDIAN
173   SwapBytes2 (&n);
174 #endif
175 }
176
177 void
178 frnetorderstream::readInt32 (kuint32& n) {
179   read (reinterpret_cast<char*>(&n), 4);
180 #ifdef WORDS_BIGENDIAN
181   SwapBytes4 (&n);
182 #endif
183 }
184
185 void
186 frnetorderstream::readFloat32 (kfloat32& n) {
187   read (reinterpret_cast<char*>(&n), 4);
188 #ifdef WORDS_BIGENDIAN
189   SwapBytes4 (&n);
190 #endif
191 }
192
193 void
194 frnetorderstream::readFloat64 (kfloat64& n) {
195   read (reinterpret_cast<char*>(&n), 8);
196 #ifdef WORDS_BIGENDIAN
197   SwapBytes8 (&n);
198 #endif
199 }
200