From 168dbbd28dcb562ed945fbbfa4ec0932192b193a Mon Sep 17 00:00:00 2001 From: Michael Friedrich Date: Mon, 29 Jul 2013 19:37:58 +0200 Subject: [PATCH] livestatus: add separator support fixes #4478 --- components/livestatus/query.cpp | 30 ++++++++++++++++++++++++------ components/livestatus/query.h | 1 + 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/components/livestatus/query.cpp b/components/livestatus/query.cpp index 6333691b8..f60e7fe5c 100644 --- a/components/livestatus/query.cpp +++ b/components/livestatus/query.cpp @@ -57,6 +57,12 @@ Query::Query(const std::vector& lines) return; } + /* default separators */ + m_Separators.push_back("\n"); + m_Separators.push_back(";"); + m_Separators.push_back(","); + m_Separators.push_back("|"); + String line = lines[0]; size_t sp_index = line.FindFirstOf(" "); @@ -89,6 +95,7 @@ Query::Query(const std::vector& lines) size_t col_index = line.FindFirstOf(":"); String header = line.SubStr(0, col_index); String params; + std::vector separators; if (line.GetLength() > col_index + 2) params = line.SubStr(col_index + 2); @@ -99,6 +106,17 @@ Query::Query(const std::vector& lines) m_OutputFormat = params; else if (header == "Columns") boost::algorithm::split(m_Columns, params, boost::is_any_of(" ")); + else if (header == "Separators") + boost::algorithm::split(separators, params, boost::is_any_of(" ")); + /* ugly ascii long to char conversion, but works */ + if (separators.size() > 0) + m_Separators[0] = String(1, static_cast(Convert::ToLong(separators[0]))); + if (separators.size() > 1) + m_Separators[1] = String(1, static_cast(Convert::ToLong(separators[1]))); + if (separators.size() > 2) + m_Separators[2] = String(1, static_cast(Convert::ToLong(separators[2]))); + if (separators.size() > 3) + m_Separators[3] = String(1, static_cast(Convert::ToLong(separators[3]))); else if (header == "ColumnHeaders") m_ColumnHeaders = (params == "on"); else if (header == "Filter") { @@ -276,12 +294,12 @@ void Query::PrintResultSet(std::ostream& fp, const std::vector& columns, if (first) first = false; else - fp << ";"; + fp << m_Separators[1]; fp << column; } - fp << "\n"; + fp << m_Separators[0]; } if (m_OutputFormat == "csv") { @@ -295,7 +313,7 @@ void Query::PrintResultSet(std::ostream& fp, const std::vector& columns, if (first) first = false; else - fp << ";"; + fp << m_Separators[1]; if (value.IsObjectType()) PrintCsvArray(fp, value, 0); @@ -303,7 +321,7 @@ void Query::PrintResultSet(std::ostream& fp, const std::vector& columns, fp << value; } - fp << "\n"; + fp << m_Separators[0]; } } else if (m_OutputFormat == "json") { fp << Value(rs).Serialize(); @@ -319,7 +337,7 @@ void Query::PrintCsvArray(std::ostream& fp, const Array::Ptr& array, int level) if (first) first = false; else - fp << ((level == 0) ? "," : "|"); + fp << ((level == 0) ? m_Separators[2] : m_Separators[3]); if (value.IsObjectType()) PrintCsvArray(fp, value, level + 1); @@ -424,7 +442,7 @@ void Query::PrintFixed16(const Stream::Ptr& stream, int code, const String& data String sCode = Convert::ToString(code); String sLength = Convert::ToString(data.GetLength()); - String header = sCode + String(16 - 3 - sLength.GetLength() - 1, ' ') + sLength + "\n"; + String header = sCode + String(16 - 3 - sLength.GetLength() - 1, ' ') + sLength + m_Separators[0]; stream->Write(header.CStr(), header.GetLength()); } diff --git a/components/livestatus/query.h b/components/livestatus/query.h index f76414dbc..6e408d798 100644 --- a/components/livestatus/query.h +++ b/components/livestatus/query.h @@ -61,6 +61,7 @@ private: /* Parameters for GET queries. */ String m_Table; std::vector m_Columns; + std::vector m_Separators; Filter::Ptr m_Filter; std::deque m_Aggregators;