some further invalidation details:
[ardour.git] / libs / pbd / locale_guard.cc
1 /*
2     Copyright (C) 2012 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <stdlib.h>
21 #include <string.h>
22 #include <assert.h>
23 #include <locale.h>
24
25 #include "pbd/compose.h"
26 #include "pbd/debug.h"
27 #include "pbd/error.h"
28 #include "pbd/locale_guard.h"
29
30 using namespace PBD;
31
32 /* Neither C nor C++ pick up a user's preferred locale choice without the
33  * application actively taking steps to make this happen.
34  *
35  * For C: setlocale (LC_ALL, "");
36  * For C++ (assuming that the C version was called):
37  *      std::locale::global (std::locale (setlocale (LC_ALL, 0)));
38  *
39  * The application needs to make these calls, probably in main().
40  *
41  * Setting the C++ locale will change the C locale, but not the other way 'round.
42  * and some plugin may change either behind our back.
43  */
44
45 LocaleGuard::LocaleGuard ()
46         : old_c_locale (0)
47 {
48         /* A LocaleGuard object ensures that the
49          * LC_NUMERIC/std::locale::numeric aspect of the C and C++ locales are
50          * set to "C" during its lifetime, so that printf/iostreams use a
51          * portable format for numeric output (i.e. 1234.5 is always 1234.5 and
52          * not sometimes 1234,5, as it would be in fr or de locales)
53          */
54
55         char const * const current_c_locale = setlocale (LC_NUMERIC, 0);
56
57         if (strcmp ("C", current_c_locale) != 0) {
58
59                 old_c_locale = strdup (current_c_locale);
60
61                 try {
62                         /* set the C++ global/default locale to whatever we are using
63                          * now, but with "C" numeric handling.
64                          *
65                          * this also sets the C locale, so no additional call to setlocale() is required.
66                          */
67
68                         std::locale::global (std::locale (old_cpp_locale, "C", std::locale::numeric));
69                         pre_cpp_locale = std::locale();
70                         DEBUG_TRACE (DEBUG::Locale, string_compose ("LG: change C & C++ locale from '%1' => %2\n", old_cpp_locale.name(), pre_cpp_locale.name()));
71
72                 } catch (...) {
73                         /* Apple in particular have historically done a
74                          * terrible job supporting setlocale and even more so
75                          * with the C++ API. Using any locale other than "C" or
76                          * "POSIX" will fail, and in the case of the C++ API, 
77                          * will throw an exception. In that case, just try to
78                          * use setlocale() to reset *only* the numeric aspect
79                          * of the current locale settings back to "C", which is
80                          * likely to work everywhere.
81                          */
82
83                         setlocale (LC_NUMERIC, "C");
84                         pre_cpp_locale = std::locale();
85                         DEBUG_TRACE (DEBUG::Locale, string_compose ("LG: C++ locale API failed, change just C locale from '%1' => 'C' (C++ locale is %2)\n", old_c_locale, pre_cpp_locale.name()));
86                 }
87
88         }
89 }
90
91 LocaleGuard::~LocaleGuard ()
92 {
93         char const * current_c_locale = setlocale (LC_NUMERIC, 0);
94         std::locale current_cpp_locale;
95
96         if (current_cpp_locale != pre_cpp_locale) {
97
98                 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 ? %2",
99                                               old_cpp_locale.name(), current_cpp_locale.name(),
100                                               (std::use_facet<std::numpunct<char> >(std::locale()).decimal_point() == '.'))
101                            << endmsg;
102
103                 try {
104                         /* this resets C & C++ locales */
105                         std::locale::global (old_cpp_locale);
106                         DEBUG_TRACE (DEBUG::Locale, string_compose ("LG: restore C & C++ locale: '%1'\n", std::locale().name()));
107                 } catch (...) {
108                         /* see comments in the constructor regarding the
109                          * exception.
110                          *
111                          * This should restore restore numeric handling back to
112                          * the default (which may reflect user
113                          * preferences). This probably can't fail, because
114                          * old_c_locale was already in use during the
115                          * constructor for this object.
116                          *
117                          * Still ... Apple ... locale support ... just sayin' ....
118                          */
119                         setlocale (LC_NUMERIC, old_c_locale);
120                         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()));
121                 }
122
123         } else if (old_c_locale && (strcmp (current_c_locale, old_c_locale) != 0)) {
124
125                 /* reset only the C locale */
126                 setlocale (LC_NUMERIC, old_c_locale);
127                 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()));
128         }
129
130         free (const_cast<char*> (old_c_locale));
131 }