2013-01-16 13:15:38 -05:00
|
|
|
/*
|
2015-10-04 14:51:05 -04:00
|
|
|
Copyright (C) 2012 Paul Davis
|
2013-01-16 13:15:38 -05:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2009-10-28 17:36:40 -04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2016-05-05 14:02:23 -04:00
|
|
|
#include <assert.h>
|
2009-10-28 17:36:40 -04:00
|
|
|
#include <locale.h>
|
|
|
|
|
2016-05-07 06:15:12 -04:00
|
|
|
#include "pbd/compose.h"
|
|
|
|
#include "pbd/debug.h"
|
2016-05-05 14:29:28 -04:00
|
|
|
#include "pbd/error.h"
|
2016-05-07 06:15:12 -04:00
|
|
|
#include "pbd/locale_guard.h"
|
2009-10-28 17:36:40 -04:00
|
|
|
|
|
|
|
using namespace PBD;
|
|
|
|
|
2016-05-05 14:21:02 -04:00
|
|
|
/* The initial C++ locale is "C" regardless of the user's preferred locale.
|
2016-05-05 14:29:28 -04:00
|
|
|
* The C locale from setlocale() matches the user's preferred locale
|
2016-05-05 14:02:23 -04:00
|
|
|
*
|
|
|
|
* Setting the C++ locale will change the C locale, but not the other way 'round.
|
|
|
|
* and some plugin may change either behind our back.
|
|
|
|
*/
|
2015-01-18 10:43:05 -05:00
|
|
|
|
2016-05-05 14:02:23 -04:00
|
|
|
LocaleGuard::LocaleGuard ()
|
|
|
|
: old_c (0)
|
|
|
|
{
|
|
|
|
char* actual = setlocale (LC_NUMERIC, NULL);
|
|
|
|
if (strcmp ("C", actual)) {
|
2016-05-07 06:15:12 -04:00
|
|
|
DEBUG_TRACE (DEBUG::Locale, string_compose ("LG: change locale from '%1' to 'C'\n", actual));
|
2016-05-05 14:21:02 -04:00
|
|
|
/* purpose of LocaleGuard is to make sure we're using "C" for
|
|
|
|
the numeric locale during its lifetime, so make it so.
|
|
|
|
*/
|
2016-05-05 14:02:23 -04:00
|
|
|
old_c = strdup (actual);
|
2016-05-07 06:15:12 -04:00
|
|
|
/* this changes both C++ and C locale */
|
2016-05-05 14:02:23 -04:00
|
|
|
std::locale::global (std::locale (std::locale::classic(), "C", std::locale::numeric));
|
2015-01-18 10:43:05 -05:00
|
|
|
}
|
2016-05-05 14:29:28 -04:00
|
|
|
if (old_cpp != std::locale::classic ()) {
|
|
|
|
PBD::error << "LocaleGuard: initial C++ locale is not 'C'. Expect non-portable session files.\n";
|
2016-05-07 06:15:12 -04:00
|
|
|
DEBUG_TRACE (DEBUG::Locale, "LG: initial C++ locale is not 'C'. Expect non-portable session files.\n");
|
2016-05-05 14:29:28 -04:00
|
|
|
}
|
2009-10-28 17:36:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
LocaleGuard::~LocaleGuard ()
|
|
|
|
{
|
2016-05-05 14:02:23 -04:00
|
|
|
char* actual = setlocale (LC_NUMERIC, NULL);
|
|
|
|
std::locale current;
|
2010-03-30 11:18:43 -04:00
|
|
|
|
2016-05-05 14:02:23 -04:00
|
|
|
if (current != old_cpp) {
|
|
|
|
/* the C++ locale should always be "C", that's the default
|
2016-05-05 14:21:55 -04:00
|
|
|
* at application start, and ardour never changes it to
|
|
|
|
* anything but "C".
|
|
|
|
*
|
2016-05-05 14:02:23 -04:00
|
|
|
* if it's not: some plugin meddled with it.
|
|
|
|
*/
|
2016-05-05 14:29:28 -04:00
|
|
|
if (old_cpp != std::locale::classic ()) {
|
|
|
|
PBD::error << "LocaleGuard: someone (a plugin) changed the C++ locale, expect non-portable session files.\n";
|
2016-05-07 06:15:12 -04:00
|
|
|
DEBUG_TRACE (DEBUG::Locale, "LG: someone (a plugin) changed the C++ locale, expect non-portable session files.\n");
|
2016-05-05 14:29:28 -04:00
|
|
|
}
|
2016-05-05 14:02:23 -04:00
|
|
|
std::locale::global (old_cpp);
|
2016-05-07 06:15:12 -04:00
|
|
|
DEBUG_TRACE (DEBUG::Locale, string_compose ("LG: restore C++ locale: '%1'\n", old_cpp.name ()));
|
2015-01-18 10:43:05 -05:00
|
|
|
}
|
2016-05-05 14:02:23 -04:00
|
|
|
if (old_c && strcmp (old_c, actual)) {
|
|
|
|
setlocale (LC_NUMERIC, old_c);
|
2016-05-07 06:15:12 -04:00
|
|
|
DEBUG_TRACE (DEBUG::Locale, string_compose ("LG: restore C locale: '%1'\n", old_c));
|
2016-05-05 14:02:23 -04:00
|
|
|
}
|
|
|
|
free (old_c);
|
2009-10-28 17:36:40 -04:00
|
|
|
}
|