[Lldb-commits] [lldb] r238260 - Modify the ApplePropertyList to be able to create StructuredData objects from the plist XML.

Greg Clayton gclayton at apple.com
Tue May 26 15:49:19 PDT 2015


Author: gclayton
Date: Tue May 26 17:49:19 2015
New Revision: 238260

URL: http://llvm.org/viewvc/llvm-project?rev=238260&view=rev
Log:
Modify the ApplePropertyList to be able to create StructuredData objects from the plist XML.


Modified:
    lldb/trunk/include/lldb/Core/StructuredData.h
    lldb/trunk/include/lldb/Host/XML.h
    lldb/trunk/source/Host/common/XML.cpp

Modified: lldb/trunk/include/lldb/Core/StructuredData.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Core/StructuredData.h?rev=238260&r1=238259&r2=238260&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Core/StructuredData.h (original)
+++ lldb/trunk/include/lldb/Core/StructuredData.h Tue May 26 17:49:19 2015
@@ -326,9 +326,9 @@ public:
     class Integer  : public Object
     {
     public:
-        Integer () :
+        Integer (uint64_t i = 0) :
             Object (Type::eTypeInteger),
-            m_value ()
+            m_value (i)
         {
         }
 
@@ -357,9 +357,9 @@ public:
     class Float  : public Object
     {
     public:
-        Float () :
+        Float (double d = 0.0) :
             Object (Type::eTypeFloat),
-            m_value ()
+            m_value (d)
         {
         }
 
@@ -388,9 +388,9 @@ public:
     class Boolean  : public Object
     {
     public:
-        Boolean () :
+        Boolean (bool b = false) :
             Object (Type::eTypeBoolean),
-            m_value ()
+            m_value (b)
         {
         }
 
@@ -421,10 +421,24 @@ public:
     class String  : public Object
     {
     public:
-        String () :
+        String (const char *cstr = NULL) :
             Object (Type::eTypeString),
             m_value ()
         {
+            if (cstr)
+                m_value = cstr;
+        }
+
+        String (const std::string &s) :
+            Object (Type::eTypeString),
+            m_value (s)
+        {
+        }
+
+        String (const std::string &&s) :
+            Object (Type::eTypeString),
+            m_value (s)
+        {
         }
 
         void

Modified: lldb/trunk/include/lldb/Host/XML.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Host/XML.h?rev=238260&r1=238259&r2=238260&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Host/XML.h (original)
+++ lldb/trunk/include/lldb/Host/XML.h Tue May 26 17:49:19 2015
@@ -27,6 +27,7 @@
 #include "lldb/lldb-private.h"
 #include "llvm/ADT/StringRef.h"
 #include "lldb/Core/StreamString.h"
+#include "lldb/Core/StructuredData.h"
 
 
 namespace lldb_private {
@@ -73,7 +74,13 @@ namespace lldb_private {
         
         bool
         GetElementText (std::string &text) const;
-        
+
+        bool
+        GetElementTextAsUnsigned (uint64_t &value, uint64_t fail_value = 0, int base = 0) const;
+
+        bool
+        GetElementTextAsFloat (double &value, double fail_value = 0.0) const;
+
         bool
         NameIs (const char *name) const;
         
@@ -197,6 +204,9 @@ namespace lldb_private {
         bool
         GetValueAsString (const char *key, std::string &value) const;
 
+        StructuredData::ObjectSP
+        GetStructuredData();
+
     protected:
 
         // Using a node returned from GetValueNode() extract its value as a

Modified: lldb/trunk/source/Host/common/XML.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Host/common/XML.cpp?rev=238260&r1=238259&r2=238260&view=diff
==============================================================================
--- lldb/trunk/source/Host/common/XML.cpp (original)
+++ lldb/trunk/source/Host/common/XML.cpp Tue May 26 17:49:19 2015
@@ -7,7 +7,10 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include <stdlib.h>     /* atof */
+
 #include "lldb/Host/XML.h"
+#include "lldb/Host/StringConvert.h"
 
 using namespace lldb;
 using namespace lldb_private;
@@ -372,6 +375,44 @@ XMLNode::GetElementText (std::string &te
 }
 
 
+bool
+XMLNode::GetElementTextAsUnsigned (uint64_t &value, uint64_t fail_value, int base) const
+{
+    bool success = false;
+#if defined( LIBXML2_DEFINED )
+    if (IsValid())
+    {
+        std::string text;
+        if (GetElementText(text))
+            value = StringConvert::ToUInt64(text.c_str(), fail_value, base, &success);
+    }
+#endif
+    if (!success)
+        value = fail_value;
+    return success;
+}
+
+bool
+XMLNode::GetElementTextAsFloat (double &value, double fail_value) const
+{
+    bool success = false;
+#if defined( LIBXML2_DEFINED )
+    if (IsValid())
+    {
+        std::string text;
+        if (GetElementText(text))
+        {
+            value = atof(text.c_str());
+            success = true;
+        }
+    }
+#endif
+    if (!success)
+        value = fail_value;
+    return success;
+}
+
+
 
 bool
 XMLNode::NameIs (const char *name) const
@@ -546,3 +587,88 @@ ApplePropertyList::ExtractStringFromValu
     return false;
 }
 
+#if defined( LIBXML2_DEFINED )
+
+namespace {
+
+    StructuredData::ObjectSP
+    CreatePlistValue (XMLNode node)
+    {
+        llvm::StringRef element_name = node.GetName();
+        if (element_name == "array")
+        {
+            std::shared_ptr<StructuredData::Array> array_sp(new StructuredData::Array());
+            node.ForEachChildElement([&array_sp](const XMLNode &node) -> bool {
+                array_sp->AddItem(CreatePlistValue(node));
+                return true; // Keep iterating through all child elements of the array
+            });
+            return array_sp;
+        }
+        else if (element_name == "dict")
+        {
+            XMLNode key_node;
+            std::shared_ptr<StructuredData::Dictionary> dict_sp(new StructuredData::Dictionary());
+            node.ForEachChildElement([&key_node, &dict_sp](const XMLNode &node) -> bool {
+                if (node.NameIs("key"))
+                {
+                    // This is a "key" element node
+                    key_node = node;
+                }
+                else
+                {
+                    // This is a value node
+                    if (key_node)
+                    {
+                        dict_sp->AddItem(key_node.GetName(), CreatePlistValue(node));
+                        key_node.Clear();
+                    }
+                }
+                return true; // Keep iterating through all child elements of the dictionary
+            });
+            return dict_sp;
+        }
+        else if (element_name == "real")
+        {
+            double value = 0.0;
+            node.GetElementTextAsFloat(value);
+            return StructuredData::ObjectSP(new StructuredData::Float(value));
+        }
+        else if (element_name == "integer")
+        {
+            uint64_t value = 0;
+            node.GetElementTextAsUnsigned(value, 0, 0);
+            return StructuredData::ObjectSP(new StructuredData::Integer(value));
+        }
+        else if ((element_name == "string") || (element_name == "data"))
+        {
+            std::string text;
+            node.GetElementText(text);
+            return StructuredData::ObjectSP(new StructuredData::String(std::move(text)));
+        }
+        else if (element_name == "true")
+        {
+            return StructuredData::ObjectSP(new StructuredData::Boolean(true));
+        }
+        else if (element_name == "false")
+        {
+            return StructuredData::ObjectSP(new StructuredData::Boolean(false));
+        }
+        return StructuredData::ObjectSP(new StructuredData::Null());
+    }
+}
+#endif
+
+StructuredData::ObjectSP
+ApplePropertyList::GetStructuredData()
+{
+    StructuredData::ObjectSP root_sp;
+#if defined( LIBXML2_DEFINED )
+    if (IsValid())
+    {
+        return CreatePlistValue(m_dict_node);
+    }
+#endif
+    return root_sp;
+}
+
+





More information about the lldb-commits mailing list