Fixed an encoding problem

This commit is contained in:
Zane U. Ji 2014-04-13 14:48:02 +08:00
parent f39ac69e7b
commit 17f79c10c0
5 changed files with 14 additions and 13 deletions

View File

@ -23,9 +23,9 @@
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
WrapExpat::WrapExpat ( bool nameSpaceAware ) WrapExpat::WrapExpat ( bool nameSpaceAware, const char *encoding )
{ {
p = ( nameSpaceAware ) ? XML_ParserCreateNS ( NULL, ( XML_Char ) ':' ) : XML_ParserCreate ( NULL ); p = ( nameSpaceAware ) ? XML_ParserCreateNS ( encoding, ( XML_Char ) ':' ) : XML_ParserCreate ( encoding );
if ( p == 0 ) if ( p == 0 )
throw runtime_error ( "WrapExpat::WrapExpat" ); throw runtime_error ( "WrapExpat::WrapExpat" );
} }

View File

@ -32,7 +32,7 @@ using namespace std;
class WrapExpat class WrapExpat
{ {
public: public:
WrapExpat ( bool nameSpaceAware = false ); WrapExpat ( bool nameSpaceAware = false, const char *encoding = NULL );
virtual ~WrapExpat(); virtual ~WrapExpat();
bool parse ( const string &buffer, bool isFinal = true ); bool parse ( const string &buffer, bool isFinal = true );
bool parse ( const char *buffer, size_t size, bool isFinal = true ); bool parse ( const char *buffer, size_t size, bool isFinal = true );

View File

@ -5921,22 +5921,21 @@ void MyFrame::OnWordCount ( wxCommandEvent& event )
XmlDoc *doc; XmlDoc *doc;
if ( ( doc = getActiveDocument() ) == NULL ) if ( ( doc = getActiveDocument() ) == NULL )
return; return;
wxString wideBuffer;
std::string buffer;
wideBuffer = doc->GetText();
buffer = wideBuffer.mb_str ( wxConvUTF8 );
auto_ptr<XmlWordCount> xwc ( new XmlWordCount() ); std::string rawBufferUtf8;
getRawText ( doc, rawBufferUtf8 );
XmlWordCount xwc ( "UTF-8" );
wxString msg; wxString msg;
if ( !xwc->parse ( buffer.c_str() ) ) if ( !xwc.parse ( rawBufferUtf8 ) )
{ {
statusProgress ( wxEmptyString ); statusProgress ( wxEmptyString );
msg.Printf ( _ ( "Cannot count words: %s" ), xwc->getLastError().c_str() ); msg.Printf ( _ ( "Cannot count words: %s" ), xwc.getLastError().c_str() );
messagePane ( msg, CONST_STOP ); messagePane ( msg, CONST_STOP );
return; return;
} }
int count = xwc->getWordCount(); int count = xwc.getWordCount();
msg.Printf ( msg.Printf (
wxPLURAL ( "%s contains %i word", "%s contains %i words", count ), wxPLURAL ( "%s contains %i word", "%s contains %i words", count ),

View File

@ -22,7 +22,9 @@
#include "xmlwordcount.h" #include "xmlwordcount.h"
#include "getword.h" #include "getword.h"
XmlWordCount::XmlWordCount() : wcd ( new WordCountData() ) XmlWordCount::XmlWordCount ( const char *encoding )
: WrapExpat ( false, encoding )
, wcd ( new WordCountData() )
{ {
wcd->wordCount = 0; wcd->wordCount = 0;
XML_SetUserData ( p, wcd.get() ); XML_SetUserData ( p, wcd.get() );

View File

@ -33,7 +33,7 @@ struct WordCountData : public ParserData
class XmlWordCount : public WrapExpat class XmlWordCount : public WrapExpat
{ {
public: public:
XmlWordCount(); XmlWordCount ( const char *encoding = NULL );
virtual ~XmlWordCount(); virtual ~XmlWordCount();
int getWordCount(); int getWordCount();