DATRW++ library: seismic data I/O with multiple formats
ibinarystream.cc
Go to the documentation of this file.
1 /*! \file ibinarystream.cc
2  * \brief input raw binary data (implementation)
3  *
4  * ----------------------------------------------------------------------------
5  *
6  * \author Thomas Forbriger
7  * \date 18/10/2011
8  *
9  * input raw binary data (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  * - 18/10/2011 V1.0 Thomas Forbriger
32  * - 18/11/2016 V1.1 use debug flag in base class
33  *
34  * ============================================================================
35  */
36 #define DATRW_IBINARYSTREAM_CC_VERSION \
37  "DATRW_IBINARYSTREAM_CC V1.1"
38 
39 #include<datrwxx/binary.h>
40 #include<datrwxx/error.h>
41 #include<datrwxx/util.h>
42 #include<datrwxx/debug.h>
43 
44 namespace datrw {
45 
46  const std::ios_base::openmode
47  ibinarystream::openmode=std::ios_base::in|std::ios_base::binary;
48 
49  /*----------------------------------------------------------------------*/
50 
51  ibinarystream::ibinarystream(std::istream& is,
52  const bool& debug):
53  Tbase(is, true, true, true, debug),
54  Mibs(is, ::datrw::binary::magic, debug)
55  {
56  char flags;
57  Mibs >> Mversion;
58  DATRW_debug(Mdebug, "ibinarystream::ibinarystream",
59  "file version " << Mversion);
61  {
62  std::cerr << "*** library version: " << datrw::binary::version <<
63  std::endl;
64  std::cerr << "*** file version: " << Mversion << std::endl;
65  }
67  "ERROR: file version does not match library version");
68  Mibs >> flags;
70  if (flags & ::datrw::binary::Fsrce)
71  {
72  DATRW_debug(Mdebug, "ibinarystream::ibinarystream",
73  "reading SRCE line");
74  ::sff::SRCE srce;
75  Mibs >> srce;
76  this->setsrce(srce);
77  }
78  if (flags & ::datrw::binary::Ffree)
79  {
80  ::sff::FREE free;
81  Mibs >> free;
82  this->setfilefree(free);
83  DATRW_debug(Mdebug, "ibinarystream::ibinarystream",
84  "reading file FREE block");
85  }
86  Mibs >> Mnextflags;
87  DATRW_assert(Mis.good(),
88  "ERROR: could not read behind file header");
90  } // ibinarystream::ibinarystream
91 
92  /*----------------------------------------------------------------------*/
93 
95  {
96  DATRW_debug(Mdebug, "ibinarystream::readflags()",
97  "Mis.good(): " << Mis.good());
100  try {
101  Mibs >> Mnextflags;
102  } catch (datrw::Exception)
103  {
104  this->setlast();
105  }
107  DATRW_debug(Mdebug, "ibinarystream::readflags()",
108  "Mis.good(): " << Mis.good());
109  if (!Mis.good())
110  {
111  this->setlast();
112  }
113  if (!this->last()) { binary::checktraceflags(Mnextflags); }
114  }
115 
116  /*----------------------------------------------------------------------*/
117 
119  {
120  this->newtrace();
121  ::sff::WID2 wid2;
122  Mibs >> wid2;
123  this->setwid2(wid2);
125  {
126  ::sff::INFO info;
127  Mibs >> info;
128  this->setinfo(info);
129  }
131  {
132  ::sff::FREE free;
133  Mibs >> free;
134  this->settracefree(free);
135  }
136  } // void ibinarystream::readheader()
137 
138  /*----------------------------------------------------------------------*/
139 
140  namespace binary {
141 
142  namespace {
143 
144  /*! read a sequence of samples
145  *
146  * \param T type of sample value (double, float, int)
147  *
148  * \param is raw binary input stream to read from
149  * \param series container to write samples to
150  * \param flags indicating data type
151  */
152  template<typename T>
153  void readany(ibinstream& is,
154  typename aff::Series<T>& series,
155  const char& flags)
156  {
157  // typedef typename aff::Series<T> Tinseries;
158  if (flags & datrw::binary::Fdouble)
159  {
160  Tdseries inseries;
161  is >> inseries;
162  util::convert(inseries, series);
163  }
164  else if (flags & datrw::binary::Ffloat)
165  {
166  Tfseries inseries;
167  is >> inseries;
168  util::convert(inseries, series);
169  }
170  else if (flags & datrw::binary::Fint)
171  {
172  Tiseries inseries;
173  is >> inseries;
174  util::convert(inseries, series);
175  }
176  else
177  {
178  std::cerr << "flags: " << int(flags) << std::endl;
179  DATRW_abort("unkown data type!");
180  }
181  }
182 
183  } // namespace
184 
185  } // namespace binary
186 
187  /*----------------------------------------------------------------------*/
188 
190  {
191  this->readheader();
192  Tdseries retval;
194  this->setnsamples(retval.size());
195  this->readflags();
196  return(retval);
197  } // Tdseries ibinarystream::dseries()
198 
199  /*----------------------------------------------------------------------*/
200 
202  {
203  this->readheader();
204  Tfseries retval;
206  this->setnsamples(retval.size());
207  this->readflags();
208  return(retval);
209  } // Tfseries ibinarystream::fseries()
210 
211  /*----------------------------------------------------------------------*/
212 
214  {
215  this->readheader();
216  Tiseries retval;
218  this->setnsamples(retval.size());
219  this->readflags();
220  return(retval);
221  } // Tiseries ibinarystream::iseries()
222 
223  /*----------------------------------------------------------------------*/
224 
226  {
227  unsigned int nsamples;
228  this->readheader();
230  {
231  nsamples=Mibs.skipdseries();
232  }
233  else if (Mnextflags & ::datrw::binary::Ffloat)
234  {
236  }
237  else if (Mnextflags & ::datrw::binary::Fint)
238  {
240  }
241  else
242  {
243  DATRW_abort("ERROR (ibinarystream::skipseries): data type not indicated");
244  }
245  this->setnsamples(nsamples);
246  this->readflags();
247  } // void ibinarystream::skipseries()
248 
249  /*----------------------------------------------------------------------*/
250 
251  void ibinarystream::setnsamples(const unsigned int& nsamples)
252  {
253  ::sff::WID2 wid2=this->wid2();
254  wid2.nsamples=nsamples;
255  this->setwid2(wid2);
256  } // void ibinarystream::setnsamples(const unsigned int& nsamples)
257 
258  /*----------------------------------------------------------------------*/
259 
260  void ibinarystream::help(std::ostream& os)
261  {
262  os <<
263  std::endl <<
264  "BINARY reading functions" << std::endl <<
265  "------------------------" << std::endl <<
266  DATRW_IBINARYSTREAM_CC_VERSION << std::endl <<
267  std::endl;
268  os <<
269  "This module reads binary data.\n"
270  "See the output module for further comments.\n"
271  << std::endl;
272  } // void ibinarystream::help(std::ostream& os)
273 
274 } // namespace datrw
275 
276 /* ----- END OF ibinarystream.cc ----- */
static void help(std::ostream &os=std::cout)
#define DATRW_assert(C, M)
Check an assertion and report by throwing an exception.
Definition: error.h:92
macro function for debugging output (prototypes)
Cout convert(const typename Cin::Tcoc &data)
function template to convert a whole series
Definition: util.h:74
void setinfo(const sff::INFO &info)
Definition: datread.cc:121
aff::Series< float > Tfseries
Definition: types.h:46
void checktraceflags(const char &flags)
abort if trace flags are inconsistent
Definition: binary.cc:66
virtual void skipseries()
void checkfileflags(const char &flags)
abort if file flags are inconsistent
Definition: binary.cc:58
sff::WID2 wid2() const
Definition: datread.h:111
void setfilefree(const sff::FREE &free)
Definition: datread.cc:97
trace has INFO header
Definition: binary.h:81
virtual Tfseries fseries()
ibinarystream(std::istream &is, const bool &debug=false)
write raw binary data (prototypes)
const char *const magic
magic number to identify file type and bytesex
Definition: binary.cc:53
unsigned int skipfseries()
Definition: ibinstream.cc:289
static bool report_on_construct_flag()
return report on construct flag
Definition: exception.h:93
void setsrce(const sff::SRCE &srce)
Definition: datread.cc:129
static void dont_report_on_construct()
Issue NO screen report on construction of exception.
Definition: exception.cc:90
aff::Series< double > Tdseries
Definition: types.h:45
#define DATRW_IBINARYSTREAM_CC_VERSION
const int nsamples
number of samples per minute block and channel
Definition: hpmodata.h:51
exception class declaration for libdatrwxx (prototypes)
unsigned int skipdseries()
Definition: ibinstream.cc:275
trace has int data
Definition: binary.h:84
Base class for exceptions.
Definition: exception.h:62
std::istream & Mis
Definition: datread.h:126
bool last() const
Definition: datread.h:94
static const std::ios_base::openmode openmode
Definition: binary.h:114
void setwid2(const sff::WID2 &wid2)
Definition: datread.cc:113
trace has SRCE header
Definition: binary.h:79
const short version
a version number for files - just in case
Definition: binary.cc:54
sff::INFO info() const
Definition: datread.h:110
trace has float data
Definition: binary.h:83
Root namespace of library.
Definition: aalibdatrwxx.cc:16
void setnsamples(const unsigned int &nsamples)
trace has double data
Definition: binary.h:82
utilities used by more than one type of data reader (prototypes)
virtual Tiseries iseries()
void readany(ibinstream &is, typename aff::Series< T > &series, const char &flags)
sff::SRCE srce() const
Definition: datread.h:109
sff::FREE free() const
Definition: datread.cc:79
#define DATRW_abort(M)
Abort and give a message.
Definition: error.h:101
void settracefree(const sff::FREE &free)
Definition: datread.cc:105
aff::Series< int > Tiseries
Definition: types.h:47
stream like class for binary input of basic types and classes
Definition: ibinstream.h:56
virtual Tdseries dseries()
#define DATRW_debug(C, N, M)
produce debug output
Definition: debug.h:50
char Mnextflags
flags for next trace
Definition: binary.h:121
unsigned int skipiseries()
Definition: ibinstream.cc:303
trace has FREE header
Definition: binary.h:80
binary::ibinstream Mibs
Definition: binary.h:119