DATRW++ library: seismic data I/O with multiple formats
obinstream.cc
Go to the documentation of this file.
1 /*! \file obinstream.cc
2  * \brief basic binary output stream (implementation)
3  *
4  * ----------------------------------------------------------------------------
5  *
6  * \author Thomas Forbriger
7  * \date 10/11/2011
8  *
9  * basic binary output 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_OBINSTREAM_CC_VERSION \
38  "DATRW_OBINSTREAM_CC V1.1"
39 
40 #include <datrwxx/obinstream.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  obinstream::obinstream(std::ostream& os,
50  const char* const magic,
51  const bool& debug):
52  Mos(os), Mdebug(debug)
53  {
55  } // obinstream::obinstream(std::ofstream& is,
56 
57  /*----------------------------------------------------------------------*/
58 
59  void obinstream::write(const char& v)
60  {
61  Mos.write(reinterpret_cast<const char *>(&v), sizeof(char));
62  } // void obinstream::write(const char& v)
63 
64  /*----------------------------------------------------------------------*/
65 
66  void obinstream::write(const short& v)
67  {
68  DATRW_debug(Mdebug, "obinstream::write(const short& v)", "v=" << v);
69  Mos.write(reinterpret_cast<const char *>(&v), sizeof(short));
70  } // void obinstream::write(const short& v)
71 
72  /*----------------------------------------------------------------------*/
73 
74  void obinstream::write(const int& v)
75  {
76  Mos.write(reinterpret_cast<const char *>(&v), sizeof(int));
77  } // void obinstream::write(const int& v)
78 
79  /*----------------------------------------------------------------------*/
80 
81  void obinstream::write(const unsigned int& v)
82  {
83  Mos.write(reinterpret_cast<const char *>(&v), sizeof(unsigned int));
84  } // void obinstream::write(const unsigned int& v)
85 
86  /*----------------------------------------------------------------------*/
87  void obinstream::write(const double& v)
88  {
89  Mos.write(reinterpret_cast<const char *>(&v), sizeof(double));
90  } // void obinstream::write(const double& v)
91 
92  /*----------------------------------------------------------------------*/
93 
94  void obinstream::write(const float& v)
95  {
96  Mos.write(reinterpret_cast<const char *>(&v), sizeof(float));
97  } // void obinstream::write(const float& v)
98 
99  /*----------------------------------------------------------------------*/
100 
101  void obinstream::write(const std::string& v)
102  {
103  unsigned int length=v.length();
104  this->write(length);
105  Mos.write(reinterpret_cast<const char *>(v.c_str()),
106  length*sizeof(char));
107  } // void obinstream::write(const std::string& v)
108 
109  /*----------------------------------------------------------------------*/
110 
111  void obinstream::write(const libtime::TAbsoluteTime& v)
112  {
113  this->write(static_cast<unsigned int>(v.year()));
114  this->write(static_cast<char>(v.month()));
115  this->write(static_cast<char>(v.day()));
116  this->write(static_cast<char>(v.hour()));
117  this->write(static_cast<char>(v.minute()));
118  this->write(static_cast<char>(v.second()));
119  this->write(static_cast<short>(v.milsec()));
120  this->write(static_cast<short>(v.micsec()));
121  } // void obinstream::write(const libtime::TAbsoluteTime& v)
122 
123  /*----------------------------------------------------------------------*/
124 
125  void obinstream::write(const ::sff::FREE& v)
126  {
127  const ::sff::FREE::Tlines& lines=v.lines;
128  unsigned int nlines=lines.size();
129  this->write(nlines);
130  ::sff::FREE::Tlines::const_iterator I=lines.begin();
131  while (I!=lines.end())
132  {
133  this->write(*I);
134  ++I;
135  }
136  } // void obinstream::write(const ::sff::FREE& v)
137 
138  /*----------------------------------------------------------------------*/
139 
140  void obinstream::write(const ::sff::WID2& v)
141  {
142  this->write(v.date);
143  this->write(v.dt);
144  this->write(v.nsamples);
145  this->write(v.station);
146  this->write(v.channel);
147  this->write(v.auxid);
148  this->write(v.instype);
149  this->write(v.calib);
150  this->write(v.calper);
151  this->write(v.hang);
152  this->write(v.vang);
153  } // void obinstream::write(const ::sff::WID2& v)
154 
155  /*----------------------------------------------------------------------*/
156 
157  void obinstream::write(const ::sff::SRCE& v)
158  {
159  this->write(v.date);
160  this->write(v.type);
161  char cs=::sff::coosysID(v.cs);
162  this->write(cs);
163  this->write(v.cx);
164  this->write(v.cy);
165  this->write(v.cz);
166  } // void obinstream::write(const ::sff::SRCE& v)
167 
168  /*----------------------------------------------------------------------*/
169 
170  void obinstream::write(const ::sff::INFO& v)
171  {
172  char cs=::sff::coosysID(v.cs);
173  this->write(cs);
174  this->write(v.cx);
175  this->write(v.cy);
176  this->write(v.cz);
177  this->write(v.nstacks);
178  } // void obinstream::write(const ::sff::INFO& v)
179 
180  /*----------------------------------------------------------------------*/
181 
182  void obinstream::write(const Tdseries::Tcoc& v)
183  {
184  int first=v.f();
185  int last=v.l();
186  this->write(first);
187  this->write(last);
188  Mos.write(reinterpret_cast<const char *>(v.pointer()),
189  v.size()*sizeof(Tdseries::Tvalue));
190  } // void obinstream::write(const Tdseries::Tcoc& v)
191 
192  /*----------------------------------------------------------------------*/
193 
194  void obinstream::write(const Tfseries::Tcoc& v)
195  {
196  int first=v.f();
197  int last=v.l();
198  this->write(first);
199  this->write(last);
200  Mos.write(reinterpret_cast<const char *>(v.pointer()),
201  v.size()*sizeof(Tfseries::Tvalue));
202  } // void obinstream::write(const Tfseries::Tcoc& v)
203 
204  /*----------------------------------------------------------------------*/
205 
206  void obinstream::write(const Tiseries::Tcoc& v)
207  {
208  int first=v.f();
209  int last=v.l();
210  this->write(first);
211  this->write(last);
212  Mos.write(reinterpret_cast<const char *>(v.pointer()),
213  v.size()*sizeof(Tiseries::Tvalue));
214  } // void obinstream::write(const Tiseries::Tcoc& v)
215 
216  } // namespace binary
217 
218 } // namespace datrw
219 
220 /* ----- END OF obinstream.cc ----- */
macro function for debugging output (prototypes)
const char *const magic
magic number to identify file type and bytesex
Definition: binary.cc:53
exception class declaration for libdatrwxx (prototypes)
A copy of bytesex.h from libtfxx (prototypes)
void write(const char &v)
Definition: obinstream.cc:59
int Tvalue
Definition: pdasread.h:75
Root namespace of library.
Definition: aalibdatrwxx.cc:16
obinstream(std::ostream &os, const char *const magic, const bool &debug=false)
Definition: obinstream.cc:49
#define DATRW_debug(C, N, M)
produce debug output
Definition: debug.h:50
std::ostream & Mos
Definition: obinstream.h:77
void file_magic_write(std::ostream &os, const char *const cmagic, const bool &fortranmode)
write magic number to file
Definition: bytesex.cc:128