DATRW++ library: seismic data I/O with multiple formats
formatmodifier.cc
Go to the documentation of this file.
1 /*! \file formatmodifier.cc
2  * \brief provide format modifiers (implementation)
3  *
4  * \ingroup group_formatmodifiers
5  * ----------------------------------------------------------------------------
6  *
7  * \author Thomas Forbriger
8  * \date 28/07/2011
9  *
10  * provide format modifiers (implementation)
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
32  * - 21/01/2012 V1.1 provide online help output support class
33  *
34  * ============================================================================
35  */
36 #define DATRW_FORMATMODIFIER_CC_VERSION \
37  "DATRW_FORMATMODIFIER_CC V1.1"
38 
39 #include <iostream>
40 #include <iomanip>
41 #include <datrwxx/formatmodifier.h>
42 #include <datrwxx/util.h>
43 #include <algorithm>
44 
45 namespace datrw {
46 
47  namespace formatmodifiers {
48 
49  /*----------------------------------------------------------------------*/
50 
51  Tparamap makeparamap(const std::string& p,
52  const std::string& delimiter,
53  const std::string& assign)
54  {
55  std::string parameters=p;
56  Tparamap retval;
57  while (parameters.length()>0)
58  {
59  std::string value=datrw::util::clipstring(parameters, delimiter);
60  std::string key
62  if (value.length()==0)
63  {
64  retval[key]=Value("true");
65  }
66  else
67  {
68  retval[key]=Value(value);
69  }
70  } // while (parameters.length()>0)
71  return retval;
72  } // Tparamap makeparamap()
73 
74  /*----------------------------------------------------------------------*/
75 
76  void online_help(std::ostream& os)
77  {
78  os << "format modifiers" << std::endl;
79  os << "----------------" << std::endl;
80  os <<
81  "Some I/O streams support format modifiers. These can be used to\n"
82  "control the behaviour of the specific I/O stream. The modifiers\n"
83  "are passed through a string to the I/O stream. Each modifier\n"
84  "consists of a keyword and an optional parameter string. Several\n"
85  "modifiers can be concatenated being separated by a colon (:).\n"
86  "Keyword and parameter are separated by an equal sign (=).\n"
87  "If the program takes format identifiers as string values,\n"
88  "format modifiers usually can be appended to the format ID with\n"
89  "a separating colon in between.\n"
90  "\n"
91  "Example: To read a seife format file, using 4.7.2011 as the day\n"
92  "of recording and \"BFO\" as recording station, you could use\n"
93  "\n"
94  " seife:date=2011/7/4:station=BFO\n"
95  "\n"
96  "to specify the input format.";
97  os << std::endl;
98  } // void online_help(std::ostream& os)
99 
100  } // namespace formatmodifiers
101 
102  /*======================================================================*/
103  // class Subformat
104 
105  std::istringstream&
106  Subformat::operator()(const std::string& k,
107  const std::string& def) const
108  {
109  Mstream.clear();
110  Mstream.str(util::commatospace(this->value(k, def)));
111  return(Mstream);
112  } // std::istringstream&
113  // Subformat::operator()(const std::string& k,
114  // const std::string& def) const
115 
116  /*----------------------------------------------------------------------*/
117 
118  std::string
119  Subformat::value(const std::string& k,
120  const std::string& def) const
121  {
122  std::string retval;
123  formatmodifiers::Tparamap::const_iterator P=Mparamap.find(k);
124  if (P!=Mparamap.end())
125  {
126  retval=P->second.value;
127  P->second.checked=true;
128  }
129  else
130  {
131  retval=def;
132  }
133  return retval;
134  } // std::istringstream&
135  // Subformat::value(const std::string& k,
136  // const std::string& def) const
137 
138 
139  /*----------------------------------------------------------------------*/
140 
141  bool Subformat::isset(const std::string& k) const
142  {
143  formatmodifiers::Tparamap::const_iterator P=Mparamap.find(k);
144  bool retval=(P!=Mparamap.end());
145  if (retval)
146  {
147  P->second.checked=true;
148  }
149  return(retval);
150  } // bool Subformat::isset(const std::string& k) const
151 
152  /*----------------------------------------------------------------------*/
153 
155  {
156  bool retval=true;
157  formatmodifiers::Tparamap::const_iterator I=Mparamap.begin();
158  while (I!=Mparamap.end())
159  {
160  if (!I->second.checked) { retval=false; }
161  ++I;
162  }
163  return(retval);
164  } // bool Subformat::allarechecked() const
165 
166  /*----------------------------------------------------------------------*/
167 
168  void Subformat::notchecked(std::ostream& os) const
169  {
170  if (!this->allarechecked())
171  {
172  formatmodifiers::Tparamap::const_iterator I=Mparamap.begin();
173  os << "Format modifiers which were not recognized:" << std::endl;
174  while (I!=Mparamap.end())
175  {
176  if (!I->second.checked)
177  {
178  os << " " << I->first << "=" << I->second.value << std::endl;
179  }
180  ++I;
181  }
182  }
183  } // void Subformat::notchecked(std::ostream& os) const
184 
185  /*======================================================================*/
186 
187  namespace formatmodifiers {
188 
190  {
191  Mos.width(Mwidth);
192  Mos << std::left << std::setfill(' ') << " " << " ";
193  return(Mos);
194  } // std::ostream& ModifierHelp::operator() ()
195 
196  /*----------------------------------------------------------------------*/
197 
198  std::ostream& ModifierHelp::operator() (const std::string& key)
199  {
200  Mos.width(Mwidth);
201  std::string keyval=key;
202  keyval += ": ";
203  Mos << std::left << std::setfill('.') << keyval << " ";
204  return(Mos);
205  } // std::ostream& ModifierHelp::operator() (key)
206 
207  /*----------------------------------------------------------------------*/
208 
209  std::ostream& ModifierHelp::operator() (const std::string& key,
210  const std::string& val)
211  {
212  Mos.width(Mwidth);
213  std::string keyval=key;
214  keyval += "=" + val + ": ";
215  Mos << std::left << std::setfill('.') << keyval << " ";
216  return(Mos);
217  } // std::ostream& ModifierHelp::operator() (key, val)
218 
219  } // namespace formatmodifiers
220 
221 } // namespace datrw
222 
223 /* ----- END OF formatmodifier.cc ----- */
std::string commatospace(std::string s)
Definition: util.cc:121
provide format modifiers (prototypes)
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
Tparamap makeparamap(const std::string &p, const std::string &delimiter, const std::string &assign)
Create a parameter map from a parameter string.
std::string trimws(std::string s)
remove leading and trailing whitespace
Definition: util.cc:129
Root namespace of library.
Definition: aalibdatrwxx.cc:16
utilities used by more than one type of data reader (prototypes)
formatmodifiers::Tparamap Mparamap
actual container for parameters
std::map< std::string, Value > Tparamap
A map to store parameters.
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
std::istringstream Mstream
string stream used by function operator member function
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