#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memory>
#include "namespaces.h"
using namespace P6R;
namespace P6EXAMPLES {
CNamespaces::CNamespaces()
: m_bInitialized( false ),
m_pMapping( NULL ),
m_mapSize( 0 ),
m_nextIndex( 0 )
{
}
CNamespaces::~CNamespaces()
{
if (NULL != m_pMapping)
{
{
if (NULL != m_pMapping[i].pPrefix) free( m_pMapping[i].pPrefix );
if (NULL != m_pMapping[i].pURI ) free( m_pMapping[i].pURI );
}
delete [] m_pMapping;
}
}
{
if (m_bInitialized) return eAlreadyInitialized;
m_mapSize = mapSize;
if (NULL == (m_pMapping = new (std::nothrow) prefixToURL_t[ m_mapSize ])) return eNoMemory;
{
m_pMapping[i].pPrefix = NULL;
m_pMapping[i].prefixLength = 0;
m_pMapping[i].pURI = NULL;
m_pMapping[i].uriLength = 0;
}
return eOk;
}
{
if (!m_bInitialized) return eNotInitialized;
if (NULL == pPrefix || 0 == prefixLength || NULL == pURI || 0 == uriLength) return eInvalidArg;
if (m_nextIndex == m_mapSize) return eTooBig;
m_pMapping[ m_nextIndex ].pPrefix = strdup( pPrefix );
m_pMapping[ m_nextIndex ].prefixLength = prefixLength;
m_pMapping[ m_nextIndex ].pURI = strdup( pURI );
m_pMapping[ m_nextIndex ].uriLength = uriLength;
if (NULL == m_pMapping[ m_nextIndex ].pPrefix || NULL == m_pMapping[ m_nextIndex ].pURI) err = eNoMemory;
m_nextIndex++;
return err;
}
{
if (!m_bInitialized) return eNotInitialized;
if (NULL == pPrefix || 0 == prefixLength || NULL == pLength) return eInvalidArg;
for(
P6UINT32 i=0; i < m_nextIndex; i++ )
{
if (NULL != m_pMapping[i].pPrefix && NULL != m_pMapping[i].pURI)
{
if (0 == strcmp( pPrefix, m_pMapping[i].pPrefix ))
{
if (NULL != pURI)
{
if ( *pLength > m_pMapping[i].uriLength )
{
strcpy( pURI, m_pMapping[i].pURI );
*pLength = strlen( pURI );
return eOk;
}
else err = eTooSmall;
}
*pLength = m_pMapping[i].uriLength + 1;
return err;
}
}
}
return eNotFound;
}
}