| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| |
|
| | #ifndef MUJOCO_SRC_XML_XML_UTIL_H_ |
| | #define MUJOCO_SRC_XML_XML_UTIL_H_ |
| |
|
| | #include <array> |
| | #include <functional> |
| | #include <optional> |
| | #include <set> |
| | #include <string> |
| | #include <vector> |
| | #include <sstream> |
| |
|
| | #include <mujoco/mujoco.h> |
| | #include "user/user_util.h" |
| | #include "tinyxml2.h" |
| |
|
| | |
| | void mjCopyError(char* dst, const char* src, int maxlen); |
| |
|
| | using tinyxml2::XMLElement; |
| |
|
| | XMLElement* FirstChildElement(XMLElement* e, const char* name = nullptr); |
| | XMLElement* NextSiblingElement(XMLElement* e, const char* name = nullptr); |
| |
|
| | |
| | class [[nodiscard]] mjXError { |
| | public: |
| | mjXError(const tinyxml2::XMLElement* elem = 0, |
| | const char* msg = 0, |
| | const char* str = 0, |
| | int pos = 0); |
| | ~mjXError() = default; |
| |
|
| | char message[1000]; |
| | }; |
| |
|
| |
|
| | |
| | #define mjXATTRNUM 36 |
| |
|
| |
|
| | |
| | class mjXSchema { |
| | public: |
| | mjXSchema(const char* schema[][mjXATTRNUM], unsigned nrow); |
| |
|
| | std::string GetError(); |
| | void Print(std::stringstream& str, int level) const; |
| | void PrintHTML(std::stringstream& str, int level, bool pad) const; |
| |
|
| | bool NameMatch(tinyxml2::XMLElement* elem, int level); |
| | tinyxml2::XMLElement* Check(tinyxml2::XMLElement* elem, int level); |
| |
|
| | private: |
| | std::string name_; |
| | char type_; |
| | std::set<std::string> attr_; |
| | std::vector<mjXSchema> subschema_; |
| |
|
| | int refcnt_ = 0; |
| | std::string error; |
| | }; |
| |
|
| |
|
| | |
| | struct _mjMap { |
| | const char* key; |
| | int value; |
| | }; |
| | typedef struct _mjMap mjMap; |
| |
|
| |
|
| | |
| | class mjXUtil { |
| | public: |
| | mjXUtil() = default; |
| | virtual ~mjXUtil() = default; |
| |
|
| | |
| | template<typename T> |
| | static bool SameVector(const T* vec1, const T* vec2, int n); |
| |
|
| | |
| | static int FindKey(const mjMap* map, int mapsz, std::string key); |
| |
|
| | |
| | static std::string FindValue(const mjMap* map, int mapsz, int value); |
| |
|
| | |
| | template<typename T> |
| | static std::optional<std::vector<T>> ReadAttrVec(tinyxml2::XMLElement* elem, |
| | const char* attr, |
| | bool required = false); |
| |
|
| | |
| | static std::optional<std::string> ReadAttrStr(tinyxml2::XMLElement* elem, |
| | const char* attr, |
| | bool required = false); |
| |
|
| | |
| | static std::optional<mujoco::user::FilePath> |
| | ReadAttrFile(tinyxml2::XMLElement* elem, const char* attr, |
| | const mjVFS* vfs, |
| | const mujoco::user::FilePath& dir = mujoco::user::FilePath(), |
| | bool required = false); |
| |
|
| | |
| | template<typename T> |
| | static std::optional<T> ReadAttrNum(tinyxml2::XMLElement* elem, |
| | const char* attr, |
| | bool required = false); |
| |
|
| | |
| | |
| | template<typename T, int N> |
| | static std::optional<std::array<T, N>> ReadAttrArr(tinyxml2::XMLElement* elem, |
| | const char* attr, |
| | bool required = false) { |
| | std::array<T, N> arr; |
| | int n = 0; |
| | if (!ReadAttrValues<T>(elem, attr, [&](int i, T num) { arr[i] = num; n++; }, N)) { |
| | throw mjXError(elem, "attribute '%s' has too much data", attr); |
| | } |
| |
|
| | if (!n) { |
| | if (required) { |
| | throw mjXError(elem, "required attribute missing: '%s'", attr); |
| | } else { |
| | return std::nullopt; |
| | } |
| | } |
| |
|
| | if (n < N) { |
| | throw mjXError(elem, "attribute '%s' does not have enough data", attr); |
| | } |
| | return arr; |
| | } |
| |
|
| | |
| | template<typename T> |
| | static int ReadAttr(tinyxml2::XMLElement* elem, const char* attr, int len, |
| | T* data, std::string& text, |
| | bool required = false, bool exact = true); |
| |
|
| | static int ReadQuat(tinyxml2::XMLElement* elem, const char* attr, double* data, |
| | std::string& text, bool required = false); |
| |
|
| | |
| | static int ReadVector(tinyxml2::XMLElement* elem, const char* attr, |
| | std::vector<double>& vec, std::string& text, bool required = false); |
| |
|
| | |
| | static bool ReadAttrTxt(tinyxml2::XMLElement* elem, const char* attr, std::string& text, |
| | bool required = false); |
| |
|
| | |
| | static bool ReadAttrInt(tinyxml2::XMLElement* elem, const char* attr, int* data, |
| | bool required = false); |
| |
|
| | |
| | static void Vector2String(std::string& txt, const std::vector<float>& vec, int ncol = 0); |
| |
|
| |
|
| | |
| | static tinyxml2::XMLElement* FindSubElem(tinyxml2::XMLElement* elem, std::string name, |
| | bool required = false); |
| |
|
| | |
| | static bool MapValue(tinyxml2::XMLElement* elem, const char* attr, int* data, |
| | const mjMap* map, int mapSz, bool required = false); |
| |
|
| | |
| | template<typename T> |
| | static void WriteAttr(tinyxml2::XMLElement* elem, std::string name, int n, const T* data, |
| | const T* def = 0, bool trim = false); |
| |
|
| | |
| | static void WriteVector(tinyxml2::XMLElement* elem, std::string name, |
| | const std::vector<double>& vec); |
| | static void WriteVector(tinyxml2::XMLElement* elem, std::string name, |
| | const std::vector<double>& vec, const std::vector<double>& def); |
| |
|
| | |
| | static void WriteAttrTxt(tinyxml2::XMLElement* elem, std::string name, std::string value); |
| |
|
| | |
| | static void WriteAttrInt(tinyxml2::XMLElement* elem, std::string name, int data, int def = -12345); |
| |
|
| | |
| | static void WriteAttrKey(tinyxml2::XMLElement* elem, std::string name, |
| | const mjMap* map, int mapsz, int data, int def = -12345); |
| |
|
| | private: |
| | template<typename T> |
| | static bool ReadAttrValues(tinyxml2::XMLElement* elem, const char* attr, |
| | std::function<void (int, T)> push, int max = -1); |
| | }; |
| |
|
| | #endif |
| |
|