liborigin 2.0.0

/tmp/slackdce/slackrepo.5NA6jZ/build_liborigin/liborigin/endianfstream.hh
Go to the documentation of this file.
1/***************************************************************************
2 File : endianfstream.hh
3 --------------------------------------------------------------------
4 Copyright : (C) 2008 Alex Kargovsky
5 Email (use @ for *) : kargovsky*yumr.phys.msu.su
6 Description : Endianless file stream class
7
8 ***************************************************************************/
9
10/***************************************************************************
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 as published by *
14 * the Free Software Foundation; either version 2 of the License, or *
15 * (at your option) any later version. *
16 * *
17 * This program is distributed in the hope that it will be useful, *
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
20 * GNU General Public License for more details. *
21 * *
22 * You should have received a copy of the GNU General Public License *
23 * along with this program; if not, write to the Free Software *
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
25 * Boston, MA 02110-1301 USA *
26 * *
27 ***************************************************************************/
28
29#ifndef ENDIAN_FSTREAM_H
30#define ENDIAN_FSTREAM_H
31
32#include <fstream>
33#include "OriginObj.h"
34
35 namespace std
36{
37 class iendianfstream : public ifstream
38 {
39 public:
40 iendianfstream(const char *_Filename, ios_base::openmode _Mode = ios_base::in)
41 : ifstream(_Filename, _Mode)
42 {
43 short word = 0x4321;
44 bigEndian = (*(char*)& word) != 0x21;
45 };
46
47 iendianfstream& operator>>(bool& value)
48 {
49 char c;
50 get(c);
51 value = (c != 0);
52 return *this;
53 }
54
55 iendianfstream& operator>>(char& value)
56 {
57 get(value);
58 return *this;
59 }
60
61 iendianfstream& operator>>(unsigned char& value)
62 {
63 get(reinterpret_cast<char&>(value));
64 return *this;
65 }
66
67 iendianfstream& operator>>(short& value)
68 {
69 read(reinterpret_cast<char*>(&value), sizeof(value));
70 if(bigEndian)
71 swap_bytes(reinterpret_cast<unsigned char*>(&value), sizeof(value));
72
73 return *this;
74 }
75
76 iendianfstream& operator>>(unsigned short& value)
77 {
78 read(reinterpret_cast<char*>(&value), sizeof(value));
79 if(bigEndian)
80 swap_bytes(reinterpret_cast<unsigned char*>(&value), sizeof(value));
81
82 return *this;
83 }
84
85 iendianfstream& operator>>(int& value)
86 {
87 read(reinterpret_cast<char*>(&value), sizeof(value));
88 if(bigEndian)
89 swap_bytes(reinterpret_cast<unsigned char*>(&value), sizeof(value));
90
91 return *this;
92 }
93
94 iendianfstream& operator>>(unsigned int& value)
95 {
96 read(reinterpret_cast<char*>(&value), sizeof(value));
97 if(bigEndian)
98 swap_bytes(reinterpret_cast<unsigned char*>(&value), sizeof(value));
99
100 return *this;
101 }
102
103 iendianfstream& operator>>(long& value)
104 {
105 read(reinterpret_cast<char*>(&value), sizeof(value));
106 if(bigEndian)
107 swap_bytes(reinterpret_cast<unsigned char*>(&value), sizeof(value));
108
109 return *this;
110 }
111
112 iendianfstream& operator>>(unsigned long& value)
113 {
114 read(reinterpret_cast<char*>(&value), sizeof(value));
115 if(bigEndian)
116 swap_bytes(reinterpret_cast<unsigned char*>(&value), sizeof(value));
117
118 return *this;
119 }
120
121 iendianfstream& operator>>(float& value)
122 {
123 read(reinterpret_cast<char*>(&value), sizeof(value));
124 if(bigEndian)
125 swap_bytes(reinterpret_cast<unsigned char*>(&value), sizeof(value));
126
127 return *this;
128 }
129
130 iendianfstream& operator>>(double& value)
131 {
132 read(reinterpret_cast<char*>(&value), sizeof(value));
133 if(bigEndian)
134 swap_bytes(reinterpret_cast<unsigned char*>(&value), sizeof(value));
135
136 return *this;
137 }
138
139 iendianfstream& operator>>(long double& value)
140 {
141 read(reinterpret_cast<char*>(&value), sizeof(value));
142 if(bigEndian)
143 swap_bytes(reinterpret_cast<unsigned char*>(&value), sizeof(value));
144
145 return *this;
146 }
147
148 iendianfstream& operator>>(string& value)
149 {
150 read(reinterpret_cast<char*>(&value[0]), value.size());
151 string::size_type pos = value.find_first_of('\0');
152 if(pos != string::npos)
153 value.resize(pos);
154
155 return *this;
156 }
157
158 iendianfstream& operator>>(Origin::Color& value)
159 {
160 unsigned char color[4];
161 read(reinterpret_cast<char*>(&color), sizeof(color));
162 switch(color[3])
163 {
164 case 0:
165 if(color[0] < 0x64)
166 {
167 value.type = Origin::Color::Regular;
168 value.regular = color[0];
169 }
170 else
171 {
172 switch(color[2])
173 {
174 case 0:
175 value.type = Origin::Color::Indexing;
176 break;
177 case 0x40:
178 value.type = Origin::Color::Mapping;
179 break;
180 case 0x80:
181 value.type = Origin::Color::RGB;
182 break;
183 }
184
185 value.column = color[0] - 0x64;
186 }
187
188 break;
189 case 1:
190 value.type = Origin::Color::Custom;
191 for(int i = 0; i < 3; ++i)
192 value.custom[i] = color[i];
193 break;
194 case 0x20:
195 value.type = Origin::Color::Increment;
196 value.starting = color[1];
197 break;
198 case 0xFF:
199 if(color[0] == 0xFC)
200 value.type = Origin::Color::None;
201 else if(color[0] == 0xF7)
202 value.type = Origin::Color::Automatic;
203
204 break;
205
206 default:
207 value.type = Origin::Color::Regular;
208 value.regular = color[0];
209 break;
210
211 }
212
213 return *this;
214 }
215
216 private:
217 bool bigEndian;
218 void swap_bytes(unsigned char* data, int size)
219 {
220 register int i = 0;
221 register int j = size - 1;
222 while(i < j)
223 {
224 std::swap(data[i], data[j]);
225 ++i, --j;
226 }
227 }
228 };
229}
230
231#endif // ENDIAN_FSTREAM_H
OriginObj.h
std::iendianfstream
Definition: endianfstream.hh:38
std::iendianfstream::operator>>
iendianfstream & operator>>(unsigned char &value)
Definition: endianfstream.hh:61
std::iendianfstream::bigEndian
bool bigEndian
Definition: endianfstream.hh:217
std::iendianfstream::operator>>
iendianfstream & operator>>(bool &value)
Definition: endianfstream.hh:47
std::iendianfstream::iendianfstream
iendianfstream(const char *_Filename, ios_base::openmode _Mode=ios_base::in)
Definition: endianfstream.hh:40
std::iendianfstream::operator>>
iendianfstream & operator>>(float &value)
Definition: endianfstream.hh:121
std::iendianfstream::operator>>
iendianfstream & operator>>(string &value)
Definition: endianfstream.hh:148
std::iendianfstream::operator>>
iendianfstream & operator>>(long double &value)
Definition: endianfstream.hh:139
std::iendianfstream::operator>>
iendianfstream & operator>>(int &value)
Definition: endianfstream.hh:85
std::iendianfstream::operator>>
iendianfstream & operator>>(short &value)
Definition: endianfstream.hh:67
std::iendianfstream::swap_bytes
void swap_bytes(unsigned char *data, int size)
Definition: endianfstream.hh:218
std::iendianfstream::operator>>
iendianfstream & operator>>(char &value)
Definition: endianfstream.hh:55
std::iendianfstream::operator>>
iendianfstream & operator>>(double &value)
Definition: endianfstream.hh:130
std::iendianfstream::operator>>
iendianfstream & operator>>(unsigned int &value)
Definition: endianfstream.hh:94
std::iendianfstream::operator>>
iendianfstream & operator>>(unsigned short &value)
Definition: endianfstream.hh:76
std::iendianfstream::operator>>
iendianfstream & operator>>(long &value)
Definition: endianfstream.hh:103
std::iendianfstream::operator>>
iendianfstream & operator>>(Origin::Color &value)
Definition: endianfstream.hh:158
std::iendianfstream::operator>>
iendianfstream & operator>>(unsigned long &value)
Definition: endianfstream.hh:112
std
Definition: endianfstream.hh:36
Origin::Color
Definition: OriginObj.h:54
Origin::Color::starting
unsigned char starting
Definition: OriginObj.h:65
Origin::Color::type
ColorType type
Definition: OriginObj.h:60
Origin::Color::custom
unsigned char custom[3]
Definition: OriginObj.h:64
Origin::Color::regular
unsigned char regular
Definition: OriginObj.h:63
Origin::Color::column
unsigned char column
Definition: OriginObj.h:66
Origin::Color::RGB
@ RGB
Definition: OriginObj.h:55
Origin::Color::Custom
@ Custom
Definition: OriginObj.h:55
Origin::Color::Regular
@ Regular
Definition: OriginObj.h:55
Origin::Color::Mapping
@ Mapping
Definition: OriginObj.h:55
Origin::Color::Increment
@ Increment
Definition: OriginObj.h:55
Origin::Color::Indexing
@ Indexing
Definition: OriginObj.h:55
Origin::Color::None
@ None
Definition: OriginObj.h:55
Origin::Color::Automatic
@ Automatic
Definition: OriginObj.h:55