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 {
153 bool formatValue(
const T & value,
const std::string & format, std::ostream & ostr) {
157 const char firstChar = format.at(0);
158 const char lastChar = format.at(format.size()-1);
160 if (firstChar ==
':'){
162 mout.
attention<LOG_DEBUG>(
"substring extraction: ", format);
168 std::vector<size_t> v;
171 size_t count = s.size();
179 if (pos >= s.size()){
180 mout.
warn(
"index ", pos,
" greater than size (", s.size(),
") of string value '", s,
"' of '", value,
"'");
183 count = std::min(count, s.size()-pos);
184 ostr << s.substr(v[1], count);
187 mout.
warn(
"unsupported formatting '", format,
"' for variable '", value,
"'");
188 mout.advice(
"use :startpos or :startpos:count for substring extraction");
193 else if (firstChar ==
'%'){
195 mout.
attention<LOG_DEBUG>(
"string formatting: ", format);
200 const size_t BUFFER_SIZE = 256;
201 char buffer[BUFFER_SIZE];
211 n = std::sprintf(buffer, format.c_str(), s.c_str());
219 n = std::sprintf(buffer, format.c_str(), s.at(0));
239 n = std::sprintf(buffer, format.c_str(), d);
255 n = std::sprintf(buffer, format.c_str(), i);
259 mout.
warn(
"formatting '", format,
"' requested for '", value,
"' : unsupported type key: '", lastChar,
"'");
264 if (n > BUFFER_SIZE){
265 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:308
Logger & attention(const TT &... args)
Possible error, but execution can continue. Special type of Logger::warn().
Definition: Log.h:472
Logger & fail(const TT &... args)
Possible error, but execution can continue. Special type of Logger::warn().
Definition: Log.h:449
Logger & unimplemented(const TT &... args)
Feature to be done. Special type of Logger::note().
Definition: Log.h:507
Logger & warn(const TT &... args)
Possible error, but execution can continue.
Definition: Log.h:426
Definition: DataSelector.cpp:1277