⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 didl_lite.cpp

📁 Windows CE 6.0 Server 源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft shared
// source or premium shared source license agreement under which you licensed
// this source code. If you did not accept the terms of the license agreement,
// you are not authorized to use this source code. For the terms of the license,
// please see the license agreement between you and Microsoft or, if applicable,
// see the SOURCE.RTF on your install media or the root of your tools installation.
// THE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES.
//

#include "av_upnp.h"
#include "av_upnp_ctrl_internal.h"

using namespace av_upnp;
using namespace av_upnp::DIDL_Lite;
using namespace av_upnp::DIDL_Lite::details;


/////////////////////////////////////////////////
// DIDL_Lite::parser
/////////////////////////////////////////////////


// ~parser
DIDL_Lite::parser::~parser()
{
    delete pObjects;
}


//
// GetFirstObject
//
bool DIDL_Lite::parser::GetFirstObject(LPCWSTR pszXml, object* pObj)
{
    //
    // Create instance of "objects" if it doesn't exist yet. 
    // Existing instance will be reinitialized for reuse in SAX::startDocument
    //
    if(!pObjects)
    {
        if(!(pObjects = new objects))
        {
            DEBUGMSG(ZONE_AV_ERROR, (AV_TEXT("OOM creating \"objects\"")));
            return false;
        }
    }
    
    //
    // Parse provided DIDL_Lite document
    //
    ce::SAXReader   Reader;
    HRESULT         hr;
    ce::variant     varXML(pszXml);
    
    if(Reader.valid())
    {
        hr = Reader->putContentHandler(pObjects);
        
        assert(SUCCEEDED(hr));

        if(FAILED(hr = Reader->parse(varXML)))
        {
            DEBUGMSG(TRUE, (TEXT("Error 0x%08x when parsing DIDL_Lite"), hr));
            return false;
        }
    }
    else
    {
        DEBUGMSG(TRUE, (TEXT("Error 0x%08x instantiating SAXXMLReader"), Reader.Error()));
        return false;
    }
    
    //
    // Return first object
    //
    return pObjects->GetFirstObject(pObj);
}


//
// GetNextObject
//
bool DIDL_Lite::parser::GetNextObject(object* pObj)
{
    //
    // GetNextObject called w/o calling GetFirstObject (or after GetFirstObject failed)
    //
    assert(pObjects);
    
    return pObjects->GetNextObject(pObj);
}


//
// AddNamespaceMapping
//
bool DIDL_Lite::parser::AddNamespaceMapping(LPCWSTR pszNamespace, LPCWSTR pszPrefix)
{
    //
    // Create instance of "objects" if it doesn't exist yet. 
    // Existing instance will be reinitialized for reuse in SAX::startDocument
    //
    if(!pObjects)
    {
        if(!(pObjects = new objects))
        {
            DEBUGMSG(ZONE_AV_ERROR, (AV_TEXT("OOM creating \"objects\"")));
            return false;
        }
    }
    
    return pObjects->AddNamespaceMapping(pszNamespace, pszPrefix);
}


/////////////////////////////////////////////////
// DIDL_Lite::object
/////////////////////////////////////////////////

// GetProperty
bool DIDL_Lite::object::GetProperty(LPCWSTR pszName, wstring* pValue, unsigned long nIndex/* = 0*/)
{
    assert(pProperties);
    
    return pProperties->GetProperty(pszName, pValue, nIndex);
}


// GetProperty
bool DIDL_Lite::object::GetProperty(LPCWSTR pszName, signed long* pValue, unsigned long nIndex/* = 0*/)
{
    wstring strValue;
    
    if(!GetProperty(pszName, &strValue, nIndex))
        return false;
        
    signed long lValue = _wtol(strValue);
    
    if(0 == lValue && wstring::npos != strValue.find_first_not_of(L"0"))
        return false;
    
    *pValue = lValue;
    
    return true;
}


// GetProperty
bool DIDL_Lite::object::GetProperty(LPCWSTR pszName, unsigned long* pValue, unsigned long nIndex/* = 0*/)
{
    signed long lValue;
    
    if(!GetProperty(pszName, &lValue, nIndex))
        return false;
        
    if(lValue < 0)
        return false;
        
    *pValue = static_cast<unsigned long>(lValue);
    
    return true;
}


// GetProperty
bool DIDL_Lite::object::GetProperty(LPCWSTR pszName, bool* pValue, unsigned long nIndex/* = 0*/)
{
    wstring strValue;
    
    if(!GetProperty(pszName, &strValue, nIndex))
        return false;
    
    if(strValue == L"0" || strValue == L"false")
        *pValue = false;
    else if(strValue == L"1" || strValue == L"true")
        *pValue = true;
    else
        return false;
    
    return true;
}


/////////////////////////////////////////////////
// DIDL_Lite::details::objects
/////////////////////////////////////////////////

DIDL_Lite::details::objects::objects()
{
    m_itCurrent = m_listObjects.end();
    
    // initialize namespace prefixes
    AddNamespaceMapping(L"urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/", L"");
    AddNamespaceMapping(L"http://purl.org/dc/elements/1.1/", L"dc");
    AddNamespaceMapping(L"urn:schemas-upnp-org:metadata-1-0/upnp/", L"upnp");
}


//
// GetFirstObject
//
bool DIDL_Lite::details::objects::GetFirstObject(object* pObj)
{
    assert(pObj);
    
    m_itCurrent = m_listObjects.begin();
    
    return GetNextObject(pObj);
}


//
// GetNextObject
//
bool DIDL_Lite::details::objects::GetNextObject(object* pObj)
{
    assert(pObj);
    
    if(m_itCurrent == m_listObjects.end())
        return false;
    
    pObj->pProperties = &*m_itCurrent++;
    
    //
    // Parser validates that all required property are present
    // and are correct type
    //
    bool bResult;
    
    bResult = pObj->GetProperty(L"@id", &pObj->strID);
    assert(bResult);
    
    bResult = pObj->GetProperty(L"upnp:class", &pObj->strClass);
    assert(bResult);
    
    bResult = pObj->GetProperty(L"@parentID", &pObj->strParentID);
    assert(bResult);
    
    bResult = pObj->GetProperty(L"@restricted", &pObj->bRestricted);
    assert(bResult);
    
    bResult = pObj->GetProperty(L"dc:title", &pObj->strTitle);
    assert(bResult);
    
    pObj->bContainer = (0 == pObj->strClass.compare(L"object.container", 16));
    
    return true;
}


//
// AddNamespaceMapping
//
bool DIDL_Lite::details::objects::AddNamespaceMapping(LPCWSTR pszNamespace, LPCWSTR pszPrefix)
{
    assert(pszNamespace);
    assert(pszPrefix);
    assert(pszPrefix[0] != L':');
    
    wstring strPrefix;
    
    if(*pszPrefix)
    {
        strPrefix.assign(pszPrefix);
        strPrefix.append(L":");
    }
    

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -