Optimize automation-event process splitting
[ardour.git] / libs / pbd / locale_guard.cc
index 62318c6445bbc0024f38282b3aeebec07c51d4d7..dc2657c239896ae6423fc87b42503827b0dcfa0e 100644 (file)
 #include <assert.h>
 #include <locale.h>
 
+#include "pbd/compose.h"
+#include "pbd/debug.h"
+#include "pbd/error.h"
 #include "pbd/locale_guard.h"
 
 using namespace PBD;
 
-/* The initial C++ locale is "C" regardless of the user's preferred locale.
- * and affects std::sprintf() et al from <cstdio>
+/* Neither C nor C++ pick up a user's preferred locale choice without the
+ * application actively taking steps to make this happen.
  *
- * the C locale from setlocale() matches the user's preferred locale
- * and effects ::sprintf() et al from <stdio.h>
+ * For C: setlocale (LC_ALL, "");
+ * For C++ (assuming that the C version was called):
+ *      std::locale::global (std::locale (setlocale (LC_ALL, 0)));
+ *
+ * The application needs to make these calls, probably in main().
  *
  * Setting the C++ locale will change the C locale, but not the other way 'round.
  * and some plugin may change either behind our back.
  */
 
-LocaleGuard::LocaleGuard (const char*)
-       : old_c (0)
-{
-       init ();
-}
-
 LocaleGuard::LocaleGuard ()
-       : old_c (0)
+       : old_c_locale (0)
 {
-       init ();
-}
+       /* A LocaleGuard object ensures that the
+        * LC_NUMERIC/std::locale::numeric aspect of the C and C++ locales are
+        * set to "C" during its lifetime, so that printf/iostreams use a
+        * portable format for numeric output (i.e. 1234.5 is always 1234.5 and
+        * not sometimes 1234,5, as it would be in fr or de locales)
+        */
 
-void
-LocaleGuard::init ()
-{
-       char* actual = setlocale (LC_NUMERIC, NULL);
-       if (strcmp ("C", actual)) {
-               /* purpose of LocaleGuard is to make sure we're using "C" for
-                  the numeric locale during its lifetime, so make it so.
-               */
-               old_c = strdup (actual);
-               /* this changes both C++ and C locale */
-               std::locale::global (std::locale (std::locale::classic(), "C", std::locale::numeric));
+       char const * const current_c_locale = setlocale (LC_NUMERIC, 0);
+
+       if (strcmp ("C", current_c_locale) != 0) {
+               old_c_locale = strdup (current_c_locale);
+               setlocale (LC_NUMERIC, "C");
+               pre_cpp_locale = std::locale();
+               DEBUG_TRACE (DEBUG::Locale, string_compose ("LG: change C locale from '%1' => 'C' (C++ locale is %2)\n", old_c_locale, pre_cpp_locale.name()));
        }
-       assert (old_cpp == std::locale::classic ());
 }
 
 LocaleGuard::~LocaleGuard ()
 {
-       char* actual = setlocale (LC_NUMERIC, NULL);
-       std::locale current;
-
-       if (current != old_cpp) {
-               /* the C++ locale should always be "C", that's the default
-                * at application start, and ardour never changes it to
-                * anything but "C".
-                *
-                * if it's not: some plugin meddled with it.
-                */
-               assert (old_cpp == std::locale::classic ());
-               std::locale::global (old_cpp);
+       char const * current_c_locale = setlocale (LC_NUMERIC, 0);
+       std::locale current_cpp_locale;
+
+       if (current_cpp_locale != pre_cpp_locale) {
+
+               PBD::warning << string_compose ("LocaleGuard: someone (a plugin) changed the C++ locale from\n\t%1\nto\n\t%2\n, expect non-portable session files. Decimal OK ? %3",
+                                             old_cpp_locale.name(), current_cpp_locale.name(),
+                                             (std::use_facet<std::numpunct<char> >(std::locale()).decimal_point() == '.'))
+                          << endmsg;
+
+               try {
+                       /* this resets C & C++ locales */
+                       std::locale::global (old_cpp_locale);
+                       DEBUG_TRACE (DEBUG::Locale, string_compose ("LG: restore C & C++ locale: '%1'\n", std::locale().name()));
+               } catch (...) {
+                       /* see comments in the constructor regarding the
+                        * exception.
+                        *
+                        * This should restore restore numeric handling back to
+                        * the default (which may reflect user
+                        * preferences). This probably can't fail, because
+                        * old_c_locale was already in use during the
+                        * constructor for this object.
+                        *
+                        * Still ... Apple ... locale support ... just sayin' ....
+                        */
+                       setlocale (LC_NUMERIC, old_c_locale);
+                       DEBUG_TRACE (DEBUG::Locale, string_compose ("LG: C++ locale API failed, restore C locale from %1 to\n'%2'\n(C++ is '%3')\n", current_c_locale, old_c_locale, std::locale().name()));
+               }
+
        }
-       if (old_c && strcmp (old_c, actual)) {
-               setlocale (LC_NUMERIC, old_c);
+       if (old_c_locale && (strcmp (current_c_locale, old_c_locale) != 0)) {
+               /* reset only the C locale */
+               setlocale (LC_NUMERIC, old_c_locale);
+               DEBUG_TRACE (DEBUG::Locale, string_compose ("LG: restore C locale from %1 to\n'%2'\n(C++ is '%3')\n", current_c_locale, old_c_locale, std::locale().name()));
        }
-       free (old_c);
+
+       free (const_cast<char*> (old_c_locale));
 }