Loading...
Searching...
No Matches
VariableFormatter.h
1/*
2
3MIT License
4
5Copyright (c) 2017 FMI Open Development / Markus Peura, first.last@fmi.fi
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software and associated documentation files (the "Software"), to deal
9in the Software without restriction, including without limitation the rights
10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in all
15copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23SOFTWARE.
24
25*/
26/*
27Part of Rack development has been done in the BALTRAD projects part-financed
28by the European Union (European Regional Development Fund and European
29Neighbourhood Partnership Instrument, Baltic Sea Region Programme 2007-2013)
30*/
31
32#ifndef VARIABLE_FORMATTER_H_
33#define VARIABLE_FORMATTER_H_
34
35#include <drain/Log.h>
36#include <map>
37#include <list>
38#include <iterator>
39#include <sstream>
40
41#include <drain/Convert.h>
42#include <drain/RegExp.h>
43#include <drain/StringTools.h>
44// #include <drain/VariableAssign.h>
45#include <drain/SmartMapTools.h>
46//#include "Variable.h"
47
48#include "IosFormat.h"
49//#include "MapTools.h"
50
51//#include "Sprinter.h"
52#include "Time.h"
53
54namespace drain {
55
56
58
70template <class T=std::string>
72
73public:
74
75 inline virtual
77
78 IosFormat iosFormat;
79
80 typedef std::map<std::string,T> map_t;
81
83
90 virtual
91 bool handle(const std::string & key, const map_t & variables, std::ostream & ostr) const final {
92
93 drain::Logger mout(__FILE__, __FUNCTION__);
94
95 std::string k,format;
96 drain::StringTools::split2(key, k, format, '|');
97 // mout.attention("split '", key, "' to ", k, " + ", format);
98 return handle(k, format, variables, ostr);
99 }
100
102
109 virtual
110 bool handle(const std::string & key, const std::string & format, const map_t & variables, std::ostream & ostr) const {
111
112 const typename std::map<std::string,T>::const_iterator it = variables.find(key);
113 if (it == variables.end()) {
114 // Don't format.
115 return false;
116 }
117
118 const T & value = it->second;
119 if (format.empty()){
120 iosFormat.copyTo(ostr);
121 // vostr.width(width);
122 // vstd::cerr << __FILE__ << " assign -> " << stringlet << std::endl;
123 // vstd::cerr << __FILE__ << " assign <---- " << mit->second << std::endl;
124 ostr << value;
125 return true;
126 }
127 else {
128 // mout.attention("delegating '", k, "' to formatVariable: ", format);
129 // return formatVariable(k, variables, format, ostr);
130 return formatVariable(key, value, format, ostr);
131 }
132
133 }
134
135
137
142 // NOTE: must return false, if not found. Then, further processors may handle the variable tag (remove, change, leave it).
143 virtual
144 bool formatVariable(const std::string & key, const T & value, const std::string & format, std::ostream & ostr) const {
145 // drain::Logger mout(__FILE__, __FUNCTION__);
146 // Default implementation discards \c key.
147 return formatValue(value, format, ostr);
148 }
149
151 virtual inline
152 bool formatVariable(const std::string & key, const T & value, const std::string & format, std::string & str) const final {
153 std::stringstream sstr;
154 const bool result = formatVariable(key, value, format, sstr);
155 str = sstr.str();
156 return result;
157 }
158
159
161
175 static
176 bool formatValue(const T & value, const std::string & format, std::ostream & ostr) {
177
178 drain::Logger mout(__FILE__, __FUNCTION__);
179
180 if (format.empty()){
181 ostr << value;
182 return true;
183 }
184
185 const char firstChar = format.at(0);
186 const char lastChar = format.at(format.size()-1);
187
188 if (firstChar == ':'){
189
190 mout.attention<LOG_DEBUG>("substring extraction: ", format);
191
192 std::string s;
193 drain::Convert::convert(value, s);
194 //drain::StringTools::import(variable, s);
195
196 std::vector<size_t> v;
197 drain::StringTools::split(format, v, ':');
198 size_t pos = 0;
199 size_t count = s.size();
200
201 switch (v.size()) {
202 case 3:
203 count = v[2];
204 // no break
205 case 2:
206 pos = v[1];
207 if (pos >= s.size()){
208 mout.warn("index ", pos, " greater than size (", s.size(), ") of string value '", s, "' of '", value, "'");
209 return true;
210 }
211 count = std::min(count, s.size()-pos);
212 ostr << s.substr(v[1], count);
213 break;
214 default:
215 mout.warn("unsupported formatting '", format, "' for variable '", value, "'");
216 mout.advice("use :startpos or :startpos:count for substring extraction");
217 }
218 return true;
219
220 }
221 else if (firstChar == '%'){
222
223 mout.attention<LOG_DEBUG>("string formatting: ", format);
224
225 //else if (format.find('%') != std::string::npos){
226 //drain::MapTools::get(variables, key, s);
227
228 const size_t BUFFER_SIZE = 256;
229 char buffer[BUFFER_SIZE];
230 buffer[0] = '\0';
231 size_t n = 0;
232
233 switch (lastChar){
234 case 's':
235 {
236 std::string s;
237 drain::Convert::convert(value, s);
238 // drain::StringTools::import(variable, s);
239 n = std::sprintf(buffer, format.c_str(), s.c_str());
240 }
241 break;
242 case 'c':
243 {
244 std::string s;
245 drain::Convert::convert(value, s);
246 //drain::StringTools::import(variable, s);
247 n = std::sprintf(buffer, format.c_str(), s.at(0)); // ?
248 }
249 break;
250 case 'p':
251 mout.unimplemented("pointer type: ", format);
252 break;
253 case 'f':
254 case 'F':
255 case 'e':
256 case 'E':
257 case 'a':
258 case 'A':
259 case 'g':
260 case 'G':
261 {
262 double d = NAN; //nand()
263 drain::Convert::convert(value, d);
264 // drain::StringTools::import(variable, d);
265 //drain::MapTools::get(variables, key, d);
266 // ostr << d << "=>"; // debugging
267 n = std::sprintf(buffer, format.c_str(), d);
268 }
269 break;
270 case 'd':
271 case 'i':
272 case 'o':
273 case 'u':
274 case 'x':
275 case 'X':
276 {
277 int i = 0;
278 drain::Convert::convert(value, i);
279 // drain::convertAny(variable, i);
280 // drain::StringTools::import(variable, i);
281 // drain::MapTools::get(variables, key, i);
282 // ostr << i << "=>"; // debugging
283 n = std::sprintf(buffer, format.c_str(), i);
284 }
285 break;
286 default:
287 mout.warn("formatting '", format, "' requested for '", value, "' : unsupported type key: '", lastChar, "'");
288 return false; // could be also true, if seen as handled this way?
289 }
290
291 ostr << buffer;
292 if (n > BUFFER_SIZE){
293 mout.fail("formatting with '", format, "' exceeded buffer size (", BUFFER_SIZE, ')');
294 }
295
296 // mout.warn("time formatting '", format, "' requested for '", k, "' not ending with 'time' or 'date'!");
297 }
298
299 return true;
300 }
301
302
303
304};
305
306/*
307inline
308std::ostream & operator<<(std::ostream & ostr, const StringMapper & strmap){
309 return strmap.toStream(ostr);
310}
311*/
312
313
314} // NAMESPACE
315
316#endif /* STRINGMAPPER_H_ */
317
318// Drain
static void convert(const T1 &src, T2 &dst)
General case: dispatch to Converter<T1> traits.
Definition Convert.h:131
Stores precision, fillChar and fieldWidth applied by STD streams.
Definition IosFormat.h:45
LogSourc e is the means for a function or any program segment to "connect" to a Log.
Definition Log.h:326
Logger & warn(const TT &... args)
Possible error, but execution can continue.
Definition Log.h:440
Logger & fail(const TT &... args)
Possible error, but execution can continue. Special type of Logger::warn().
Definition Log.h:463
Logger & attention(const TT &... args)
Possible error, but execution can continue. Special type of Logger::warn().
Definition Log.h:486
Logger & unimplemented(const TT &... args)
Feature to be done. Special type of Logger::note().
Definition Log.h:521
static bool split2(const std::string &s, T1 &first, T2 &second, const C &separators, const std::string &trimChars=" \t\n")
Splits and trims a given std::string to a std Sequence.
static void split(const std::string &s, T &sequence, const C &separators, const std::string &trimChars=" \t\n")
Splits and trims a given std::string to a std Sequence.
Definition StringTools.h:516
Formats variables to output stream.
Definition VariableFormatter.h:71
static bool formatValue(const T &value, const std::string &format, std::ostream &ostr)
Given a value, print it formatted to stream.
Definition VariableFormatter.h:176
virtual bool formatVariable(const std::string &key, const T &value, const std::string &format, std::ostream &ostr) const
Given a key, retrieve an associated value from the map and print the value formatted into a stream.
Definition VariableFormatter.h:144
virtual bool formatVariable(const std::string &key, const T &value, const std::string &format, std::string &str) const final
Checks if variables have ODIM names (keys), and apply special formatting (currently with time stamps)
Definition VariableFormatter.h:152
virtual bool handle(const std::string &key, const map_t &variables, std::ostream &ostr) const final
Searches given key in a map, and if found, processes (formats) the value to ostream....
Definition VariableFormatter.h:91
virtual bool handle(const std::string &key, const std::string &format, const map_t &variables, std::ostream &ostr) const
Searches given key in a map, and if found, processes (formats) the value to ostream....
Definition VariableFormatter.h:110
Definition Data.h:1473