2007-09-07 23:17:30 +02:00
|
|
|
#include <string>
|
|
|
|
#include <stdexcept>
|
|
|
|
#include "xmlwordcount.h"
|
|
|
|
#include "getword.h"
|
|
|
|
|
2007-09-08 00:25:30 +02:00
|
|
|
XmlWordCount::XmlWordCount() : wcd ( new WordCountData() )
|
2007-09-07 23:17:30 +02:00
|
|
|
{
|
2007-09-08 00:25:30 +02:00
|
|
|
wcd->wordCount = 0;
|
|
|
|
XML_SetUserData ( p, wcd.get() );
|
|
|
|
XML_SetCharacterDataHandler ( p, characterdata );
|
|
|
|
XML_SetEndElementHandler ( p, end );
|
2007-09-07 23:17:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
XmlWordCount::~XmlWordCount()
|
2007-09-08 00:25:30 +02:00
|
|
|
{}
|
2007-09-07 23:17:30 +02:00
|
|
|
|
|
|
|
int XmlWordCount::getWordCount()
|
|
|
|
{
|
2007-09-08 00:25:30 +02:00
|
|
|
return wcd->wordCount;
|
2007-09-07 23:17:30 +02:00
|
|
|
}
|
|
|
|
|
2007-09-08 00:25:30 +02:00
|
|
|
void XMLCALL XmlWordCount::characterdata (
|
|
|
|
void *data,
|
|
|
|
const XML_Char *s,
|
|
|
|
int len )
|
2007-09-07 23:17:30 +02:00
|
|
|
{
|
2007-09-08 00:25:30 +02:00
|
|
|
WordCountData *wcd;
|
|
|
|
wcd = ( WordCountData * ) data;
|
|
|
|
wcd->buffer.append ( s, len );
|
2007-09-07 23:17:30 +02:00
|
|
|
}
|
|
|
|
|
2007-09-08 00:25:30 +02:00
|
|
|
void XMLCALL XmlWordCount::end ( void *data, const XML_Char *el )
|
2007-09-07 23:17:30 +02:00
|
|
|
{
|
2007-09-08 00:25:30 +02:00
|
|
|
WordCountData *wcd;
|
|
|
|
wcd = ( WordCountData * ) data;
|
2007-09-07 23:17:30 +02:00
|
|
|
|
2007-09-08 00:25:30 +02:00
|
|
|
char *s = ( char * ) wcd->buffer.c_str();
|
|
|
|
size_t len, myCount;
|
|
|
|
myCount = 0;
|
|
|
|
while ( GetWord::run ( &s, &len ) )
|
|
|
|
++myCount;
|
|
|
|
wcd->wordCount += myCount;
|
|
|
|
wcd->buffer = "";
|
2007-09-07 23:17:30 +02:00
|
|
|
}
|