32 #ifndef VARIABLE_FORMATTER_H_
33 #define VARIABLE_FORMATTER_H_
35 #include <drain/Log.h>
41 #include <drain/Convert.h>
42 #include <drain/RegExp.h>
43 #include <drain/String.h>
45 #include <drain/SmartMapTools.h>
48 #include "IosFormat.h"
70 template <
class T=std::
string>
80 typedef std::map<std::string,T> map_t;
88 bool handle(
const std::string & key,
const map_t & variables, std::ostream & ostr)
const {
96 const typename std::map<std::string,T>::const_iterator it = variables.find(k);
97 if (it == variables.end()) {
104 iosFormat.copyTo(ostr);
128 bool formatVariable(
const std::string & key,
const T & value,
const std::string & format, std::ostream & ostr)
const {
136 bool formatVariable(
const std::string & key,
const T & value,
const std::string & format, std::string & str)
const final {
137 std::stringstream sstr;
149 bool formatValue(
const T & value,
const std::string & format, std::ostream & ostr) {
153 const char firstChar = format.at(0);
154 const char lastChar = format.at(format.size()-1);
156 if (firstChar ==
':'){
158 mout.
attention<LOG_DEBUG>(
"substring extraction: ", format);
164 std::vector<size_t> v;
167 size_t count = s.size();
175 if (pos >= s.size()){
176 mout.
warn(
"index ", pos,
" greater than size (", s.size(),
") of string value '", s,
"' of '", value,
"'");
179 count = std::min(count, s.size()-pos);
180 ostr << s.substr(v[1], count);
183 mout.
warn(
"unsupported formatting '", format,
"' for variable '", value,
"'");
184 mout.advice(
"use :startpos or :startpos:count for substring extraction");
189 else if (firstChar ==
'%'){
191 mout.
attention<LOG_DEBUG>(
"string formatting: ", format);
196 const size_t BUFFER_SIZE = 256;
197 char buffer[BUFFER_SIZE];
207 n = std::sprintf(buffer, format.c_str(), s.c_str());
215 n = std::sprintf(buffer, format.c_str(), s.at(0));
235 n = std::sprintf(buffer, format.c_str(), d);
251 n = std::sprintf(buffer, format.c_str(), i);
255 mout.
warn(
"formatting '", format,
"' requested for '", value,
"' : unsupported type key: '", lastChar,
"'");
260 if (n > BUFFER_SIZE){
261 mout.
fail(
"formatting with '", format,
"' exceeded buffer size (", BUFFER_SIZE,
')');
static void convert(const T &src, T &dst)
Trivial case: source and destination are of same class.
Definition: Convert.h:94
static void convert(const T &src, T &dst)
Trivial case: source and destination are of same class.
Definition: Convert.h:58
LogSourc e is the means for a function or any program segment to "connect" to a Log.
Definition: Log.h:310
Logger & attention(const TT &... args)
Possible error, but execution can continue. Special type of Logger::warn().
Definition: Log.h:474
Logger & fail(const TT &... args)
Possible error, but execution can continue. Special type of Logger::warn().
Definition: Log.h:451
Logger & unimplemented(const TT &... args)
Feature to be done. Special type of Logger::note().
Definition: Log.h:509
Logger & warn(const TT &... args)
Possible error, but execution can continue.
Definition: Log.h:428
Definition: DataSelector.cpp:1277