diff options
Diffstat (limited to 'indra/llxml/llxmlnode.cpp')
-rw-r--r-- | indra/llxml/llxmlnode.cpp | 724 |
1 files changed, 476 insertions, 248 deletions
diff --git a/indra/llxml/llxmlnode.cpp b/indra/llxml/llxmlnode.cpp index 1fcacb46a5..8168f968cd 100644 --- a/indra/llxml/llxmlnode.cpp +++ b/indra/llxml/llxmlnode.cpp @@ -3,30 +3,25 @@ * @author Tom Yedwab * @brief LLXMLNode implementation * - * $LicenseInfo:firstyear=2005&license=viewergpl$ - * - * Copyright (c) 2005-2007, Linden Research, Inc. - * + * $LicenseInfo:firstyear=2005&license=viewerlgpl$ * Second Life Viewer Source Code - * The source code in this file ("Source Code") is provided by Linden Lab - * to you under the terms of the GNU General Public License, version 2.0 - * ("GPL"), unless you have obtained a separate licensing agreement - * ("Other License"), formally executed by you and Linden Lab. Terms of - * the GPL can be found in doc/GPL-license.txt in this distribution, or - * online at http://secondlife.com/developers/opensource/gplv2 + * Copyright (C) 2010, Linden Research, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; + * version 2.1 of the License only. * - * There are special exceptions to the terms and conditions of the GPL as - * it is applied to this Source Code. View the full text of the exception - * in the file doc/FLOSS-exception.txt in this software distribution, or - * online at http://secondlife.com/developers/opensource/flossexception + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. * - * By copying, modifying or distributing this software, you acknowledge - * that you have read and understood your obligations described above, - * and agree to abide by those obligations. + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * - * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO - * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY, - * COMPLETENESS OR PERFORMANCE. + * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA * $/LicenseInfo$ */ @@ -46,6 +41,7 @@ #include "llquaternion.h" #include "llstring.h" #include "lluuid.h" +#include "lldir.h" const S32 MAX_COLUMN_WIDTH = 80; @@ -63,15 +59,19 @@ LLXMLNode::LLXMLNode() : mPrecision(64), mType(TYPE_CONTAINER), mEncoding(ENCODING_DEFAULT), + mLineNumber(-1), mParent(NULL), mChildren(NULL), + mAttributes(), + mPrev(NULL), + mNext(NULL), mName(NULL), mValue(""), mDefault(NULL) { } -LLXMLNode::LLXMLNode(const LLString& name, BOOL is_attribute) : +LLXMLNode::LLXMLNode(const char* name, BOOL is_attribute) : mID(""), mParser(NULL), mIsAttribute(is_attribute), @@ -81,8 +81,12 @@ LLXMLNode::LLXMLNode(const LLString& name, BOOL is_attribute) : mPrecision(64), mType(TYPE_CONTAINER), mEncoding(ENCODING_DEFAULT), + mLineNumber(-1), mParent(NULL), mChildren(NULL), + mAttributes(), + mPrev(NULL), + mNext(NULL), mValue(""), mDefault(NULL) { @@ -99,19 +103,70 @@ LLXMLNode::LLXMLNode(LLStringTableEntry* name, BOOL is_attribute) : mPrecision(64), mType(TYPE_CONTAINER), mEncoding(ENCODING_DEFAULT), + mLineNumber(-1), mParent(NULL), mChildren(NULL), + mAttributes(), + mPrev(NULL), + mNext(NULL), mName(name), mValue(""), mDefault(NULL) { } +// copy constructor (except for the children) +LLXMLNode::LLXMLNode(const LLXMLNode& rhs) : + mID(rhs.mID), + mIsAttribute(rhs.mIsAttribute), + mVersionMajor(rhs.mVersionMajor), + mVersionMinor(rhs.mVersionMinor), + mLength(rhs.mLength), + mPrecision(rhs.mPrecision), + mType(rhs.mType), + mEncoding(rhs.mEncoding), + mLineNumber(0), + mParser(NULL), + mParent(NULL), + mChildren(NULL), + mAttributes(), + mPrev(NULL), + mNext(NULL), + mName(rhs.mName), + mValue(rhs.mValue), + mDefault(rhs.mDefault) +{ +} + +// returns a new copy of this node and all its children +LLXMLNodePtr LLXMLNode::deepCopy() +{ + LLXMLNodePtr newnode = LLXMLNodePtr(new LLXMLNode(*this)); + if (mChildren.notNull()) + { + for (LLXMLChildList::iterator iter = mChildren->map.begin(); + iter != mChildren->map.end(); ++iter) + { + newnode->addChild(iter->second->deepCopy()); + } + } + for (LLXMLAttribList::iterator iter = mAttributes.begin(); + iter != mAttributes.end(); ++iter) + { + newnode->addChild(iter->second->deepCopy()); + } + + return newnode; +} + // virtual LLXMLNode::~LLXMLNode() { // Strictly speaking none of this should be required execept 'delete mChildren'... - if (mChildren) + // Sadly, that's only true if we hadn't had reference-counted smart pointers linked + // in three different directions. This entire class is a frightening, hard-to-maintain + // mess. + if (mChildren.notNull()) { for (LLXMLChildList::iterator iter = mChildren->map.begin(); iter != mChildren->map.end(); ++iter) @@ -124,7 +179,7 @@ LLXMLNode::~LLXMLNode() mChildren->map.clear(); mChildren->head = NULL; mChildren->tail = NULL; - delete mChildren; + mChildren = NULL; } for (LLXMLAttribList::iterator iter = mAttributes.begin(); iter != mAttributes.end(); ++iter) @@ -160,7 +215,7 @@ BOOL LLXMLNode::removeChild(LLXMLNode *target_child) return TRUE; } } - else if (mChildren) + else if (mChildren.notNull()) { LLXMLChildList::iterator children_itr = mChildren->map.find(target_child->mName); while (children_itr != mChildren->map.end()) @@ -171,6 +226,10 @@ BOOL LLXMLNode::removeChild(LLXMLNode *target_child) { mChildren->head = target_child->mNext; } + if (target_child == mChildren->tail) + { + mChildren->tail = target_child->mPrev; + } LLXMLNodePtr prev = target_child->mPrev; LLXMLNodePtr next = target_child->mNext; @@ -183,7 +242,6 @@ BOOL LLXMLNode::removeChild(LLXMLNode *target_child) mChildren->map.erase(children_itr); if (mChildren->map.empty()) { - delete mChildren; mChildren = NULL; } return TRUE; @@ -201,7 +259,7 @@ BOOL LLXMLNode::removeChild(LLXMLNode *target_child) return FALSE; } -void LLXMLNode::addChild(LLXMLNodePtr new_child) +void LLXMLNode::addChild(LLXMLNodePtr new_child, LLXMLNodePtr after_child) { if (new_child->mParent != NULL) { @@ -215,23 +273,61 @@ void LLXMLNode::addChild(LLXMLNodePtr new_child) new_child->mParent = this; if (new_child->mIsAttribute) { - mAttributes.insert(std::pair<LLStringTableEntry*, LLXMLNodePtr>(new_child->mName, new_child)); + mAttributes.insert(std::make_pair(new_child->mName, new_child)); } else { - if (!mChildren) + if (mChildren.isNull()) { mChildren = new LLXMLChildren(); mChildren->head = new_child; mChildren->tail = new_child; } - mChildren->map.insert(std::pair<LLStringTableEntry*, LLXMLNodePtr>(new_child->mName, new_child)); + mChildren->map.insert(std::make_pair(new_child->mName, new_child)); - if (mChildren->tail != new_child) + // if after_child is specified, it damn well better be in the list of children + // for this node. I'm not going to assert that, because it would be expensive, + // but don't specify that parameter if you didn't get the value for it from the + // list of children of this node! + if (after_child.isNull()) { - mChildren->tail->mNext = new_child; - new_child->mPrev = mChildren->tail; - mChildren->tail = new_child; + if (mChildren->tail != new_child) + { + mChildren->tail->mNext = new_child; + new_child->mPrev = mChildren->tail; + mChildren->tail = new_child; + } + } + // if after_child == parent, then put new_child at beginning + else if (after_child == this) + { + // add to front of list + new_child->mNext = mChildren->head; + if (mChildren->head) + { + mChildren->head->mPrev = new_child; + mChildren->head = new_child; + } + else // no children + { + mChildren->head = new_child; + mChildren->tail = new_child; + } + } + else + { + if (after_child->mNext.notNull()) + { + // if after_child was not the last item, fix up some pointers + after_child->mNext->mPrev = new_child; + new_child->mNext = after_child->mNext; + } + new_child->mPrev = after_child; + after_child->mNext = new_child; + if (mChildren->tail == after_child) + { + mChildren->tail = new_child; + } } } @@ -239,7 +335,7 @@ void LLXMLNode::addChild(LLXMLNodePtr new_child) } // virtual -LLXMLNodePtr LLXMLNode::createChild(const LLString& name, BOOL is_attribute) +LLXMLNodePtr LLXMLNode::createChild(const char* name, BOOL is_attribute) { return createChild(gStringTable.addStringEntry(name), is_attribute); } @@ -248,7 +344,7 @@ LLXMLNodePtr LLXMLNode::createChild(const LLString& name, BOOL is_attribute) LLXMLNodePtr LLXMLNode::createChild(LLStringTableEntry* name, BOOL is_attribute) { LLXMLNode* ret = new LLXMLNode(name, is_attribute); - ret->mID = ""; + ret->mID.clear(); addChild(ret); return ret; } @@ -293,7 +389,7 @@ void LLXMLNode::updateDefault() } } - if (mChildren) + if (mChildren.notNull()) { LLXMLChildList::const_iterator children_itr; LLXMLChildList::const_iterator children_end = mChildren->map.end(); @@ -311,8 +407,9 @@ void XMLCALL StartXMLNode(void *userData, { // Create a new node LLXMLNode *new_node_ptr = new LLXMLNode(name, FALSE); + LLXMLNodePtr new_node = new_node_ptr; - new_node->mID = ""; + new_node->mID.clear(); LLXMLNodePtr ptr_new_node = new_node; // Set the parent-child relationship with the current active node @@ -325,7 +422,8 @@ void XMLCALL StartXMLNode(void *userData, } new_node_ptr->mParser = parent->mParser; - + new_node_ptr->setLineNumber(XML_GetCurrentLineNumber(*new_node_ptr->mParser)); + // Set the current active node to the new node XML_Parser *parser = parent->mParser; XML_SetUserData(*parser, (void *)new_node_ptr); @@ -334,8 +432,8 @@ void XMLCALL StartXMLNode(void *userData, U32 pos = 0; while (atts[pos] != NULL) { - LLString attr_name = atts[pos]; - LLString attr_value = atts[pos+1]; + std::string attr_name = atts[pos]; + std::string attr_value = atts[pos+1]; // Special cases if ('i' == attr_name[0] && "id" == attr_name) @@ -413,9 +511,10 @@ void XMLCALL StartXMLNode(void *userData, // only one attribute child per description LLXMLNodePtr attr_node; - if (!new_node->getAttribute(attr_name, attr_node, FALSE)) + if (!new_node->getAttribute(attr_name.c_str(), attr_node, FALSE)) { - attr_node = new LLXMLNode(attr_name, TRUE); + attr_node = new LLXMLNode(attr_name.c_str(), TRUE); + attr_node->setLineNumber(XML_GetCurrentLineNumber(*new_node_ptr->mParser)); } attr_node->setValue(attr_value); new_node->addChild(attr_node); @@ -439,7 +538,7 @@ void XMLCALL EndXMLNode(void *userData, // SJB: total hack: if (LLXMLNode::sStripWhitespaceValues) { - LLString value = node->getValue(); + std::string value = node->getValue(); BOOL is_empty = TRUE; for (std::string::size_type s = 0; s < value.length(); s++) { @@ -463,13 +562,13 @@ void XMLCALL XMLData(void *userData, int len) { LLXMLNode* current_node = (LLXMLNode *)userData; - LLString value = current_node->getValue(); + std::string value = current_node->getValue(); if (LLXMLNode::sStripEscapedStrings) { if (s[0] == '\"' && s[len-1] == '\"') { // Special-case: Escaped string. - LLString unescaped_string; + std::string unescaped_string; for (S32 pos=1; pos<len-1; ++pos) { if (s[pos] == '\\' && s[pos+1] == '\\') @@ -492,7 +591,7 @@ void XMLCALL XMLData(void *userData, return; } } - value.append(LLString(s, 0, len)); + value.append(std::string(s, len)); current_node->setValue(value); } @@ -540,8 +639,8 @@ bool LLXMLNode::updateNode( { for (child = node->getFirstChild(); child.notNull(); child = child->getNextSibling()) { - LLString nodeName; - LLString updateName; + std::string nodeName; + std::string updateName; updateChild->getAttributeString("name", updateName); child->getAttributeString("name", nodeName); @@ -566,16 +665,32 @@ bool LLXMLNode::updateNode( } +// static +LLXMLNodePtr LLXMLNode::replaceNode(LLXMLNodePtr node, LLXMLNodePtr update_node) +{ + if (!node || !update_node) + { + llwarns << "Node invalid" << llendl; + return node; + } + + LLXMLNodePtr cloned_node = update_node->deepCopy(); + node->mParent->addChild(cloned_node, node); // add after node + LLXMLNodePtr parent = node->mParent; + parent->removeChild(node); + parent->updateDefault(); + + return cloned_node; +} + // static -bool LLXMLNode::parseFile( - LLString filename, - LLXMLNodePtr& node, - LLXMLNode* defaults_tree) +bool LLXMLNode::parseFile(const std::string& filename, LLXMLNodePtr& node, LLXMLNode* defaults_tree) { // Read file - LLFILE* fp = LLFile::fopen(filename.c_str(), "rb"); /* Flawfinder: ignore */ + LL_DEBUGS("XMLNode") << "parsing XML file: " << filename << LL_ENDL; + LLFILE* fp = LLFile::fopen(filename, "rb"); /* Flawfinder: ignore */ if (fp == NULL) { node = new LLXMLNode(); @@ -620,7 +735,7 @@ bool LLXMLNode::parseBuffer( { llwarns << "Error parsing xml error code: " << XML_ErrorString(XML_GetErrorCode(my_parser)) - << " on lne " << XML_GetCurrentLineNumber(my_parser) + << " on line " << XML_GetCurrentLineNumber(my_parser) << llendl; } @@ -724,7 +839,7 @@ BOOL LLXMLNode::isFullyDefault() && has_default_length && has_default_attribute) { - if (mChildren) + if (mChildren.notNull()) { LLXMLChildList::const_iterator children_itr; LLXMLChildList::const_iterator children_end = mChildren->map.end(); @@ -744,12 +859,66 @@ BOOL LLXMLNode::isFullyDefault() } // static -void LLXMLNode::writeHeaderToFile(LLFILE *fOut) +bool LLXMLNode::getLayeredXMLNode(const std::string &xui_filename, LLXMLNodePtr& root, + const std::vector<std::string>& paths) +{ + std::string full_filename = gDirUtilp->findSkinnedFilename(paths.front(), xui_filename); + if (full_filename.empty()) + { + return false; + } + + if (!LLXMLNode::parseFile(full_filename, root, NULL)) + { + // try filename as passed in since sometimes we load an xml file from a user-supplied path + if (!LLXMLNode::parseFile(xui_filename, root, NULL)) + { + llwarns << "Problem reading UI description file: " << xui_filename << llendl; + return false; + } + } + + LLXMLNodePtr updateRoot; + + std::vector<std::string>::const_iterator itor; + + for (itor = paths.begin(), ++itor; itor != paths.end(); ++itor) + { + std::string nodeName; + std::string updateName; + + std::string layer_filename = gDirUtilp->findSkinnedFilename((*itor), xui_filename); + if(layer_filename.empty()) + { + // no localized version of this file, that's ok, keep looking + continue; + } + + if (!LLXMLNode::parseFile(layer_filename, updateRoot, NULL)) + { + llwarns << "Problem reading localized UI description file: " << (*itor) + gDirUtilp->getDirDelimiter() + xui_filename << llendl; + return false; + } + + updateRoot->getAttributeString("name", updateName); + root->getAttributeString("name", nodeName); + + if (updateName == nodeName) + { + LLXMLNode::updateNode(root, updateRoot); + } + } + + return true; +} + +// static +void LLXMLNode::writeHeaderToFile(LLFILE *out_file) { - fprintf(fOut, "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\" ?>\n"); + fprintf(out_file, "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\" ?>\n"); } -void LLXMLNode::writeToFile(LLFILE *fOut, LLString indent) +void LLXMLNode::writeToFile(LLFILE *out_file, const std::string& indent, bool use_type_decorations) { if (isFullyDefault()) { @@ -758,15 +927,16 @@ void LLXMLNode::writeToFile(LLFILE *fOut, LLString indent) } std::ostringstream ostream; - writeToOstream(ostream, indent); - LLString outstring = ostream.str(); - if (fwrite(outstring.c_str(), 1, outstring.length(), fOut) != outstring.length()) + writeToOstream(ostream, indent, use_type_decorations); + std::string outstring = ostream.str(); + size_t written = fwrite(outstring.c_str(), 1, outstring.length(), out_file); + if (written != outstring.length()) { llwarns << "Short write" << llendl; } } -void LLXMLNode::writeToOstream(std::ostream& output_stream, const LLString& indent) +void LLXMLNode::writeToOstream(std::ostream& output_stream, const std::string& indent, bool use_type_decorations) { if (isFullyDefault()) { @@ -780,84 +950,86 @@ void LLXMLNode::writeToOstream(std::ostream& output_stream, const LLString& inde BOOL has_default_length = mDefault.isNull()?FALSE:(mLength == mDefault->mLength); // stream the name - output_stream << indent.c_str() << "<" << mName->mString; + output_stream << indent << "<" << mName->mString << "\n"; - // ID - if (mID != "") + if (use_type_decorations) { - output_stream << " id=\"" << mID.c_str() << "\""; - } + // ID + if (mID != "") + { + output_stream << indent << " id=\"" << mID << "\"\n"; + } - // Type - if (!has_default_type) - { - switch (mType) + // Type + if (!has_default_type) { - case TYPE_BOOLEAN: - output_stream << " type=\"boolean\""; - break; - case TYPE_INTEGER: - output_stream << " type=\"integer\""; - break; - case TYPE_FLOAT: - output_stream << " type=\"float\""; - break; - case TYPE_STRING: - output_stream << " type=\"string\""; - break; - case TYPE_UUID: - output_stream << " type=\"uuid\""; - break; - case TYPE_NODEREF: - output_stream << " type=\"noderef\""; - break; - default: - // default on switch(enum) eliminates a warning on linux - break; - }; - } + switch (mType) + { + case TYPE_BOOLEAN: + output_stream << indent << " type=\"boolean\"\n"; + break; + case TYPE_INTEGER: + output_stream << indent << " type=\"integer\"\n"; + break; + case TYPE_FLOAT: + output_stream << indent << " type=\"float\"\n"; + break; + case TYPE_STRING: + output_stream << indent << " type=\"string\"\n"; + break; + case TYPE_UUID: + output_stream << indent << " type=\"uuid\"\n"; + break; + case TYPE_NODEREF: + output_stream << indent << " type=\"noderef\"\n"; + break; + default: + // default on switch(enum) eliminates a warning on linux + break; + }; + } - // Encoding - if (!has_default_encoding) - { - switch (mEncoding) + // Encoding + if (!has_default_encoding) { - case ENCODING_DECIMAL: - output_stream << " encoding=\"decimal\""; - break; - case ENCODING_HEX: - output_stream << " encoding=\"hex\""; - break; - /*case ENCODING_BASE32: - output_stream << " encoding=\"base32\""; - break;*/ - default: - // default on switch(enum) eliminates a warning on linux - break; - }; - } + switch (mEncoding) + { + case ENCODING_DECIMAL: + output_stream << indent << " encoding=\"decimal\"\n"; + break; + case ENCODING_HEX: + output_stream << indent << " encoding=\"hex\"\n"; + break; + /*case ENCODING_BASE32: + output_stream << indent << " encoding=\"base32\"\n"; + break;*/ + default: + // default on switch(enum) eliminates a warning on linux + break; + }; + } - // Precision - if (!has_default_precision && (mType == TYPE_INTEGER || mType == TYPE_FLOAT)) - { - output_stream << " precision=\"" << mPrecision << "\""; - } + // Precision + if (!has_default_precision && (mType == TYPE_INTEGER || mType == TYPE_FLOAT)) + { + output_stream << indent << " precision=\"" << mPrecision << "\"\n"; + } - // Version - if (mVersionMajor > 0 || mVersionMinor > 0) - { - output_stream << " version=\"" << mVersionMajor << "." << mVersionMinor << "\""; - } + // Version + if (mVersionMajor > 0 || mVersionMinor > 0) + { + output_stream << indent << " version=\"" << mVersionMajor << "." << mVersionMinor << "\"\n"; + } - // Array length - if (!has_default_length && mLength > 0) - { - output_stream << " length=\"" << mLength << "\""; + // Array length + if (!has_default_length && mLength > 0) + { + output_stream << indent << " length=\"" << mLength << "\"\n"; + } } { // Write out attributes - S32 col_pos = 0; LLXMLAttribList::const_iterator attr_itr; LLXMLAttribList::const_iterator attr_end = mAttributes.end(); for (attr_itr = mAttributes.begin(); attr_itr != attr_end; ++attr_itr) @@ -865,32 +1037,30 @@ void LLXMLNode::writeToOstream(std::ostream& output_stream, const LLString& inde LLXMLNodePtr child = (*attr_itr).second; if (child->mDefault.isNull() || child->mDefault->mValue != child->mValue) { - LLString attr = child->mName->mString; - if (attr == "id" || - attr == "type" || - attr == "encoding" || - attr == "precision" || - attr == "version" || - attr == "length") + std::string attr = child->mName->mString; + if (use_type_decorations + && (attr == "id" || + attr == "type" || + attr == "encoding" || + attr == "precision" || + attr == "version" || + attr == "length")) { continue; // skip built-in attributes } - LLString attr_str = llformat(" %s=\"%s\"", + std::string attr_str = llformat(" %s=\"%s\"", attr.c_str(), escapeXML(child->mValue).c_str()); - if (col_pos + (S32)attr_str.length() > MAX_COLUMN_WIDTH) - { - output_stream << "\n" << indent << " "; - col_pos = 4; - } - col_pos += attr_str.length(); - output_stream << attr_str; + output_stream << indent << attr_str << "\n"; } } } - if (!mChildren && mValue == "") + // erase last \n before attaching final > or /> + output_stream.seekp(-1, std::ios::cur); + + if (mChildren.isNull() && mValue == "") { output_stream << " />\n"; return; @@ -898,33 +1068,33 @@ void LLXMLNode::writeToOstream(std::ostream& output_stream, const LLString& inde else { output_stream << ">\n"; - if (mChildren) + if (mChildren.notNull()) { // stream non-attributes - LLString next_indent = indent + "\t"; + std::string next_indent = indent + " "; for (LLXMLNode* child = getFirstChild(); child; child = child->getNextSibling()) { - child->writeToOstream(output_stream, next_indent); + child->writeToOstream(output_stream, next_indent, use_type_decorations); } } if (!mValue.empty()) { - LLString contents = getTextContents(); - output_stream << indent.c_str() << "\t" << escapeXML(contents) << "\n"; + std::string contents = getTextContents(); + output_stream << indent << " " << escapeXML(contents) << "\n"; } - output_stream << indent.c_str() << "</" << mName->mString << ">\n"; + output_stream << indent << "</" << mName->mString << ">\n"; } } -void LLXMLNode::findName(const LLString& name, LLXMLNodeList &results) +void LLXMLNode::findName(const std::string& name, LLXMLNodeList &results) { LLStringTableEntry* name_entry = gStringTable.checkStringEntry(name); if (name_entry == mName) { - results.insert(std::pair<LLString, LLXMLNode*>(this->mName->mString, this)); + results.insert(std::make_pair(this->mName->mString, this)); return; } - if (mChildren) + if (mChildren.notNull()) { LLXMLChildList::const_iterator children_itr; LLXMLChildList::const_iterator children_end = mChildren->map.end(); @@ -940,10 +1110,10 @@ void LLXMLNode::findName(LLStringTableEntry* name, LLXMLNodeList &results) { if (name == mName) { - results.insert(std::pair<LLString, LLXMLNode*>(this->mName->mString, this)); + results.insert(std::make_pair(this->mName->mString, this)); return; } - if (mChildren) + if (mChildren.notNull()) { LLXMLChildList::const_iterator children_itr; LLXMLChildList::const_iterator children_end = mChildren->map.end(); @@ -955,14 +1125,14 @@ void LLXMLNode::findName(LLStringTableEntry* name, LLXMLNodeList &results) } } -void LLXMLNode::findID(const LLString& id, LLXMLNodeList &results) +void LLXMLNode::findID(const std::string& id, LLXMLNodeList &results) { if (id == mID) { - results.insert(std::pair<LLString, LLXMLNode*>(this->mName->mString, this)); + results.insert(std::make_pair(this->mName->mString, this)); return; } - if (mChildren) + if (mChildren.notNull()) { LLXMLChildList::const_iterator children_itr; LLXMLChildList::const_iterator children_end = mChildren->map.end(); @@ -976,11 +1146,11 @@ void LLXMLNode::findID(const LLString& id, LLXMLNodeList &results) void LLXMLNode::scrubToTree(LLXMLNode *tree) { - if (!tree || !tree->mChildren) + if (!tree || tree->mChildren.isNull()) { return; } - if (mChildren) + if (mChildren.notNull()) { std::vector<LLXMLNodePtr> to_delete_list; LLXMLChildList::iterator itor = mChildren->map.begin(); @@ -1018,14 +1188,14 @@ void LLXMLNode::scrubToTree(LLXMLNode *tree) } } -bool LLXMLNode::getChild(const LLString& name, LLXMLNodePtr& node, BOOL use_default_if_missing) +bool LLXMLNode::getChild(const char* name, LLXMLNodePtr& node, BOOL use_default_if_missing) { return getChild(gStringTable.checkStringEntry(name), node, use_default_if_missing); } bool LLXMLNode::getChild(const LLStringTableEntry* name, LLXMLNodePtr& node, BOOL use_default_if_missing) { - if (mChildren) + if (mChildren.notNull()) { LLXMLChildList::const_iterator child_itr = mChildren->map.find(name); if (child_itr != mChildren->map.end()) @@ -1042,14 +1212,14 @@ bool LLXMLNode::getChild(const LLStringTableEntry* name, LLXMLNodePtr& node, BOO return false; } -void LLXMLNode::getChildren(const LLString& name, LLXMLNodeList &children, BOOL use_default_if_missing) const +void LLXMLNode::getChildren(const char* name, LLXMLNodeList &children, BOOL use_default_if_missing) const { getChildren(gStringTable.checkStringEntry(name), children, use_default_if_missing); } void LLXMLNode::getChildren(const LLStringTableEntry* name, LLXMLNodeList &children, BOOL use_default_if_missing) const { - if (mChildren) + if (mChildren.notNull()) { LLXMLChildList::const_iterator child_itr = mChildren->map.find(name); if (child_itr != mChildren->map.end()) @@ -1062,7 +1232,7 @@ void LLXMLNode::getChildren(const LLStringTableEntry* name, LLXMLNodeList &child { break; } - children.insert(std::pair<LLString, LLXMLNodePtr>(child->mName->mString, child)); + children.insert(std::make_pair(child->mName->mString, child)); child_itr++; } } @@ -1073,7 +1243,26 @@ void LLXMLNode::getChildren(const LLStringTableEntry* name, LLXMLNodeList &child } } -bool LLXMLNode::getAttribute(const LLString& name, LLXMLNodePtr& node, BOOL use_default_if_missing) +// recursively walks the tree and returns all children at all nesting levels matching the name +void LLXMLNode::getDescendants(const LLStringTableEntry* name, LLXMLNodeList &children) const +{ + if (mChildren.notNull()) + { + for (LLXMLChildList::const_iterator child_itr = mChildren->map.begin(); + child_itr != mChildren->map.end(); ++child_itr) + { + LLXMLNodePtr child = (*child_itr).second; + if (name == child->mName) + { + children.insert(std::make_pair(child->mName->mString, child)); + } + // and check each child as well + child->getDescendants(name, children); + } + } +} + +bool LLXMLNode::getAttribute(const char* name, LLXMLNodePtr& node, BOOL use_default_if_missing) { return getAttribute(gStringTable.checkStringEntry(name), node, use_default_if_missing); } @@ -1094,7 +1283,7 @@ bool LLXMLNode::getAttribute(const LLStringTableEntry* name, LLXMLNodePtr& node, return false; } -bool LLXMLNode::setAttributeString(const LLString& attr, const LLString& value) +bool LLXMLNode::setAttributeString(const char* attr, const std::string& value) { LLStringTableEntry* name = gStringTable.checkStringEntry(attr); LLXMLAttribList::const_iterator child_itr = mAttributes.find(name); @@ -1107,25 +1296,42 @@ bool LLXMLNode::setAttributeString(const LLString& attr, const LLString& value) return false; } -BOOL LLXMLNode::hasAttribute(const LLString& name ) +BOOL LLXMLNode::hasAttribute(const char* name ) { LLXMLNodePtr node; return getAttribute(name, node); } -BOOL LLXMLNode::getAttributeBOOL(const LLString& name, BOOL& value ) +// the structure of these getAttribute_ functions is ugly, but it's because the +// underlying system is based on BOOL and LLString; if we change +// so that they're based on more generic mechanisms, these will be +// simplified. +bool LLXMLNode::getAttribute_bool(const char* name, bool& value ) +{ + LLXMLNodePtr node; + if (!getAttribute(name, node)) + { + return false; + } + BOOL temp; + bool retval = node->getBoolValue(1, &temp); + value = temp; + return retval; +} + +BOOL LLXMLNode::getAttributeBOOL(const char* name, BOOL& value ) { LLXMLNodePtr node; return (getAttribute(name, node) && node->getBoolValue(1, &value)); } -BOOL LLXMLNode::getAttributeU8(const LLString& name, U8& value ) +BOOL LLXMLNode::getAttributeU8(const char* name, U8& value ) { LLXMLNodePtr node; return (getAttribute(name, node) && node->getByteValue(1, &value)); } -BOOL LLXMLNode::getAttributeS8(const LLString& name, S8& value ) +BOOL LLXMLNode::getAttributeS8(const char* name, S8& value ) { LLXMLNodePtr node; S32 val; @@ -1137,7 +1343,7 @@ BOOL LLXMLNode::getAttributeS8(const LLString& name, S8& value ) return true; } -BOOL LLXMLNode::getAttributeU16(const LLString& name, U16& value ) +BOOL LLXMLNode::getAttributeU16(const char* name, U16& value ) { LLXMLNodePtr node; U32 val; @@ -1149,7 +1355,7 @@ BOOL LLXMLNode::getAttributeU16(const LLString& name, U16& value ) return true; } -BOOL LLXMLNode::getAttributeS16(const LLString& name, S16& value ) +BOOL LLXMLNode::getAttributeS16(const char* name, S16& value ) { LLXMLNodePtr node; S32 val; @@ -1161,73 +1367,73 @@ BOOL LLXMLNode::getAttributeS16(const LLString& name, S16& value ) return true; } -BOOL LLXMLNode::getAttributeU32(const LLString& name, U32& value ) +BOOL LLXMLNode::getAttributeU32(const char* name, U32& value ) { LLXMLNodePtr node; return (getAttribute(name, node) && node->getUnsignedValue(1, &value)); } -BOOL LLXMLNode::getAttributeS32(const LLString& name, S32& value ) +BOOL LLXMLNode::getAttributeS32(const char* name, S32& value ) { LLXMLNodePtr node; return (getAttribute(name, node) && node->getIntValue(1, &value)); } -BOOL LLXMLNode::getAttributeF32(const LLString& name, F32& value ) +BOOL LLXMLNode::getAttributeF32(const char* name, F32& value ) { LLXMLNodePtr node; return (getAttribute(name, node) && node->getFloatValue(1, &value)); } -BOOL LLXMLNode::getAttributeF64(const LLString& name, F64& value ) +BOOL LLXMLNode::getAttributeF64(const char* name, F64& value ) { LLXMLNodePtr node; return (getAttribute(name, node) && node->getDoubleValue(1, &value)); } -BOOL LLXMLNode::getAttributeColor(const LLString& name, LLColor4& value ) +BOOL LLXMLNode::getAttributeColor(const char* name, LLColor4& value ) { LLXMLNodePtr node; return (getAttribute(name, node) && node->getFloatValue(4, value.mV)); } -BOOL LLXMLNode::getAttributeColor4(const LLString& name, LLColor4& value ) +BOOL LLXMLNode::getAttributeColor4(const char* name, LLColor4& value ) { LLXMLNodePtr node; return (getAttribute(name, node) && node->getFloatValue(4, value.mV)); } -BOOL LLXMLNode::getAttributeColor4U(const LLString& name, LLColor4U& value ) +BOOL LLXMLNode::getAttributeColor4U(const char* name, LLColor4U& value ) { LLXMLNodePtr node; return (getAttribute(name, node) && node->getByteValue(4, value.mV)); } -BOOL LLXMLNode::getAttributeVector3(const LLString& name, LLVector3& value ) +BOOL LLXMLNode::getAttributeVector3(const char* name, LLVector3& value ) { LLXMLNodePtr node; return (getAttribute(name, node) && node->getFloatValue(3, value.mV)); } -BOOL LLXMLNode::getAttributeVector3d(const LLString& name, LLVector3d& value ) +BOOL LLXMLNode::getAttributeVector3d(const char* name, LLVector3d& value ) { LLXMLNodePtr node; return (getAttribute(name, node) && node->getDoubleValue(3, value.mdV)); } -BOOL LLXMLNode::getAttributeQuat(const LLString& name, LLQuaternion& value ) +BOOL LLXMLNode::getAttributeQuat(const char* name, LLQuaternion& value ) { LLXMLNodePtr node; return (getAttribute(name, node) && node->getFloatValue(4, value.mQ)); } -BOOL LLXMLNode::getAttributeUUID(const LLString& name, LLUUID& value ) +BOOL LLXMLNode::getAttributeUUID(const char* name, LLUUID& value ) { LLXMLNodePtr node; return (getAttribute(name, node) && node->getUUIDValue(1, &value)); } -BOOL LLXMLNode::getAttributeString(const LLString& name, LLString& value ) +BOOL LLXMLNode::getAttributeString(const char* name, std::string& value ) { LLXMLNodePtr node; if (!getAttribute(name, node)) @@ -1539,14 +1745,14 @@ U32 LLXMLNode::getBoolValue(U32 expected_length, BOOL *array) return 0; } - LLString *str_array = new LLString[expected_length]; + std::string *str_array = new std::string[expected_length]; U32 length = getStringValue(expected_length, str_array); U32 ret_length = 0; for (U32 i=0; i<length; ++i) { - LLString::toLower(str_array[i]); + LLStringUtil::toLower(str_array[i]); if (str_array[i] == "false") { array[ret_length++] = FALSE; @@ -1875,7 +2081,7 @@ U32 LLXMLNode::getDoubleValue(U32 expected_length, F64 *array, Encoding encoding return i; } -U32 LLXMLNode::getStringValue(U32 expected_length, LLString *array) +U32 LLXMLNode::getStringValue(U32 expected_length, std::string *array) { llassert(array); @@ -1950,7 +2156,7 @@ U32 LLXMLNode::getUUIDValue(U32 expected_length, LLUUID *array) memcpy(uuid_string, value_string, (UUID_STR_LENGTH-1)); /* Flawfinder: ignore */ uuid_string[(UUID_STR_LENGTH-1)] = 0; - if (!LLUUID::parseUUID(uuid_string, &uuid_value)) + if (!LLUUID::parseUUID(std::string(uuid_string), &uuid_value)) { break; } @@ -1978,7 +2184,7 @@ U32 LLXMLNode::getNodeRefValue(U32 expected_length, LLXMLNode **array) return 0; } - LLString *string_array = new LLString[expected_length]; + std::string *string_array = new std::string[expected_length]; U32 num_strings = getStringValue(expected_length, string_array); @@ -2018,7 +2224,7 @@ void LLXMLNode::setBoolValue(U32 length, const BOOL *array) { if (length == 0) return; - LLString new_value; + std::string new_value; for (U32 pos=0; pos<length; ++pos) { if (pos > 0) @@ -2041,7 +2247,7 @@ void LLXMLNode::setByteValue(U32 length, const U8* const array, Encoding encodin { if (length == 0) return; - LLString new_value; + std::string new_value; if (encoding == ENCODING_DEFAULT || encoding == ENCODING_DECIMAL) { for (U32 pos=0; pos<length; ++pos) @@ -2084,7 +2290,7 @@ void LLXMLNode::setIntValue(U32 length, const S32 *array, Encoding encoding) { if (length == 0) return; - LLString new_value; + std::string new_value; if (encoding == ENCODING_DEFAULT || encoding == ENCODING_DECIMAL) { for (U32 pos=0; pos<length; ++pos) @@ -2131,7 +2337,7 @@ void LLXMLNode::setUnsignedValue(U32 length, const U32* array, Encoding encoding { if (length == 0) return; - LLString new_value; + std::string new_value; if (encoding == ENCODING_DEFAULT || encoding == ENCODING_DECIMAL) { for (U32 pos=0; pos<length; ++pos) @@ -2180,7 +2386,7 @@ void LLXMLNode::setLongValue(U32 length, const U64* array, Encoding encoding) { if (length == 0) return; - LLString new_value; + std::string new_value; if (encoding == ENCODING_DEFAULT || encoding == ENCODING_DECIMAL) { for (U32 pos=0; pos<length; ++pos) @@ -2229,21 +2435,21 @@ void LLXMLNode::setFloatValue(U32 length, const F32 *array, Encoding encoding, U { if (length == 0) return; - LLString new_value; + std::string new_value; if (encoding == ENCODING_DEFAULT || encoding == ENCODING_DECIMAL) { - char format_string[10]; /* Flawfinder: ignore */ + std::string format_string; if (precision > 0) { if (precision > 25) { precision = 25; } - snprintf(format_string, sizeof(format_string), "%%.%dg", precision); /* Flawfinder: ignore */ + format_string = llformat( "%%.%dg", precision); } else { - snprintf(format_string, sizeof(format_string), "%%g"); /* Flawfinder: ignore */ + format_string = llformat( "%%g"); } for (U32 pos=0; pos<length; ++pos) @@ -2251,11 +2457,11 @@ void LLXMLNode::setFloatValue(U32 length, const F32 *array, Encoding encoding, U if (pos > 0) { new_value.append(" "); - new_value.append(llformat(format_string, array[pos])); + new_value.append(llformat(format_string.c_str(), array[pos])); } else { - new_value.assign(llformat(format_string, array[pos])); + new_value.assign(llformat(format_string.c_str(), array[pos])); } } mValue = new_value; @@ -2280,32 +2486,32 @@ void LLXMLNode::setDoubleValue(U32 length, const F64 *array, Encoding encoding, { if (length == 0) return; - LLString new_value; + std::string new_value; if (encoding == ENCODING_DEFAULT || encoding == ENCODING_DECIMAL) { - char format_string[10]; /* Flawfinder: ignore */ + std::string format_string; if (precision > 0) { if (precision > 25) { precision = 25; } - snprintf(format_string, sizeof(format_string), "%%.%dg", precision); /* Flawfinder: ignore */ + format_string = llformat( "%%.%dg", precision); } else { - snprintf(format_string, sizeof(format_string), "%%g"); /* Flawfinder: ignore */ + format_string = llformat( "%%g"); } for (U32 pos=0; pos<length; ++pos) { if (pos > 0) { new_value.append(" "); - new_value.append(llformat(format_string, array[pos])); + new_value.append(llformat(format_string.c_str(), array[pos])); } else { - new_value.assign(llformat(format_string, array[pos])); + new_value.assign(llformat(format_string.c_str(), array[pos])); } } mValue = new_value; @@ -2328,10 +2534,10 @@ void LLXMLNode::setDoubleValue(U32 length, const F64 *array, Encoding encoding, } // static -LLString LLXMLNode::escapeXML(const LLString& xml) +std::string LLXMLNode::escapeXML(const std::string& xml) { - LLString out; - for (LLString::size_type i = 0; i < xml.size(); ++i) + std::string out; + for (std::string::size_type i = 0; i < xml.size(); ++i) { char c = xml[i]; switch(c) @@ -2347,14 +2553,15 @@ LLString LLXMLNode::escapeXML(const LLString& xml) return out; } -void LLXMLNode::setStringValue(U32 length, const LLString *array) +void LLXMLNode::setStringValue(U32 length, const std::string *strings) { if (length == 0) return; - LLString new_value; + std::string new_value; for (U32 pos=0; pos<length; ++pos) { - new_value.append(escapeXML(array[pos])); + // *NOTE: Do not escape strings here - do it on output + new_value.append( strings[pos] ); if (pos < length-1) new_value.append(" "); } @@ -2368,7 +2575,7 @@ void LLXMLNode::setUUIDValue(U32 length, const LLUUID *array) { if (length == 0) return; - LLString new_value; + std::string new_value; for (U32 pos=0; pos<length; ++pos) { new_value.append(array[pos].asString()); @@ -2385,7 +2592,7 @@ void LLXMLNode::setNodeRefValue(U32 length, const LLXMLNode **array) { if (length == 0) return; - LLString new_value; + std::string new_value; for (U32 pos=0; pos<length; ++pos) { if (array[pos]->mID != "") @@ -2405,7 +2612,7 @@ void LLXMLNode::setNodeRefValue(U32 length, const LLXMLNode **array) mType = TYPE_NODEREF; } -void LLXMLNode::setValue(const LLString& value) +void LLXMLNode::setValue(const std::string& value) { if (TYPE_CONTAINER == mType) { @@ -2442,7 +2649,7 @@ void LLXMLNode::findDefault(LLXMLNode *defaults_list) mDefault = NULL; } -BOOL LLXMLNode::deleteChildren(const LLString& name) +BOOL LLXMLNode::deleteChildren(const std::string& name) { U32 removed_count = 0; LLXMLNodeList node_list; @@ -2494,7 +2701,7 @@ void LLXMLNode::setAttributes(LLXMLNode::ValueType type, U32 precision, LLXMLNod mLength = length; } -void LLXMLNode::setName(const LLString& name) +void LLXMLNode::setName(const std::string& name) { setName(gStringTable.addStringEntry(name)); } @@ -2515,14 +2722,15 @@ void LLXMLNode::setName(LLStringTableEntry* name) } } -void LLXMLNode::appendValue(const LLString& value) -{ - mValue.append(value); -} +// Unused +// void LLXMLNode::appendValue(const std::string& value) +// { +// mValue.append(value); +// } U32 LLXMLNode::getChildCount() const { - if (mChildren) + if (mChildren.notNull()) { return mChildren->map.size(); } @@ -2541,7 +2749,7 @@ U32 get_rand(U32 max_value) LLXMLNode *get_rand_node(LLXMLNode *node) { - if (node->mChildren) + if (node->mChildren.notNull()) { U32 num_children = node->mChildren->map.size(); if (get_rand(2) == 0) @@ -2568,14 +2776,13 @@ LLXMLNode *get_rand_node(LLXMLNode *node) void LLXMLNode::createUnitTest(S32 max_num_children) { // Random ID - char rand_id[20]; /* Flawfinder: ignore */ + std::string rand_id; U32 rand_id_len = get_rand(10)+5; - U32 pos = 0; - for (; pos<rand_id_len; ++pos) + for (U32 pos = 0; pos<rand_id_len; ++pos) { - rand_id[pos] = get_rand(26)+'a'; + char c = 'a' + get_rand(26); + rand_id.append(1, c); } - rand_id[pos] = 0; mID = rand_id; if (max_num_children < 2) @@ -2597,26 +2804,24 @@ void LLXMLNode::createUnitTest(S32 max_num_children) for (U32 child_num=0; child_num<num_children; ++child_num) { // Random Name - char child_name[20]; /* Flawfinder: ignore */ + std::string child_name; U32 child_name_len = get_rand(10)+5; - pos = 0; - for (; pos<child_name_len; ++pos) + for (U32 pos = 0; pos<child_name_len; ++pos) { - child_name[pos] = get_rand(26)+'a'; + char c = 'a' + get_rand(26); + child_name.append(1, c); } - child_name[pos] = 0; - LLXMLNode *new_child = createChild(child_name, FALSE); + LLXMLNode *new_child = createChild(child_name.c_str(), FALSE); // Random ID - char child_id[20]; /* Flawfinder: ignore */ + std::string child_id; U32 child_id_len = get_rand(10)+5; - pos = 0; - for (; pos<child_id_len; ++pos) + for (U32 pos=0; pos<child_id_len; ++pos) { - child_id[pos] = get_rand(26)+'a'; + char c = 'a' + get_rand(26); + child_id.append(1, c); } - child_id[pos] = 0; new_child->mID = child_id; // Random Length @@ -2750,9 +2955,9 @@ void LLXMLNode::createUnitTest(S32 max_num_children) createChild("float_checksum", TRUE)->setUnsignedValue(1, &float_checksum, LLXMLNode::ENCODING_HEX); } -BOOL LLXMLNode::performUnitTest(LLString &error_buffer) +BOOL LLXMLNode::performUnitTest(std::string &error_buffer) { - if (!mChildren) + if (mChildren.isNull()) { error_buffer.append(llformat("ERROR Node %s: No children found.\n", mName->mString)); return FALSE; @@ -3011,23 +3216,36 @@ BOOL LLXMLNode::performUnitTest(LLString &error_buffer) return TRUE; } -LLXMLNodePtr LLXMLNode::getFirstChild() +LLXMLNodePtr LLXMLNode::getFirstChild() const { - if (!mChildren) return NULL; + if (mChildren.isNull()) return NULL; LLXMLNodePtr ret = mChildren->head; return ret; } -LLXMLNodePtr LLXMLNode::getNextSibling() +LLXMLNodePtr LLXMLNode::getNextSibling() const { LLXMLNodePtr ret = mNext; return ret; } -LLString LLXMLNode::getTextContents() const +std::string LLXMLNode::getSanitizedValue() const +{ + if (mIsAttribute) + { + return getValue() ; + } + else + { + return getTextContents(); + } +} + + +std::string LLXMLNode::getTextContents() const { std::string msg; - LLString contents = mValue; + std::string contents = mValue; std::string::size_type n = contents.find_first_not_of(" \t\n"); if (n != std::string::npos && contents[n] == '\"') { @@ -3071,10 +3289,10 @@ LLString LLXMLNode::getTextContents() const else { // Case 2: node has embedded text (beginning and trailing whitespace trimmed) - LLString::size_type start = mValue.find_first_not_of(" \t\n"); + std::string::size_type start = mValue.find_first_not_of(" \t\n"); if (start != mValue.npos) { - LLString::size_type end = mValue.find_last_not_of(" \t\n"); + std::string::size_type end = mValue.find_last_not_of(" \t\n"); if (end != mValue.npos) { msg = mValue.substr(start, end+1-start); @@ -3089,3 +3307,13 @@ LLString LLXMLNode::getTextContents() const } return msg; } + +void LLXMLNode::setLineNumber(S32 line_number) +{ + mLineNumber = line_number; +} + +S32 LLXMLNode::getLineNumber() +{ + return mLineNumber; +} |