File: fnetorderstream.cpp

package info (click to toggle)
ctsim 4.5.2-1.1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 6,536 kB
  • ctags: 3,720
  • sloc: cpp: 26,768; sh: 3,691; ansic: 1,254; perl: 296; makefile: 263
file content (200 lines) | stat: -rw-r--r-- 4,129 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*****************************************************************************
**  This is part of the CTSim program
**  Copyright (c) 1983-2001 Kevin Rosenberg
**
**  $Id: fnetorderstream.cpp 7061 2003-09-07 06:34:45Z kevin $
**
**  This program is free software; you can redistribute it and/or modify
**  it under the terms of the GNU General Public License (version 2) as
**  published by the Free Software Foundation.
**
**  This program is distributed in the hope that it will be useful,
**  but WITHOUT ANY WARRANTY; without even the implied warranty of
**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
**  GNU General Public License for more details.
**
**  You should have received a copy of the GNU General Public License
**  along with this program; if not, write to the Free Software
**  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
******************************************************************************/

#if HAVE_CONFIG_H
#include "config.h"
#endif

#if HAVE_UNISTD_H
#include <unistd.h>
#endif

#include "ctsupport.h"
#include "fnetorderstream.h"


void 
ConvertNetworkOrder (void* buffer, size_t bytes)
{
#ifndef WORDS_BIGENDIAN
    if (bytes < 2)
	return;

    char* start = static_cast<char*>(buffer);
    char* end = start + bytes - 1;   // last byte
    size_t nSwap = bytes / 2;
    
    while (nSwap-- > 0) {
	unsigned char c = *start;
	*start++ = *end;
	*end-- = c;
    }
#endif    
}

void 
ConvertReverseNetworkOrder (void* buffer, size_t bytes)
{
#ifdef WORDS_BIGENDIAN
    if (bytes < 2)
	return;

    char* start = static_cast<char*>(buffer);
    char* end = start + bytes - 1;  // last byte 
    size_t nSwap = bytes / 2;
    
    while (nSwap-- > 0) {
	unsigned char c = *start;
	*start++ = *end;
	*end-- = c;
    }
#endif    
}

void
fnetorderstream::writeInt16 (kuint16 n) {
#ifndef WORDS_BIGENDIAN
  SwapBytes2 (&n);
#endif
  write (reinterpret_cast<const char*>(&n), 2);
}

void
fnetorderstream::writeInt32 (kuint32 n) {
#ifndef WORDS_BIGENDIAN
  SwapBytes4(&n);
#endif
  write (reinterpret_cast<const char*>(&n), 4);
}

void
fnetorderstream::writeFloat32 (kfloat32 n) {
#ifndef WORDS_BIGENDIAN
  SwapBytes4 (&n);
#endif
  write (reinterpret_cast<const char*>(&n), 4);
}

void
fnetorderstream::writeFloat64 (kfloat64 n) {
#ifndef WORDS_BIGENDIAN
  SwapBytes8 (&n);
#endif
  write (reinterpret_cast<const char*>(&n), 8);
}

void
fnetorderstream::readInt16 (kuint16& n) {
  read (reinterpret_cast<char*>(&n), 2);
#ifndef WORDS_BIGENDIAN
  SwapBytes2 (&n);
#endif
}

void
fnetorderstream::readInt32 (kuint32& n) {
  read (reinterpret_cast<char*>(&n), 4);
#ifndef WORDS_BIGENDIAN
  SwapBytes4 (&n);
#endif
}

void
fnetorderstream::readFloat32 (kfloat32& n) {
  read (reinterpret_cast<char*>(&n), 4);
#ifndef WORDS_BIGENDIAN
  SwapBytes4 (&n);
#endif
}

void
fnetorderstream::readFloat64 (kfloat64& n) {
  read (reinterpret_cast<char*>(&n), 8);
#ifndef WORDS_BIGENDIAN
  SwapBytes8 (&n);
#endif
}



void
frnetorderstream::writeInt16 (kuint16 n) {
#ifdef WORDS_BIGENDIAN
  SwapBytes2 (&n);
#endif
  write (reinterpret_cast<char*>(&n), 2);
}

void
frnetorderstream::writeInt32 (kuint32 n) {
#ifdef WORDS_BIGENDIAN
  SwapBytes4(&n);
#endif
  write (reinterpret_cast<char*>(&n), 4);
}

void 
frnetorderstream::writeFloat32 (kfloat32 n) {
#ifdef WORDS_BIGENDIAN
  SwapBytes4 (&n);
#endif
  write (reinterpret_cast<char*>(&n), 4);
}

void 
frnetorderstream::writeFloat64 (kfloat64 n) {
#ifdef WORDS_BIGENDIAN
  SwapBytes8 (&n);
#endif
  write (reinterpret_cast<char*>(&n), 8);
}

void 
frnetorderstream::readInt16 (kuint16& n) {
  read (reinterpret_cast<char*>(&n), 2);
#ifdef WORDS_BIGENDIAN
  SwapBytes2 (&n);
#endif
}

void 
frnetorderstream::readInt32 (kuint32& n) {
  read (reinterpret_cast<char*>(&n), 4);
#ifdef WORDS_BIGENDIAN
  SwapBytes4 (&n);
#endif
}

void
frnetorderstream::readFloat32 (kfloat32& n) {
  read (reinterpret_cast<char*>(&n), 4);
#ifdef WORDS_BIGENDIAN
  SwapBytes4 (&n);
#endif
}

void
frnetorderstream::readFloat64 (kfloat64& n) {
  read (reinterpret_cast<char*>(&n), 8);
#ifdef WORDS_BIGENDIAN
  SwapBytes8 (&n);
#endif
}