1 /*****************************************************************************
4 ** Name: fnetorderstream.h
5 ** Purpose: Network-order file stream header
6 ** Programmer: Kevin Rosenberg
7 ** Date Started: Sep 2000
9 ** This is part of the CTSim program
10 ** Copyright (c) 1983-2009 Kevin Rosenberg
12 ** This program is free software; you can redistribute it and/or modify
13 ** it under the terms of the GNU General Public License (version 2) as
14 ** published by the Free Software Foundation.
16 ** This program is distributed in the hope that it will be useful,
17 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ** GNU General Public License for more details.
21 ** You should have received a copy of the GNU General Public License
22 ** along with this program; if not, write to the Free Software
23 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 ******************************************************************************/
39 inline bool NativeBigEndian (void)
41 #ifdef WORDS_BIGENDIAN
49 SwapBytes2 (void* buffer)
51 unsigned char* p = static_cast<unsigned char*>(buffer);
52 unsigned char temp = p[0];
57 // 0<->3 1<->2 = 0123 -> 3210
59 SwapBytes4 (void* buffer)
61 unsigned char* p = static_cast<unsigned char*>(buffer);
62 unsigned char temp = p[0];
70 // 0<->7 1<->6 2<->5 3<->4 = 01234567 -> 76543210
72 SwapBytes8 (void* buffer)
74 unsigned char* p = static_cast<unsigned char*>(buffer);
75 unsigned char temp = p[0];
90 SwapBytes2IfLittleEndian (void* buffer)
92 #ifndef WORDS_BIGENDIAN
98 SwapBytes4IfLittleEndian (void* buffer)
100 #ifndef WORDS_BIGENDIAN
106 SwapBytes8IfLittleEndian (void* buffer)
108 #ifndef WORDS_BIGENDIAN
113 void ConvertNetworkOrder (void* buffer, size_t bytes);
114 void ConvertReverseNetworkOrder (void* buffer, size_t bytes);
117 class fnetorderstream : public fstream {
119 fnetorderstream (const char* filename, std::ios::openmode mode)
120 : fstream (filename, mode) {}
122 ~fnetorderstream (void)
125 virtual void writeInt16 (kuint16 n);
126 virtual void writeInt32 (kuint32 n);
127 virtual void writeFloat32 (kfloat32 n);
128 virtual void writeFloat64 (kfloat64 n);
130 virtual void readInt16 (kuint16& n);
131 virtual void readInt32 (kuint32& n);
132 virtual void readFloat32 (kfloat32& n);
133 virtual void readFloat64 (kfloat64& n);
137 class frnetorderstream : public fnetorderstream {
139 frnetorderstream (const char* filename, std::ios::openmode mode)
140 : fnetorderstream (filename, mode) {}
142 virtual void writeInt16 (kuint16 n);
143 virtual void writeInt32 (kuint32 n);
144 virtual void writeFloat32 (kfloat32 n);
145 virtual void writeFloat64 (kfloat64 n);
147 virtual void readInt16 (kuint16& n);
148 virtual void readInt32 (kuint32& n);
149 virtual void readFloat32 (kfloat32& n);
150 virtual void readFloat64 (kfloat64& n);