DATRW++ library: seismic data I/O with multiple formats
ibinstream.cc
Go to the documentation of this file.
1 /*! \file ibinstream.cc
2  * \brief basic binary input stream (implementation)
3  *
4  * ----------------------------------------------------------------------------
5  *
6  * \author Thomas Forbriger
7  * \date 10/11/2011
8  *
9  * basic binary input stream (implementation)
10  *
11  * Copyright (c) 2011 by Thomas Forbriger (BFO Schiltach)
12  *
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 as published by
16  * the Free Software Foundation; either version 2 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27  * ----
28  *
29  *
30  * REVISIONS and CHANGES
31  * - 10/11/2011 V1.0 Thomas Forbriger
32  * - 31/01/2012 V1.1 use precisely the same variable type for
33  * TAbsoluteTime input and output
34  *
35  * ============================================================================
36  */
37 #define DATRW_IBINSTREAM_CC_VERSION \
38  "DATRW_IBINSTREAM_CC V1.1"
39 
40 #include <datrwxx/ibinstream.h>
41 #include <datrwxx/error.h>
42 #include <datrwxx/debug.h>
43 #include <datrwxx/bytesex.h>
44 
45 namespace datrw {
46 
47  namespace binary {
48 
49  ibinstream::ibinstream(std::istream& is,
50  const char* const magic,
51  const bool& debug):
52  Mis(is), Mdebug(debug)
53  {
57  "magic number in file does not match!");
59  } // ibinstream::ibinstream(std::ifstream& is
60 
61  /*----------------------------------------------------------------------*/
62 
63  void ibinstream::read(char& v)
64  {
65  DATRW_assert(Mis.read(reinterpret_cast<char *>(&v), sizeof(char)),
66  "ERROR (ibinstream::read): reading char value");
67  } // void ibinstream::read(char& v)
68 
69  /*----------------------------------------------------------------------*/
70 
71  void ibinstream::read(short& v)
72  {
73  DATRW_assert(Mis.read(reinterpret_cast<char *>(&v), sizeof(short)),
74  "ERROR (ibinstream::read): reading short value");
75  if (Mswap) { v=::datrw::util::swap(v); }
76  DATRW_debug(Mdebug, "ibinstream::read(short& v)", "v=" << v);
77  } // void ibinstream::read(short& v)
78 
79  /*----------------------------------------------------------------------*/
80 
81  void ibinstream::read(int& v)
82  {
83  DATRW_assert(Mis.read(reinterpret_cast<char *>(&v), sizeof(int)),
84  "ERROR (ibinstream::read): reading integer value");
85  if (Mswap) { v=::datrw::util::swap(v); }
86  } // void ibinstream::read(int& v)
87 
88  /*----------------------------------------------------------------------*/
89 
90  void ibinstream::read(unsigned int& v)
91  {
92  DATRW_assert(Mis.read(reinterpret_cast<char *>(&v),
93  sizeof(unsigned int)),
94  "ERROR (ibinstream::read): reading unsigned int value");
95  if (Mswap) { v=::datrw::util::swap(v); }
96  } // void ibinstream::read(unsigned int& v)
97 
98  /*----------------------------------------------------------------------*/
99 
100  void ibinstream::read(double& v)
101  {
102  DATRW_assert(Mis.read(reinterpret_cast<char *>(&v), sizeof(double)),
103  "ERROR (ibinstream::read): reading double value");
104  if (Mswap) { v=::datrw::util::swap(v); }
105  } // void ibinstream::read(double& v)
106 
107  /*----------------------------------------------------------------------*/
108 
109  void ibinstream::read(float& v)
110  {
111  DATRW_assert(Mis.read(reinterpret_cast<char *>(&v), sizeof(float)),
112  "ERROR (ibinstream::read): reading float value");
113  if (Mswap) { v=::datrw::util::swap(v); }
114  } // void ibinstream::read(float& v)
115 
116  /*----------------------------------------------------------------------*/
117 
118  void ibinstream::read(std::string& v)
119  {
120  unsigned int length;
121  this->read(length);
122  aff::Series<char> s(length+1);
123  DATRW_assert(Mis.read(reinterpret_cast<char *>(s.pointer()),
124  length*sizeof(char)),
125  "ERROR (ibinstream::read): reading string");
126  s(s.l())='\0';
127  v=std::string(s.pointer());
128  } // void ibinstream::read(std::string& v)
129 
130  /*----------------------------------------------------------------------*/
131 
132  void ibinstream::read(libtime::TAbsoluteTime& v)
133  {
134  unsigned int year;
135  char month, day, hour, minute, second;
136  short milsec, micsec;
137  this->read(year);
138  this->read(month);
139  this->read(day);
140  this->read(hour);
141  this->read(minute);
142  this->read(second);
143  this->read(milsec);
144  this->read(micsec);
145  v=libtime::TAbsoluteTime(year, month, day, hour, minute, second,
146  milsec, micsec);
147  } // void ibinstream::read(libtime::TAbsoluteTime& v)
148 
149  /*----------------------------------------------------------------------*/
150 
151  void ibinstream::read(::sff::FREE& v)
152  {
153  unsigned int nlines;
154  this->read(nlines);
155  for (unsigned int i=0; i<nlines; ++i)
156  {
157  std::string s;
158  this->read(s);
159  v.append(s);
160  }
161  } // void ibinstream::read(::sff::FREE& v)
162 
163  /*----------------------------------------------------------------------*/
164 
165  void ibinstream::read(::sff::WID2& v)
166  {
167  this->read(v.date);
168  this->read(v.dt);
169  this->read(v.nsamples);
170  this->read(v.station);
171  this->read(v.channel);
172  this->read(v.auxid);
173  this->read(v.instype);
174  this->read(v.calib);
175  this->read(v.calper);
176  this->read(v.hang);
177  this->read(v.vang);
178  } // void ibinstream::read(::sff::WID2& v)
179 
180  /*----------------------------------------------------------------------*/
181 
182  void ibinstream::read(::sff::SRCE& v)
183  {
184  this->read(v.date);
185  this->read(v.type);
186  char cs;
187  this->read(cs);
188  v.cs=::sff::coosysID(cs);
189  this->read(v.cx);
190  this->read(v.cy);
191  this->read(v.cz);
192  } // void ibinstream::read(::sff::SRCE& v)
193 
194  /*----------------------------------------------------------------------*/
195 
196  void ibinstream::read(::sff::INFO& v)
197  {
198  char cs;
199  this->read(cs);
200  v.cs=::sff::coosysID(cs);
201  this->read(v.cx);
202  this->read(v.cy);
203  this->read(v.cz);
204  this->read(v.nstacks);
205  } // void ibinstream::read(::sff::INFO& v)
206 
207  /*----------------------------------------------------------------------*/
208 
210  {
211  int first, last;
212  this->read(first);
213  this->read(last);
214  v=Tdseries(first,last);
215  DATRW_assert(Mis.read(reinterpret_cast<char *>(v.pointer()),
216  v.size()*sizeof(Tdseries::Tvalue)),
217  "ERROR (ibinstream::read): reading double series");
218  if (Mswap)
219  {
220  aff::Iterator<Tdseries> I(v);
221  while (I.valid())
222  {
223  *I=::datrw::util::swap(*I);
224  ++I;
225  }
226  }
227  } // void ibinstream::read(Tdseries& v)
228 
229  /*----------------------------------------------------------------------*/
230 
232  {
233  int first, last;
234  this->read(first);
235  this->read(last);
236  v=Tfseries(first,last);
237  DATRW_assert(Mis.read(reinterpret_cast<char *>(v.pointer()),
238  v.size()*sizeof(Tfseries::Tvalue)),
239  "ERROR (ibinstream::read): reading float series");
240  if (Mswap)
241  {
242  aff::Iterator<Tfseries> I(v);
243  while (I.valid())
244  {
245  *I=::datrw::util::swap(*I);
246  ++I;
247  }
248  }
249  } // void ibinstream::read(Tfseries& v)
250 
251  /*----------------------------------------------------------------------*/
252 
254  {
255  int first, last;
256  this->read(first);
257  this->read(last);
258  v=Tiseries(first,last);
259  DATRW_assert(Mis.read(reinterpret_cast<char *>(v.pointer()),
260  v.size()*sizeof(Tiseries::Tvalue)),
261  "ERROR (ibinstream::read): reading integer series");
262  if (Mswap)
263  {
264  aff::Iterator<Tiseries> I(v);
265  while (I.valid())
266  {
267  *I=::datrw::util::swap(*I);
268  ++I;
269  }
270  }
271  } // void ibinstream::read(Tiseries& v)
272 
273  /*----------------------------------------------------------------------*/
274 
275  unsigned int ibinstream::skipdseries()
276  {
277  int first, last;
278  this->read(first);
279  this->read(last);
280  DATRW_assert(last>=first,
281  "ERROR (ibinstream::skipdseries): unreasonable index range");
282  unsigned int size=last-first+1;
283  Mis.seekg(size*sizeof(Tdseries::Tvalue), Mis.cur);
284  return(size);
285  } // unsigned int ibinstream::skipdseries()
286 
287  /*----------------------------------------------------------------------*/
288 
289  unsigned int ibinstream::skipfseries()
290  {
291  int first, last;
292  this->read(first);
293  this->read(last);
294  DATRW_assert(last>=first,
295  "ERROR (ibinstream::skipfseries): unreasonable index range");
296  unsigned int size=last-first+1;
297  Mis.seekg(size*sizeof(Tfseries::Tvalue), Mis.cur);
298  return(size);
299  } // unsigned int ibinstream::skipfseries()
300 
301  /*----------------------------------------------------------------------*/
302 
303  unsigned int ibinstream::skipiseries()
304  {
305  int first, last;
306  this->read(first);
307  this->read(last);
308  DATRW_assert(last>=first,
309  "ERROR (ibinstream::skipiseries): unreasonable index range");
310  unsigned int size=last-first+1;
311  Mis.seekg(size*sizeof(Tiseries::Tvalue), Mis.cur);
312  return(size);
313  } // unsigned int ibinstream::skipiseries()
314 
315  } // namespace binary
316 
317 } // namespace datrw
318 
319 /* ----- END OF ibinstream.cc ----- */
#define DATRW_assert(C, M)
Check an assertion and report by throwing an exception.
Definition: error.h:92
macro function for debugging output (prototypes)
aff::Series< float > Tfseries
Definition: types.h:46
ibinstream(std::istream &is, const char *const magic, const bool &debug=false)
Definition: ibinstream.cc:49
const char *const magic
magic number to identify file type and bytesex
Definition: binary.cc:53
unsigned int skipfseries()
Definition: ibinstream.cc:289
Emagic_type file_magic_test(std::istream &is, const char *const cmagic, const bool &fortranmode)
check magic number in file
Definition: bytesex.cc:105
The magic number does match the file.
Definition: bytesex.h:104
aff::Series< double > Tdseries
Definition: types.h:45
std::istream & Mis
Definition: ibinstream.h:80
exception class declaration for libdatrwxx (prototypes)
T swap(const T &value)
How to swap any generic type.
Definition: bytesex.h:118
unsigned int skipdseries()
Definition: ibinstream.cc:275
A copy of bytesex.h from libtfxx (prototypes)
int Tvalue
Definition: pdasread.h:75
Root namespace of library.
Definition: aalibdatrwxx.cc:16
aff::Series< int > Tiseries
Definition: types.h:47
The bytesex of the file must be swapped to match this machine.
Definition: bytesex.h:102
#define DATRW_debug(C, N, M)
produce debug output
Definition: debug.h:50
unsigned int skipiseries()
Definition: ibinstream.cc:303
Emagic_type
Define bytesex indicator for magic number test.
Definition: bytesex.h:98