Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
codecvt.cpp
Go to the documentation of this file.
1
29#include <iostream>
30
31#include <codecvte.h>
32#include <examples.h>
33
34int main ()
35{
36 // not used, must be zero-initialized and supplied to facet
37 std::mbstate_t state = std::mbstate_t ();
38
39 // three strings to use as buffers
40 std::string ins ("\xfc \xcc \xcd \x61 \xe1 \xd9 \xc6 \xe6 \xf5");
41 std::string ins2 (ins.size (), '.');
42 std::string outs (ins.size () / ex_codecvt ().encoding (), '.');
43
44 // Print initial contents of buffers
45 std::cout << "Before:\n"
46 << ins << '\n'
47 << ins2 << '\n'
48 << outs << '\n' << '\n';
49
50 // Create a user defined codecvt fact
51 // This facet converts from ISO Latin Alphabet No. 1 (ISO 8859-1)
52 // to U.S. ASCII code page 437.
53
54 // Replace the default codecvt<char, char, mbstate_t>.
55 std::locale loc (std::cout.getloc (), new ex_codecvt);
56
57 // Retrieve the facet from the locale.
58 typedef std::codecvt<char, char, std::mbstate_t> CodeCvt;
59 const CodeCvt& cdcvt = std::use_facet<CodeCvt>(loc);
60
61 // unused, must be provided to codecvt<>::in/out
62 const char *const_out_next = 0;
63 const char *const_in_next = 0;
64 char *in_next = 0;
65 char *out_next = 0;
66
67 // convert the buffer
68 cdcvt.in (state, ins.c_str (), ins.c_str () + ins.length (), const_in_next,
69 &outs [0], &outs [0] + outs.length (), out_next);
70
71 std::cout << "After in:\n"
72 << ins << '\n'
73 << ins2 << '\n'
74 << outs << "\n\n";
75
76 // zero-initialize (unused) state object
77 state = std::mbstate_t ();
78
79 // Finally, convert back to the original codeset
80 cdcvt.out (state, outs.c_str (), outs.c_str () + outs.length (),
81 const_out_next, &ins [0], &ins [0] + ins.length (), in_next);
82
83 std::cout << "After out:\n"
84 << ins << '\n'
85 << ins2 << '\n'
86 << outs << '\n';
87
88 return 0;
89}
#define loc
Definition 17.names.cpp:492
int main()
Definition codecvt.cpp:34