bb084b15e5c65148d11c5867dfabbb9f36784cb0
[ctsim.git] / include / fnetorderstream.h
1 /*****************************************************************************
2 ** FILE IDENTIFICATION
3 **
4 **   Name:          fnetorderstream.h
5 **   Purpose:       Network-order file stream header
6 **   Programmer:    Kevin Rosenberg
7 **   Date Started:  Sep 2000
8 **
9 **  This is part of the CTSim program
10 **  Copyright (c) 1983-2001 Kevin Rosenberg
11 **
12 **  $Id: fnetorderstream.h,v 1.11 2002/05/30 06:18:11 kevin Exp $
13 **
14 **  This program is free software; you can redistribute it and/or modify
15 **  it under the terms of the GNU General Public License (version 2) as
16 **  published by the Free Software Foundation.
17 **
18 **  This program is distributed in the hope that it will be useful,
19 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
20 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 **  GNU General Public License for more details.
22 **
23 **  You should have received a copy of the GNU General Public License
24 **  along with this program; if not, write to the Free Software
25 **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26 ******************************************************************************/
27
28
29 #ifndef NETORDER_H
30 #define NETORDER_H
31
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
35
36 #include <fstream>
37 #include <iostream>
38 #include <string>
39
40
41 inline bool NativeBigEndian (void)
42 {
43 #ifdef WORDS_BIGENDIAN
44     return true;
45 #else
46     return false;
47 #endif
48 }
49
50 inline void
51 SwapBytes2 (void* buffer)
52 {
53   unsigned char* p = static_cast<unsigned char*>(buffer);
54   unsigned char temp = p[0];
55   p[0] = p[1];
56   p[1] = temp;
57 }
58
59 // 0<->3  1<->2 = 0123 -> 3210
60 inline void
61 SwapBytes4 (void* buffer)
62 {
63   unsigned char* p = static_cast<unsigned char*>(buffer);
64   unsigned char temp = p[0];
65   p[0] = p[3];
66   p[3] = temp;
67   temp = p[1];
68   p[1] = p[2];
69   p[2] = temp;
70 }
71
72 // 0<->7 1<->6 2<->5 3<->4 = 01234567 -> 76543210
73 inline void
74 SwapBytes8 (void* buffer)
75 {
76   unsigned char* p = static_cast<unsigned char*>(buffer);
77   unsigned char temp = p[0];
78   p[0] = p[7];
79   p[7] = temp;
80   temp = p[1];
81   p[1] = p[6];
82   p[6] = temp;
83   temp = p[2];
84   p[2] = p[5];
85   p[5] = temp;
86   temp = p[3];
87   p[3] = p[4];
88   p[4] = temp;
89 }
90
91 inline void
92 SwapBytes2IfLittleEndian (void* buffer)
93 {
94 #ifndef WORDS_BIGENDIAN
95   SwapBytes2 (buffer);
96 #endif
97 }
98
99 inline void
100 SwapBytes4IfLittleEndian (void* buffer)
101 {
102 #ifndef WORDS_BIGENDIAN
103   SwapBytes4 (buffer);
104 #endif
105 }
106
107 inline void
108 SwapBytes8IfLittleEndian (void* buffer)
109 {
110 #ifndef WORDS_BIGENDIAN
111   SwapBytes8 (buffer);
112 #endif
113 }
114
115 void ConvertNetworkOrder (void* buffer, size_t bytes);
116 void ConvertReverseNetworkOrder (void* buffer, size_t bytes);
117
118 using std::fstream;
119 class fnetorderstream : public fstream {
120  public:
121   fnetorderstream (const char* filename, std::ios::openmode mode)
122           : fstream (filename, mode) {}
123
124   ~fnetorderstream (void)
125       {}
126
127   virtual void writeInt16 (kuint16 n);
128   virtual void writeInt32 (kuint32 n);
129   virtual void  writeFloat32 (kfloat32 n);
130   virtual void  writeFloat64 (kfloat64 n);
131   
132   virtual void  readInt16 (kuint16& n);
133   virtual void  readInt32 (kuint32& n);
134   virtual void  readFloat32 (kfloat32& n);
135   virtual void  readFloat64 (kfloat64& n);
136 };
137
138
139 class frnetorderstream : public fnetorderstream {
140  public:
141   frnetorderstream (const char* filename, std::ios::openmode mode)
142     : fnetorderstream (filename, mode) {}
143
144   virtual void writeInt16 (kuint16 n);
145   virtual void writeInt32 (kuint32 n);
146   virtual void writeFloat32 (kfloat32 n);
147   virtual void writeFloat64 (kfloat64 n);
148   
149   virtual void readInt16 (kuint16& n);
150   virtual void readInt32 (kuint32& n);
151   virtual void readFloat32 (kfloat32& n);
152   virtual void readFloat64 (kfloat64& n);
153 };
154
155 #endif