Debian Codegraph @

include/fnetorderstream.h

raw
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-2009 Kevin Rosenberg
11 **
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.
15 **
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.
20 **
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 ******************************************************************************/
25
26
27 #ifndef NETORDER_H
28 #define NETORDER_H
29
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33
34 #include <fstream>
35 #include <iostream>
36 #include <string>
37
38
39 inline bool NativeBigEndian (void)
40 {
41 #ifdef WORDS_BIGENDIAN
42 return true;
43 #else
44 return false;
45 #endif
46 }
47
48 inline void
49 SwapBytes2 (void* buffer)
50 {
51 unsigned char* p = static_cast<unsigned char*>(buffer);
52 unsigned char temp = p[0];
53 p[0] = p[1];
54 p[1] = temp;
55 }
56
57 // 0<->3 1<->2 = 0123 -> 3210
58 inline void
59 SwapBytes4 (void* buffer)
60 {
61 unsigned char* p = static_cast<unsigned char*>(buffer);
62 unsigned char temp = p[0];
63 p[0] = p[3];
64 p[3] = temp;
65 temp = p[1];
66 p[1] = p[2];
67 p[2] = temp;
68 }
69
70 // 0<->7 1<->6 2<->5 3<->4 = 01234567 -> 76543210
71 inline void
72 SwapBytes8 (void* buffer)
73 {
74 unsigned char* p = static_cast<unsigned char*>(buffer);
75 unsigned char temp = p[0];
76 p[0] = p[7];
77 p[7] = temp;
78 temp = p[1];
79 p[1] = p[6];
80 p[6] = temp;
81 temp = p[2];
82 p[2] = p[5];
83 p[5] = temp;
84 temp = p[3];
85 p[3] = p[4];
86 p[4] = temp;
87 }
88
89 inline void
90 SwapBytes2IfLittleEndian (void* buffer)
91 {
92 #ifndef WORDS_BIGENDIAN
93 SwapBytes2 (buffer);
94 #endif
95 }
96
97 inline void
98 SwapBytes4IfLittleEndian (void* buffer)
99 {
100 #ifndef WORDS_BIGENDIAN
101 SwapBytes4 (buffer);
102 #endif
103 }
104
105 inline void
106 SwapBytes8IfLittleEndian (void* buffer)
107 {
108 #ifndef WORDS_BIGENDIAN
109 SwapBytes8 (buffer);
110 #endif
111 }
112
113 void ConvertNetworkOrder (void* buffer, size_t bytes);
114 void ConvertReverseNetworkOrder (void* buffer, size_t bytes);
115
116 using std::fstream;
117 class fnetorderstream : public fstream {
118 public:
119 fnetorderstream (const char* filename, std::ios::openmode mode)
120 : fstream (filename, mode) {}
121
122 ~fnetorderstream (void)
123 {}
124
125 virtual void writeInt16 (kuint16 n);
126 virtual void writeInt32 (kuint32 n);
127 virtual void writeFloat32 (kfloat32 n);
128 virtual void writeFloat64 (kfloat64 n);
129
130 virtual void readInt16 (kuint16& n);
131 virtual void readInt32 (kuint32& n);
132 virtual void readFloat32 (kfloat32& n);
133 virtual void readFloat64 (kfloat64& n);
134 };
135
136
137 class frnetorderstream : public fnetorderstream {
138 public:
139 frnetorderstream (const char* filename, std::ios::openmode mode)
140 : fnetorderstream (filename, mode) {}
141
142 virtual void writeInt16 (kuint16 n);
143 virtual void writeInt32 (kuint32 n);
144 virtual void writeFloat32 (kfloat32 n);
145 virtual void writeFloat64 (kfloat64 n);
146
147 virtual void readInt16 (kuint16& n);
148 virtual void readInt32 (kuint32& n);
149 virtual void readFloat32 (kfloat32& n);
150 virtual void readFloat64 (kfloat64& n);
151 };
152
153 #endif