mirror of
https://github.com/ceph/ceph
synced 2025-01-03 09:32:43 +00:00
common: Formatter: cosmetic re-indent
Signed-off-by: Andreas Peters <andreas.joachim.peters@cern.ch>
This commit is contained in:
parent
e797dcf6e9
commit
d994e601d2
@ -59,35 +59,31 @@ FormatterAttrs::FormatterAttrs(const char *attr, ...)
|
||||
va_end(ap);
|
||||
}
|
||||
|
||||
Formatter::Formatter()
|
||||
{
|
||||
}
|
||||
Formatter::Formatter() { }
|
||||
|
||||
Formatter::~Formatter()
|
||||
{
|
||||
}
|
||||
Formatter::~Formatter() { }
|
||||
|
||||
Formatter *
|
||||
new_formatter(const std::string &type)
|
||||
{
|
||||
std::string mytype = type;
|
||||
if (mytype == "")
|
||||
mytype = "json-pretty";
|
||||
std::string mytype = type;
|
||||
if (mytype == "")
|
||||
mytype = "json-pretty";
|
||||
|
||||
if (mytype == "json")
|
||||
return new JSONFormatter(false);
|
||||
else if (mytype == "json-pretty")
|
||||
return new JSONFormatter(true);
|
||||
else if (mytype == "xml")
|
||||
return new XMLFormatter(false);
|
||||
else if (mytype == "xml-pretty")
|
||||
return new XMLFormatter(true);
|
||||
else if (mytype == "table")
|
||||
return new TableFormatter();
|
||||
else if (mytype == "table-kv")
|
||||
return new TableFormatter(true);
|
||||
else
|
||||
return (Formatter *)NULL;
|
||||
if (mytype == "json")
|
||||
return new JSONFormatter(false);
|
||||
else if (mytype == "json-pretty")
|
||||
return new JSONFormatter(true);
|
||||
else if (mytype == "xml")
|
||||
return new XMLFormatter(false);
|
||||
else if (mytype == "xml-pretty")
|
||||
return new XMLFormatter(true);
|
||||
else if (mytype == "table")
|
||||
return new TableFormatter();
|
||||
else if (mytype == "table-kv")
|
||||
return new TableFormatter(true);
|
||||
else
|
||||
return (Formatter *) NULL;
|
||||
}
|
||||
|
||||
void Formatter::dump_format(const char *name, const char *fmt, ...)
|
||||
@ -98,7 +94,7 @@ void Formatter::dump_format(const char *name, const char *fmt, ...)
|
||||
va_end(ap);
|
||||
}
|
||||
|
||||
void Formatter:: dump_format_ns(const char *name, const char *ns, const char *fmt, ...)
|
||||
void Formatter::dump_format_ns(const char *name, const char *ns, const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
@ -107,7 +103,6 @@ void Formatter:: dump_format_ns(const char *name, const char *ns, const char *fm
|
||||
|
||||
}
|
||||
|
||||
|
||||
void Formatter::dump_format_unquoted(const char *name, const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
@ -117,8 +112,9 @@ void Formatter::dump_format_unquoted(const char *name, const char *fmt, ...)
|
||||
}
|
||||
|
||||
// -----------------------
|
||||
|
||||
JSONFormatter::JSONFormatter(bool p)
|
||||
: m_pretty(p), m_is_pending_string(false)
|
||||
: m_pretty(p), m_is_pending_string(false)
|
||||
{
|
||||
reset();
|
||||
}
|
||||
@ -145,14 +141,14 @@ void JSONFormatter::print_comma(json_formatter_stack_entry_d& entry)
|
||||
if (entry.size) {
|
||||
if (m_pretty) {
|
||||
m_ss << ",\n";
|
||||
for (unsigned i=1; i < m_stack.size(); i++)
|
||||
m_ss << " ";
|
||||
for (unsigned i = 1; i < m_stack.size(); i++)
|
||||
m_ss << " ";
|
||||
} else {
|
||||
m_ss << ",";
|
||||
}
|
||||
} else if (entry.is_array && m_pretty) {
|
||||
m_ss << "\n";
|
||||
for (unsigned i=1; i < m_stack.size(); i++)
|
||||
for (unsigned i = 1; i < m_stack.size(); i++)
|
||||
m_ss << " ";
|
||||
}
|
||||
if (m_pretty && entry.is_array)
|
||||
@ -178,9 +174,9 @@ void JSONFormatter::print_name(const char *name)
|
||||
if (!entry.is_array) {
|
||||
if (m_pretty) {
|
||||
if (entry.size)
|
||||
m_ss << " ";
|
||||
m_ss << " ";
|
||||
else
|
||||
m_ss << " ";
|
||||
m_ss << " ";
|
||||
}
|
||||
m_ss << "\"" << name << "\"";
|
||||
if (m_pretty)
|
||||
@ -307,7 +303,7 @@ const char *XMLFormatter::XML_1_DTD =
|
||||
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
|
||||
|
||||
XMLFormatter::XMLFormatter(bool pretty)
|
||||
: m_pretty(pretty)
|
||||
: m_pretty(pretty)
|
||||
{
|
||||
reset();
|
||||
}
|
||||
@ -436,7 +432,7 @@ void XMLFormatter::dump_format_va(const char* name, const char *ns, bool quoted,
|
||||
std::string e(name);
|
||||
print_spaces();
|
||||
if (ns) {
|
||||
m_ss << "<" << e << " xmlns=\"" << ns << "\">" << buf << "</" << e << ">";
|
||||
m_ss << "<" << e << " xmlns=\"" << ns << "\">" << buf << "</" << e << ">";
|
||||
} else {
|
||||
m_ss << "<" << e << ">" << escape_xml_str(buf) << "</" << e << ">";
|
||||
}
|
||||
@ -479,8 +475,7 @@ void XMLFormatter::open_section_in_ns(const char *name, const char *ns, const Fo
|
||||
|
||||
if (ns) {
|
||||
m_ss << "<" << name << attrs_str << " xmlns=\"" << ns << "\">";
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
m_ss << "<" << name << attrs_str << ">";
|
||||
}
|
||||
if (m_pretty)
|
||||
@ -492,7 +487,7 @@ void XMLFormatter::finish_pending_string()
|
||||
{
|
||||
if (!m_pending_string_name.empty()) {
|
||||
m_ss << escape_xml_str(m_pending_string.str().c_str())
|
||||
<< "</" << m_pending_string_name << ">";
|
||||
<< "</" << m_pending_string_name << ">";
|
||||
m_pending_string_name.clear();
|
||||
m_pending_string.str(std::string());
|
||||
if (m_pretty) {
|
||||
@ -532,110 +527,110 @@ void TableFormatter::flush(std::ostream& os)
|
||||
std::set<int> need_header_set;
|
||||
|
||||
// auto-sizing columns
|
||||
for (size_t i=0; i< m_vec.size(); i++) {
|
||||
for (size_t j=0; j< m_vec[i].size(); j++) {
|
||||
for (size_t i = 0; i < m_vec.size(); i++) {
|
||||
for (size_t j = 0; j < m_vec[i].size(); j++) {
|
||||
column_size.resize(m_vec[i].size());
|
||||
column_name.resize(m_vec[i].size());
|
||||
if (i>0) {
|
||||
if (m_vec[i-1][j] != m_vec[i][j]) {
|
||||
// changing row labels require to show the header
|
||||
need_header_set.insert(i);
|
||||
column_name[i] = m_vec[i][j].first;
|
||||
}
|
||||
if (i > 0) {
|
||||
if (m_vec[i - 1][j] != m_vec[i][j]) {
|
||||
// changing row labels require to show the header
|
||||
need_header_set.insert(i);
|
||||
column_name[i] = m_vec[i][j].first;
|
||||
}
|
||||
} else {
|
||||
column_name[i] = m_vec[i][j].first;
|
||||
column_name[i] = m_vec[i][j].first;
|
||||
}
|
||||
|
||||
if (m_vec[i][j].second.length()> column_size[j])
|
||||
column_size[j]=m_vec[i][j].second.length();
|
||||
if (m_vec[i][j].first.length()> column_size[j])
|
||||
column_size[j]=m_vec[i][j].first.length();
|
||||
if (m_vec[i][j].second.length() > column_size[j])
|
||||
column_size[j] = m_vec[i][j].second.length();
|
||||
if (m_vec[i][j].first.length() > column_size[j])
|
||||
column_size[j] = m_vec[i][j].first.length();
|
||||
}
|
||||
}
|
||||
|
||||
bool need_header=false;
|
||||
if ( (column_size.size() == m_column_size.size() ) ) {
|
||||
for (size_t i=0; i<column_size.size(); i++) {
|
||||
|
||||
bool need_header = false;
|
||||
if ((column_size.size() == m_column_size.size())) {
|
||||
for (size_t i = 0; i < column_size.size(); i++) {
|
||||
if (column_size[i] != m_column_size[i]) {
|
||||
need_header = true;
|
||||
break;
|
||||
need_header = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
need_header = true;
|
||||
}
|
||||
|
||||
|
||||
if (need_header) {
|
||||
// first row always needs a header if there wasn't one before
|
||||
need_header_set.insert(0);
|
||||
}
|
||||
|
||||
|
||||
m_column_size = column_size;
|
||||
for (size_t i=0; i< m_vec.size(); i++) {
|
||||
if (i==0) {
|
||||
for (size_t i = 0; i < m_vec.size(); i++) {
|
||||
if (i == 0) {
|
||||
if (need_header_set.count(i)) {
|
||||
// print the header
|
||||
if (!m_keyval) {
|
||||
os << "+";
|
||||
for (size_t j=0; j< m_vec[i].size(); j++) {
|
||||
for (size_t v=0; v< m_column_size[j]+3; v++)
|
||||
os << "-";
|
||||
os << "+";
|
||||
}
|
||||
os << "\n";
|
||||
os << "|";
|
||||
|
||||
for (size_t j=0; j< m_vec[i].size(); j++) {
|
||||
os << " ";
|
||||
std::stringstream fs;
|
||||
fs << boost::format ("%%-%is") % (m_column_size[j]+2);
|
||||
os << boost::format (fs.str()) % m_vec[i][j].first;
|
||||
os << "|";
|
||||
}
|
||||
os << "\n";
|
||||
os << "+";
|
||||
for (size_t j=0; j< m_vec[i].size(); j++) {
|
||||
for (size_t v=0; v< m_column_size[j]+3;v++)
|
||||
os << "-";
|
||||
os << "+";
|
||||
}
|
||||
os << "\n";
|
||||
}
|
||||
// print the header
|
||||
if (!m_keyval) {
|
||||
os << "+";
|
||||
for (size_t j = 0; j < m_vec[i].size(); j++) {
|
||||
for (size_t v = 0; v < m_column_size[j] + 3; v++)
|
||||
os << "-";
|
||||
os << "+";
|
||||
}
|
||||
os << "\n";
|
||||
os << "|";
|
||||
|
||||
for (size_t j = 0; j < m_vec[i].size(); j++) {
|
||||
os << " ";
|
||||
std::stringstream fs;
|
||||
fs << boost::format("%%-%is") % (m_column_size[j] + 2);
|
||||
os << boost::format(fs.str()) % m_vec[i][j].first;
|
||||
os << "|";
|
||||
}
|
||||
os << "\n";
|
||||
os << "+";
|
||||
for (size_t j = 0; j < m_vec[i].size(); j++) {
|
||||
for (size_t v = 0; v < m_column_size[j] + 3; v++)
|
||||
os << "-";
|
||||
os << "+";
|
||||
}
|
||||
os << "\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
// print body
|
||||
if (!m_keyval)
|
||||
os << "|";
|
||||
for (size_t j=0; j< m_vec[i].size(); j++) {
|
||||
for (size_t j = 0; j < m_vec[i].size(); j++) {
|
||||
if (!m_keyval)
|
||||
os << " ";
|
||||
os << " ";
|
||||
std::stringstream fs;
|
||||
|
||||
|
||||
if (m_keyval) {
|
||||
os << "key::";
|
||||
os << m_vec[i][j].first;
|
||||
os << "=";
|
||||
os << "\"";
|
||||
os << m_vec[i][j].second;
|
||||
os << "\" ";
|
||||
os << "key::";
|
||||
os << m_vec[i][j].first;
|
||||
os << "=";
|
||||
os << "\"";
|
||||
os << m_vec[i][j].second;
|
||||
os << "\" ";
|
||||
} else {
|
||||
fs << boost::format ("%%-%is") % (m_column_size[j]+2);
|
||||
os << boost::format (fs.str()) % m_vec[i][j].second;
|
||||
os << "|";
|
||||
fs << boost::format("%%-%is") % (m_column_size[j] + 2);
|
||||
os << boost::format(fs.str()) % m_vec[i][j].second;
|
||||
os << "|";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
os << "\n";
|
||||
if (!m_keyval) {
|
||||
if ( i == (m_vec.size()-1) ) {
|
||||
// print trailer
|
||||
os << "+";
|
||||
for (size_t j=0; j< m_vec[i].size(); j++) {
|
||||
for (size_t v=0; v< m_column_size[j]+3;v++)
|
||||
os << "-";
|
||||
os << "+";
|
||||
}
|
||||
os << "\n";
|
||||
if (i == (m_vec.size() - 1)) {
|
||||
// print trailer
|
||||
os << "+";
|
||||
for (size_t j = 0; j < m_vec[i].size(); j++) {
|
||||
for (size_t v = 0; v < m_column_size[j] + 3; v++)
|
||||
os << "-";
|
||||
os << "+";
|
||||
}
|
||||
os << "\n";
|
||||
}
|
||||
}
|
||||
m_vec[i].clear();
|
||||
@ -693,7 +688,7 @@ void TableFormatter::close_section()
|
||||
//
|
||||
m_section_open--;
|
||||
if (m_section.size()) {
|
||||
m_section_cnt[m_section.back()]=0;
|
||||
m_section_cnt[m_section.back()] = 0;
|
||||
m_section.pop_back();
|
||||
}
|
||||
}
|
||||
@ -713,23 +708,22 @@ size_t TableFormatter::m_vec_index(const char *name)
|
||||
if (m_vec.size()) {
|
||||
if (m_vec[i].size()) {
|
||||
if (m_vec[i][0].first == key) {
|
||||
// start a new column if a key is repeated
|
||||
m_vec.resize(m_vec.size()+1);
|
||||
i++;
|
||||
// start a new column if a key is repeated
|
||||
m_vec.resize(m_vec.size() + 1);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return i;
|
||||
}
|
||||
|
||||
std::string TableFormatter::get_section_name(const char* name)
|
||||
{
|
||||
std::string t_name=name;
|
||||
for (size_t i=0; i< m_section.size(); i++)
|
||||
{
|
||||
t_name.insert(0,":");
|
||||
t_name.insert(0,m_section[i]);
|
||||
std::string t_name = name;
|
||||
for (size_t i = 0; i < m_section.size(); i++) {
|
||||
t_name.insert(0, ":");
|
||||
t_name.insert(0, m_section[i]);
|
||||
}
|
||||
if (m_section_open) {
|
||||
std::stringstream lss;
|
||||
@ -748,7 +742,7 @@ void TableFormatter::dump_unsigned(const char *name, uint64_t u)
|
||||
finish_pending_string();
|
||||
size_t i = m_vec_index(name);
|
||||
m_ss << u;
|
||||
m_vec[i].push_back(std::make_pair(get_section_name(name),m_ss.str()));
|
||||
m_vec[i].push_back(std::make_pair(get_section_name(name), m_ss.str()));
|
||||
m_ss.clear();
|
||||
m_ss.str("");
|
||||
}
|
||||
@ -758,7 +752,7 @@ void TableFormatter::dump_int(const char *name, int64_t u)
|
||||
finish_pending_string();
|
||||
size_t i = m_vec_index(name);
|
||||
m_ss << u;
|
||||
m_vec[i].push_back(std::make_pair(get_section_name(name),m_ss.str()));
|
||||
m_vec[i].push_back(std::make_pair(get_section_name(name), m_ss.str()));
|
||||
m_ss.clear();
|
||||
m_ss.str("");
|
||||
}
|
||||
@ -769,7 +763,7 @@ void TableFormatter::dump_float(const char *name, double d)
|
||||
size_t i = m_vec_index(name);
|
||||
m_ss << d;
|
||||
|
||||
m_vec[i].push_back(std::make_pair(get_section_name(name),m_ss.str()));
|
||||
m_vec[i].push_back(std::make_pair(get_section_name(name), m_ss.str()));
|
||||
m_ss.clear();
|
||||
m_ss.str("");
|
||||
}
|
||||
@ -780,7 +774,7 @@ void TableFormatter::dump_string(const char *name, std::string s)
|
||||
size_t i = m_vec_index(name);
|
||||
m_ss << s;
|
||||
|
||||
m_vec[i].push_back(std::make_pair(get_section_name(name),m_ss.str()));
|
||||
m_vec[i].push_back(std::make_pair(get_section_name(name), m_ss.str()));
|
||||
m_ss.clear();
|
||||
m_ss.str("");
|
||||
}
|
||||
@ -794,12 +788,11 @@ void TableFormatter::dump_string_with_attrs(const char *name, std::string s, con
|
||||
get_attrs_str(&attrs, attrs_str);
|
||||
m_ss << attrs_str << s;
|
||||
|
||||
m_vec[i].push_back(std::make_pair(get_section_name(name),m_ss.str()));
|
||||
m_vec[i].push_back(std::make_pair(get_section_name(name), m_ss.str()));
|
||||
m_ss.clear();
|
||||
m_ss.str("");
|
||||
}
|
||||
|
||||
|
||||
void TableFormatter::dump_format_va(const char* name, const char *ns, bool quoted, const char *fmt, va_list ap)
|
||||
{
|
||||
finish_pending_string();
|
||||
@ -809,10 +802,10 @@ void TableFormatter::dump_format_va(const char* name, const char *ns, bool quote
|
||||
size_t i = m_vec_index(name);
|
||||
if (ns) {
|
||||
m_ss << ns << "." << buf;
|
||||
} else
|
||||
} else
|
||||
m_ss << buf;
|
||||
|
||||
m_vec[i].push_back(std::make_pair(get_section_name(name),m_ss.str()));
|
||||
m_vec[i].push_back(std::make_pair(get_section_name(name), m_ss.str()));
|
||||
m_ss.clear();
|
||||
m_ss.str("");
|
||||
}
|
||||
@ -821,7 +814,7 @@ std::ostream& TableFormatter::dump_stream(const char *name)
|
||||
{
|
||||
finish_pending_string();
|
||||
// we don't support this
|
||||
m_pending_name=name;
|
||||
m_pending_name = name;
|
||||
return m_ss;
|
||||
}
|
||||
|
||||
@ -831,8 +824,7 @@ int TableFormatter::get_len() const
|
||||
return 0;
|
||||
}
|
||||
|
||||
void TableFormatter::write_raw_data(const char *data)
|
||||
{
|
||||
void TableFormatter::write_raw_data(const char *data) {
|
||||
// not supported
|
||||
}
|
||||
|
||||
@ -855,9 +847,9 @@ void TableFormatter::finish_pending_string()
|
||||
std::string ss = m_ss.str();
|
||||
m_ss.clear();
|
||||
m_ss.str("");
|
||||
std::string pending_name=m_pending_name;
|
||||
m_pending_name="";
|
||||
dump_string(pending_name.c_str(),ss);
|
||||
std::string pending_name = m_pending_name;
|
||||
m_pending_name = "";
|
||||
dump_string(pending_name.c_str(), ss);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -17,180 +17,184 @@
|
||||
|
||||
namespace ceph {
|
||||
|
||||
struct FormatterAttrs {
|
||||
std::list< std::pair<std::string, std::string> > attrs;
|
||||
|
||||
struct FormatterAttrs {
|
||||
std::list< std::pair<std::string, std::string> > attrs;
|
||||
|
||||
FormatterAttrs(const char *attr, ...);
|
||||
};
|
||||
|
||||
class Formatter {
|
||||
public:
|
||||
Formatter();
|
||||
virtual ~Formatter();
|
||||
|
||||
virtual void flush(std::ostream& os) = 0;
|
||||
void flush(bufferlist &bl) {
|
||||
std::stringstream os;
|
||||
flush(os);
|
||||
bl.append(os.str());
|
||||
}
|
||||
virtual void reset() = 0;
|
||||
|
||||
virtual void open_array_section(const char *name) = 0;
|
||||
virtual void open_array_section_in_ns(const char *name, const char *ns) = 0;
|
||||
virtual void open_object_section(const char *name) = 0;
|
||||
virtual void open_object_section_in_ns(const char *name, const char *ns) = 0;
|
||||
virtual void close_section() = 0;
|
||||
virtual void dump_unsigned(const char *name, uint64_t u) = 0;
|
||||
virtual void dump_int(const char *name, int64_t s) = 0;
|
||||
virtual void dump_float(const char *name, double d) = 0;
|
||||
virtual void dump_string(const char *name, std::string s) = 0;
|
||||
virtual void dump_bool(const char *name, bool b) {
|
||||
dump_format_unquoted(name, "%s", (b ? "true" : "false"));
|
||||
}
|
||||
virtual std::ostream& dump_stream(const char *name) = 0;
|
||||
virtual void dump_format_va(const char *name, const char *ns, bool quoted, const char *fmt, va_list ap) = 0;
|
||||
virtual void dump_format(const char *name, const char *fmt, ...);
|
||||
virtual void dump_format_ns(const char *name, const char *ns, const char *fmt, ...);
|
||||
virtual void dump_format_unquoted(const char *name, const char *fmt, ...);
|
||||
virtual int get_len() const = 0;
|
||||
virtual void write_raw_data(const char *data) = 0;
|
||||
|
||||
/* with attrs */
|
||||
virtual void open_array_section_with_attrs(const char *name, const FormatterAttrs& attrs) {
|
||||
open_array_section(name);
|
||||
}
|
||||
virtual void open_object_section_with_attrs(const char *name, const FormatterAttrs& attrs) {
|
||||
open_object_section(name);
|
||||
}
|
||||
virtual void dump_string_with_attrs(const char *name, std::string s, const FormatterAttrs& attrs) {
|
||||
dump_string(name, s);
|
||||
}
|
||||
};
|
||||
|
||||
Formatter *new_formatter(const std::string &type);
|
||||
|
||||
class JSONFormatter : public Formatter {
|
||||
public:
|
||||
JSONFormatter(bool p=false);
|
||||
|
||||
void flush(std::ostream& os);
|
||||
void reset();
|
||||
virtual void open_array_section(const char *name);
|
||||
void open_array_section_in_ns(const char *name, const char *ns);
|
||||
void open_object_section(const char *name);
|
||||
void open_object_section_in_ns(const char *name, const char *ns);
|
||||
void close_section();
|
||||
void dump_unsigned(const char *name, uint64_t u);
|
||||
void dump_int(const char *name, int64_t u);
|
||||
void dump_float(const char *name, double d);
|
||||
void dump_string(const char *name, std::string s);
|
||||
std::ostream& dump_stream(const char *name);
|
||||
void dump_format_va(const char *name, const char *ns, bool quoted, const char *fmt, va_list ap);
|
||||
int get_len() const;
|
||||
void write_raw_data(const char *data);
|
||||
|
||||
private:
|
||||
struct json_formatter_stack_entry_d {
|
||||
int size;
|
||||
bool is_array;
|
||||
json_formatter_stack_entry_d() : size(0), is_array(false) {}
|
||||
FormatterAttrs(const char *attr, ...);
|
||||
};
|
||||
|
||||
bool m_pretty;
|
||||
void open_section(const char *name, bool is_array);
|
||||
void print_quoted_string(const char *s);
|
||||
void print_name(const char *name);
|
||||
void print_comma(json_formatter_stack_entry_d& entry);
|
||||
void finish_pending_string();
|
||||
class Formatter {
|
||||
public:
|
||||
Formatter();
|
||||
virtual ~Formatter();
|
||||
|
||||
std::stringstream m_ss, m_pending_string;
|
||||
std::list<json_formatter_stack_entry_d> m_stack;
|
||||
bool m_is_pending_string;
|
||||
};
|
||||
virtual void flush(std::ostream& os) = 0;
|
||||
void flush(bufferlist &bl)
|
||||
{
|
||||
std::stringstream os;
|
||||
flush(os);
|
||||
bl.append(os.str());
|
||||
}
|
||||
virtual void reset() = 0;
|
||||
|
||||
class XMLFormatter : public Formatter {
|
||||
public:
|
||||
static const char *XML_1_DTD;
|
||||
XMLFormatter(bool pretty = false);
|
||||
virtual void open_array_section(const char *name) = 0;
|
||||
virtual void open_array_section_in_ns(const char *name, const char *ns) = 0;
|
||||
virtual void open_object_section(const char *name) = 0;
|
||||
virtual void open_object_section_in_ns(const char *name, const char *ns) = 0;
|
||||
virtual void close_section() = 0;
|
||||
virtual void dump_unsigned(const char *name, uint64_t u) = 0;
|
||||
virtual void dump_int(const char *name, int64_t s) = 0;
|
||||
virtual void dump_float(const char *name, double d) = 0;
|
||||
virtual void dump_string(const char *name, std::string s) = 0;
|
||||
virtual void dump_bool(const char *name, bool b)
|
||||
{
|
||||
dump_format_unquoted(name, "%s", (b ? "true" : "false"));
|
||||
}
|
||||
virtual std::ostream& dump_stream(const char *name) = 0;
|
||||
virtual void dump_format_va(const char *name, const char *ns, bool quoted, const char *fmt, va_list ap) = 0;
|
||||
virtual void dump_format(const char *name, const char *fmt, ...);
|
||||
virtual void dump_format_ns(const char *name, const char *ns, const char *fmt, ...);
|
||||
virtual void dump_format_unquoted(const char *name, const char *fmt, ...);
|
||||
virtual int get_len() const = 0;
|
||||
virtual void write_raw_data(const char *data) = 0;
|
||||
/* with attrs */
|
||||
virtual void open_array_section_with_attrs(const char *name, const FormatterAttrs& attrs)
|
||||
{
|
||||
open_array_section(name);
|
||||
}
|
||||
virtual void open_object_section_with_attrs(const char *name, const FormatterAttrs& attrs)
|
||||
{
|
||||
open_object_section(name);
|
||||
}
|
||||
virtual void dump_string_with_attrs(const char *name, std::string s, const FormatterAttrs& attrs)
|
||||
{
|
||||
dump_string(name, s);
|
||||
}
|
||||
};
|
||||
|
||||
void flush(std::ostream& os);
|
||||
void reset();
|
||||
void open_array_section(const char *name);
|
||||
void open_array_section_in_ns(const char *name, const char *ns);
|
||||
void open_object_section(const char *name);
|
||||
void open_object_section_in_ns(const char *name, const char *ns);
|
||||
void close_section();
|
||||
void dump_unsigned(const char *name, uint64_t u);
|
||||
void dump_int(const char *name, int64_t u);
|
||||
void dump_float(const char *name, double d);
|
||||
void dump_string(const char *name, std::string s);
|
||||
std::ostream& dump_stream(const char *name);
|
||||
void dump_format_va(const char *name, const char *ns, bool quoted, const char *fmt, va_list ap);
|
||||
int get_len() const;
|
||||
void write_raw_data(const char *data);
|
||||
Formatter *new_formatter(const std::string &type);
|
||||
|
||||
/* with attrs */
|
||||
void open_array_section_with_attrs(const char *name, const FormatterAttrs& attrs);
|
||||
void open_object_section_with_attrs(const char *name, const FormatterAttrs& attrs);
|
||||
void dump_string_with_attrs(const char *name, std::string s, const FormatterAttrs& attrs);
|
||||
private:
|
||||
void open_section_in_ns(const char *name, const char *ns, const FormatterAttrs *attrs);
|
||||
void finish_pending_string();
|
||||
void print_spaces();
|
||||
static std::string escape_xml_str(const char *str);
|
||||
void get_attrs_str(const FormatterAttrs *attrs, std::string& attrs_str);
|
||||
class JSONFormatter : public Formatter {
|
||||
public:
|
||||
JSONFormatter(bool p = false);
|
||||
|
||||
std::stringstream m_ss, m_pending_string;
|
||||
std::deque<std::string> m_sections;
|
||||
bool m_pretty;
|
||||
std::string m_pending_string_name;
|
||||
};
|
||||
void flush(std::ostream& os);
|
||||
void reset();
|
||||
virtual void open_array_section(const char *name);
|
||||
void open_array_section_in_ns(const char *name, const char *ns);
|
||||
void open_object_section(const char *name);
|
||||
void open_object_section_in_ns(const char *name, const char *ns);
|
||||
void close_section();
|
||||
void dump_unsigned(const char *name, uint64_t u);
|
||||
void dump_int(const char *name, int64_t u);
|
||||
void dump_float(const char *name, double d);
|
||||
void dump_string(const char *name, std::string s);
|
||||
std::ostream& dump_stream(const char *name);
|
||||
void dump_format_va(const char *name, const char *ns, bool quoted, const char *fmt, va_list ap);
|
||||
int get_len() const;
|
||||
void write_raw_data(const char *data);
|
||||
|
||||
class TableFormatter : public Formatter {
|
||||
public:
|
||||
TableFormatter(bool keyval=false);
|
||||
private:
|
||||
|
||||
void flush(std::ostream& os);
|
||||
void reset();
|
||||
virtual void open_array_section(const char *name);
|
||||
void open_array_section_in_ns(const char *name, const char *ns);
|
||||
void open_object_section(const char *name);
|
||||
void open_object_section_in_ns(const char *name, const char *ns);
|
||||
struct json_formatter_stack_entry_d {
|
||||
int size;
|
||||
bool is_array;
|
||||
json_formatter_stack_entry_d() : size(0), is_array(false) { }
|
||||
};
|
||||
|
||||
void open_array_section_with_attrs(const char *name, const FormatterAttrs& attrs);
|
||||
void open_object_section_with_attrs(const char *name, const FormatterAttrs& attrs);
|
||||
bool m_pretty;
|
||||
void open_section(const char *name, bool is_array);
|
||||
void print_quoted_string(const char *s);
|
||||
void print_name(const char *name);
|
||||
void print_comma(json_formatter_stack_entry_d& entry);
|
||||
void finish_pending_string();
|
||||
|
||||
void close_section();
|
||||
void dump_unsigned(const char *name, uint64_t u);
|
||||
void dump_int(const char *name, int64_t u);
|
||||
void dump_float(const char *name, double d);
|
||||
void dump_string(const char *name, std::string s);
|
||||
void dump_format_va(const char *name, const char *ns, bool quoted, const char *fmt, va_list ap);
|
||||
void dump_string_with_attrs(const char *name, std::string s, const FormatterAttrs& attrs);
|
||||
std::ostream& dump_stream(const char *name);
|
||||
std::stringstream m_ss, m_pending_string;
|
||||
std::list<json_formatter_stack_entry_d> m_stack;
|
||||
bool m_is_pending_string;
|
||||
};
|
||||
|
||||
int get_len() const;
|
||||
void write_raw_data(const char *data);
|
||||
void get_attrs_str(const FormatterAttrs *attrs, std::string& attrs_str);
|
||||
class XMLFormatter : public Formatter {
|
||||
public:
|
||||
static const char *XML_1_DTD;
|
||||
XMLFormatter(bool pretty = false);
|
||||
|
||||
private:
|
||||
void open_section_in_ns(const char *name, const char *ns, const FormatterAttrs *attrs);
|
||||
std::vector< std::vector<std::pair<std::string,std::string> > > m_vec;
|
||||
std::stringstream m_ss;
|
||||
size_t m_vec_index(const char* name);
|
||||
std::string get_section_name(const char* name);
|
||||
void finish_pending_string();
|
||||
std::string m_pending_name;
|
||||
bool m_keyval;
|
||||
void flush(std::ostream& os);
|
||||
void reset();
|
||||
void open_array_section(const char *name);
|
||||
void open_array_section_in_ns(const char *name, const char *ns);
|
||||
void open_object_section(const char *name);
|
||||
void open_object_section_in_ns(const char *name, const char *ns);
|
||||
void close_section();
|
||||
void dump_unsigned(const char *name, uint64_t u);
|
||||
void dump_int(const char *name, int64_t u);
|
||||
void dump_float(const char *name, double d);
|
||||
void dump_string(const char *name, std::string s);
|
||||
std::ostream& dump_stream(const char *name);
|
||||
void dump_format_va(const char *name, const char *ns, bool quoted, const char *fmt, va_list ap);
|
||||
int get_len() const;
|
||||
void write_raw_data(const char *data);
|
||||
|
||||
int m_section_open;
|
||||
std::vector< std::string > m_section;
|
||||
std::map<std::string,int> m_section_cnt;
|
||||
std::vector<size_t> m_column_size;
|
||||
std::vector< std::string > m_column_name;
|
||||
};
|
||||
/* with attrs */
|
||||
void open_array_section_with_attrs(const char *name, const FormatterAttrs& attrs);
|
||||
void open_object_section_with_attrs(const char *name, const FormatterAttrs& attrs);
|
||||
void dump_string_with_attrs(const char *name, std::string s, const FormatterAttrs& attrs);
|
||||
private:
|
||||
void open_section_in_ns(const char *name, const char *ns, const FormatterAttrs *attrs);
|
||||
void finish_pending_string();
|
||||
void print_spaces();
|
||||
static std::string escape_xml_str(const char *str);
|
||||
void get_attrs_str(const FormatterAttrs *attrs, std::string& attrs_str);
|
||||
|
||||
std::stringstream m_ss, m_pending_string;
|
||||
std::deque<std::string> m_sections;
|
||||
bool m_pretty;
|
||||
std::string m_pending_string_name;
|
||||
};
|
||||
|
||||
class TableFormatter : public Formatter {
|
||||
public:
|
||||
TableFormatter(bool keyval = false);
|
||||
|
||||
void flush(std::ostream& os);
|
||||
void reset();
|
||||
virtual void open_array_section(const char *name);
|
||||
void open_array_section_in_ns(const char *name, const char *ns);
|
||||
void open_object_section(const char *name);
|
||||
void open_object_section_in_ns(const char *name, const char *ns);
|
||||
|
||||
void open_array_section_with_attrs(const char *name, const FormatterAttrs& attrs);
|
||||
void open_object_section_with_attrs(const char *name, const FormatterAttrs& attrs);
|
||||
|
||||
void close_section();
|
||||
void dump_unsigned(const char *name, uint64_t u);
|
||||
void dump_int(const char *name, int64_t u);
|
||||
void dump_float(const char *name, double d);
|
||||
void dump_string(const char *name, std::string s);
|
||||
void dump_format_va(const char *name, const char *ns, bool quoted, const char *fmt, va_list ap);
|
||||
void dump_string_with_attrs(const char *name, std::string s, const FormatterAttrs& attrs);
|
||||
std::ostream& dump_stream(const char *name);
|
||||
|
||||
int get_len() const;
|
||||
void write_raw_data(const char *data);
|
||||
void get_attrs_str(const FormatterAttrs *attrs, std::string& attrs_str);
|
||||
|
||||
private:
|
||||
void open_section_in_ns(const char *name, const char *ns, const FormatterAttrs *attrs);
|
||||
std::vector< std::vector<std::pair<std::string, std::string> > > m_vec;
|
||||
std::stringstream m_ss;
|
||||
size_t m_vec_index(const char* name);
|
||||
std::string get_section_name(const char* name);
|
||||
void finish_pending_string();
|
||||
std::string m_pending_name;
|
||||
bool m_keyval;
|
||||
|
||||
int m_section_open;
|
||||
std::vector< std::string > m_section;
|
||||
std::map<std::string, int> m_section_cnt;
|
||||
std::vector<size_t> m_column_size;
|
||||
std::vector< std::string > m_column_name;
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
|
@ -5,12 +5,13 @@
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
TEST(tableformatter, singleline) {
|
||||
TEST(tableformatter, singleline)
|
||||
{
|
||||
std::stringstream sout;
|
||||
TableFormatter formatter;
|
||||
formatter.dump_int("integer",10);
|
||||
formatter.dump_float("float",10.0);
|
||||
formatter.dump_string("string","string");
|
||||
formatter.dump_int("integer", 10);
|
||||
formatter.dump_float("float", 10.0);
|
||||
formatter.dump_string("string", "string");
|
||||
formatter.flush(sout);
|
||||
|
||||
std::string cmp = ""
|
||||
@ -19,40 +20,42 @@ TEST(tableformatter, singleline) {
|
||||
"+----------+--------+---------+\n"
|
||||
"| 10 | 10 | string |\n"
|
||||
"+----------+--------+---------+\n";
|
||||
EXPECT_EQ(cmp,sout.str());
|
||||
EXPECT_EQ(cmp, sout.str());
|
||||
}
|
||||
|
||||
TEST(tableformatter, multiline) {
|
||||
TEST(tableformatter, multiline)
|
||||
{
|
||||
std::stringstream sout;
|
||||
TableFormatter formatter;
|
||||
formatter.dump_int("integer",10);
|
||||
formatter.dump_float("float",10.0);
|
||||
formatter.dump_string("string","string");
|
||||
formatter.dump_int("integer",20);
|
||||
formatter.dump_float("float",20.0);
|
||||
formatter.dump_string("string","string");
|
||||
formatter.dump_int("integer", 10);
|
||||
formatter.dump_float("float", 10.0);
|
||||
formatter.dump_string("string", "string");
|
||||
formatter.dump_int("integer", 20);
|
||||
formatter.dump_float("float", 20.0);
|
||||
formatter.dump_string("string", "string");
|
||||
|
||||
std::string cmp = ""
|
||||
"+----------+--------+---------+\n"
|
||||
"| integer | float | string |\n"
|
||||
"+----------+--------+---------+\n"
|
||||
"| 10 | 10 | string |\n"
|
||||
"| 20 | 20 | string |\n"
|
||||
"+----------+--------+---------+\n";
|
||||
"+----------+--------+---------+\n"
|
||||
"| integer | float | string |\n"
|
||||
"+----------+--------+---------+\n"
|
||||
"| 10 | 10 | string |\n"
|
||||
"| 20 | 20 | string |\n"
|
||||
"+----------+--------+---------+\n";
|
||||
|
||||
formatter.flush(sout);
|
||||
EXPECT_EQ(cmp,sout.str());
|
||||
EXPECT_EQ(cmp, sout.str());
|
||||
}
|
||||
|
||||
TEST(tableformatter, multiflush) {
|
||||
TEST(tableformatter, multiflush)
|
||||
{
|
||||
std::stringstream sout1;
|
||||
std::stringstream sout2;
|
||||
TableFormatter formatter;
|
||||
formatter.dump_int("integer",10);
|
||||
formatter.dump_float("float",10.0);
|
||||
formatter.dump_string("string","string");
|
||||
formatter.dump_int("integer", 10);
|
||||
formatter.dump_float("float", 10.0);
|
||||
formatter.dump_string("string", "string");
|
||||
formatter.flush(sout1);
|
||||
|
||||
|
||||
std::string cmp = ""
|
||||
"+----------+--------+---------+\n"
|
||||
"| integer | float | string |\n"
|
||||
@ -60,32 +63,33 @@ TEST(tableformatter, multiflush) {
|
||||
"| 10 | 10 | string |\n"
|
||||
"+----------+--------+---------+\n";
|
||||
|
||||
EXPECT_EQ(cmp,sout1.str());
|
||||
EXPECT_EQ(cmp, sout1.str());
|
||||
|
||||
formatter.dump_int("integer",20);
|
||||
formatter.dump_float("float",20.0);
|
||||
formatter.dump_string("string","string");
|
||||
formatter.dump_int("integer", 20);
|
||||
formatter.dump_float("float", 20.0);
|
||||
formatter.dump_string("string", "string");
|
||||
formatter.flush(sout2);
|
||||
|
||||
cmp = ""
|
||||
"| 20 | 20 | string |\n"
|
||||
"+----------+--------+---------+\n";
|
||||
|
||||
EXPECT_EQ(cmp,sout2.str());
|
||||
EXPECT_EQ(cmp, sout2.str());
|
||||
|
||||
}
|
||||
|
||||
TEST(tableformatter, multireset) {
|
||||
TEST(tableformatter, multireset)
|
||||
{
|
||||
std::stringstream sout;
|
||||
TableFormatter formatter;
|
||||
formatter.dump_int("integer",10);
|
||||
formatter.dump_float("float",10.0);
|
||||
formatter.dump_string("string","string");
|
||||
formatter.dump_int("integer", 10);
|
||||
formatter.dump_float("float", 10.0);
|
||||
formatter.dump_string("string", "string");
|
||||
formatter.flush(sout);
|
||||
formatter.reset();
|
||||
formatter.dump_int("integer",20);
|
||||
formatter.dump_float("float",20.0);
|
||||
formatter.dump_string("string","string");
|
||||
formatter.dump_int("integer", 20);
|
||||
formatter.dump_float("float", 20.0);
|
||||
formatter.dump_string("string", "string");
|
||||
formatter.flush(sout);
|
||||
|
||||
std::string cmp = ""
|
||||
@ -100,19 +104,20 @@ TEST(tableformatter, multireset) {
|
||||
"| 20 | 20 | string |\n"
|
||||
"+----------+--------+---------+\n";
|
||||
|
||||
EXPECT_EQ(cmp,sout.str());
|
||||
EXPECT_EQ(cmp, sout.str());
|
||||
}
|
||||
|
||||
TEST(tableformatter, changingheaderlength) {
|
||||
TEST(tableformatter, changingheaderlength)
|
||||
{
|
||||
std::stringstream sout;
|
||||
TableFormatter formatter;
|
||||
formatter.dump_int("integer",10);
|
||||
formatter.dump_float("float",10.0);
|
||||
formatter.dump_string("string","string");
|
||||
formatter.dump_int("integer", 10);
|
||||
formatter.dump_float("float", 10.0);
|
||||
formatter.dump_string("string", "string");
|
||||
formatter.flush(sout);
|
||||
formatter.dump_int("integer",20);
|
||||
formatter.dump_float("float",20.0);
|
||||
formatter.dump_string("string","stringstring");
|
||||
formatter.dump_int("integer", 20);
|
||||
formatter.dump_float("float", 20.0);
|
||||
formatter.dump_string("string", "stringstring");
|
||||
formatter.flush(sout);
|
||||
|
||||
std::string cmp = ""
|
||||
@ -127,19 +132,20 @@ TEST(tableformatter, changingheaderlength) {
|
||||
"| 20 | 20 | stringstring |\n"
|
||||
"+----------+--------+---------------+\n";
|
||||
|
||||
EXPECT_EQ(cmp,sout.str());
|
||||
EXPECT_EQ(cmp, sout.str());
|
||||
}
|
||||
|
||||
TEST(tableformatter, changingheader) {
|
||||
TEST(tableformatter, changingheader)
|
||||
{
|
||||
std::stringstream sout;
|
||||
TableFormatter formatter;
|
||||
formatter.dump_int("integer",10);
|
||||
formatter.dump_float("float",10.0);
|
||||
formatter.dump_string("string","string");
|
||||
formatter.dump_int("integer", 10);
|
||||
formatter.dump_float("float", 10.0);
|
||||
formatter.dump_string("string", "string");
|
||||
formatter.flush(sout);
|
||||
formatter.dump_int("longinteger",20);
|
||||
formatter.dump_float("double",20.0);
|
||||
formatter.dump_string("char*","stringstring");
|
||||
formatter.dump_int("longinteger", 20);
|
||||
formatter.dump_float("double", 20.0);
|
||||
formatter.dump_string("char*", "stringstring");
|
||||
formatter.flush(sout);
|
||||
|
||||
std::string cmp = ""
|
||||
@ -154,20 +160,21 @@ TEST(tableformatter, changingheader) {
|
||||
"| 20 | 20 | stringstring |\n"
|
||||
"+--------------+---------+---------------+\n";
|
||||
|
||||
EXPECT_EQ(cmp,sout.str());
|
||||
EXPECT_EQ(cmp, sout.str());
|
||||
}
|
||||
|
||||
TEST(tableformatter, extendingheader) {
|
||||
TEST(tableformatter, extendingheader)
|
||||
{
|
||||
std::stringstream sout;
|
||||
TableFormatter formatter;
|
||||
formatter.dump_int("integer",10);
|
||||
formatter.dump_float("float",10.0);
|
||||
formatter.dump_string("string","string");
|
||||
formatter.dump_int("integer", 10);
|
||||
formatter.dump_float("float", 10.0);
|
||||
formatter.dump_string("string", "string");
|
||||
formatter.flush(sout);
|
||||
formatter.dump_int("integer",20);
|
||||
formatter.dump_float("float",20.0);
|
||||
formatter.dump_string("string","string");
|
||||
formatter.dump_string("char*","abcde");
|
||||
formatter.dump_int("integer", 20);
|
||||
formatter.dump_float("float", 20.0);
|
||||
formatter.dump_string("string", "string");
|
||||
formatter.dump_string("char*", "abcde");
|
||||
formatter.flush(sout);
|
||||
|
||||
std::string cmp = ""
|
||||
@ -182,10 +189,11 @@ TEST(tableformatter, extendingheader) {
|
||||
"| 20 | 20 | string | abcde |\n"
|
||||
"+----------+--------+---------+--------+\n";
|
||||
|
||||
EXPECT_EQ(cmp,sout.str());
|
||||
EXPECT_EQ(cmp, sout.str());
|
||||
}
|
||||
|
||||
TEST(tableformatter, stream) {
|
||||
TEST(tableformatter, stream)
|
||||
{
|
||||
std::stringstream sout;
|
||||
TableFormatter* formatter = (TableFormatter*) new_formatter("table");
|
||||
formatter->dump_stream("integer") << 10;
|
||||
@ -201,29 +209,30 @@ TEST(tableformatter, stream) {
|
||||
"| 10 | 10 | string |\n"
|
||||
"+----------+--------+---------+\n";
|
||||
|
||||
EXPECT_EQ(cmp,sout.str());
|
||||
EXPECT_EQ(cmp, sout.str());
|
||||
}
|
||||
|
||||
TEST(tableformatter, multiline_keyval) {
|
||||
TEST(tableformatter, multiline_keyval)
|
||||
{
|
||||
std::stringstream sout;
|
||||
TableFormatter* formatter = (TableFormatter*) new_formatter("table-kv");
|
||||
formatter->dump_int("integer",10);
|
||||
formatter->dump_float("float",10.0);
|
||||
formatter->dump_string("string","string");
|
||||
formatter->dump_int("integer",20);
|
||||
formatter->dump_float("float",20.0);
|
||||
formatter->dump_string("string","string");
|
||||
formatter->dump_int("integer", 10);
|
||||
formatter->dump_float("float", 10.0);
|
||||
formatter->dump_string("string", "string");
|
||||
formatter->dump_int("integer", 20);
|
||||
formatter->dump_float("float", 20.0);
|
||||
formatter->dump_string("string", "string");
|
||||
formatter->flush(sout);
|
||||
delete formatter;
|
||||
|
||||
std::string cmp = ""
|
||||
"key::integer=\"10\" key::float=\"10\" key::string=\"string\" \n"
|
||||
"key::integer=\"20\" key::float=\"20\" key::string=\"string\" \n";
|
||||
|
||||
EXPECT_EQ(cmp,sout.str());
|
||||
"key::integer=\"10\" key::float=\"10\" key::string=\"string\" \n"
|
||||
"key::integer=\"20\" key::float=\"20\" key::string=\"string\" \n";
|
||||
|
||||
EXPECT_EQ(cmp, sout.str());
|
||||
}
|
||||
|
||||
/*
|
||||
/*
|
||||
* Local Variables:
|
||||
* compile-command: "cd ../.. ; make -j4 &&
|
||||
* make unittest_tableformatter &&
|
||||
|
Loading…
Reference in New Issue
Block a user