formatting.h
//
// 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)
//
#ifndef BOOSTER_LOCALE_FORMATTING_H_INCLUDED
#define BOOSTER_LOCALE_FORMATTING_H_INCLUDED
#include <booster/config.h>
#ifdef BOOSTER_MSVC
# pragma warning(push)
# pragma warning(disable : 4275 4251 4231 4660)
#endif
#include <booster/cstdint.h>
#include <booster/locale/time_zone.h>
#include <ostream>
#include <istream>
#include <string>
#include <string.h>
#include <typeinfo>
namespace booster {
namespace locale {
namespace flags {
typedef enum {
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,
datetime_flags_mask = date_flags_mask | time_flags_mask
} display_flags_type;
typedef enum {
datetime_pattern,
time_zone_id
} pattern_type;
typedef enum {
domain_id
} value_type;
} // flags
class BOOSTER_API ios_info {
public:
ios_info();
ios_info(ios_info const &);
ios_info const &operator=(ios_info const &);
~ios_info();
static ios_info &get(std::ios_base &ios);
void display_flags(uint64_t flags);
void currency_flags(uint64_t flags);
void date_flags(uint64_t flags);
void time_flags(uint64_t flags);
void datetime_flags(uint64_t flags);
void domain_id(int);
void time_zone(std::string const &);
template<typename CharType>
void date_time_pattern(std::basic_string<CharType> const &str)
{
string_set &s = date_time_pattern_set();
s.set<CharType>(str.c_str());
}
uint64_t display_flags() const;
uint64_t currency_flags() const;
uint64_t date_flags() const;
uint64_t time_flags() const;
uint64_t datetime_flags() const;
int domain_id() const;
std::string time_zone() const;
template<typename CharType>
std::basic_string<CharType> date_time_pattern() const
{
string_set const &s = date_time_pattern_set();
return s.get<CharType>();
}
void on_imbue();
private:
class string_set;
string_set const &date_time_pattern_set() const;
string_set &date_time_pattern_set();
class BOOSTER_API string_set {
public:
string_set();
~string_set();
string_set(string_set const &other);
string_set const &operator=(string_set const &other);
void swap(string_set &other);
template<typename Char>
void set(Char const *s)
{
delete [] ptr;
ptr = 0;
type=&typeid(Char);
Char const *end = s;
while(*end!=0) end++;
// if ptr = 0 it does not matter what is value of size
size = sizeof(Char)*(end - s+1);
ptr = new char[size];
memcpy(ptr,s,size);
}
template<typename Char>
std::basic_string<Char> get() const
{
if(type==0 || *type!=typeid(Char))
throw std::bad_cast();
std::basic_string<Char> result = reinterpret_cast<Char const *>(ptr);
return result;
}
private:
std::type_info const *type;
size_t size;
char *ptr;
};
uint64_t flags_;
int domain_id_;
std::string time_zone_;
string_set datetime_;
struct data;
data *d;
};
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 details {
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,add_ftime<CharType> const &fmt)
{
fmt.apply(out);
return out;
}
template<typename CharType>
std::basic_istream<CharType> &operator>>(std::basic_istream<CharType> &in,add_ftime<CharType> const &fmt)
{
fmt.apply(in);
return in;
}
}
template<typename CharType>
#ifdef BOOSTER_LOCALE_DOXYGEN
unspecified_type
#else
details::add_ftime<CharType>
#endif
ftime(std::basic_string<CharType> const &format)
{
details::add_ftime<CharType> fmt;
fmt.ftime=format;
return fmt;
}
template<typename CharType>
#ifdef BOOSTER_LOCALE_DOXYGEN
unspecified_type
#else
details::add_ftime<CharType>
#endif
ftime(CharType const *format)
{
details::add_ftime<CharType> fmt;
fmt.ftime=format;
return fmt;
}
namespace details {
struct set_timezone {
std::string id;
};
template<typename CharType>
std::basic_ostream<CharType> &operator<<(std::basic_ostream<CharType> &out,set_timezone const &fmt)
{
ios_info::get(out).time_zone(fmt.id);
return out;
}
template<typename CharType>
std::basic_istream<CharType> &operator>>(std::basic_istream<CharType> &in,set_timezone const &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 BOOSTER_LOCALE_DOXYGEN
unspecified_type
#else
details::set_timezone
#endif
time_zone(char const *id)
{
details::set_timezone tz;
tz.id=id;
return tz;
}
inline
#ifdef BOOSTER_LOCALE_DOXYGEN
unspecified_type
#else
details::set_timezone
#endif
time_zone(std::string const &id)
{
details::set_timezone tz;
tz.id=id;
return tz;
}
} // as manipulators
} // locale
} // boost
#ifdef BOOSTER_MSVC
#pragma warning(pop)
#endif
#endif
// vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
booster::locale::as::date_short
std::ios_base & date_short(std::ios_base &ios)
Definition: formatting.h:455
booster::locale::as::currency_national
std::ios_base & currency_national(std::ios_base &ios)
Definition: formatting.h:392
booster::locale::flags::time_zone_id
time zone name
Definition: formatting.h:79
booster::locale::ios_info::date_time_pattern
std::basic_string< CharType > date_time_pattern() const
Definition: formatting.h:202
booster::locale::as::date
std::ios_base & date(std::ios_base &ios)
Definition: formatting.h:319
booster::locale::as::time_zone
details::set_timezone time_zone(char const *id)
Definition: formatting.h:634
booster::locale::operator>>
std::basic_istream< CharType > & operator>>(std::basic_istream< CharType > &in, date_time &t)
Definition: date_time.h:901
booster::locale::as::time_zone
details::set_timezone time_zone(std::string const &id)
Definition: formatting.h:650
booster::locale::as::time_full
std::ios_base & time_full(std::ios_base &ios)
Definition: formatting.h:437
booster::locale::as::time_medium
std::ios_base & time_medium(std::ios_base &ios)
Definition: formatting.h:419
booster::locale::flags::pattern_type
pattern_type
Definition: formatting.h:77
booster::locale::as::datetime
std::ios_base & datetime(std::ios_base &ios)
Definition: formatting.h:337
booster::locale::as::currency
std::ios_base & currency(std::ios_base &ios)
Definition: formatting.h:301
booster::locale::flags::value_type
value_type
Definition: formatting.h:85
booster::locale::ios_info::currency_flags
void currency_flags(uint64_t flags)
booster::locale::flags::datetime_pattern
strftime like formatting
Definition: formatting.h:78
booster::locale::ios_info::get
static ios_info & get(std::ios_base &ios)
booster::locale::as::time_short
std::ios_base & time_short(std::ios_base &ios)
Definition: formatting.h:410
booster::locale::flags::domain_id
Domain code - for message formatting.
Definition: formatting.h:86
booster::locale::flags::display_flags_type
display_flags_type
Definition: formatting.h:35
booster::locale::as::date_default
std::ios_base & date_default(std::ios_base &ios)
Definition: formatting.h:446
booster::locale::as::ftime
details::add_ftime< CharType > ftime(std::basic_string< CharType > const &format)
Definition: formatting.h:563
booster::locale::as::spellout
std::ios_base & spellout(std::ios_base &ios)
Definition: formatting.h:356
booster::locale::as::gmt
std::ios_base & gmt(std::ios_base &ios)
Definition: formatting.h:610
booster::locale::as::ordinal
std::ios_base & ordinal(std::ios_base &ios)
Definition: formatting.h:365
booster::locale::as::currency_default
std::ios_base & currency_default(std::ios_base &ios)
Definition: formatting.h:374
booster::locale::as::time_long
std::ios_base & time_long(std::ios_base &ios)
Definition: formatting.h:428
booster::locale::as::percent
std::ios_base & percent(std::ios_base &ios)
Definition: formatting.h:310
booster::locale::as::date_medium
std::ios_base & date_medium(std::ios_base &ios)
Definition: formatting.h:464
booster::locale::as::posix
std::ios_base & posix(std::ios_base &ios)
Definition: formatting.h:282
booster::locale::time_zone::global
BOOSTER_API std::string global()
booster::locale::ios_info::time_zone
void time_zone(std::string const &)
booster::locale::as::number
std::ios_base & number(std::ios_base &ios)
Definition: formatting.h:292
booster::locale::as::currency_iso
std::ios_base & currency_iso(std::ios_base &ios)
Definition: formatting.h:383
booster::locale::ios_info::date_time_pattern
void date_time_pattern(std::basic_string< CharType > const &str)
Definition: formatting.h:155
booster::locale::ios_info::time_flags
void time_flags(uint64_t flags)
booster::locale::format
basic_format< char > format
Definition: format.h:469
booster::locale::as::strftime
std::ios_base & strftime(std::ios_base &ios)
Definition: formatting.h:347
booster::locale::as::date_long
std::ios_base & date_long(std::ios_base &ios)
Definition: formatting.h:473
booster::locale::as::date_full
std::ios_base & date_full(std::ios_base &ios)
Definition: formatting.h:482
booster::locale::as::time_default
std::ios_base & time_default(std::ios_base &ios)
Definition: formatting.h:401
booster::locale::as::local_time
std::ios_base & local_time(std::ios_base &ios)
Definition: formatting.h:619
booster::locale::ios_info::display_flags
void display_flags(uint64_t flags)
booster::locale::basic_format
a printf like class that allows type-safe and locale aware message formatting
Definition: format.h:204
booster::locale::ios_info::date_flags
void date_flags(uint64_t flags)
booster::locale::ios_info
This class holds an external data - beyond existing fmtflags that std::ios_base holds.
Definition: formatting.h:98
booster::locale::as::time
std::ios_base & time(std::ios_base &ios)
Definition: formatting.h:328