Loading...
Searching...
No Matches
VariableFormatterODIM.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#ifndef VARIABLE_FORMATTER_ODIM
32#define VARIABLE_FORMATTER_ODIM
33
34#include <ostream>
35#include <cmath>
36#include <string>
37#include <set>
38#include <algorithm>
39//
40#include <drain/util/VariableFormatter.h> // for VariableHandler
41#include <drain/util/Time.h>
42#include "ODIM.h"
43
44//#include "EncodingODIM.h"
45
46namespace rack {
47
49
64template <class T>
66
67public:
68
69 typedef typename drain::VariableFormatter<T>::map_t map_t;
70
71 /*
72 typedef std::set<std::string> nameSet;
73
74 static
75 const nameSet timeKeys; // = {"time", "starttime", "endtime"};
76
77 static
78 const nameSet dateKeys; // = {"date", "startdate", "enddate"};
79
80 static
81 const nameSet locationKeys; // = {"site", "src", "lat", "lon", "PLC", "NOD", "WMO"};
82 */
83
84 virtual inline
86
88 static
89 bool formatDate(std::ostream & ostr, const std::string & key, const T & value, const std::string & format = "%Y/%m/%d"){
90
91 /*
92 const size_t i = key.find(':');
93 if (i != std::string::npos){
94 return formatDate(ostr, key.substr(i+1), value, format);
95 }
96 */
97
98 if (ODIM::dateKeys.count(key) > 0){
99 // if (drain::StringTools::endsWith(key, "date")){
100 // ostr << "DATE:" << key << "=" << value << " {" << format << "} -> ";
101 ostr << drain::Time(value, "%Y%m%d").str(format);
102 return true;
103 }
104 else {
105 return false;
106 }
107 }
108
110 static
111 bool formatTime(std::ostream & ostr, const std::string & key, const T & value, const std::string & format = "%H:%M"){// add UTC
112
113 /*
114 const size_t i = key.find(':');
115 if (i != std::string::npos){
116 return formatTime(ostr, key.substr(i+1), value, format);
117 }
118 */
119
120 if (ODIM::timeKeys.count(key) > 0){
121 // if (drain::StringTools::endsWith(key, "time")){
122 // ostr << "TIME:" << key << "=" << value << " {" << format << "} -> ";
123 ostr << drain::Time(value, "%H%M%S").str(format);
124 return true;
125 }
126 else {
127 return false;
128 }
129 }
130
132 static
133 bool formatPlace(std::ostream & ostr, const std::string & key, const T & value, const std::string & format = ""){
134
135 if (ODIM::locationKeys.count(key) > 0){
136 // Recognize location/place, but still apply default formatter, to support numeric formatting like ${where:lon|%04.3f}
137 // drain::VariableFormatter<T>::formatVariable(key, value, format, ostr);
138 ostr << value;
139 return true;
140 }
141 else {
142 return false;
143 }
144 }
145
147 virtual
148 bool formatVariable(const std::string & key, const T & value, const std::string & format, std::ostream & ostr) const override;
149
150
151
152};
153
155template <class T>
156bool VariableFormatterODIM<T>::formatVariable(const std::string & key, const T & value, const std::string & format, std::ostream & ostr) const {
157
158 drain::Logger mout(__FILE__, __FUNCTION__);
159 // mout.warn("trying time format: ", key, " + ", format);
160
161 if (format.find('%') != std::string::npos){
162 // Time formatting (instead of C-stype printf formatting)td::ostream & ostr
163 if (formatDate(ostr, key, value, format)){
164 // mout.attention("formatting DATE");
165 return true;
166 }
167 else if (formatTime(ostr, key, value, format)){
168 // mout.attention("formatting TIME");
169 return true;
170 }
171 else if (ODIM::locationKeys.count(key) > 0){ // skip formatPlace
172 // if (formatPlace(ostr, key, value, format)){
173 // Recognize location/place, but still apply default formatter, to support numeric formatting like ${where:lon|%04.3f}
174 drain::VariableFormatter<T>::formatVariable(key, value, format, ostr);
175 // mout.attention("formatting PLACE");
176 return true;
177 }
178 }
179
180 // Else, use default formatting:
181 return drain::VariableFormatter<T>::formatVariable(key, value, format, ostr); // basic/trad.
182}
183
184
185
186} // namespace rack
187
188
189#endif
LogSourc e is the means for a function or any program segment to "connect" to a Log.
Definition Log.h:312
Utility for handling time. Internally, uses tm (C time structure).
Definition Time.h:54
const std::string & str(const std::string &format="") const
Returns the std::string using formatting as defined by strftime()
Definition Time.cpp:76
Formats variables to output stream.
Definition VariableFormatter.h:71
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:128
Formats variables to output stream.
Definition VariableFormatterODIM.h:65
static bool formatDate(std::ostream &ostr, const std::string &key, const T &value, const std::string &format="%Y/%m/%d")
Recognizes and format a date. Assumes that the variable name (key ) ends with "date".
Definition VariableFormatterODIM.h:89
virtual bool formatVariable(const std::string &key, const T &value, const std::string &format, std::ostream &ostr) const override
Checks if variables have ODIM names (keys), and apply special formatting (currently with time stamps)
Definition VariableFormatterODIM.h:156
static bool formatPlace(std::ostream &ostr, const std::string &key, const T &value, const std::string &format="")
Currently, only recognizes a place, and writes it directly in stream.
Definition VariableFormatterODIM.h:133
static bool formatTime(std::ostream &ostr, const std::string &key, const T &value, const std::string &format="%H:%M")
Reformat a time. Assumes that the variable name (key ) ends with "time".
Definition VariableFormatterODIM.h:111
Definition DataSelector.cpp:44