Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:42:49

0001 //===-- XML.h ---------------------------------------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLDB_HOST_XML_H
0010 #define LLDB_HOST_XML_H
0011 
0012 #include "lldb/Host/Config.h"
0013 
0014 #if LLDB_ENABLE_LIBXML2
0015 #include <libxml/xmlreader.h>
0016 #endif
0017 
0018 #include <functional>
0019 #include <string>
0020 #include <vector>
0021 
0022 #include "llvm/ADT/StringRef.h"
0023 
0024 #include "lldb/Utility/StreamString.h"
0025 #include "lldb/Utility/StructuredData.h"
0026 #include "lldb/lldb-private.h"
0027 
0028 namespace lldb_private {
0029 
0030 #if LLDB_ENABLE_LIBXML2
0031 typedef xmlNodePtr XMLNodeImpl;
0032 typedef xmlDocPtr XMLDocumentImpl;
0033 #else
0034 typedef void *XMLNodeImpl;
0035 typedef void *XMLDocumentImpl;
0036 #endif
0037 
0038 class XMLNode;
0039 
0040 typedef std::vector<std::string> NamePath;
0041 typedef std::function<bool(const XMLNode &node)> NodeCallback;
0042 typedef std::function<bool(const llvm::StringRef &name,
0043                            const llvm::StringRef &value)>
0044     AttributeCallback;
0045 
0046 class XMLNode {
0047 public:
0048   XMLNode();
0049 
0050   XMLNode(XMLNodeImpl node);
0051 
0052   ~XMLNode();
0053 
0054   explicit operator bool() const { return IsValid(); }
0055 
0056   void Clear();
0057 
0058   bool IsValid() const;
0059 
0060   bool IsElement() const;
0061 
0062   llvm::StringRef GetName() const;
0063 
0064   bool GetElementText(std::string &text) const;
0065 
0066   bool GetElementTextAsUnsigned(uint64_t &value, uint64_t fail_value = 0,
0067                                 int base = 0) const;
0068 
0069   bool GetElementTextAsFloat(double &value, double fail_value = 0.0) const;
0070 
0071   bool NameIs(const char *name) const;
0072 
0073   XMLNode GetParent() const;
0074 
0075   XMLNode GetSibling() const;
0076 
0077   XMLNode GetChild() const;
0078 
0079   std::string GetAttributeValue(const char *name,
0080                                 const char *fail_value = nullptr) const;
0081 
0082   bool GetAttributeValueAsUnsigned(const char *name, uint64_t &value,
0083                                    uint64_t fail_value = 0, int base = 0) const;
0084 
0085   XMLNode FindFirstChildElementWithName(const char *name) const;
0086 
0087   XMLNode GetElementForPath(const NamePath &path);
0088 
0089   // Iterate through all sibling nodes of any type
0090   void ForEachSiblingNode(NodeCallback const &callback) const;
0091 
0092   // Iterate through only the sibling nodes that are elements
0093   void ForEachSiblingElement(NodeCallback const &callback) const;
0094 
0095   // Iterate through only the sibling nodes that are elements and whose name
0096   // matches \a name.
0097   void ForEachSiblingElementWithName(const char *name,
0098                                      NodeCallback const &callback) const;
0099 
0100   void ForEachChildNode(NodeCallback const &callback) const;
0101 
0102   void ForEachChildElement(NodeCallback const &callback) const;
0103 
0104   void ForEachChildElementWithName(const char *name,
0105                                    NodeCallback const &callback) const;
0106 
0107   void ForEachAttribute(AttributeCallback const &callback) const;
0108 
0109 protected:
0110   XMLNodeImpl m_node = nullptr;
0111 };
0112 
0113 class XMLDocument {
0114 public:
0115   XMLDocument();
0116 
0117   ~XMLDocument();
0118 
0119   explicit operator bool() const { return IsValid(); }
0120 
0121   bool IsValid() const;
0122 
0123   void Clear();
0124 
0125   bool ParseFile(const char *path);
0126 
0127   bool ParseMemory(const char *xml, size_t xml_length,
0128                    const char *url = "untitled.xml");
0129 
0130   // If \a name is nullptr, just get the root element node, else only return a
0131   // value XMLNode if the name of the root element matches \a name.
0132   XMLNode GetRootElement(const char *required_name = nullptr);
0133 
0134   llvm::StringRef GetErrors() const;
0135 
0136   static void ErrorCallback(void *ctx, const char *format, ...);
0137 
0138   static bool XMLEnabled();
0139 
0140 protected:
0141   XMLDocumentImpl m_document = nullptr;
0142   StreamString m_errors;
0143 };
0144 
0145 class ApplePropertyList {
0146 public:
0147   ApplePropertyList();
0148 
0149   ApplePropertyList(const char *path);
0150 
0151   ~ApplePropertyList();
0152 
0153   bool ParseFile(const char *path);
0154 
0155   llvm::StringRef GetErrors() const;
0156 
0157   explicit operator bool() const { return IsValid(); }
0158 
0159   bool IsValid() const;
0160 
0161   XMLNode GetValueNode(const char *key) const;
0162 
0163   bool GetValueAsString(const char *key, std::string &value) const;
0164 
0165   StructuredData::ObjectSP GetStructuredData();
0166 
0167 protected:
0168   // Using a node returned from GetValueNode() extract its value as a string
0169   // (if possible). Array and dictionary nodes will return false as they have
0170   // no string value. Boolean nodes will return true and \a value will be
0171   // "true" or "false" as the string value comes from the element name itself.
0172   // All other nodes will return the text content of the XMLNode.
0173   static bool ExtractStringFromValueNode(const XMLNode &node,
0174                                          std::string &value);
0175 
0176   XMLDocument m_xml_doc;
0177   XMLNode m_dict_node;
0178 };
0179 
0180 } // namespace lldb_private
0181 
0182 #endif // LLDB_HOST_XML_H