r348: fix linefeed problem
[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-2000 Kevin Rosenberg
11 **
12 **  $Id: fnetorderstream.h,v 1.7 2001/01/02 16:02:12 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
92 void ConvertNetworkOrder (void* buffer, size_t bytes);
93 void ConvertReverseNetworkOrder (void* buffer, size_t bytes);
94
95 using std::fstream;
96 class fnetorderstream : public fstream {
97  public:
98   fnetorderstream (const char* filename, int mode)
99           : fstream (filename, mode) {}
100
101   ~fnetorderstream (void)
102       {}
103
104   virtual void writeInt16 (kuint16 n);
105   virtual void writeInt32 (kuint32 n);
106   virtual void  writeFloat32 (kfloat32 n);
107   virtual void  writeFloat64 (kfloat64 n);
108   
109   virtual void  readInt16 (kuint16& n);
110   virtual void  readInt32 (kuint32& n);
111   virtual void  readFloat32 (kfloat32& n);
112   virtual void  readFloat64 (kfloat64& n);
113 };
114
115
116 class frnetorderstream : public fnetorderstream {
117  public:
118   frnetorderstream (const char* filename, int mode)
119     : fnetorderstream (filename, mode) {}
120
121   virtual void writeInt16 (kuint16 n);
122   virtual void writeInt32 (kuint32 n);
123   virtual void writeFloat32 (kfloat32 n);
124   virtual void writeFloat64 (kfloat64 n);
125   
126   virtual void readInt16 (kuint16& n);
127   virtual void readInt32 (kuint32& n);
128   virtual void readFloat32 (kfloat32& n);
129   virtual void readFloat64 (kfloat64& n);
130 };
131
132 #endif