liborigin 2.0.0
/usr/src/slapt-src/libraries/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
35namespace 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
48 {
49 char c;
50 get(c);
51 value = (c != 0);
52 return *this;
53 }
54
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
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
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
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
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
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
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
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 {
168 value.regular = color[0];
169 }
170 else
171 {
172 switch(color[2])
173 {
174 case 0:
176 break;
177 case 0x40:
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:
191 for(int i = 0; i < 3; ++i)
192 value.custom[i] = color[i];
193 break;
194 case 0x20:
196 value.starting = color[1];
197 break;
198 case 0xFF:
199 if(color[0] == 0xFC)
201 else if(color[0] == 0xF7)
203
204 break;
205
206 default:
208 value.regular = color[0];
209 break;
210
211 }
212
213 return *this;
214 }
215
216 private:
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
Definition: endianfstream.hh:38
iendianfstream & operator>>(unsigned char &value)
Definition: endianfstream.hh:61
bool bigEndian
Definition: endianfstream.hh:217
iendianfstream & operator>>(bool &value)
Definition: endianfstream.hh:47
iendianfstream(const char *_Filename, ios_base::openmode _Mode=ios_base::in)
Definition: endianfstream.hh:40
iendianfstream & operator>>(float &value)
Definition: endianfstream.hh:121
iendianfstream & operator>>(string &value)
Definition: endianfstream.hh:148
iendianfstream & operator>>(long double &value)
Definition: endianfstream.hh:139
iendianfstream & operator>>(int &value)
Definition: endianfstream.hh:85
iendianfstream & operator>>(short &value)
Definition: endianfstream.hh:67
void swap_bytes(unsigned char *data, int size)
Definition: endianfstream.hh:218
iendianfstream & operator>>(char &value)
Definition: endianfstream.hh:55
iendianfstream & operator>>(double &value)
Definition: endianfstream.hh:130
iendianfstream & operator>>(unsigned int &value)
Definition: endianfstream.hh:94
iendianfstream & operator>>(unsigned short &value)
Definition: endianfstream.hh:76
iendianfstream & operator>>(long &value)
Definition: endianfstream.hh:103
iendianfstream & operator>>(Origin::Color &value)
Definition: endianfstream.hh:158
iendianfstream & operator>>(unsigned long &value)
Definition: endianfstream.hh:112
Definition: endianfstream.hh:36
Definition: OriginObj.h:54
unsigned char starting
Definition: OriginObj.h:65
ColorType type
Definition: OriginObj.h:60
unsigned char custom[3]
Definition: OriginObj.h:64
unsigned char regular
Definition: OriginObj.h:63
unsigned char column
Definition: OriginObj.h:66
@ RGB
Definition: OriginObj.h:55
@ Custom
Definition: OriginObj.h:55
@ Regular
Definition: OriginObj.h:55
@ Mapping
Definition: OriginObj.h:55
@ Increment
Definition: OriginObj.h:55
@ Indexing
Definition: OriginObj.h:55
@ None
Definition: OriginObj.h:55
@ Automatic
Definition: OriginObj.h:55