From 6e4a80da1804bb31168bce7faacb729fa42ceb10 Mon Sep 17 00:00:00 2001 From: donho Date: Fri, 26 Dec 2008 18:45:10 +0000 Subject: [PATCH] [BUG_FIXED] Fix nativeLang loading problem if Notepad++ location path contains Unicode characters. git-svn-id: svn://svn.tuxfamily.org/svnroot/notepadplus/repository@376 f5eea248-9336-0410-98b8-ebc06183d4e3 --- PowerEditor/src/Parameters.cpp | 21 +------ PowerEditor/src/TinyXml/tinyXmlA/tinyxmlA.cpp | 58 +++++++++++++++++++ PowerEditor/src/TinyXml/tinyXmlA/tinyxmlA.h | 4 ++ 3 files changed, 64 insertions(+), 19 deletions(-) diff --git a/PowerEditor/src/Parameters.cpp b/PowerEditor/src/Parameters.cpp index 850f023a..fa49438f 100644 --- a/PowerEditor/src/Parameters.cpp +++ b/PowerEditor/src/Parameters.cpp @@ -687,26 +687,9 @@ bool NppParameters::load() PathAppend(nativeLangPath, TEXT("nativeLang.xml")); } -/* - _pXmlNativeLangDoc = new TiXmlDocument(nativeLangPath); - loadOkay = _pXmlNativeLangDoc->LoadFile(); - if (!loadOkay) - { - delete _pXmlNativeLangDoc; - _pXmlNativeLangDoc = NULL; - isAllLaoded = false; - } -*/ + _pXmlNativeLangDocA = new TiXmlDocumentA(); -#ifdef UNICODE - WcharMbcsConvertor *wmc = WcharMbcsConvertor::getInstance(); - const char * nativeLangPathA = wmc->wchar2char(nativeLangPath, CP_ANSI_LATIN_1); - _pXmlNativeLangDocA = new TiXmlDocumentA(nativeLangPathA); -#else - _pXmlNativeLangDocA = new TiXmlDocumentA(nativeLangPath); -#endif - - loadOkay = _pXmlNativeLangDocA->LoadFile(); + loadOkay = _pXmlNativeLangDocA->LoadUnicodeFilePath(nativeLangPath); if (!loadOkay) { delete _pXmlNativeLangDocA; diff --git a/PowerEditor/src/TinyXml/tinyXmlA/tinyxmlA.cpp b/PowerEditor/src/TinyXml/tinyXmlA/tinyxmlA.cpp index 7a7bde01..3f9ad06a 100644 --- a/PowerEditor/src/TinyXml/tinyXmlA/tinyxmlA.cpp +++ b/PowerEditor/src/TinyXml/tinyXmlA/tinyxmlA.cpp @@ -755,6 +755,64 @@ bool TiXmlDocumentA::LoadFile( const char* filename ) return false; } +bool TiXmlDocumentA::LoadUnicodeFilePath( const TCHAR* filename ) +{ + + // Delete the existing data: + Clear(); + location.Clear(); + + // There was a really terrifying little bug here. The code: + // value = filename + // in the STL case, cause the assignment method of the std::generic_string to + // be called. What is strange, is that the std::generic_string had the same + // address as it's c_str() method, and so bad things happen. Looks + // like a bug in the Microsoft STL implementation. + // See STL_STRING_BUG above. + // Fixed with the StringToBuffer class. + + FILE* file = generic_fopen(filename, TEXT("r")); + + if ( file ) + { + // Get the file size, so we can pre-allocate the generic_string. HUGE speed impact. + long length = 0; + fseek( file, 0, SEEK_END ); + length = ftell( file ); + fseek( file, 0, SEEK_SET ); + + // Strange case, but good to handle up front. + if ( length == 0 ) + { + fclose( file ); + return false; + } + + // If we have a file, assume it is all one big XML file, and read it in. + // The document parser may decide the document ends sooner than the entire file, however. + TIXMLA_STRING data; + data.reserve( length ); + + const int BUF_SIZE = 2048; + char buf[BUF_SIZE]; + + while( fgets( buf, BUF_SIZE, file ) ) + { + data += buf; + } + fclose( file ); + + Parse( data.c_str(), 0 ); + + if ( Error() ) + return false; + else + return true; + } + SetError( TIXMLA_ERROR_OPENING_FILE, 0, 0 ); + return false; +} + bool TiXmlDocumentA::SaveFile( const char * filename ) const { // The old c stuff lives on... diff --git a/PowerEditor/src/TinyXml/tinyXmlA/tinyxmlA.h b/PowerEditor/src/TinyXml/tinyXmlA/tinyxmlA.h index 572f7fd3..50cd395a 100644 --- a/PowerEditor/src/TinyXml/tinyXmlA/tinyxmlA.h +++ b/PowerEditor/src/TinyXml/tinyXmlA/tinyxmlA.h @@ -36,6 +36,8 @@ distribution. #include #include #include +#include +#include "Common.h" // Help out windows: #if defined( _DEBUG ) && !defined( DEBUG ) @@ -1012,6 +1014,8 @@ public: /// Save a file using the given filename. Returns true if successful. bool SaveFile( const char * filename ) const; + bool LoadUnicodeFilePath(const TCHAR* filename); + #ifdef TIXMLA_USE_STL bool LoadFile( const std::string& filename ) ///< STL std::string version. {