From 17f79c10c075225adab964188ee12e27882a3370 Mon Sep 17 00:00:00 2001 From: "Zane U. Ji" Date: Sun, 13 Apr 2014 14:48:02 +0800 Subject: [PATCH] Fixed an encoding problem --- src/wrapexpat.cpp | 4 ++-- src/wrapexpat.h | 2 +- src/xmlcopyeditor.cpp | 15 +++++++-------- src/xmlwordcount.cpp | 4 +++- src/xmlwordcount.h | 2 +- 5 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/wrapexpat.cpp b/src/wrapexpat.cpp index 06ad740..402c981 100644 --- a/src/wrapexpat.cpp +++ b/src/wrapexpat.cpp @@ -23,9 +23,9 @@ #include #include -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 ) throw runtime_error ( "WrapExpat::WrapExpat" ); } diff --git a/src/wrapexpat.h b/src/wrapexpat.h index 039b480..e71ef88 100644 --- a/src/wrapexpat.h +++ b/src/wrapexpat.h @@ -32,7 +32,7 @@ using namespace std; class WrapExpat { public: - WrapExpat ( bool nameSpaceAware = false ); + WrapExpat ( bool nameSpaceAware = false, const char *encoding = NULL ); virtual ~WrapExpat(); bool parse ( const string &buffer, bool isFinal = true ); bool parse ( const char *buffer, size_t size, bool isFinal = true ); diff --git a/src/xmlcopyeditor.cpp b/src/xmlcopyeditor.cpp index 8837117..e341919 100644 --- a/src/xmlcopyeditor.cpp +++ b/src/xmlcopyeditor.cpp @@ -5921,22 +5921,21 @@ void MyFrame::OnWordCount ( wxCommandEvent& event ) XmlDoc *doc; if ( ( doc = getActiveDocument() ) == NULL ) return; - wxString wideBuffer; - std::string buffer; - wideBuffer = doc->GetText(); - buffer = wideBuffer.mb_str ( wxConvUTF8 ); - auto_ptr xwc ( new XmlWordCount() ); + std::string rawBufferUtf8; + getRawText ( doc, rawBufferUtf8 ); + + XmlWordCount xwc ( "UTF-8" ); wxString msg; - if ( !xwc->parse ( buffer.c_str() ) ) + if ( !xwc.parse ( rawBufferUtf8 ) ) { 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 ); return; } - int count = xwc->getWordCount(); + int count = xwc.getWordCount(); msg.Printf ( wxPLURAL ( "%s contains %i word", "%s contains %i words", count ), diff --git a/src/xmlwordcount.cpp b/src/xmlwordcount.cpp index b1b0106..1ef88e7 100644 --- a/src/xmlwordcount.cpp +++ b/src/xmlwordcount.cpp @@ -22,7 +22,9 @@ #include "xmlwordcount.h" #include "getword.h" -XmlWordCount::XmlWordCount() : wcd ( new WordCountData() ) +XmlWordCount::XmlWordCount ( const char *encoding ) + : WrapExpat ( false, encoding ) + , wcd ( new WordCountData() ) { wcd->wordCount = 0; XML_SetUserData ( p, wcd.get() ); diff --git a/src/xmlwordcount.h b/src/xmlwordcount.h index 47b0ee9..c2a816c 100644 --- a/src/xmlwordcount.h +++ b/src/xmlwordcount.h @@ -33,7 +33,7 @@ struct WordCountData : public ParserData class XmlWordCount : public WrapExpat { public: - XmlWordCount(); + XmlWordCount ( const char *encoding = NULL ); virtual ~XmlWordCount(); int getWordCount();