DATRW++ library: seismic data I/O with multiple formats
formatmodifier.h
Go to the documentation of this file.
1 /*! \file formatmodifier.h
2  * \brief provide format modifiers (prototypes)
3  *
4  * \ingroup group_formatmodifiers
5  * ----------------------------------------------------------------------------
6  *
7  * \author Thomas Forbriger
8  * \date 28/07/2011
9  *
10  * provide format modifiers (prototypes)
11  *
12  * ----
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26  * ----
27  *
28  * Copyright (c) 2011 by Thomas Forbriger (BFO Schiltach)
29  *
30  * REVISIONS and CHANGES
31  * - 28/07/2011 V1.0 Thomas Forbriger (thof)
32  * - 21/01/2012 V1.1 provide online help output support class
33  * - 05/07/2016 V1.2 thof: provide output operator for line indentation
34  * with modifier help
35  * - 08/07/2016 V1.3 make correct use of new DATRW_report_assert
36  *
37  * ============================================================================
38  */
39 
40 // include guard
41 #ifndef DATRW_FORMATMODIFIER_H_VERSION
42 
43 #define DATRW_FORMATMODIFIER_H_VERSION \
44  "DATRW_FORMATMODIFIER_H V1.3"
45 
46 #include <string>
47 #include <sstream>
48 #include <map>
49 #include <datrwxx/formats.h>
50 #include <datrwxx/error.h>
51 
52 /*! \brief Provide mechanism to extract format modifiers
53  *
54  * \defgroup group_formatmodifiers Internal utility: Support for format modifiers
55  */
56 
57 /*! \brief Issue warning if modifiers are passed but not evaluated
58  *
59  * \ingroup group_formatmodifiers
60  * \param F format enum
61  * \param M modifier string
62  */
63 #define DATRW_expect_no_modifier(F,M) \
64  DATRW_report_assert(M.length()<1, \
65  "no format modifier should be passed!\n" << \
66  "format " << anyID( F ) \
67  << " is not expected to handle format modifiers\n" \
68  << "any modifiers are silently ignored!");
69 
70 /*! \brief abort if user passed unused modifiers
71  *
72  * \ingroup group_formatmodifiers
73  * \param S instance of type datrw::Subformat to check
74  * \param F name of function issuing the message
75  */
76 #define DATRW_assert_modifiers_are_recognized(S,F) \
77  if (!S.allarechecked()) \
78  { \
79  std::cerr << "ERROR (" << F << "):" << std::endl; \
80  S.notchecked(std::cerr); \
81  } \
82  DATRW_assert(S.allarechecked(), "ERROR: additional unrecognized modifiers");
83 
84 namespace datrw {
85 
86  /*! \brief Namespace containing components of format modifiers
87  * \ingroup group_formatmodifiers
88  */
89  namespace formatmodifiers {
90 
91  /*----------------------------------------------------------------------*/
92 
93  /*! \brief A struct to store values for a given key
94  * \ingroup group_formatmodifiers
95  */
96  struct Value {
97  Value(const std::string& v):
98  value(v), checked(false) { }
99  Value():
100  value(""), checked(false) { }
101  std::string value; //!< value passed with parameter string
102  mutable bool checked; //!< true if value was checked
103  }; // struct Value
104 
105  /*----------------------------------------------------------------------*/
106 
107  /*! \brief A map to store parameters.
108  * \ingroup group_formatmodifiers
109  */
110  typedef std::map<std::string,Value> Tparamap;
111 
112  /*----------------------------------------------------------------------*/
113 
114  /*! \brief Create a parameter map from a parameter string
115  * \ingroup group_formatmodifiers
116  *
117  * \param p parameter string
118  * \param delimiter delimiter which separates two parameters
119  * \param assign symbol seprating key and value
120  * \return a multimap created from the parameter string
121  */
122  Tparamap makeparamap(const std::string& p,
123  const std::string& delimiter=":",
124  const std::string& assign="=");
125  /*----------------------------------------------------------------------*/
126 
127  /*! \brief Provide online help on modifiers
128  * \ingroup group_formatmodifiers
129  *
130  * \param os output stream to which online help should be written
131  */
132  void online_help(std::ostream& os);
133 
134  /*----------------------------------------------------------------------*/
135 
136  /*! \brief Help formatting modifier online help
137  * \ingroup group_formatmodifiers
138  * \sa datrw::osustream::help()
139  */
141  {
142  public:
143  ModifierHelp(std::ostream& os,
144  const unsigned int& width):
145  Mos(os), Mwidth(width) { }
146  std::ostream& operator()(const std::string& key,
147  const std::string& val);
148  std::ostream& operator()(const std::string& key);
149  std::ostream& operator()();
150  private:
151  std::ostream& Mos;
152  unsigned int Mwidth;
153  }; // class ModifierHelp
154 
155  /*! \brief Output operator for ModifierHelp
156  * \ingroup group_formatmodifiers
157  *
158  * For line indentation an object of type ModifierHelp can be used like an
159  * output stream;
160  *
161  * \sa datrw::mseed::help()
162  */
163  template<class C>
164  std::ostream& operator<<(ModifierHelp& mh, const C& c)
165  { return(mh() << c); }
166 
167  } // namespace formatmodifiers
168 
169  /*----------------------------------------------------------------------*/
170 
171  /*! \brief Class to handle format modifiers
172  * \ingroup group_formatmodifiers
173  *
174  * This class is used to parse a format modifier string.
175  * Detailed instructions will be given upon request.
176  * For some hints have a look at tests/libdatrwxxtests.c
177  */
178  class Subformat {
179  public:
180  //! \brief constructor parses format modifier string
181  Subformat(const std::string& modifiers):
182  Mparamap(formatmodifiers::makeparamap(modifiers, ":", "=")),
183  Mstream("")
184  { }
185  /*! \brief function operator returns string
186  * \param k key for which parameters should be read
187  * \param dev default value to be used, if parameter is not set
188  * \return string containing parameter value
189  */
190  std::string value(const std::string& k,
191  const std::string& dev="false") const;
192  /*! \brief function operator returns string stream
193  * \param k key for which parameters should be read
194  * \param dev default value to be used, if parameter is not set
195  * \return string stream from which parameters can be read using
196  * stream input operator
197  * This function replaces commas by whitespace before passing the
198  * parameter value to the string stream.
199  */
200  std::istringstream& operator()(const std::string& k,
201  const std::string& dev="false") const;
202  /*! \brief check if user provided this key
203  * \param k key to check for
204  * \return true if key is set in modifier string
205  */
206  bool isset(const std::string& k) const;
207  /*! \brief check if user provided keys not being recognized
208  * \return true is all modifiers passed by the user haven been evaluated
209  */
210  bool allarechecked() const;
211  /*! \brief output all modifier which have not been checked yet
212  */
213  void notchecked(std::ostream& os) const;
214  private:
215  //! actual container for parameters
217  //! string stream used by function operator member function
218  mutable std::istringstream Mstream;
219  }; // class Subformat
220 
221 } // namespace datrw
222 
223 #endif // DATRW_FORMATMODIFIER_H_VERSION (includeguard)
224 
225 /* ----- END OF formatmodifier.h ----- */
Subformat(const std::string &modifiers)
constructor parses format modifier string
void online_help(std::ostream &os)
Provide online help on modifiers.
bool allarechecked() const
check if user provided keys not being recognized
std::istringstream & operator()(const std::string &k, const std::string &dev="false") const
function operator returns string stream
Help formatting modifier online help.
Tparamap makeparamap(const std::string &p, const std::string &delimiter, const std::string &assign)
Create a parameter map from a parameter string.
Value(const std::string &v)
exception class declaration for libdatrwxx (prototypes)
std::string value
value passed with parameter string
Root namespace of library.
Definition: aalibdatrwxx.cc:16
std::ostream & operator<<(ModifierHelp &mh, const C &c)
Output operator for ModifierHelpFor line indentation an object of type ModifierHelp can be used like ...
ModifierHelp(std::ostream &os, const unsigned int &width)
formatmodifiers::Tparamap Mparamap
actual container for parameters
std::map< std::string, Value > Tparamap
A map to store parameters.
std::istringstream Mstream
string stream used by function operator member function
Class to handle format modifiersThis class is used to parse a format modifier string. Detailed instructions will be given upon request. For some hints have a look at tests/libdatrwxxtests.c.
bool isset(const std::string &k) const
check if user provided this key
A struct to store values for a given key.
void notchecked(std::ostream &os) const
output all modifier which have not been checked yet
std::string value(const std::string &k, const std::string &dev="false") const
function operator returns string
common description of formats (prototypes)
bool checked
true if value was checked