From c640e388bc097391ba32536cd37a22ed7c74c320 Mon Sep 17 00:00:00 2001 From: "johannes.gotlen" Date: Thu, 30 Jan 2025 14:44:41 +0100 Subject: [PATCH 1/4] Implemented CTinyxml2Deserializer::DeserializeEyeTrackerSettings, CTinyxml2Deserializer::ReadXmlBool --- RTProtocol.cpp | 4 +- Tinyxml2Serializer.cpp | 108 +++++++++++++++++++++-------------------- Tinyxml2Serializer.h | 2 +- 3 files changed, 60 insertions(+), 54 deletions(-) diff --git a/RTProtocol.cpp b/RTProtocol.cpp index 23d90da..e044350 100644 --- a/RTProtocol.cpp +++ b/RTProtocol.cpp @@ -19,6 +19,8 @@ #include "Network.h" #include +#include "Tinyxml2Serializer.h" + #ifdef _WIN32 #include // import the internet protocol helper library. @@ -1772,7 +1774,7 @@ bool CRTProtocol::ReadEyeTrackerSettings(bool& bDataAvailable) return false; } - auto deserializer = CMarkupDeserializer(data, mnMajorVersion, mnMinorVersion); + CTinyxml2Deserializer deserializer(data, mnMajorVersion, mnMinorVersion); return deserializer.DeserializeEyeTrackerSettings(mvsEyeTrackerSettings, bDataAvailable); } diff --git a/Tinyxml2Serializer.cpp b/Tinyxml2Serializer.cpp index b32a6e8..fd37f4e 100644 --- a/Tinyxml2Serializer.cpp +++ b/Tinyxml2Serializer.cpp @@ -226,30 +226,31 @@ namespace } -bool CTinyxml2Deserializer::ReadXmlBool(tinyxml2::XMLDocument* xml, const std::string& element, bool& value) const +bool CTinyxml2Deserializer::ReadXmlBool(tinyxml2::XMLElement* xml, const std::string& element, bool& value) const { - //if (!xml->FindChildElem(element.c_str())) - //{ - // return false; - //} + auto xmlElem = xml->FirstChildElement(element.c_str()); + if (!xmlElem) + { + return false; + } - //auto str = xml->GetChildData(); - //RemoveInvalidChars(str); - //str = ToLower(str); + auto str = std::string(xmlElem->GetText()); + RemoveInvalidChars(str); + str = ToLower(str); - //if (str == "true") - //{ - // value = true; - //} - //else if (str == "false") - //{ - // value = false; - //} - //else - //{ - // // Don't change value, just report error. - // return false; - //} + if (str == "true") + { + value = true; + } + else if (str == "false") + { + value = false; + } + else + { + // Don't change value, just report error. + return false; + } return true; } @@ -284,8 +285,9 @@ SRotation CTinyxml2Deserializer::ReadXMLRotation(tinyxml2::XMLDocument& xml, con } CTinyxml2Deserializer::CTinyxml2Deserializer(const char* pData, std::uint32_t pMajorVersion, std::uint32_t pMinorVersion) - : mnMajorVersion(pMajorVersion), mnMinorVersion(pMinorVersion), maErrorStr{ 0 }, oXML(pData) + : mnMajorVersion(pMajorVersion), mnMinorVersion(pMinorVersion), maErrorStr{ 0 } { + oXML.Parse(pData); } bool CTinyxml2Deserializer::DeserializeGeneralSettings(SSettingsGeneral& pGeneralSettings) @@ -1958,45 +1960,47 @@ bool CTinyxml2Deserializer::DeserializeGazeVectorSettings(std::vector& pEyeTrackerSettings, bool& pDataAvailable) { - //pDataAvailable = false; - - //pEyeTrackerSettings.clear(); + pDataAvailable = false; - //if (!oXML.FindChildElem("Eye_Tracker")) - //{ - // return true; // NO eye tracker data available. - //} - //oXML.IntoElem(); + pEyeTrackerSettings.clear(); - //std::string tEyeTrackerName; + auto rootElem = oXML.RootElement(); + if (!rootElem) + { + return true; + } - //int nEyeTrackerCount = 0; + tinyxml2::XMLElement* eyeTrackerElem = rootElem->FirstChildElement("Eye_Tracker"); - //while (oXML.FindChildElem("Device")) - //{ - // oXML.IntoElem(); + if (!eyeTrackerElem) + { + return true; // NO eye tracker data available. + } - // if (!oXML.FindChildElem("Name")) - // { - // return false; - // } - // tEyeTrackerName = oXML.GetChildData(); + for (auto deviceElem = eyeTrackerElem->FirstChildElement("Device"); deviceElem != nullptr; deviceElem = deviceElem->NextSiblingElement("Device")) + { + std::string name; + if (auto nameElem = deviceElem->FirstChildElement("Name")) + { + name = nameElem->GetText(); + }else + { + return false; + } - // float frequency = 0; - // if (oXML.FindChildElem("Frequency")) - // { - // frequency = (float)atof(oXML.GetChildData().c_str()); - // } + float frequency = 0; + if (auto frequencyElem = deviceElem->FirstChildElement("Frequency")) + { + frequency = (float)atof(frequencyElem->GetText()); + } - // bool hwSync = false; - // ReadXmlBool(&oXML, "Hardware_Sync", hwSync); + bool hwSync = false; + ReadXmlBool(deviceElem, "Hardware_Sync", hwSync); - // pEyeTrackerSettings.push_back({ tEyeTrackerName, frequency, hwSync }); - // nEyeTrackerCount++; - // oXML.OutOfElem(); // Vector - //} + pEyeTrackerSettings.push_back({ name, frequency, hwSync }); + } - //pDataAvailable = true; + pDataAvailable = true; return true; } // ReadEyeTrackerSettings diff --git a/Tinyxml2Serializer.h b/Tinyxml2Serializer.h index 768e51a..4320b03 100644 --- a/Tinyxml2Serializer.h +++ b/Tinyxml2Serializer.h @@ -30,7 +30,7 @@ namespace CRTProtocolNs { static bool ParseString(const std::string& str, float& value); static bool ParseString(const std::string& str, double& value); static bool ParseString(const std::string& str, bool& value); - bool ReadXmlBool(tinyxml2::XMLDocument* xml, const std::string& element, bool& value) const; + bool ReadXmlBool(tinyxml2::XMLElement* xml, const std::string& element, bool& value) const; SPosition ReadXMLPosition(tinyxml2::XMLDocument& xml, const std::string& element); SRotation ReadXMLRotation(tinyxml2::XMLDocument& xml, const std::string& element); bool ReadXMLDegreesOfFreedom(tinyxml2::XMLDocument& xml, const std::string& element, std::vector& degreesOfFreedom); From 8e6f16631131ed60f0e352f5b1851d7937ae9a56 Mon Sep 17 00:00:00 2001 From: "johannes.gotlen" Date: Thu, 30 Jan 2025 14:48:44 +0100 Subject: [PATCH 2/4] formatted else --- Tinyxml2Serializer.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Tinyxml2Serializer.cpp b/Tinyxml2Serializer.cpp index fd37f4e..ba13185 100644 --- a/Tinyxml2Serializer.cpp +++ b/Tinyxml2Serializer.cpp @@ -1983,7 +1983,8 @@ bool CTinyxml2Deserializer::DeserializeEyeTrackerSettings(std::vectorFirstChildElement("Name")) { name = nameElem->GetText(); - }else + } + else { return false; } From 8cfaa4f3974dd9b9e99ab0b4429c63c04f2788cf Mon Sep 17 00:00:00 2001 From: "johannes.gotlen" Date: Thu, 30 Jan 2025 14:54:14 +0100 Subject: [PATCH 3/4] removed extra include --- RTProtocol.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/RTProtocol.cpp b/RTProtocol.cpp index e044350..edfe70c 100644 --- a/RTProtocol.cpp +++ b/RTProtocol.cpp @@ -19,8 +19,6 @@ #include "Network.h" #include -#include "Tinyxml2Serializer.h" - #ifdef _WIN32 #include // import the internet protocol helper library. From 73b2a13240066d23345148d756640d79b75c0b5e Mon Sep 17 00:00:00 2001 From: "johannes.gotlen" Date: Thu, 30 Jan 2025 21:33:06 +0100 Subject: [PATCH 4/4] use static_cast --- Tinyxml2Serializer.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tinyxml2Serializer.cpp b/Tinyxml2Serializer.cpp index e77a455..d3149e3 100644 --- a/Tinyxml2Serializer.cpp +++ b/Tinyxml2Serializer.cpp @@ -1992,7 +1992,7 @@ bool CTinyxml2Deserializer::DeserializeEyeTrackerSettings(std::vectorFirstChildElement("Frequency")) { - frequency = (float)atof(frequencyElem->GetText()); + frequency = static_cast(atof(frequencyElem->GetText())); } bool hwSync = false;