Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
wctype.cpp
Go to the documentation of this file.
1
32#include <cwctype> // for iswxxx(), wint_t
33#include <stdexcept> // for runtime_error
34#include <iomanip> // for setfill, setw
35#include <ios> // for oct
36#include <iostream> // for cerr, cout
37#include <locale> // for isxxx(), locale
38
39
40static std::locale
41make_named_locale ()
42{
43 static const char* const
44 locale_names [] = {
45 "es_ES.ISO8859-1", // AIX, Solaris, Tru64
46 "es_ES.iso88591", // HP-UX, Linux
47 "es_ES.88591",
48 "De_DE.88591", // Reliant
49 "es_ES",
50 "es", // Linux, Solaris
51 "Spanish",
52 "spanish", // Linux
53 "espanol", // Linux
54 "spanish_spain.1252", // Windows
55 "POSIX", // POSIX systems
56 "C", // all C/C++ systems
57 0 // (sentinel)
58 };
59
60 std::locale german;
61
62 // iterate over the know locale names above until a valid one
63 // is found (i.e., one that doesn't cause locale to throw)
64 for (const char* const *names = locale_names; ; ) {
65 try {
66 german = std::locale (names [0]);
67 break;
68 }
69 catch (std::runtime_error&) {
70 // continue trying until the next name is null
71 if (0 == *++names)
72 throw;
73 }
74 catch (...) {
75 throw;
76 }
77 }
78
79 return german;
80}
81
82
83int main ()
84{
85 std::locale locale;
86
87 try {
88 // try to contruct a named locale
89 locale = make_named_locale ();
90 }
91 catch (std::runtime_error& e) {
92 // a runtime_error will be thrown if the locale cannot be constructed
93 std::cerr << "Caught runtime_error:\n";
94 std::cerr << e.what () << '\n';
95
96 return 1;
97 }
98 catch (...) {
99 std::cerr << "Caught an unknown exception\n";
100
101 return 2;
102 }
103
104 // set the global C/C++ locale to be used by the C classification
105 // functions (such as iswalpha())
106 std::locale::global (locale);
107
108 // imbue the named locale in wcout
109 std::wcout.imbue (locale);
110
111 std::wcout << "Wide character classification in "
112 << locale.name ().c_str () << " locale.\n";
113
114
115 // number of mismatched classifications between C and C++
116 // (expect zero for 100% conforming implementation)
117 int mismatch_count = 0;
118
119 // iterate over all characters in the extended ASCII range
120 // printing out the value of each along with its character
121 // class using the letters A, a, C, D, G, L, P, p, S, U,
122 // and X to denote each of the C/C++ classes corresponding
123 // to the C and C++ classification functions
124 for (std::wint_t wi = 0; wi < std::wint_t (256); ++wi) {
125
126 // convert the wint_t to wchar_t
127 const wchar_t wc (wi);
128
129 char cxx_class [11]; // C++ classification letters
130 char c_class [11]; // C classification letters
131
132 std::size_t i = 0;
133
134 // assign the appropriate letter for the C++ classification
135 cxx_class [i++] = (std::isalnum)(wc, locale) ? 'A' : '-';
136 cxx_class [i++] = (std::isalpha)(wc, locale) ? 'a' : '-';
137 cxx_class [i++] = (std::iscntrl)(wc, locale) ? 'C' : '-';
138 cxx_class [i++] = (std::isdigit)(wc, locale) ? 'D' : '-';
139 cxx_class [i++] = (std::isgraph)(wc, locale) ? 'G' : '-';
140 cxx_class [i++] = (std::islower)(wc, locale) ? 'L' : '-';
141 cxx_class [i++] = (std::isprint)(wc, locale) ? 'P' : '-';
142 cxx_class [i++] = (std::ispunct)(wc, locale) ? 'p' : '-';
143 cxx_class [i++] = (std::isspace)(wc, locale) ? 'S' : '-';
144 cxx_class [i++] = (std::isupper)(wc, locale) ? 'U' : '-';
145 cxx_class [i++] = (std::isxdigit)(wc, locale) ? 'X' : '-';
146
147 i = 0;
148
149 // assign the appropriate letter for the C classification
150 c_class [i++] = (std::iswalnum)(wc) ? 'A' : '-';
151 c_class [i++] = (std::iswalpha)(wc) ? 'a' : '-';
152 c_class [i++] = (std::iswcntrl)(wc) ? 'C' : '-';
153 c_class [i++] = (std::iswdigit)(wc) ? 'D' : '-';
154 c_class [i++] = (std::iswgraph)(wc) ? 'G' : '-';
155 c_class [i++] = (std::iswlower)(wc) ? 'L' : '-';
156 c_class [i++] = (std::iswprint)(wc) ? 'P' : '-';
157 c_class [i++] = (std::iswpunct)(wc) ? 'p' : '-';
158 c_class [i++] = (std::iswspace)(wc) ? 'S' : '-';
159 c_class [i++] = (std::iswupper)(wc) ? 'U' : '-';
160 c_class [i++] = (std::iswxdigit)(wc) ? 'X' : '-';
161
162 // print the value of the character and its glyph only when
163 // it's printable, otherwise print a space
164 std::wcout << " '\\" << std::setw (3)
165 << std::setfill (L'0') << std::oct << wi
166 << "' ('" << ((std::isprint)(wc, locale) ? wc : L' ')
167 << "') ";
168
169 bool mismatch = false;
170
171 // print out the letters denoting each character class
172 for (i = 0; i != sizeof cxx_class; ++i) {
173
174 if (cxx_class [i] == c_class [i])
175 std::wcout << cxx_class [i] << ' ';
176 else {
177 std::wcout << cxx_class [i] << ':' << c_class [i] << ' ';
178 mismatch = true;
179 }
180 }
181
182 if (mismatch) {
183 std::wcout << " *** C/C++ mismatch ***";
184
185 // increment the number of mimatched character classes
186 ++mismatch_count;
187 }
188
189 std::wcout << '\n';
190 }
191
192 return mismatch_count ? 1 : 0;
193}
#define i
Definition 17.names.cpp:126
#define e
Definition 17.names.cpp:118
int main()
Definition wctype.cpp:83