From ef892af6556724b6d04dfb1767dce132f9d97257 Mon Sep 17 00:00:00 2001 From: Vincent Richard Date: Thu, 16 Jan 2014 00:27:51 +0100 Subject: [PATCH] Do not make calls to setlocale() in a library. Use default user locale in tests and examples. --- examples/example1.cpp | 14 +++++++++++-- examples/example2.cpp | 16 ++++++++++++--- examples/example3.cpp | 14 +++++++++++-- examples/example4.cpp | 14 +++++++++++-- examples/example5.cpp | 14 +++++++++++-- examples/example6.cpp | 14 +++++++++++-- examples/example7.cpp | 5 ++--- src/vmime/platforms/posix/posixHandler.cpp | 6 +----- tests/parser/emailAddressTest.cpp | 24 ++++++++++++++++++++++ tests/parser/parameterTest.cpp | 24 ++++++++++++++++++++++ tests/parser/textTest.cpp | 24 ++++++++++++++++++++++ 11 files changed, 148 insertions(+), 21 deletions(-) diff --git a/examples/example1.cpp b/examples/example1.cpp index 9948d606..b80fe636 100644 --- a/examples/example1.cpp +++ b/examples/example1.cpp @@ -32,6 +32,8 @@ // #include +#include +#include #include "vmime/vmime.hpp" #include "vmime/platforms/posix/posixHandler.hpp" @@ -41,8 +43,16 @@ int main() { std::cout << std::endl; - // VMime initialization - vmime::platform::setHandler(); + // Set the global C and C++ locale to the user-configured locale. + // The locale should use UTF-8 encoding for these tests to run successfully. + try + { + std::locale::global(std::locale("")); + } + catch (std::exception &) + { + std::setlocale(LC_ALL, ""); + } try { diff --git a/examples/example2.cpp b/examples/example2.cpp index 56f01b03..67b8d844 100644 --- a/examples/example2.cpp +++ b/examples/example2.cpp @@ -32,6 +32,8 @@ // #include +#include +#include #include "vmime/vmime.hpp" #include "vmime/platforms/posix/posixHandler.hpp" @@ -41,8 +43,16 @@ int main() { std::cout << std::endl; - // VMime initialization - vmime::platform::setHandler(); + // Set the global C and C++ locale to the user-configured locale. + // The locale should use UTF-8 encoding for these tests to run successfully. + try + { + std::locale::global(std::locale("")); + } + catch (std::exception &) + { + std::setlocale(LC_ALL, ""); + } try { @@ -71,7 +81,7 @@ int main() // Adding an attachment vmime::shared_ptr a = vmime::make_shared ( - "./example2.cpp", // full path to file + __FILE__, // full path to file vmime::mediaType("application/octet-stream"), // content type vmime::text("My first attachment") // description ); diff --git a/examples/example3.cpp b/examples/example3.cpp index 56b3c7e7..a472e05c 100644 --- a/examples/example3.cpp +++ b/examples/example3.cpp @@ -32,6 +32,8 @@ // #include +#include +#include #include "vmime/vmime.hpp" #include "vmime/platforms/posix/posixHandler.hpp" @@ -41,8 +43,16 @@ int main() { std::cout << std::endl; - // VMime initialization - vmime::platform::setHandler(); + // Set the global C and C++ locale to the user-configured locale. + // The locale should use UTF-8 encoding for these tests to run successfully. + try + { + std::locale::global(std::locale("")); + } + catch (std::exception &) + { + std::setlocale(LC_ALL, ""); + } try { diff --git a/examples/example4.cpp b/examples/example4.cpp index 3dbfe548..1d077575 100644 --- a/examples/example4.cpp +++ b/examples/example4.cpp @@ -32,6 +32,8 @@ // #include +#include +#include #include "vmime/vmime.hpp" #include "vmime/platforms/posix/posixHandler.hpp" @@ -41,8 +43,16 @@ int main() { std::cout << std::endl; - // VMime initialization - vmime::platform::setHandler(); + // Set the global C and C++ locale to the user-configured locale. + // The locale should use UTF-8 encoding for these tests to run successfully. + try + { + std::locale::global(std::locale("")); + } + catch (std::exception &) + { + std::setlocale(LC_ALL, ""); + } try { diff --git a/examples/example5.cpp b/examples/example5.cpp index 159b503c..849ffa75 100644 --- a/examples/example5.cpp +++ b/examples/example5.cpp @@ -32,6 +32,8 @@ // #include +#include +#include #include "vmime/vmime.hpp" #include "vmime/platforms/posix/posixHandler.hpp" @@ -41,8 +43,16 @@ int main() { std::cout << std::endl; - // VMime initialization - vmime::platform::setHandler(); + // Set the global C and C++ locale to the user-configured locale. + // The locale should use UTF-8 encoding for these tests to run successfully. + try + { + std::locale::global(std::locale("")); + } + catch (std::exception &) + { + std::setlocale(LC_ALL, ""); + } try { diff --git a/examples/example6.cpp b/examples/example6.cpp index 30767c3e..54f1364b 100644 --- a/examples/example6.cpp +++ b/examples/example6.cpp @@ -25,6 +25,8 @@ #include #include #include +#include +#include #include "vmime/vmime.hpp" #include "vmime/platforms/posix/posixHandler.hpp" @@ -916,8 +918,16 @@ static bool menu() int main() { - // VMime initialization - vmime::platform::setHandler(); + // Set the global C and C++ locale to the user-configured locale. + // The locale should use UTF-8 encoding for these tests to run successfully. + try + { + std::locale::global(std::locale("")); + } + catch (std::exception &) + { + std::setlocale(LC_ALL, ""); + } for (bool quit = false ; !quit ; ) { diff --git a/examples/example7.cpp b/examples/example7.cpp index 241b4d33..e69f65f3 100644 --- a/examples/example7.cpp +++ b/examples/example7.cpp @@ -32,6 +32,8 @@ // #include +#include +#include #include "vmime/vmime.hpp" #include "vmime/platforms/posix/posixHandler.hpp" @@ -39,9 +41,6 @@ int main() { - // VMime initialization - vmime::platform::setHandler(); - // Enumerate encoders vmime::shared_ptr ef = vmime::utility::encoder::encoderFactory::getInstance(); diff --git a/src/vmime/platforms/posix/posixHandler.cpp b/src/vmime/platforms/posix/posixHandler.cpp index 7ab0341a..e0bfd27f 100644 --- a/src/vmime/platforms/posix/posixHandler.cpp +++ b/src/vmime/platforms/posix/posixHandler.cpp @@ -168,11 +168,7 @@ const vmime::charset posixHandler::getLocalCharset() const { const PLockHelper lock; - const char* prevLocale = ::setlocale(LC_ALL, ""); - vmime::charset ch(::nl_langinfo(CODESET)); - ::setlocale(LC_ALL, prevLocale); - - return (ch); + return vmime::charset(::nl_langinfo(CODESET)); } diff --git a/tests/parser/emailAddressTest.cpp b/tests/parser/emailAddressTest.cpp index 378cc065..4916aaa9 100644 --- a/tests/parser/emailAddressTest.cpp +++ b/tests/parser/emailAddressTest.cpp @@ -25,6 +25,9 @@ #include "vmime/platform.hpp" +#include +#include + VMIME_TEST_SUITE_BEGIN(emailAddressTest) @@ -41,6 +44,27 @@ VMIME_TEST_SUITE_BEGIN(emailAddressTest) VMIME_TEST_LIST_END + void setUp() + { + // Set the global C and C++ locale to the user-configured locale. + // The locale should use UTF-8 encoding for these tests to run successfully. + try + { + std::locale::global(std::locale("")); + } + catch (std::exception &) + { + std::setlocale(LC_ALL, ""); + } + } + + void tearDown() + { + // Restore default locale + std::locale::global(std::locale("C")); + } + + void testParseASCII() { vmime::emailAddress eml1("local@domain"); diff --git a/tests/parser/parameterTest.cpp b/tests/parser/parameterTest.cpp index 3b0edebf..4f6f8677 100644 --- a/tests/parser/parameterTest.cpp +++ b/tests/parser/parameterTest.cpp @@ -23,6 +23,9 @@ #include "tests/testUtils.hpp" +#include +#include + VMIME_TEST_SUITE_BEGIN(parameterTest) @@ -62,6 +65,27 @@ VMIME_TEST_SUITE_BEGIN(parameterTest) (p.getParameterAt(n)->getValue().getBuffer()) + void setUp() + { + // Set the global C and C++ locale to the user-configured locale. + // The locale should use UTF-8 encoding for these tests to run successfully. + try + { + std::locale::global(std::locale("")); + } + catch (std::exception &) + { + std::setlocale(LC_ALL, ""); + } + } + + void tearDown() + { + // Restore default locale + std::locale::global(std::locale("C")); + } + + void testParse() { // Simple parameter diff --git a/tests/parser/textTest.cpp b/tests/parser/textTest.cpp index 274687a5..f56f972d 100644 --- a/tests/parser/textTest.cpp +++ b/tests/parser/textTest.cpp @@ -23,6 +23,9 @@ #include "tests/testUtils.hpp" +#include +#include + VMIME_TEST_SUITE_BEGIN(textTest) @@ -78,6 +81,27 @@ VMIME_TEST_SUITE_BEGIN(textTest) } + void setUp() + { + // Set the global C and C++ locale to the user-configured locale. + // The locale should use UTF-8 encoding for these tests to run successfully. + try + { + std::locale::global(std::locale("")); + } + catch (std::exception &) + { + std::setlocale(LC_ALL, ""); + } + } + + void tearDown() + { + // Restore default locale + std::locale::global(std::locale("C")); + } + + void testConstructors() { vmime::text t1;