void BBWinConfig::LoadConfiguration(const string & fileName, const string & configNameSpace, bbwinconfig_t & config) { string configName(BBWIN_CONFIG_ROOT); EnterCriticalSection(&m_configCriticalSection); m_path = fileName; m_doc = new TiXmlDocument(m_path.c_str()); if (m_doc == NULL) throw BBWinConfigException("can't allocare tinyxml instance"); bool loadOkay = m_doc->LoadFile(); if ( !loadOkay ) { LeaveCriticalSection(&m_configCriticalSection); throw BBWinConfigException(m_doc->ErrorDesc()); } TiXmlElement *root = m_doc->FirstChildElement( "configuration" ); if ( root ) { TiXmlNode * nameSpaceNode = root->FirstChild( configNameSpace.c_str() ); if ( nameSpaceNode ) { TiXmlElement * elem; for (elem = nameSpaceNode->FirstChildElement(); elem ; elem = elem->NextSiblingElement()) { bbwinconfig_attr_t config_attr; if (elem->Type() == TiXmlNode::ELEMENT) { TiXmlAttribute * attr; for (attr = elem->FirstAttribute(); attr; attr = attr->Next()) { config_attr.insert(pair< string, string >(attr->Name(), attr->Value())); } } config.insert(pair < string, bbwinconfig_attr_t > (elem->Value(), config_attr)); } } } delete m_doc; m_doc = NULL; LeaveCriticalSection(&m_configCriticalSection); }
/*********************************************************************************************************** * 程序作者:赵进军 * 函数功能:通过节点名获取XML中所有符合条件的节点指针 * 参数说明: * pRootEle:XML文件的根节点 * strNodeName:需要获取的节点指针的名称 * retNode:获取到的符合条件的节点指针数组 * 注意事项:在使用该函数之前 arrayIndex全局变量必须置为0 * 修改日期:2015/12/13 13:56:00 ***********************************************************************************************************/ bool OperationProfile_XML::GetNodePointerByNameAll(TiXmlElement* pRootEle, char* strNodeName, TiXmlElement* retNode[]) { if (pRootEle == NULL) return false; if (!strcmp(strNodeName, pRootEle->Value())) // 假如等于节点名就自动退出 { if (arrayIndex < groupNodeCount) retNode[arrayIndex] = pRootEle; else printf("请重新定义节点的数量"); arrayIndex++; return true; } TiXmlElement* pchild = pRootEle->FirstChildElement(); while (pchild) { int t = pchild->Type(); if (t == TiXmlNode::TINYXML_ELEMENT) GetNodePointerByNameAll(pchild, strNodeName, retNode); pchild = pchild->NextSiblingElement(); } if (NULL == retNode) return false; else return true; }
/*********************************************************************************************************** * 程序作者:赵进军 * 函数功能:通过节点名获取XML的节点指针 * 参数说明: * pRootEle:XML文件的根节点 * strNodeName:需要获取的节点指针的名称 * node:获取到的符合条件的节点指针 * nodeIndex:节点的位置 * 注意事项:在使用该函数之前 arrayIndex全局变量必须置为0 * 修改日期:2015/12/12 17:18:00 ***********************************************************************************************************/ bool OperationProfile_XML::GetNodePointerByName(TiXmlElement* pRootEle, char* strNodeName, TiXmlElement* &node, int nodeIndex) { if (pRootEle == NULL) return false; if (!strcmp(strNodeName, pRootEle->Value())) // 假如等于节点名就自动退出 { if (arrayIndex == nodeIndex) node = pRootEle; arrayIndex++; return true; } TiXmlElement* pchild = pRootEle->FirstChildElement(); while (pchild) { int t = pchild->Type(); if (t == TiXmlNode::TINYXML_ELEMENT) GetNodePointerByName(pchild, strNodeName, node, nodeIndex); pchild = pchild->NextSiblingElement(); } if (NULL == node) return false; else return true; }
bool XmlParseConfig(Config &c, TiXmlElement *conf) { if ( conf == NULL ) return false; stringstream ss; TiXmlElement *config = conf->FirstChildElement(); for ( ; config; config = config->NextSiblingElement() ) { if ( config->Type() != TiXmlNode::TINYXML_ELEMENT ) continue; string configType(config->Value()); const char *name = config->Attribute("Name"); if ( !name ) { Severe("Config Name attribute missing"); return false; } string configName(name); if ( configType == "Float" ) { double value; if ( !config->Attribute("Value", &value) ) { ss << "Config [" << configName << "] missing value"; Severe(ss); return false; } c.AddAttribute(configName, (float) value); } else if ( configType == "Int32" ) { int i; if ( !config->Attribute("Value", &i) ) { ss << "Config [" << configName << "] missing value"; Severe(ss); return false; } c.AddAttribute(configName, i); } else if ( configType == "Vector" ) { Vector vec; TiXmlElement *elemVec = config->FirstChildElement("Value"); if ( !elemVec ) { ss << "Config [" << configName << "] missing value"; Severe(ss); return false; } if ( !XmlParseVector(vec, elemVec) ) return false; c.AddAttribute(configName, vec); } else if ( configType == "Bool" ) { int b; if ( !config->Attribute("Value", &b) ) { ss << "Config [" << configName << "] missing value"; Severe(ss); return false; } c.AddAttribute(configName, b != 0); } else { ss << "Unknown config type: [" << configType << "]"; Severe(ss); return false; } } return true; }
int em::EmXml::ReadRootChildAttrList( EmVecMapStr &rVecMapStr ) { int iResult = 0; rVecMapStr.clear(); TiXmlElement *pElemRoot = m_pDoc->RootElement(); if(pElemRoot == NULL) { return 0; } TiXmlElement *pElemChild = pElemRoot->FirstChildElement(); TiXmlAttribute *pAttrChild = NULL; EmMapStr MapStr; while(true) { if(pElemChild == NULL) { break; } if(pElemChild->Type() != TiXmlNode::TINYXML_ELEMENT) { continue; } MapStr.clear(); pAttrChild = pElemChild->FirstAttribute(); while(true) { if(pAttrChild == NULL) { break; } MapStr[ pAttrChild->Name() ] = pAttrChild->Value(); pAttrChild = pAttrChild->Next(); } rVecMapStr.push_back(MapStr); pElemChild = pElemChild->NextSiblingElement(); } iResult = rVecMapStr.size(); return iResult; }
int em::EmXml::ReadRootChildAttrMap( const char* szKeyName, const char* szKeyValue, EmMapStr &rMapStr ) { int iResult = 0; rMapStr.clear(); TiXmlElement *pElemRoot = m_pDoc->RootElement(); if(pElemRoot == NULL) { return 0; } TiXmlElement *pElemChild = pElemRoot->FirstChildElement(); TiXmlAttribute *pAttrChild = NULL; while(true) { if(pElemChild == NULL) { break; } if(pElemChild->Type() != TiXmlNode::TINYXML_ELEMENT) { continue; } if(EmSz::Equal( pElemChild->Attribute( szKeyName) , szKeyValue)) { pAttrChild = pElemChild->FirstAttribute(); while(true) { if(pAttrChild == NULL) { break; } rMapStr[ pAttrChild->Name() ] = pAttrChild->Value(); pAttrChild = pAttrChild->Next(); } break; } pElemChild = pElemChild->NextSiblingElement(); } iResult = rMapStr.size(); return iResult; }
int em::EmXml::RemoveRootChild( const char* szChildName, const char* szKeyName, const char* szKeyValue ) { bool bUpdated = false; int iResult = 0; TiXmlElement *pElemRoot = m_pDoc->RootElement(); if(pElemRoot == NULL) { return 0; } TiXmlElement *pElemChild = pElemRoot->FirstChildElement(); TiXmlAttribute *pAttrChild = NULL; while(true) { if(pElemChild == NULL) { break; } if(pElemChild->Type() != TiXmlNode::TINYXML_ELEMENT) { continue; } pAttrChild = pElemChild->FirstAttribute(); if(EmSz::Equal( pElemChild->Attribute( szKeyName ), szKeyValue)) { if(EmSz::Equal(pElemChild->Value(),szChildName)) { pElemRoot->RemoveChild(pElemChild); bUpdated = true; break; } } pElemChild = pElemChild->NextSiblingElement(); } m_bUpdated = bUpdated; return iResult; }
int em::EmXml::WriteRootChildAttrMap( const char* szKeyName, const char* szKeyValue, EmMapStr &rMapStr ) { std::string strResult; bool bUpdated = false; int iResult = 0; TiXmlElement *pElemRoot = m_pDoc->RootElement(); if(pElemRoot == NULL) { return 0; } TiXmlElement *pElemChild = pElemRoot->FirstChildElement(); TiXmlAttribute *pAttrChild = NULL; while(true) { if(pElemChild == NULL) { break; } if(pElemChild->Type() != TiXmlNode::TINYXML_ELEMENT) { continue; } pAttrChild = pElemChild->FirstAttribute(); if(EmSz::Equal( pElemChild->Attribute( szKeyName), szKeyValue)) { EmMapStr::const_iterator iterMap; for(iterMap = rMapStr.begin(); iterMap != rMapStr.end(); iterMap++) { pElemChild->SetAttribute(iterMap->first , iterMap->second); } bUpdated = true; break; } pElemChild = pElemChild->NextSiblingElement(); } m_bUpdated = bUpdated; return iResult; }
std::string em::EmXml::ReadRootChildAttr( const char* szKeyName, const char* szKeyValue, const char* szAttrName ) { std::string strResult; int iResult = 0; TiXmlElement *pElemRoot = m_pDoc->RootElement(); if(pElemRoot == NULL) { return ""; } TiXmlElement *pElemChild = pElemRoot->FirstChildElement(); TiXmlAttribute *pAttrChild = NULL; while(true) { if(pElemChild == NULL) { break; } if(pElemChild->Type() != TiXmlNode::TINYXML_ELEMENT) { continue; } pAttrChild = pElemChild->FirstAttribute(); if( EmSz::Equal(pElemChild->Attribute( szKeyName ), szKeyValue) ) { strResult = pElemChild->Attribute( szAttrName); break; } pElemChild = pElemChild->NextSiblingElement(); } return strResult; }
void VersionInfoFactory::ParseOffsets(TiXmlElement * parent, VersionInfo* target, bool initial) { // we parse the groups iteratively instead of recursively // breadcrubs acts like a makeshift stack // first pair entry stores the current element of that level // second pair entry the group object from OffsetGroup typedef triple< TiXmlElement *, OffsetGroup *, INVAL_TYPE> groupTriple; vector< groupTriple > breadcrumbs; { TiXmlElement* pEntry; // we get the <Offsets>, look at the children pEntry = parent->FirstChildElement(); if(!pEntry) return; const char *cstr_invalid = parent->Attribute("valid"); INVAL_TYPE parent_inval = NOT_SET; if(cstr_invalid) { if(strcmp(cstr_invalid,"false") == 0) parent_inval = IS_INVALID; else if(strcmp(cstr_invalid,"true") == 0) parent_inval = IS_VALID; } OffsetGroup * currentGroup = reinterpret_cast<OffsetGroup *> (target); currentGroup->setInvalid(parent_inval); breadcrumbs.push_back(groupTriple(pEntry,currentGroup, parent_inval)); } // work variables OffsetGroup * currentGroup = 0; TiXmlElement * currentElem = 0; INVAL_TYPE parent_inval = NOT_SET; //cerr << "<Offsets>"<< endl; while(1) { // get current work variables currentElem = breadcrumbs.back().first; currentGroup = breadcrumbs.back().second; parent_inval = breadcrumbs.back().third; // we reached the end of the current group? if(!currentElem) { // go one level up breadcrumbs.pop_back(); // exit if no more work if(breadcrumbs.empty()) { break; } else { //cerr << "</group>" << endl; continue; } } if(!currentGroup) { groupTriple & gp = breadcrumbs.back(); gp.first = gp.first->NextSiblingElement(); continue; } // skip non-elements if (currentElem->Type() != TiXmlNode::ELEMENT) { groupTriple & gp = breadcrumbs.back(); gp.first = gp.first->NextSiblingElement(); continue; } // we have a valid current element and current group // get properties string type = currentElem->Value(); std::transform(type.begin(), type.end(), type.begin(), ::tolower); const char *cstr_name = currentElem->Attribute("name"); if(!cstr_name) { // ERROR, missing attribute } // evaluate elements const char *cstr_value = currentElem->Attribute("value"); const char *cstr_invalid = currentElem->Attribute("valid"); INVAL_TYPE child_inval = parent_inval; if(cstr_invalid) { if(strcmp(cstr_invalid,"false") == 0) child_inval = IS_INVALID; else if(strcmp(cstr_invalid,"true") == 0) child_inval = IS_VALID; } if(type == "group") { // create or get group OffsetGroup * og; if(initial) og = currentGroup->createGroup(cstr_name); else og = currentGroup->getGroup(cstr_name); // advance this level to the next element groupTriple & gp = breadcrumbs.back(); gp.first = currentElem->NextSiblingElement(); if(!og) { string fullname = currentGroup->getFullName() + cstr_name; throw Error::MissingMemoryDefinition("group", fullname); } // add a new level that will be processed in the next step breadcrumbs.push_back(groupTriple(currentElem->FirstChildElement(), og, child_inval)); og->setInvalid(child_inval); continue; } else if(type == "address") { if(child_inval == NOT_SET) child_inval = IS_VALID; if(initial) { currentGroup->createAddress(cstr_name); } else if(cstr_value) { currentGroup->setAddress(cstr_name, cstr_value, child_inval); } else { currentGroup->setAddressValidity(cstr_name, child_inval); } } else if(type == "offset") { if(child_inval == NOT_SET) child_inval = IS_VALID; if(initial) { currentGroup->createOffset(cstr_name); } else if(cstr_value) { currentGroup->setOffset(cstr_name, cstr_value, child_inval); } else { currentGroup->setOffsetValidity(cstr_name, child_inval); } } else if(type == "string") { if(child_inval == NOT_SET) child_inval = IS_VALID; if(initial) { currentGroup->createString(cstr_name); } else if(cstr_value) { currentGroup->setString(cstr_name, cstr_value, child_inval); } else { currentGroup->setStringValidity(cstr_name, child_inval); } } else if(type == "hexvalue") { if(child_inval == NOT_SET) child_inval = IS_VALID; if(initial) { currentGroup->createHexValue(cstr_name); } else if(cstr_value) { currentGroup->setHexValue(cstr_name, cstr_value, child_inval); } else { currentGroup->setHexValueValidity(cstr_name, child_inval); } } // advance to next element groupTriple & gp = breadcrumbs.back(); gp.first = currentElem->NextSiblingElement(); continue; } //cerr << "</Offsets>"<< endl; }