aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/restricted/boost/locale/src/shared/format.cpp
blob: 839ee5222606780532bc8540e792512760b0a747 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
//
//  Copyright (c) 2009-2011 Artyom Beilis (Tonkikh)
//
//  Distributed under the Boost Software License, Version 1.0. (See
//  accompanying file LICENSE_1_0.txt or copy at
//  http://www.boost.org/LICENSE_1_0.txt)
//
#define BOOST_LOCALE_SOURCE
#include <boost/locale/format.hpp>
#include <boost/locale/generator.hpp>
#include <boost/locale/info.hpp>
#include <limits>
#include <stdlib.h>

#include <iostream>

namespace boost {
    namespace locale {
        namespace details {
            struct format_parser::data {
                unsigned position;
                std::streamsize precision;
                std::ios_base::fmtflags flags;
                ios_info info;
                std::locale saved_locale;
                bool restore_locale;
                void *cookie;
                void (*imbuer)(void *,std::locale const &);
            };

            format_parser::format_parser(std::ios_base &ios,void *cookie,void (*imbuer)(void *,std::locale const &)) : 
                ios_(ios),
                d(new data)
            {
                d->position=std::numeric_limits<unsigned>::max();
                d->precision=ios.precision();
                d->flags = ios.flags();
                d->info=ios_info::get(ios);
                d->saved_locale = ios.getloc();
                d->restore_locale=false;
                d->cookie = cookie;
                d->imbuer = imbuer;
            }

            void format_parser::imbue(std::locale const &l)
            {
                d->imbuer(d->cookie,l);
            }

            format_parser::~format_parser()
            {
            }

            void format_parser::restore()
            {
                ios_info::get(ios_) = d->info;
                ios_.width(0);
                ios_.flags(d->flags);
                if(d->restore_locale)
                    imbue(d->saved_locale);
            }

            unsigned format_parser::get_position()
            {
                return d->position;
            }

            void format_parser::set_one_flag(std::string const &key,std::string const &value)
            {
                if(key.empty())
                    return;
                unsigned i;
                for(i=0;i<key.size();i++) {
                    if(key[i] < '0' || '9'< key[i])
                        break;
                }
                if(i==key.size()) {
                    d->position=atoi(key.c_str()) - 1;
                    return;
                }

                if(key=="num" || key=="number") {
                    as::number(ios_);

                    if(value=="hex")
                        ios_.setf(std::ios_base::hex,std::ios_base::basefield);
                    else if(value=="oct")
                        ios_.setf(std::ios_base::oct,std::ios_base::basefield);
                    else if(value=="sci" || value=="scientific")
                        ios_.setf(std::ios_base::scientific,std::ios_base::floatfield);
                    else if(value=="fix" || value=="fixed")
                        ios_.setf(std::ios_base::fixed,std::ios_base::floatfield);
                }
                else if(key=="cur" || key=="currency") {
                    as::currency(ios_);
                    if(value=="iso") 
                        as::currency_iso(ios_);
                    else if(value=="nat" || value=="national")
                        as::currency_national(ios_);
                }
                else if(key=="per" || key=="percent") {
                    as::percent(ios_);
                }
                else if(key=="date") {
                    as::date(ios_);
                    if(value=="s" || value=="short")
                        as::date_short(ios_);
                    else if(value=="m" || value=="medium")
                        as::date_medium(ios_);
                    else if(value=="l" || value=="long")
                        as::date_long(ios_);
                    else if(value=="f" || value=="full")
                        as::date_full(ios_);
                }
                else if(key=="time") {
                    as::time(ios_);
                    if(value=="s" || value=="short")
                        as::time_short(ios_);
                    else if(value=="m" || value=="medium")
                        as::time_medium(ios_);
                    else if(value=="l" || value=="long")
                        as::time_long(ios_);
                    else if(value=="f" || value=="full")
                        as::time_full(ios_);
                }
                else if(key=="dt" || key=="datetime") {
                    as::datetime(ios_);
                    if(value=="s" || value=="short") {
                        as::date_short(ios_);
                        as::time_short(ios_);
                    }
                    else if(value=="m" || value=="medium") {
                        as::date_medium(ios_);
                        as::time_medium(ios_);
                    }
                    else if(value=="l" || value=="long") {
                        as::date_long(ios_);
                        as::time_long(ios_);
                    }
                    else if(value=="f" || value=="full") {
                        as::date_full(ios_);
                        as::time_full(ios_);
                    }
                }
                else if(key=="spell" || key=="spellout") {
                    as::spellout(ios_);
                }
                else if(key=="ord" || key=="ordinal") {
                    as::ordinal(ios_);
                }
                else if(key=="left" || key=="<")
                    ios_.setf(std::ios_base::left,std::ios_base::adjustfield);
                else if(key=="right" || key==">")
                    ios_.setf(std::ios_base::right,std::ios_base::adjustfield);
                else if(key=="gmt")
                    as::gmt(ios_);
                else if(key=="local")
                    as::local_time(ios_);
                else if(key=="timezone" || key=="tz")
                    ios_info::get(ios_).time_zone(value);
                else if(key=="w" || key=="width")
                    ios_.width(atoi(value.c_str()));
                else if(key=="p" || key=="precision")
                    ios_.precision(atoi(value.c_str()));
                else if(key=="locale") {
                    if(!d->restore_locale) {
                        d->saved_locale=ios_.getloc();
                        d->restore_locale=true;
                    }

                    std::string encoding=std::use_facet<info>(d->saved_locale).encoding();
                    generator gen;
                    gen.categories(formatting_facet);
                   
                    std::locale new_loc;
                    if(value.find('.')==std::string::npos) 
                        new_loc = gen(value + "." +  encoding);
                    else
                        new_loc = gen(value);

                    imbue(new_loc);
                }

            }
        }
    }
}
// vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4