// Save bool COpcXmlDocument::Save(const COpcString& cFilePath) { HRESULT hResult = S_OK; VARIANT varFile; varFile.vt = VT_BSTR; varFile.bstrVal = SysAllocString((LPCWSTR)((cFilePath.IsEmpty())?m_cFilePath:cFilePath)); TRY { // save the file. hResult = m_ipDocument->save(varFile); if (FAILED(hResult)) { THROW(); } } CATCH_FINALLY { OpcVariantClear(&varFile); } return SUCCEEDED(hResult); }
// AddNamespace bool COpcXmlDocument::AddNamespace(const COpcString& cPrefix, const COpcString& cNamespace) { // check for a valid root element. COpcXmlElement cElement(GetRoot()); if (cElement == NULL) { return false; } // check for an invalid prefix if (cPrefix.IsEmpty()) { return false; } // construct the new namespace attribute name. COpcString cAttributeName; cAttributeName += OPCXML_NAMESPACE_ATTRIBUTE; cAttributeName += ":"; cAttributeName += cPrefix; // add or update the namespace string. cElement.SetAttribute(cAttributeName, cNamespace); return true; }
// GetNamespaces void COpcXmlDocument::GetNamespaces(COpcStringMap& cNamespaces) { // clear the current set. cNamespaces.RemoveAll(); // check for a valid root element. COpcXmlElement cElement(GetRoot()); if (cElement == NULL) { return; } // add the namespace for the root element. COpcString cPrefix = cElement.GetPrefix(); if (!cPrefix.IsEmpty()) { cNamespaces[cPrefix] = cElement.GetNamespace(); } // fetch the attributes from the root element. COpcXmlAttributeList cAttributes; if (cElement.GetAttributes(cAttributes) > 0) { for (UINT ii = 0; ii < cAttributes.GetSize(); ii++) { if (cAttributes[ii].GetPrefix() == OPCXML_NAMESPACE_ATTRIBUTE) { COpcString cName = cAttributes[ii].GetQualifiedName().GetName(); // don't add the default namespace. if (!cName.IsEmpty()) { cNamespaces[cName] = cAttributes[ii].GetValue(); } } } } }
COpcTextReader::COpcTextReader(const COpcString& cBuffer) : m_szBuf(NULL), m_uLength(0), m_uEndOfData(0) { if (!cBuffer.IsEmpty()) { LPCWSTR szBuf = (COpcString&)cBuffer; m_uLength = (szBuf != NULL)?wcslen(szBuf):0; m_szBuf = (WCHAR*)OpcAlloc((m_uLength+1)*sizeof(WCHAR)); wcsncpy(m_szBuf, szBuf, m_uLength); m_uEndOfData = m_uLength; m_szBuf[m_uEndOfData] = L'\0'; } }
// GetItemID COpcString COpcBrowseElement::GetItemID() const { if (m_cItemID.IsEmpty()) { COpcString cItemID; if (m_pParent != NULL) { cItemID += m_pParent->GetItemID(); if (!cItemID.IsEmpty()) { cItemID += m_pParent->GetSeparator(); } } cItemID += m_cName; return cItemID; } return m_cItemID; }
// Read template<> bool OpcXml::Read(const COpcString& cString, DateTime& cValue) { bool bResult = true; FILETIME cFileTime; // check for invalid date. if (cString.IsEmpty()) { cValue = OpcMinDate(); return true; } TRY { SYSTEMTIME cSystemTime; ZeroMemory(&cSystemTime, sizeof(cSystemTime)); COpcText cText; COpcTextReader cReader(cString); // parse date fields. cText.SetType(COpcText::Delimited); cText.SetDelims(L"T"); if (!cReader.GetNext(cText)) THROW_(bResult, false); if (!ToDate(cText, cSystemTime.wYear, cSystemTime.wMonth, cSystemTime.wDay)) { THROW_(bResult, false); } // parse time fields. cText.SetType(COpcText::Delimited); cText.SetDelims(L"Z-+"); cText.SetEofDelim(); if (!cReader.GetNext(cText)) THROW_(bResult, false); bResult = ToTime( cText, cSystemTime.wHour, cSystemTime.wMinute, cSystemTime.wSecond, cSystemTime.wMilliseconds); if (!bResult) { THROW_(bResult, false); } // convert to a UTC file time. if (!SystemTimeToFileTime(&cSystemTime, &cFileTime)) { THROW_(bResult, false); } if (cText.GetDelimChar() != _T('Z')) { // convert local system time to UTC file time. if (cText.GetEof()) { FILETIME ftUtcTime; if (!OpcLocalTimeToUtcTime(cFileTime, ftUtcTime)) { THROW_(bResult, false); } cFileTime = ftUtcTime; } // apply offset specified in the datetime string. else { bool bNegative = (cText.GetDelimChar() == _T('-')); // parse time fields. cText.SetType(COpcText::Delimited); cText.SetEofDelim(); if (!cReader.GetNext(cText)) THROW_(bResult, false); SHORT sMinutes = 0; bResult = ToOffset( cText, bNegative, sMinutes); if (!bResult) { THROW_(bResult, false); } // apply timezone offset. LONGLONG llTime = OpcToInt64(cFileTime); llTime -= ((LONGLONG)sMinutes)*60*10000000; cFileTime = OpcToFILETIME(llTime); } } cValue = cFileTime; } CATCH { ZeroMemory(&cFileTime, sizeof(cFileTime)); cValue = cFileTime; } return bResult; }
// Load bool COpcXmlDocument::Load(const COpcString& cFilePath) { HRESULT hResult = S_OK; VARIANT varFile; varFile.vt = VT_BSTR; varFile.bstrVal = SysAllocString((LPCWSTR)cFilePath); TRY { // create new document instance. if (!Init()) { THROW_(hResult, E_FAIL); } // load the file. VARIANT_BOOL bResult = VARIANT_FALSE; hResult = m_ipDocument->load(varFile, &bResult); if (FAILED(hResult)) { THROW(); } if (!bResult) { IXMLDOMParseError* ipError = NULL; hResult = m_ipDocument->get_parseError(&ipError); if (FAILED(hResult)) { THROW_(hResult, E_FAIL); } BSTR bstrReason = NULL; hResult = ipError->get_reason(&bstrReason); if (SUCCEEDED(hResult)) { SysFreeString(bstrReason); } ipError->Release(); } // update default path. if (!cFilePath.IsEmpty()) m_cFilePath = cFilePath; } CATCH { Clear(); } FINALLY { OpcVariantClear(&varFile); } return SUCCEEDED(hResult); }
// 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; }