From 2fb51b2b58edd4af6c03bcac7c6cde2348d31900 Mon Sep 17 00:00:00 2001 From: Abhishek Lekshmanan Date: Wed, 27 Aug 2014 12:02:19 +0530 Subject: [PATCH] Formatter: add a dump_format_ns to base Formatter `dump_format_ns` is a generic formatter to dump a simple format along with a namespace. It is `dump_format` with an optional ns. This also extends the XML formatter with this functionality. This allows creation of xml tags with ns and a specified format. The JSON Format doesn't define a ns, and here the functionality is exactly same as that of `dump_format` Signed-off-by: Abhishek Lekshmanan --- src/common/Formatter.cc | 35 +++++++++++++++++++++++++++++++++++ src/common/Formatter.h | 3 +++ 2 files changed, 38 insertions(+) diff --git a/src/common/Formatter.cc b/src/common/Formatter.cc index 8a6aed2b521..976af14ff50 100644 --- a/src/common/Formatter.cc +++ b/src/common/Formatter.cc @@ -255,6 +255,21 @@ void JSONFormatter::dump_format(const char *name, const char *fmt, ...) print_quoted_string(buf); } +void JSONFormatter:: dump_format_ns(const char *name, const char *ns, const char *fmt, ...) +{ + // ignore the namespace for now + char buf[LARGE_SIZE]; + va_list ap; + va_start(ap, fmt); + vsnprintf(buf, LARGE_SIZE, fmt, ap); + va_end(ap); + + print_name(name); + print_quoted_string(buf); + +} + + void JSONFormatter::dump_format_unquoted(const char *name, const char *fmt, ...) { char buf[LARGE_SIZE]; @@ -417,6 +432,26 @@ void XMLFormatter::dump_format(const char *name, const char *fmt, ...) m_ss << "\n"; } +void XMLFormatter::dump_format_ns(const char* name, const char *ns, const char *fmt, ...) +{ + char buf[LARGE_SIZE]; + va_list ap; + va_start(ap, fmt); + vsnprintf(buf, LARGE_SIZE, fmt, ap); + va_end(ap); + + std::string e(name); + print_spaces(); + if (ns) { + m_ss << "<" << e << " xmlns=\"" << ns << "\">" << buf << ""; + } else { + m_ss << "<" << e << ">" << buf << ""; + } + + if (m_pretty) + m_ss << "\n"; +} + void XMLFormatter::dump_format_unquoted(const char *name, const char *fmt, ...) { char buf[LARGE_SIZE]; diff --git a/src/common/Formatter.h b/src/common/Formatter.h index 87b225ac1e6..b9ac6393773 100644 --- a/src/common/Formatter.h +++ b/src/common/Formatter.h @@ -49,6 +49,7 @@ class Formatter { } virtual std::ostream& dump_stream(const char *name) = 0; virtual void dump_format(const char *name, const char *fmt, ...) = 0; + virtual void dump_format_ns(const char *name, const char *ns, const char *fmt, ...) = 0; virtual void dump_format_unquoted(const char *name, const char *fmt, ...) = 0; virtual int get_len() const = 0; virtual void write_raw_data(const char *data) = 0; @@ -85,6 +86,7 @@ class JSONFormatter : public Formatter { std::ostream& dump_stream(const char *name); void dump_format(const char *name, const char *fmt, ...); void dump_format_unquoted(const char *name, const char *fmt, ...); + void dump_format_ns(const char*name, const char *ns, const char *fmt, ...); int get_len() const; void write_raw_data(const char *data); @@ -125,6 +127,7 @@ class XMLFormatter : public Formatter { void dump_string(const char *name, std::string s); std::ostream& dump_stream(const char *name); void dump_format(const char *name, const char *fmt, ...); + void dump_format_ns(const char *name, const char *ns, const char *fmt, ...); void dump_format_unquoted(const char *name, const char *fmt, ...); int get_len() const; void write_raw_data(const char *data);