we always only use the "C" locale when saving.
[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/locale_guard.h"
26 #include "pbd/error.h"
27
28 using namespace PBD;
29
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
32  *
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.
35  */
36
37 LocaleGuard::LocaleGuard ()
38         : old_c (0)
39 {
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.
44                 */
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));
48         }
49         if (old_cpp != std::locale::classic ()) {
50                 PBD::error << "LocaleGuard: initial C++ locale is not 'C'. Expect non-portable session files.\n";
51         }
52 }
53
54 LocaleGuard::~LocaleGuard ()
55 {
56         char* actual = setlocale (LC_NUMERIC, NULL);
57         std::locale current;
58
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
62                  * anything but "C".
63                  *
64                  * if it's not: some plugin meddled with it.
65                  */
66                 if (old_cpp != std::locale::classic ()) {
67                         PBD::error << "LocaleGuard: someone (a plugin) changed the C++ locale, expect non-portable session files.\n";
68                 }
69                 std::locale::global (old_cpp);
70         }
71         if (old_c && strcmp (old_c, actual)) {
72                 setlocale (LC_NUMERIC, old_c);
73         }
74         free (old_c);
75 }