f848cf33f8b12f3abd1270712937f551cbb5ae50
[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 (const char*)
38         : old_c (0)
39 {
40         init ();
41 }
42
43 LocaleGuard::LocaleGuard ()
44         : old_c (0)
45 {
46         init ();
47 }
48
49 void
50 LocaleGuard::init ()
51 {
52         char* actual = setlocale (LC_NUMERIC, NULL);
53         if (strcmp ("C", actual)) {
54                 /* purpose of LocaleGuard is to make sure we're using "C" for
55                    the numeric locale during its lifetime, so make it so.
56                 */
57                 old_c = strdup (actual);
58                 /* this changes both C++ and C locale */
59                 std::locale::global (std::locale (std::locale::classic(), "C", std::locale::numeric));
60         }
61         if (old_cpp != std::locale::classic ()) {
62                 PBD::error << "LocaleGuard: initial C++ locale is not 'C'. Expect non-portable session files.\n";
63         }
64 }
65
66 LocaleGuard::~LocaleGuard ()
67 {
68         char* actual = setlocale (LC_NUMERIC, NULL);
69         std::locale current;
70
71         if (current != old_cpp) {
72                 /* the C++ locale should always be "C", that's the default
73                  * at application start, and ardour never changes it to
74                  * anything but "C".
75                  *
76                  * if it's not: some plugin meddled with it.
77                  */
78                 if (old_cpp != std::locale::classic ()) {
79                         PBD::error << "LocaleGuard: someone (a plugin) changed the C++ locale, expect non-portable session files.\n";
80                 }
81                 std::locale::global (old_cpp);
82         }
83         if (old_c && strcmp (old_c, actual)) {
84                 setlocale (LC_NUMERIC, old_c);
85         }
86         free (old_c);
87 }