Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
numpunct.cpp
Go to the documentation of this file.
1
29#include <iostream> // for cerr, cout
30#include <locale> // for locale, numpunct, use_facet
31#include <stdexcept> // for runtime_error
32
33#include <examples.h>
34
35
36static std::locale
37make_german_locale ()
38{
39 static const char* const
40 locale_names [] = {
41 "de_DE.ISO8859-1", // AIX, Solaris, Tru64
42 "de_DE.iso88591", // HP-UX, Linux
43 "de_DE.88591",
44 "De_DE.88591", // Reliant
45 "de_DE",
46 "de", // Linux, Solaris
47 "German",
48 "german", // Linux
49 "deutsch", // Linux
50 "german_germany.1252", // Windows
51 0 // (sentinel)
52 };
53
54 std::locale german;
55
56 // iterate over the know locale names above until a valid one
57 // is found (i.e., one that doesn't cause locale to throw)
58 for (const char* const *names = locale_names; ; ) {
59 try {
60 german = std::locale (names [0]);
61 break;
62 }
63 catch (std::runtime_error&) {
64 // continue trying until the next name is null
65 if (0 == *++names)
66 throw;
67 }
68 catch (...) {
69 throw;
70 }
71 }
72
73 return german;
74}
75
76
77int main ()
78{
79 typedef std::numpunct<char> Numpunct;
80
81 try {
82 // try to contruct a german locale
83 const std::locale german (make_german_locale ());
84
85 // obtain a numpunct facet for the german locale
86 const Numpunct &np = std::use_facet<Numpunct>(german);
87
88 std::cout << "German locale"
89 << "\nDecimal point = " << np.decimal_point ()
90 << "\nThousands separator = " << np.thousands_sep ()
91 << "\nTrue name = " << np.truename ()
92 << "\nFalse name = " << np.falsename()
93 << '\n';
94 }
95 catch (std::runtime_error& e) {
96 // a runtime_error will be thrown if the locale cannot be constructed
97 std::cerr << "Caught runtime_error:\n";
98 std::cerr << e.what () << '\n';
99 }
100 catch (...) {
101 std::cerr << "Caught an unknown exception\n";
102 }
103 return 0;
104}
#define e
Definition 17.names.cpp:118
std::numpunct< char > Numpunct
int main()
Definition numpunct.cpp:77