From 41ac7a5591ac733639b581b7b4389bc1a745ea81 Mon Sep 17 00:00:00 2001 From: john30 Date: Sun, 8 Jan 2017 12:07:11 +0100 Subject: [PATCH] extendd getName with optional field index --- src/lib/ebus/data.cpp | 21 +++++++++++++++++++-- src/lib/ebus/data.h | 10 +++++++--- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/src/lib/ebus/data.cpp b/src/lib/ebus/data.cpp index f8283dde..e42626e7 100644 --- a/src/lib/ebus/data.cpp +++ b/src/lib/ebus/data.cpp @@ -803,6 +803,22 @@ unsigned char DataFieldSet::getLength(PartType partType, unsigned char maxLength return length; } +string DataFieldSet::getName(signed char fieldIndex) +{ + if (fieldIndex<0) { + return m_name; + } + if ((unsigned char)fieldIndex>=m_fields.size()) { + return ""; + } + if (m_uniqueNames) { + return m_fields[fieldIndex]->getName(); + } + ostringstream ostream; + ostream << static_cast(fieldIndex); + return ostream.str(); +} + result_t DataFieldSet::derive(string name, string comment, string unit, const PartType partType, int divisor, map values, @@ -835,10 +851,11 @@ void DataFieldSet::dump(ostream& output) { bool first = true; for (vector::iterator it = m_fields.begin(); it < m_fields.end(); it++) { - if (first) + if (first) { first = false; - else + } else { output << FIELD_SEPARATOR; + } (*it)->dump(output); } } diff --git a/src/lib/ebus/data.h b/src/lib/ebus/data.h index f2b5905b..d4ce7482 100644 --- a/src/lib/ebus/data.h +++ b/src/lib/ebus/data.h @@ -128,10 +128,11 @@ public: vector& fields) = 0; /** - * Get the field name. - * @return the field name. + * Get the specified field name. + * @param fieldIndex the index of the field, or -1 for this. + * @return the field name, or the index as string if not unique or not available. */ - string getName() const { return m_name; } + virtual string getName(signed char fieldIndex=-1) const { return m_name; } /** * Get the field comment. @@ -549,6 +550,9 @@ public: // @copydoc virtual unsigned char getLength(PartType partType, unsigned char maxLength=MAX_LEN); + // @copydoc + virtual string getName(signed char fieldIndex=-1); + // @copydoc virtual result_t derive(string name, string comment, string unit, const PartType partType,