From ba5d26409846bdbe96f4ab62afbd31b0b95f4f42 Mon Sep 17 00:00:00 2001 From: John Date: Sun, 17 Apr 2022 18:12:21 +0200 Subject: [PATCH] move some methods --- src/ebusd/mqtthandler.cpp | 18 +++++++++--------- src/ebusd/mqtthandler.h | 16 ++++++++++++++++ 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/ebusd/mqtthandler.cpp b/src/ebusd/mqtthandler.cpp index 89981d86..2d158e6f 100755 --- a/src/ebusd/mqtthandler.cpp +++ b/src/ebusd/mqtthandler.cpp @@ -388,7 +388,7 @@ static const char* knownFieldNames[] = { /** the number of known field names. */ static const size_t knownFieldCount = sizeof(knownFieldNames) / sizeof(char*); -std::pair makeField(const string& name, bool isField) { +std::pair MqttReplacer::makeField(const string& name, bool isField) { if (!isField) { return {name, -1}; } @@ -400,7 +400,7 @@ std::pair makeField(const string& name, bool isField) { return {name, knownFieldCount}; } -void addPart(ostringstream& stack, int inField, vector>& parts) { +void MqttReplacer::addPart(ostringstream& stack, int inField) { string str = stack.str(); if (inField == 1 && str == "_") { inField = 0; // single "%_" pattern to reduce to "_" @@ -412,12 +412,12 @@ void addPart(ostringstream& stack, int inField, vector>& return; } stack.str(""); - if (inField == 0 && !parts.empty() && parts[parts.size()-1].second < 0) { + if (inField == 0 && !m_parts.empty() && m_parts[m_parts.size()-1].second < 0) { // append constant to previous constant - parts[parts.size()-1].first += str; + m_parts[m_parts.size()-1].first += str; return; } - parts.push_back(makeField(str, inField > 0)); + m_parts.push_back(makeField(str, inField > 0)); } bool MqttReplacer::parse(const string& templateStr, bool onlyKnown, bool noKnownDuplicates, bool emptyIfMissing) { @@ -431,24 +431,24 @@ bool MqttReplacer::parse(const string& templateStr, bool onlyKnown, bool noKnown inField = 0; stack << ch; } else { - addPart(stack, inField, m_parts); + addPart(stack, inField); inField = 1; } } else if (ch == '{' && inField == 1 && empty) { inField = 2; } else if (ch == '}' && inField == 2) { - addPart(stack, 1, m_parts); + addPart(stack, 1); inField = 0; } else { if (inField > 0 && !((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || ch == '_')) { // invalid field character - addPart(stack, inField, m_parts); + addPart(stack, inField); inField = 0; } stack << ch; } } - addPart(stack, inField, m_parts); + addPart(stack, inField); if (onlyKnown || noKnownDuplicates) { int foundMask = 0; int knownCount = knownFieldCount; diff --git a/src/ebusd/mqtthandler.h b/src/ebusd/mqtthandler.h index 28d93c7c..a01070e9 100644 --- a/src/ebusd/mqtthandler.h +++ b/src/ebusd/mqtthandler.h @@ -180,6 +180,22 @@ class MqttReplacer { /** true when the complete result is supposed to be empty when at least one referenced variable * is empty or not defined. */ bool m_emptyIfMissing; + + /** + * Create a named field or constant. + * @param name the plain string or the name of the field. + * @param isField true when it is a field. + * @return the created pair. + */ + static std::pair makeField(const string& name, bool isField); + + /** + * Add a part to the list of parts. + * @param stack the parsing stack. + * @param inField 1 after '%', 2 after '%{', 0 otherwise. + */ + void addPart(ostringstream& stack, int inField); + };