2 Copyright (C) 2012 Paul Davis
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.
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.
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.
25 #include "pbd/locale_guard.h"
26 #include "pbd/error.h"
30 /* The initial C++ locale is "C" regardless of the user's preferred locale.
31 * The C locale from setlocale() matches the user's preferred locale
33 * Setting the C++ locale will change the C locale, but not the other way 'round.
34 * and some plugin may change either behind our back.
37 LocaleGuard::LocaleGuard ()
40 char* actual = setlocale (LC_NUMERIC, NULL);
41 if (strcmp ("C", actual)) {
42 /* purpose of LocaleGuard is to make sure we're using "C" for
43 the numeric locale during its lifetime, so make it so.
45 old_c = strdup (actual);
46 /* this changes both C++ and C locale */
47 std::locale::global (std::locale (std::locale::classic(), "C", std::locale::numeric));
49 if (old_cpp != std::locale::classic ()) {
50 PBD::error << "LocaleGuard: initial C++ locale is not 'C'. Expect non-portable session files.\n";
54 LocaleGuard::~LocaleGuard ()
56 char* actual = setlocale (LC_NUMERIC, NULL);
59 if (current != old_cpp) {
60 /* the C++ locale should always be "C", that's the default
61 * at application start, and ardour never changes it to
64 * if it's not: some plugin meddled with it.
66 if (old_cpp != std::locale::classic ()) {
67 PBD::error << "LocaleGuard: someone (a plugin) changed the C++ locale, expect non-portable session files.\n";
69 std::locale::global (old_cpp);
71 if (old_c && strcmp (old_c, actual)) {
72 setlocale (LC_NUMERIC, old_c);