123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534 |
- #ifndef BOOST_LOCALE_FORMATTING_HPP_INCLUDED
- #define BOOST_LOCALE_FORMATTING_HPP_INCLUDED
- #include <boost/locale/time_zone.hpp>
- #include <boost/assert.hpp>
- #include <boost/utility/string_view.hpp>
- #include <cstdint>
- #include <cstring>
- #include <istream>
- #include <ostream>
- #include <string>
- #include <typeinfo>
- #ifdef BOOST_MSVC
- # pragma warning(push)
- # pragma warning(disable : 4275 4251 4231 4660)
- #endif
- namespace boost { namespace locale {
-
-
- namespace flags {
-
-
- enum display_flags_type {
- posix = 0,
- number = 1,
- currency = 2,
- percent = 3,
- date = 4,
- time = 5,
- datetime = 6,
- strftime = 7,
- spellout = 8,
- ordinal = 9,
- display_flags_mask = 31,
- currency_default = 0 << 5,
- currency_iso = 1 << 5,
- currency_national = 2 << 5,
- currency_flags_mask = 3 << 5,
- time_default = 0 << 7,
- time_short = 1 << 7,
- time_medium = 2 << 7,
- time_long = 3 << 7,
- time_full = 4 << 7,
- time_flags_mask = 7 << 7,
- date_default = 0 << 10,
- date_short = 1 << 10,
- date_medium = 2 << 10,
- date_long = 3 << 10,
- date_full = 4 << 10,
- date_flags_mask = 7 << 10,
- };
-
- enum pattern_type {
- datetime_pattern,
- time_zone_id
- };
-
- enum value_type {
- domain_id
- };
- }
-
-
-
-
-
- class BOOST_LOCALE_DECL ios_info {
- public:
-
- ios_info();
- ios_info(const ios_info&);
- ios_info& operator=(const ios_info&);
- ~ios_info();
-
-
- static ios_info& get(std::ios_base& ios);
-
- void display_flags(uint64_t flags);
-
- uint64_t display_flags() const;
-
- void currency_flags(uint64_t flags);
-
- uint64_t currency_flags() const;
-
- void date_flags(uint64_t flags);
-
- uint64_t date_flags() const;
-
- void time_flags(uint64_t flags);
-
- uint64_t time_flags() const;
-
- void domain_id(int);
-
- int domain_id() const;
-
- void time_zone(const std::string&);
-
- std::string time_zone() const;
-
- template<typename CharType>
- void date_time_pattern(const std::basic_string<CharType>& str)
- {
- date_time_pattern_set().set<CharType>(str);
- }
-
- template<typename CharType>
- std::basic_string<CharType> date_time_pattern() const
- {
- return date_time_pattern_set().get<CharType>();
- }
-
- void on_imbue();
-
- private:
- class string_set;
- const string_set& date_time_pattern_set() const;
- string_set& date_time_pattern_set();
- class BOOST_LOCALE_DECL string_set {
- public:
- string_set();
- ~string_set();
- string_set(const string_set& other);
- string_set& operator=(string_set other);
- void swap(string_set& other);
- template<typename Char>
- void set(const boost::basic_string_view<Char> s)
- {
- BOOST_ASSERT(!s.empty());
- delete[] ptr;
- ptr = nullptr;
- type = &typeid(Char);
- size = sizeof(Char) * s.size();
- ptr = size ? new char[size] : nullptr;
- memcpy(ptr, s.data(), size);
- }
- template<typename Char>
- std::basic_string<Char> get() const
- {
- if(type == nullptr || *type != typeid(Char))
- throw std::bad_cast();
- std::basic_string<Char> result(size / sizeof(Char), Char(0));
- memcpy(&result.front(), ptr, size);
- return result;
- }
- private:
- const std::type_info* type;
- size_t size;
- char* ptr;
- };
- uint64_t flags_;
- int domain_id_;
- std::string time_zone_;
- string_set datetime_;
- };
-
- namespace as {
-
-
-
-
-
- inline std::ios_base& posix(std::ios_base& ios)
- {
- ios_info::get(ios).display_flags(flags::posix);
- return ios;
- }
-
-
- inline std::ios_base& number(std::ios_base& ios)
- {
- ios_info::get(ios).display_flags(flags::number);
- return ios;
- }
-
- inline std::ios_base& currency(std::ios_base& ios)
- {
- ios_info::get(ios).display_flags(flags::currency);
- return ios;
- }
-
- inline std::ios_base& percent(std::ios_base& ios)
- {
- ios_info::get(ios).display_flags(flags::percent);
- return ios;
- }
-
- inline std::ios_base& date(std::ios_base& ios)
- {
- ios_info::get(ios).display_flags(flags::date);
- return ios;
- }
-
- inline std::ios_base& time(std::ios_base& ios)
- {
- ios_info::get(ios).display_flags(flags::time);
- return ios;
- }
-
- inline std::ios_base& datetime(std::ios_base& ios)
- {
- ios_info::get(ios).display_flags(flags::datetime);
- return ios;
- }
-
-
- inline std::ios_base& strftime(std::ios_base& ios)
- {
- ios_info::get(ios).display_flags(flags::strftime);
- return ios;
- }
-
- inline std::ios_base& spellout(std::ios_base& ios)
- {
- ios_info::get(ios).display_flags(flags::spellout);
- return ios;
- }
-
- inline std::ios_base& ordinal(std::ios_base& ios)
- {
- ios_info::get(ios).display_flags(flags::ordinal);
- return ios;
- }
-
- inline std::ios_base& currency_default(std::ios_base& ios)
- {
- ios_info::get(ios).currency_flags(flags::currency_default);
- return ios;
- }
-
- inline std::ios_base& currency_iso(std::ios_base& ios)
- {
- ios_info::get(ios).currency_flags(flags::currency_iso);
- return ios;
- }
-
- inline std::ios_base& currency_national(std::ios_base& ios)
- {
- ios_info::get(ios).currency_flags(flags::currency_national);
- return ios;
- }
-
- inline std::ios_base& time_default(std::ios_base& ios)
- {
- ios_info::get(ios).time_flags(flags::time_default);
- return ios;
- }
-
- inline std::ios_base& time_short(std::ios_base& ios)
- {
- ios_info::get(ios).time_flags(flags::time_short);
- return ios;
- }
-
- inline std::ios_base& time_medium(std::ios_base& ios)
- {
- ios_info::get(ios).time_flags(flags::time_medium);
- return ios;
- }
-
- inline std::ios_base& time_long(std::ios_base& ios)
- {
- ios_info::get(ios).time_flags(flags::time_long);
- return ios;
- }
-
- inline std::ios_base& time_full(std::ios_base& ios)
- {
- ios_info::get(ios).time_flags(flags::time_full);
- return ios;
- }
-
- inline std::ios_base& date_default(std::ios_base& ios)
- {
- ios_info::get(ios).date_flags(flags::date_default);
- return ios;
- }
-
- inline std::ios_base& date_short(std::ios_base& ios)
- {
- ios_info::get(ios).date_flags(flags::date_short);
- return ios;
- }
-
- inline std::ios_base& date_medium(std::ios_base& ios)
- {
- ios_info::get(ios).date_flags(flags::date_medium);
- return ios;
- }
-
- inline std::ios_base& date_long(std::ios_base& ios)
- {
- ios_info::get(ios).date_flags(flags::date_long);
- return ios;
- }
-
- inline std::ios_base& date_full(std::ios_base& ios)
- {
- ios_info::get(ios).date_flags(flags::date_full);
- return ios;
- }
-
- namespace detail {
- inline bool is_datetime_display_flags(const uint64_t display_flags)
- {
- return (display_flags == flags::date || display_flags == flags::time || display_flags == flags::datetime
- || display_flags == flags::strftime);
- }
- template<typename CharType>
- struct add_ftime {
- std::basic_string<CharType> ftime;
- void apply(std::basic_ios<CharType>& ios) const
- {
- ios_info::get(ios).date_time_pattern(ftime);
- as::strftime(ios);
- }
- };
- template<typename CharType>
- std::basic_ostream<CharType>& operator<<(std::basic_ostream<CharType>& out, const add_ftime<CharType>& fmt)
- {
- fmt.apply(out);
- return out;
- }
- template<typename CharType>
- std::basic_istream<CharType>& operator>>(std::basic_istream<CharType>& in, const add_ftime<CharType>& fmt)
- {
- fmt.apply(in);
- return in;
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- template<typename CharType>
- #ifdef BOOST_LOCALE_DOXYGEN
- unspecified_type
- #else
- detail::add_ftime<CharType>
- #endif
- ftime(const std::basic_string<CharType>& format)
- {
- detail::add_ftime<CharType> fmt;
- fmt.ftime = format;
- return fmt;
- }
-
- template<typename CharType>
- #ifdef BOOST_LOCALE_DOXYGEN
- unspecified_type
- #else
- detail::add_ftime<CharType>
- #endif
- ftime(const CharType* format)
- {
- detail::add_ftime<CharType> fmt;
- fmt.ftime = format;
- return fmt;
- }
-
- namespace detail {
- struct set_timezone {
- std::string id;
- };
- template<typename CharType>
- std::basic_ostream<CharType>& operator<<(std::basic_ostream<CharType>& out, const set_timezone& fmt)
- {
- ios_info::get(out).time_zone(fmt.id);
- return out;
- }
- template<typename CharType>
- std::basic_istream<CharType>& operator>>(std::basic_istream<CharType>& in, const set_timezone& fmt)
- {
- ios_info::get(in).time_zone(fmt.id);
- return in;
- }
- }
-
-
- inline std::ios_base& gmt(std::ios_base& ios)
- {
- ios_info::get(ios).time_zone("GMT");
- return ios;
- }
-
- inline std::ios_base& local_time(std::ios_base& ios)
- {
- ios_info::get(ios).time_zone(time_zone::global());
- return ios;
- }
-
- inline
- #ifdef BOOST_LOCALE_DOXYGEN
- unspecified_type
- #else
- detail::set_timezone
- #endif
- time_zone(const char* id)
- {
- detail::set_timezone tz;
- tz.id = id;
- return tz;
- }
-
- inline
- #ifdef BOOST_LOCALE_DOXYGEN
- unspecified_type
- #else
- detail::set_timezone
- #endif
- time_zone(const std::string& id)
- {
- detail::set_timezone tz;
- tz.id = id;
- return tz;
- }
-
- }
- }}
- #ifdef BOOST_MSVC
- # pragma warning(pop)
- #endif
- #endif
|