diff --git a/toml/format.hpp b/toml/format.hpp new file mode 100644 index 0000000..8d131f6 --- /dev/null +++ b/toml/format.hpp @@ -0,0 +1,151 @@ +#ifndef TOML11_FORMAT +#define TOML11_FORMAT +#include "value.hpp" +#include + +namespace toml +{ + +template, + typename alloc = std::allocator> +std::basic_string +format(const value& v); + +template +struct format_impl; + +template<> struct format_impl +{ + typedef detail::toml_default_type::type type; + + static std::basic_string + invoke(const type& val) + { + return val ? "true" : "false"; + } +}; + +template<> struct format_impl +{ + typedef detail::toml_default_type::type type; + + static std::basic_string + invoke(const type& val) + { + return std::to_string(val); + } +}; + +template<> struct format_impl +{ + typedef detail::toml_default_type::type type; + + static std::basic_string + invoke(const type& val) + { + std::basic_ostringstream oss; + oss << std::showpoint << val; + if(oss.str().back() == '.') oss << '0'; + return oss.str(); + } +}; + +template<> struct format_impl +{ + typedef detail::toml_default_type::type type; + + static std::basic_string + invoke(const type& val) + { + //TODO escape some charactors! + return val; + } +}; + +template<> struct format_impl +{ + typedef detail::toml_default_type::type type; + + static std::basic_string + invoke(const type& val) + { + std::basic_ostringstream oss; + oss << val; + return oss.str(); + } +}; + +// TODO max length! +template<> struct format_impl +{ + typedef detail::toml_default_type::type type; + + static std::basic_string + invoke(const type& val) + { + std::basic_string retval; + retval += '['; + for(const auto&& item : val) + { + retval += format(val); + retval += ", "; + } + retval += ']'; + return ; + } +}; + +// TODO max length && inline! +template<> struct format_impl +{ + typedef detail::toml_default_type::type type; + + static std::basic_string + invoke(const type& val) + { + std::basic_string retval; + for(const auto&& item : val) + { + retval += val.first; + retval += " = " + retval += format(val.second); + retval += '\n'; + } + return ; + } +}; + + +template +std::basic_string +format(const value& v) +{ + switch(v.type()) + { + case value_t::Boolean : return format_impl::invoke(v.template cast()); + case value_t::Integer : return format_impl::invoke(v.template cast()); + case value_t::Float : return format_impl::invoke(v.template cast()); + case value_t::String : return format_impl::invoke(v.template cast()); + case value_t::Datetime: return format_impl::invoke(v.template cast()); + case value_t::Array : return format_impl::invoke(v.template cast()); + case value_t::Table : return format_impl::invoke(v.template cast()); + case value_t::Empty : return format_impl::invoke(v.template cast()); + case value_t::Unknown : return format_impl::invoke(v.template cast()); + default throw std::logic_error("toml::format: unknown enum value"); + } +} + +template, + typename alloc = std::allocator> +std::basic_string +format(std::basic_string&& key, const value& val) +{ + std::basic_string retval( + std::forward>(key)); + retval += " = "; + retval += format(val); + return retval; +} + +} +#endif // TOML11_FORMAT