labplot  2.8.2
About: LabPlot is an application for plotting and analysis of 2D and 3D functions and data. It is a complete rewrite of LabPlot1 and lacks in the first release a lot of features available in the predecessor. On the other hand, the GUI and the usability is more superior.
  Fossies Dox: labplot-2.8.2.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

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 
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 
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 
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:
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:
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)
203 
204  break;
205 
206  default:
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  int i = 0;
221  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
iendianfstream & operator>>(Origin::Color &value)
iendianfstream & operator>>(float &value)
iendianfstream(const char *_Filename, ios_base::openmode _Mode=ios_base::in)
iendianfstream & operator>>(char &value)
iendianfstream & operator>>(short &value)
void swap_bytes(unsigned char *data, int size)
iendianfstream & operator>>(unsigned long &value)
iendianfstream & operator>>(bool &value)
iendianfstream & operator>>(string &value)
iendianfstream & operator>>(long &value)
iendianfstream & operator>>(unsigned short &value)
iendianfstream & operator>>(long double &value)
iendianfstream & operator>>(unsigned int &value)
iendianfstream & operator>>(int &value)
iendianfstream & operator>>(unsigned char &value)
iendianfstream & operator>>(double &value)
T read(std::ifstream &is)
Read value from stream.
Definition: ROOTFilter.cpp:542
unsigned char starting
Definition: OriginObj.h:89
ColorType type
Definition: OriginObj.h:84
unsigned char custom[3]
Definition: OriginObj.h:88
unsigned char regular
Definition: OriginObj.h:87
unsigned char column
Definition: OriginObj.h:90