livestatus: add separator support

fixes #4478
This commit is contained in:
Michael Friedrich 2013-07-29 19:37:58 +02:00
parent 2106322388
commit 168dbbd28d
2 changed files with 25 additions and 6 deletions

View File

@ -57,6 +57,12 @@ Query::Query(const std::vector<String>& lines)
return; return;
} }
/* default separators */
m_Separators.push_back("\n");
m_Separators.push_back(";");
m_Separators.push_back(",");
m_Separators.push_back("|");
String line = lines[0]; String line = lines[0];
size_t sp_index = line.FindFirstOf(" "); size_t sp_index = line.FindFirstOf(" ");
@ -89,6 +95,7 @@ Query::Query(const std::vector<String>& lines)
size_t col_index = line.FindFirstOf(":"); size_t col_index = line.FindFirstOf(":");
String header = line.SubStr(0, col_index); String header = line.SubStr(0, col_index);
String params; String params;
std::vector<String> separators;
if (line.GetLength() > col_index + 2) if (line.GetLength() > col_index + 2)
params = line.SubStr(col_index + 2); params = line.SubStr(col_index + 2);
@ -99,6 +106,17 @@ Query::Query(const std::vector<String>& lines)
m_OutputFormat = params; m_OutputFormat = params;
else if (header == "Columns") else if (header == "Columns")
boost::algorithm::split(m_Columns, params, boost::is_any_of(" ")); 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<char>(Convert::ToLong(separators[0])));
if (separators.size() > 1)
m_Separators[1] = String(1, static_cast<char>(Convert::ToLong(separators[1])));
if (separators.size() > 2)
m_Separators[2] = String(1, static_cast<char>(Convert::ToLong(separators[2])));
if (separators.size() > 3)
m_Separators[3] = String(1, static_cast<char>(Convert::ToLong(separators[3])));
else if (header == "ColumnHeaders") else if (header == "ColumnHeaders")
m_ColumnHeaders = (params == "on"); m_ColumnHeaders = (params == "on");
else if (header == "Filter") { else if (header == "Filter") {
@ -276,12 +294,12 @@ void Query::PrintResultSet(std::ostream& fp, const std::vector<String>& columns,
if (first) if (first)
first = false; first = false;
else else
fp << ";"; fp << m_Separators[1];
fp << column; fp << column;
} }
fp << "\n"; fp << m_Separators[0];
} }
if (m_OutputFormat == "csv") { if (m_OutputFormat == "csv") {
@ -295,7 +313,7 @@ void Query::PrintResultSet(std::ostream& fp, const std::vector<String>& columns,
if (first) if (first)
first = false; first = false;
else else
fp << ";"; fp << m_Separators[1];
if (value.IsObjectType<Array>()) if (value.IsObjectType<Array>())
PrintCsvArray(fp, value, 0); PrintCsvArray(fp, value, 0);
@ -303,7 +321,7 @@ void Query::PrintResultSet(std::ostream& fp, const std::vector<String>& columns,
fp << value; fp << value;
} }
fp << "\n"; fp << m_Separators[0];
} }
} else if (m_OutputFormat == "json") { } else if (m_OutputFormat == "json") {
fp << Value(rs).Serialize(); fp << Value(rs).Serialize();
@ -319,7 +337,7 @@ void Query::PrintCsvArray(std::ostream& fp, const Array::Ptr& array, int level)
if (first) if (first)
first = false; first = false;
else else
fp << ((level == 0) ? "," : "|"); fp << ((level == 0) ? m_Separators[2] : m_Separators[3]);
if (value.IsObjectType<Array>()) if (value.IsObjectType<Array>())
PrintCsvArray(fp, value, level + 1); 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 sCode = Convert::ToString(code);
String sLength = Convert::ToString(data.GetLength()); 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()); stream->Write(header.CStr(), header.GetLength());
} }

View File

@ -61,6 +61,7 @@ private:
/* Parameters for GET queries. */ /* Parameters for GET queries. */
String m_Table; String m_Table;
std::vector<String> m_Columns; std::vector<String> m_Columns;
std::vector<String> m_Separators;
Filter::Ptr m_Filter; Filter::Ptr m_Filter;
std::deque<Aggregator::Ptr> m_Aggregators; std::deque<Aggregator::Ptr> m_Aggregators;