home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Softwarová Záchrana 3
/
Softwarova-zachrana-3.bin
/
pserv.cpl
/
pserv-2.4.exe
/
source
/
PXmlReader.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
2005-01-05
|
6KB
|
211 lines
#include "stdafx.h"
#include "PXmlReader.h"
#include "XML\xmlparse.h"
PXmlReader::PXmlReader()
: m_lpParser( NULL ), m_bRecordStartType( FALSE ),
m_dwFlags( XMLREADERF_STRIP_TRAILING_WHITESPACES|XMLREADERF_STRIP_EMPTY_CONTENT )
{
}
PXmlReader::~PXmlReader()
{
if( m_lpParser )
{
XML_ParserFree( m_lpParser );
}
while( m_Actions.GetCount() )
{
ServiceAction* a = (ServiceAction*)m_Actions.GetHead();
m_Actions.RemoveHead();
delete a;
}
}
void PXmlReader::OnStartElement( LPCTSTR lpszName, LPCTSTR* lpszAttributes )
{
m_bRecordStartType = FALSE;
if( !lstrcmpi( lpszName, _T("service") ) )
{
if( lpszAttributes[0] && lpszAttributes[1] && !lpszAttributes[2] )
{
m_lpCurrentAction = new ServiceAction( lpszAttributes[1] );
if( m_lpCurrentAction != NULL )
{
m_Actions.AddTail( m_lpCurrentAction );
}
}
}
else if( !lstrcmpi( lpszName, _T("start") ) )
{
m_bRecordStartType = TRUE;
}
}
void PXmlReader::OnEndElement( LPCTSTR lpszName )
{
m_bRecordStartType = FALSE;
}
#define SERVICE_BOOT_START 0x00000000
#define SERVICE_SYSTEM_START 0x00000001
#define SERVICE_AUTO_START 0x00000002
#define SERVICE_DEMAND_START 0x00000003
#define SERVICE_DISABLED 0x00000004
void PXmlReader::OnCharacters( LPCTSTR lpszContent, int len )
{
if( (m_lpCurrentAction != NULL) && (m_bRecordStartType != FALSE) )
{
if( !lstrcmpi( lpszContent, _T("Boot") ) )
{
m_lpCurrentAction->m_dwActionToTake = SERVICE_BOOT_START;
}
else if( !lstrcmpi( lpszContent, _T("System") ) )
{
m_lpCurrentAction->m_dwActionToTake = SERVICE_SYSTEM_START;
}
else if( !lstrcmpi( lpszContent, _T("Automatic") ) )
{
m_lpCurrentAction->m_dwActionToTake = SERVICE_AUTO_START;
}
else if( !lstrcmpi( lpszContent, _T("Manual") ) )
{
m_lpCurrentAction->m_dwActionToTake = SERVICE_DEMAND_START;
}
else if( !lstrcmpi( lpszContent, _T("Disabled") ) )
{
m_lpCurrentAction->m_dwActionToTake = SERVICE_DISABLED;
}
}
}
void PXmlReader_OnStartElement( LPVOID lpUserData, LPCTSTR lpszName, LPCTSTR* lpszAttributes )
{
((PXmlReader*)lpUserData)->OnStartElement( lpszName, lpszAttributes );
}
void PXmlReader_OnEndElement( LPVOID lpUserData, LPCTSTR lpszName )
{
((PXmlReader*)lpUserData)->OnEndElement( lpszName );
}
void PXmlReader_OnCharacter(void *userData, LPCTSTR s, int len)
{
((PXmlReader*)userData)->OnCharactersNotZeroTerminated( s, len );
}
void PXmlReader::OnCharactersNotZeroTerminated( LPCTSTR s, int len )
{
if( m_dwFlags & XMLREADERF_STRIP_TRAILING_WHITESPACES )
{
int nCharIndex;
for( nCharIndex = 0; nCharIndex < len; nCharIndex++ )
{
if( !isspace(s[nCharIndex]) )
{
break;
}
}
s += nCharIndex;
len -= nCharIndex;
for( nCharIndex = len -1; nCharIndex >= 0; nCharIndex-- )
{
if( !isspace(s[nCharIndex]) )
{
break;
}
}
len = nCharIndex+1;
}
if( (m_dwFlags & XMLREADERF_STRIP_EMPTY_CONTENT) && !len )
{
}
else
{
if( len < (sizeof(m_szContentBuffer)-1) )
{
CopyMemory( m_szContentBuffer, s, len );
m_szContentBuffer[len] = 0;
OnCharacters( m_szContentBuffer, len );
}
else
{
TCHAR* buffer = new TCHAR[len+1];
if( buffer )
{
CopyMemory( buffer, s, len );
buffer[len] = 0;
OnCharacters( buffer, len );
delete [] buffer;
}
else
{
// notl÷sung: teilschritte im read-buffer
while( len )
{
int nBytes = sizeof(m_szContentBuffer)-1;
CopyMemory( m_szContentBuffer, s, nBytes );
m_szContentBuffer[nBytes] = 0;
OnCharacters( m_szContentBuffer, nBytes );
len -= nBytes;
}
}
}
}
}
BOOL PXmlReader::Read( LPCTSTR lpszFilename )
{
#ifdef UNICODE
return FALSE;
#else
FILE* fp = fopen(lpszFilename, TEXT("r"));
if( fp == NULL )
return FALSE;
if( m_lpParser )
{
XML_ParserFree( m_lpParser );
}
m_lpParser = XML_ParserCreate(NULL);
if( !m_lpParser )
return FALSE;
BOOL bSuccess = TRUE;
int done;
int depth = 0;
XML_SetUserData(m_lpParser, this);
XML_SetElementHandler(m_lpParser, PXmlReader_OnStartElement, PXmlReader_OnEndElement);
XML_SetCharacterDataHandler(m_lpParser, PXmlReader_OnCharacter);
m_lpCurrentAction = NULL;
do
{
size_t len = fread(m_szReadBuffer, 1, sizeof(m_szReadBuffer), fp);
done = len < sizeof(m_szReadBuffer);
if( !XML_Parse(m_lpParser, m_szReadBuffer, len, done) )
{
/*printf("ERROR, %s at line %d\n",
XML_ErrorString(XML_GetErrorCode(m_lpParser)),
XML_GetCurrentLineNumber(m_lpParser));*/
bSuccess = FALSE;
break;
}
}
while( !done );
fclose(fp);
XML_ParserFree( m_lpParser );
m_lpParser = NULL;
return bSuccess;
#endif
}