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