Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
money_get.cpp
Go to the documentation of this file.
1
29#include <locale> // for locale, money_get, use_facet
30#include <sstream> // for istringstream
31#include <iostream> // for cout, endl
32#include <iterator> // for istreambuf_iterator
33
34#include <examples.h>
35
36
37// hardcode the name of the English US locale for known systems
38#if defined (__FreeBSD__) || defined (__osf__)
39// FreeBSD and Tru64 UNIX
40const char en_US[] = "en_US.ISO8859-1";
41#elif defined (__hpux)
42// HP-UX
43const char en_US[] = "en_US.iso88591";
44// Windows
45#elif defined (_WIN32)
46const char en_US[] = "English";
47#else
48// AIX, IRIX, Linux, Solaris
49const char en_US[] = "en_US";
50#endif
51
52
53int main (int argc, char *argv[])
54{
55 // Get the monetary string and locale from the argument vector.
56 const char* const buffer = 1 < argc ? argv [1] : "$1,234.6789";
57 const char* const locname = 2 < argc ? argv [2] : en_US;
58 const bool intl = 3 < argc;
59
60 std::string smon;
61 long double fmon = 0.0;
62
63 std::ios_base::iostate state = std::ios_base::goodbit;
64
65 // Retrieve the money_get facet from the named locale.
66 const std::locale loc (locname);
67
68 typedef std::istreambuf_iterator<char> Iter;
69 typedef std::money_get<char, Iter> MoneyGet;
70
71 const MoneyGet &mgf = std::use_facet<MoneyGet>(loc);
72
73 {
74 // Build an istringstream object from the buffer
75 // and imbue the locale in it.
76 std::istringstream ins (buffer);
77 ins.imbue (loc);
78
79 // Get a string representation of the monetary value.
80 mgf.get (ins, Iter (), intl, ins, state, smon);
81 }
82 {
83 std::istringstream ins (buffer);
84 ins.imbue (loc);
85
86 // Get a floating point representation of the monetary value.
87 mgf.get (ins, Iter (), intl, ins, state, fmon);
88 }
89
90 // Output the original sequence and its string and floating point
91 // representations.
92 std::cout << buffer << " --> \"" << smon << "\" --> " << fmon << '\n';
93
94 // Return 0 on success, non-zero on failure.
95 return !(std::ios_base::eofbit == state);
96}
#define loc
Definition 17.names.cpp:492
#define buffer
Definition 17.names.cpp:453
#define main
std::ostream_iterator< int, char, std::char_traits< char > > Iter
Definition alg7.cpp:64
const char en_US[]
Definition money_get.cpp:49