r115: *** empty log message ***
[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 fnetorderstream& fnetorderstream::writeInt16 (kuint16 n) {
52 #ifndef WORDS_BIGENDIAN
53   SwapBytes2 (&n);
54 #endif
55   write (&n, 2);
56   return (*this);
57 }
58
59 fnetorderstream& fnetorderstream::writeInt32 (kuint32 n) {
60 #ifndef WORDS_BIGENDIAN
61   SwapBytes4(&n);
62 #endif
63   write (&n, 4);
64   return (*this);
65 }
66
67 fnetorderstream& fnetorderstream::writeFloat32 (kfloat32 n) {
68 #ifndef WORDS_BIGENDIAN
69   SwapBytes4 (&n);
70 #endif
71   write (&n, 4);
72   return (*this);
73 }
74
75 fnetorderstream& fnetorderstream::writeFloat64 (kfloat64 n) {
76 #ifndef WORDS_BIGENDIAN
77   SwapBytes8 (&n);
78 #endif
79   write (&n, 8);
80   return (*this);
81 }
82
83 fnetorderstream& fnetorderstream::readInt16 (kuint16& n) {
84   read (&n, 2);
85 #ifndef WORDS_BIGENDIAN
86   SwapBytes2 (&n);
87 #endif
88   return (*this);
89 }
90
91 fnetorderstream& fnetorderstream::readInt32 (kuint32& n) {
92   read (&n, 4);
93 #ifndef WORDS_BIGENDIAN
94   SwapBytes4 (&n);
95 #endif
96   return (*this);
97 }
98
99 fnetorderstream& fnetorderstream::readFloat32 (kfloat32& n) {
100   read (&n, 4);
101 #ifndef WORDS_BIGENDIAN
102   SwapBytes4 (&n);
103 #endif
104   return (*this);
105 }
106
107 fnetorderstream& fnetorderstream::readFloat64 (kfloat64& n) {
108   read (&n, 8);
109 #ifndef WORDS_BIGENDIAN
110   SwapBytes8 (&n);
111 #endif
112   return (*this);
113 }
114
115
116
117 frnetorderstream& frnetorderstream::writeInt16 (kuint16 n) {
118 #ifdef WORDS_BIGENDIAN
119   SwapBytes2 (&n);
120 #endif
121   write (&n, 2);
122   return (*this);
123 }
124
125 frnetorderstream& frnetorderstream::writeInt32 (kuint32 n) {
126 #ifdef WORDS_BIGENDIAN
127   SwapBytes4(&n);
128 #endif
129   write (&n, 4);
130   return (*this);
131 }
132
133 frnetorderstream& frnetorderstream::writeFloat32 (kfloat32 n) {
134 #ifdef WORDS_BIGENDIAN
135   SwapBytes4 (&n);
136 #endif
137   write (&n, 4);
138   return (*this);
139 }
140
141 frnetorderstream& frnetorderstream::writeFloat64 (kfloat64 n) {
142 #ifdef WORDS_BIGENDIAN
143   SwapBytes8 (&n);
144 #endif
145   write (&n, 8);
146   return (*this);
147 }
148
149 frnetorderstream& frnetorderstream::readInt16 (kuint16& n) {
150   read (&n, 2);
151 #ifdef WORDS_BIGENDIAN
152   SwapBytes2 (&n);
153 #endif
154   return (*this);
155 }
156
157 frnetorderstream& frnetorderstream::readInt32 (kuint32& n) {
158   read (&n, 4);
159 #ifdef WORDS_BIGENDIAN
160   SwapBytes4 (&n);
161 #endif
162   return (*this);
163 }
164
165 frnetorderstream& frnetorderstream::readFloat32 (kfloat32& n) {
166   read (&n, 4);
167 #ifdef WORDS_BIGENDIAN
168   SwapBytes4 (&n);
169 #endif
170   return (*this);
171 }
172
173 frnetorderstream& frnetorderstream::readFloat64 (kfloat64& n) {
174   read (&n, 8);
175 #ifdef WORDS_BIGENDIAN
176   SwapBytes8 (&n);
177 #endif
178   return (*this);
179 }
180