// Find COpcBrowseElement* COpcBrowseElement::Find(const COpcString& cPath) { // remove leading separator - if it exists. COpcString cPrefix = GetSeparator(); COpcString cLocalPath = cPath; while (cLocalPath.Find(GetSeparator()) == 0) { cLocalPath = cLocalPath.SubStr(cPrefix.GetLength()); } // recursively search children. OPC_POS pos = m_cChildren.GetHeadPosition(); while (pos != NULL) { COpcBrowseElement* pChild = m_cChildren.GetNext(pos); // check for a child with an exact name match. if (cLocalPath == pChild->m_cName) { return pChild; } // check if the path starts with the child name. COpcString cPrefix = pChild->m_cName + pChild->GetSeparator(); // check for a child with an exact name match plus trailing separator. if (cLocalPath == cPrefix) { return pChild; } UINT uIndex = cLocalPath.Find(cPrefix); // search the child node if there is a match. if (uIndex == 0) { cLocalPath = cLocalPath.SubStr(cPrefix.GetLength()); return pChild->Find(cLocalPath); } } return NULL; }
// Insert COpcBrowseElement* COpcBrowseElement::Insert(const COpcString& cPath) { COpcString cName = cPath; COpcString cSubPath = OPC_EMPTY_STRING; // check if multiple levels have been specified. do { UINT uIndex = cName.Find(GetSeparator()); if (uIndex == -1) { break; } cSubPath = cName.SubStr(uIndex + GetSeparator().GetLength()); cName = cName.SubStr(0, uIndex); if (!cName.IsEmpty()) { break; } cName = cSubPath; } while (!cSubPath.IsEmpty()); // invalid path specified. if (cName.IsEmpty()) { return NULL; } // find out if node already exists. COpcBrowseElement* pNode = NULL; OPC_POS pos = m_cChildren.GetHeadPosition(); while (pos != NULL) { pNode = m_cChildren.GetNext(pos); if (pNode->m_cName == cName) { // return existing node. if (cSubPath.IsEmpty()) { return pNode; } // insert sub-path into existing node. return pNode->Insert(cSubPath); } } // create new node. pNode = CreateInstance(); pNode->m_cName = cName; OPC_ASSERT(!pNode->m_cName.IsEmpty()); COpcBrowseElement* pChild = pNode; if (!cSubPath.IsEmpty()) { pChild = pNode->Insert(cSubPath); if (pChild == NULL) { delete pNode; return NULL; } } m_cChildren.AddTail(pNode); return pChild; }