/////////////////////////////////////////////////////////////////////////////// // // Copyright (C) 2008-2012 Artyom Beilis (Tonkikh) // // See accompanying file COPYING.TXT file for licensing details. // /////////////////////////////////////////////////////////////////////////////// #ifndef CPPCMS_IMPL_TODEC_H #define CPPCMS_IMPL_TODEC_H #include #include namespace cppcms { namespace impl { namespace details { template struct decimal_traits; template<> struct decimal_traits { template static void conv(T v,char *&begin,char *&buf) { begin = buf; while(v!=0) { *buf++ = '0' + v % 10; v/=10; } } }; template<> struct decimal_traits { template static void conv(T v,char *&begin,char *&buf) { if(v<0) { *buf++ = '-'; begin=buf; while(v!=0) { *buf++ = '0' - (v % 10); v/=10; } } else { decimal_traits::conv(v,begin,buf); } } }; } template void todec(Integer v,char *buf) { typedef std::numeric_limits limits; if(v == 0) { *buf++ = '0'; *buf++ = 0; return; } char *begin=0; details::decimal_traits::conv(v,begin,buf); *buf-- = 0; while(begin < buf) { char tmp = *begin; *begin = *buf; *buf = tmp; buf--; begin++; } } template std::string todec_string(I v) { char buf[std::numeric_limits::digits10 + 4]; todec(v,buf); std::string tmp = buf; return tmp; } namespace details { template struct write_int_to_stream { write_int_to_stream(T vi = 0) : v(vi) {} T v; void operator()(std::ostream &out) const { char buf[std::numeric_limits::digits10 + 4]; todec(v,buf); out << buf; } }; template std::ostream &operator<<(std::ostream &out,write_int_to_stream const &v) { v(out); return out; } } // details template details::write_int_to_stream cint(T v) { return details::write_int_to_stream(v); } } } #endif