DATRW++ library: seismic data I/O with multiple formats
util.h
Go to the documentation of this file.
1 /*! \file util.h
2  * \brief utilities used by more than one type of data reader (prototypes)
3  *
4  * ----------------------------------------------------------------------------
5  *
6  * \author Thomas Forbriger
7  * \date 22/12/2004
8  *
9  * utilities used by more than one type of data reader (prototypes)
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 St, Fifth Floor, Boston, MA 02110-1301 USA
25  * ----
26  *
27  * Copyright (c) 2004 by Thomas Forbriger (BFO Schiltach)
28  *
29  * REVISIONS and CHANGES
30  * - 22/12/2004 V1.0 Thomas Forbriger
31  * - 29/07/2011 V1.1 added clipstring
32  * - 02/09/2011 V1.2 remove DATRW_debug from here, this macro is
33  * presented in debug.h already
34  * - 06/09/2011 V1.3 moved string manipulation functions to this file:
35  * commatospace and trimws
36  *
37  * ============================================================================
38  */
39 
40 // include guard
41 #ifndef DATRW_UTIL_H_VERSION
42 
43 #define DATRW_UTIL_H_VERSION \
44  "DATRW_UTIL_H V1.3"
45 
46 #include<iostream>
47 #include<string>
48 #include<datrwxx/error.h>
49 #include<datrwxx/datatypes.h>
50 #include<aff/iterator.h>
51 
52 namespace datrw {
53 
54 /*! \defgroup group_util Internal utilities: Miscellaneous utility functions
55  */
56 
57  /*! \brief Some components used by several I/O format modules
58  * \ingroup group_util
59  */
60  namespace util {
61 
62  /*----------------------------------------------------------------------*/
63  /*! \brief function template to convert a whole series
64  * \ingroup group_util
65  *
66  * \param Cin series container type of the input series
67  * like datrw::Tdseries
68  * \param Cout series container type of the output series
69  * like datrw::Tfseries
70  * \param data actual input series container
71  * \return series container with converted data
72  */
73  template<class Cin, class Cout>
74  Cout convert(const typename Cin::Tcoc& data)
75  {
76  int nsamples=data.size();
77  Cout retval(nsamples);
78  typedef typename Cin::Tvalue Tinvalue;
79  typedef typename Cout::Tvalue Toutvalue;
80  aff::Iterator<Cout> IO(retval);
81  aff::Browser<Cin> BI(data);
82  ::datrw::report_conversion<Tinvalue, Toutvalue>(std::cout);
83  while (IO.valid() && BI.valid())
84  {
85  Tinvalue sample=*BI;
86  *IO=static_cast<Toutvalue>(sample);
87  ++IO;
88  ++BI;
89  }
90  return(retval);
91  } // template function convert
92 
93  /*----------------------------------------------------------------------*/
94 
95  /*! \brief two-argument conversion function which allows template function
96  * specialization
97  * \ingroup group_util
98  *
99  * \param Cin series container type of the input series
100  * like datrw::Tdseries
101  * \param Cout series container type of the output series
102  * like datrw::Tfseries
103  * \param data actual input series container
104  * \param outdata container with converted data
105  */
106  template<class Cin, class Cout>
107  void convert(const Cin& data,
108  Cout& outdata)
109  {
110  outdata=convert<Cin, Cout>(data);
111  } // template function convert
112 
113  /*----------------------------------------------------------------------*/
114 
115  /*! \brief specialization of function in case where no conversion is
116  * required
117  * \ingroup group_util
118  *
119  * \param C series container type of the input and output series
120  * like datrw::Tdseries
121  * \param data actual input series container
122  * \param outdata container with copied reference to data
123  */
124  template<class C>
125  void convert(const C& data,
126  C& outdata)
127  {
128  outdata=data;
129  } // template function convert
130 
131  /*----------------------------------------------------------------------*/
132 
133  /*! \brief read the trace data
134  * \ingroup group_util
135  \param is the input stream
136  \param nsamples the number of samples
137  \param streamname name of the stream for Exceptions
138  \return the trace
139  */
140  template<class C>
141  C readasciidouble(std::istream& is, const int nsamples,
142  const std::string& streamname)
143  {
144  std::string errorstr = "ERROR ("+streamname+
145  "::?series): number of samples is not positive!";
146  DATRW_assert(nsamples>0, errorstr.c_str());
147  C series(nsamples);
148  double inval;
149  errorstr = "ERROR ("+streamname+"::?series): bad stream!";
150  ::datrw::report_conversion<double, typename C::Tvalue>(std::cout);
151  for (int i = 0; i < nsamples; ++i)
152  {
153  DATRW_assert(is.good(), errorstr.c_str());
154  is >> inval;
155  series(i) = static_cast<typename C::Tvalue>(inval);
156  }
157  return series;
158  } // template function readascii
159 
160  /*======================================================================*/
161 
162  /*! \brief return number of significant digits
163  * \ingroup group_util
164  */
165  int nsignificantdigits(double v, const bool& debug=false);
166 
167  /*! \brief return number of trailing digits (after decimal point)
168  * \ingroup group_util
169  */
170  int ntrailingdigits(double v, const bool& debug=false);
171 
172  /*======================================================================*/
173 
174  /*! \brief strip substring
175  * \ingroup group_util
176  *
177  * Strips off first substring up to given delimiter.
178  * The string is passed as a reference and will be modified (i.e. the
179  * stripped substring as well as the delimiter will be erased).
180  *
181  * \param s input string
182  * \param delim delimiter to look for
183  * \return first part of string up to delimiter
184  */
185  std::string clipstring(std::string& s, const std::string& delim=":");
186 
187  /*----------------------------------------------------------------------*/
188 
189  /*! replace comma by whitespace
190  * \ingroup group_util
191  *
192  * \param s input string
193  * \return input string with all commas replaced by whitespace
194  */
195  std::string commatospace(std::string s);
196 
197  /*----------------------------------------------------------------------*/
198 
199  /*! \brief remove leading and trailing whitespace
200  * \ingroup group_util
201  *
202  * \param s any string
203  * \return value a input string with any leading and trailing whitespace
204  * removed
205  */
206  std::string trimws(std::string s);
207 
208  } // namespace util
209 
210 } // namespace datrw
211 
212 #endif // DATRW_UTIL_H_VERSION (includeguard)
213 
214 /* ----- END OF util.h ----- */
std::string commatospace(std::string s)
Definition: util.cc:121
#define DATRW_assert(C, M)
Check an assertion and report by throwing an exception.
Definition: error.h:92
C readasciidouble(std::istream &is, const int nsamples, const std::string &streamname)
read the trace data
Definition: util.h:141
Cout convert(const typename Cin::Tcoc &data)
function template to convert a whole series
Definition: util.h:74
const char *const streamname
name of the stream
Definition: readtfascii.cc:75
handle data types and data type conversion (prototypes)
const int nsamples
number of samples per minute block and channel
Definition: hpmodata.h:51
exception class declaration for libdatrwxx (prototypes)
const char *const data
keywords for consistency checks
std::string trimws(std::string s)
remove leading and trailing whitespace
Definition: util.cc:129
int Tvalue
Definition: pdasread.h:75
Root namespace of library.
Definition: aalibdatrwxx.cc:16
int ntrailingdigits(double v, const bool &debug)
return number of trailing digits (after decimal point)
Definition: util.cc:97
int nsignificantdigits(double v, const bool &debug)
return number of significant digits
Definition: util.cc:49
std::string clipstring(std::string &s, const std::string &delim)
strip substringStrips off first substring up to given delimiter. The string is passed as a reference ...
Definition: util.cc:105