toml11/toml/format.hpp

387 lines
12 KiB
C++
Raw Normal View History

2017-05-09 13:09:03 +00:00
#ifndef TOML11_FORMAT
#define TOML11_FORMAT
#include "value.hpp"
2017-05-09 15:43:13 +00:00
#include <type_traits>
#include <iostream>
2017-05-17 01:55:22 +00:00
#include <sstream>
2017-05-09 15:43:13 +00:00
#include <iomanip>
2017-05-13 06:00:02 +00:00
#include <locale>
2017-05-09 13:09:03 +00:00
namespace toml
{
2017-05-09 15:43:13 +00:00
// synopsis
// toml::format("key", value, toml::make_inline(80))
// toml::format("key", value, toml::forceinline)
// std::cout << toml::make_inline(80) << value;
// std::cout << toml::forceinline << value;
2017-05-17 01:55:22 +00:00
template<typename traits = std::char_traits<toml::character>,
typename alloc = std::allocator<toml::character>>
std::basic_string<toml::character, traits, alloc>
2017-05-09 13:09:03 +00:00
format(const value& v);
2017-05-17 01:55:22 +00:00
template<typename traits = std::char_traits<toml::character>,
typename alloc = std::allocator<toml::character>>
std::basic_string<toml::character, traits, alloc>
2017-05-09 15:43:13 +00:00
format(const value& v, std::size_t mk);
2017-05-17 01:55:22 +00:00
template<typename traits = std::char_traits<toml::character>,
typename alloc = std::allocator<toml::character>>
std::basic_string<toml::character, traits, alloc>
2017-05-09 15:43:13 +00:00
format(const toml::key& k, const value& v);
2017-05-17 01:55:22 +00:00
template<typename traits = std::char_traits<toml::character>,
typename alloc = std::allocator<toml::character>>
std::basic_string<toml::character, traits, alloc>
2017-05-09 15:43:13 +00:00
format(const toml::key& k, const value& v, std::size_t mk);
2017-05-09 13:09:03 +00:00
template<value_t Type>
struct format_impl;
template<> struct format_impl<value_t::Boolean>
{
typedef detail::toml_default_type<value_t::Boolean>::type type;
2017-05-17 01:55:22 +00:00
std::basic_string<toml::character>
2017-05-13 02:28:11 +00:00
operator()(const type& val)
2017-05-09 13:09:03 +00:00
{
return val ? "true" : "false";
}
};
template<> struct format_impl<value_t::Integer>
{
typedef detail::toml_default_type<value_t::Integer>::type type;
2017-05-17 01:55:22 +00:00
std::basic_string<toml::character>
2017-05-13 02:28:11 +00:00
operator()(const type& val)
2017-05-09 13:09:03 +00:00
{
return std::to_string(val);
}
};
template<> struct format_impl<value_t::Float>
{
typedef detail::toml_default_type<value_t::Float>::type type;
2017-05-17 01:55:22 +00:00
std::basic_string<toml::character>
2017-05-13 02:28:11 +00:00
operator()(const type& val)
2017-05-09 13:09:03 +00:00
{
2017-05-17 01:55:22 +00:00
std::basic_ostringstream<toml::character> oss;
2017-05-09 13:09:03 +00:00
oss << std::showpoint << val;
if(oss.str().back() == '.') oss << '0';
return oss.str();
}
};
template<> struct format_impl<value_t::String>
{
typedef detail::toml_default_type<value_t::String>::type type;
2017-05-13 02:28:11 +00:00
std::size_t max_length;
2017-05-13 06:00:02 +00:00
format_impl() : max_length(80){}
format_impl(std::size_t mx) : max_length(mx){}
2017-05-13 02:28:11 +00:00
2017-05-17 01:55:22 +00:00
std::basic_string<toml::character>
2017-05-13 02:28:11 +00:00
operator()(const type& val)
2017-05-09 13:09:03 +00:00
{
2017-05-13 02:28:11 +00:00
auto tmp = make_inline(val);
if(max_length == std::numeric_limits<std::size_t>::max() ||
tmp.size() <= max_length) return tmp;
return convert_multiline(std::move(tmp));
2017-05-09 13:09:03 +00:00
}
2017-05-13 02:28:11 +00:00
private:
2017-05-17 01:55:22 +00:00
std::basic_string<toml::character>
make_inline(const std::basic_string<toml::character>& val)
2017-05-13 02:28:11 +00:00
{
2017-05-17 01:55:22 +00:00
std::basic_string<toml::character> str;
2017-05-13 02:28:11 +00:00
str += '"';
for(const auto& c : val)
{
if('\0' < c && c < '\31')
{
switch(c)
{
case '\b': str += "\\b"; break;
case '\t': str += "\\t"; break;
case '\n': str += "\\n"; break;
case '\f': str += "\\f"; break;
case '\r': str += "\\r"; break;
default:
{
str += 'u';
2017-05-17 01:55:22 +00:00
std::basic_ostringstream<toml::character> oss;
2017-05-13 06:00:02 +00:00
oss << std::setw(4) << std::setfill('0') << std::hex
2017-05-13 02:28:11 +00:00
<< static_cast<std::int8_t>(c);
2017-05-13 06:00:02 +00:00
auto hexdig = oss.str();
2017-05-17 01:55:22 +00:00
std::transform(hexdig.begin(), hexdig.end(), hexdig.begin(), ::toupper);
2017-05-13 02:28:11 +00:00
str += oss.str();
break;
}
}
}
else if(c == '"')
{
str += "\\\"";
}
else if(c == '\\')
{
str += "\\\\";
}
else
{
str += c;
}
}
str += '"';
return str;
}
2017-05-17 01:55:22 +00:00
std::basic_string<toml::character>
convert_multiline(std::basic_string<toml::character>&& val)
2017-05-13 02:28:11 +00:00
{
2017-05-17 01:55:22 +00:00
std::basic_string<toml::character> str; str.reserve(val.size() + 6);
2017-05-13 06:00:02 +00:00
str += "\"\"\"\n";
std::size_t current = 0;
2017-05-13 02:28:11 +00:00
for(auto iter = val.begin()+1; iter != val.end()-1; ++iter)
{
if(*iter != '\\')
{
2017-05-13 06:00:02 +00:00
if(current + 1 == max_length) str += "\\\n";
2017-05-13 02:28:11 +00:00
str += *iter; continue;
}
assert(std::next(iter) < val.end()-1);
if(*std::next(iter) == 'u')
{
2017-05-13 06:00:02 +00:00
if(current + 5 == max_length) str += "\\\n";
2017-05-13 02:28:11 +00:00
assert(iter + 5 < val.end()-1);
2017-05-13 06:00:02 +00:00
str += *iter; ++iter; // u
str += *iter; ++iter; // 0
str += *iter; ++iter; // 1
str += *iter; ++iter; // 2
str += *iter; continue;// 3
2017-05-13 02:28:11 +00:00
}
2017-05-13 06:00:02 +00:00
if(current + 2 == max_length) str += "\\\n";
2017-05-13 02:28:11 +00:00
str += *iter; ++iter; str += *iter;
}
str += "\"\"\"";
return str;
}
2017-05-09 13:09:03 +00:00
};
template<> struct format_impl<value_t::Datetime>
{
typedef detail::toml_default_type<value_t::Datetime>::type type;
2017-05-17 01:55:22 +00:00
std::basic_string<toml::character>
2017-05-13 02:28:11 +00:00
operator()(const type& val)
2017-05-09 13:09:03 +00:00
{
2017-05-17 01:55:22 +00:00
std::basic_ostringstream<toml::character> oss;
2017-05-09 13:09:03 +00:00
oss << val;
return oss.str();
}
};
// TODO max length!
template<> struct format_impl<value_t::Array>
{
typedef detail::toml_default_type<value_t::Array>::type type;
2017-05-13 02:28:11 +00:00
std::size_t max_length;
2017-05-17 01:55:22 +00:00
format_impl() : max_length(80){}
format_impl(std::size_t mx) : max_length(mx){}
std::basic_string<toml::character>
2017-05-13 02:28:11 +00:00
operator()(const type& val)
2017-05-09 13:09:03 +00:00
{
2017-05-17 01:55:22 +00:00
std::basic_string<toml::character> retval;
2017-05-09 13:09:03 +00:00
retval += '[';
2017-05-17 01:55:22 +00:00
for(const auto& item : val)
2017-05-09 13:09:03 +00:00
{
2017-05-13 06:00:02 +00:00
auto tmp = format(val, max_length - 1);
2017-05-13 02:28:11 +00:00
retval += tmp;
2017-05-09 13:09:03 +00:00
retval += ", ";
2017-05-13 02:28:11 +00:00
if(tmp.size() * 2 > max_length) retval += '\n';
2017-05-09 13:09:03 +00:00
}
retval += ']';
2017-05-17 01:55:22 +00:00
return retval;
2017-05-09 13:09:03 +00:00
}
};
// TODO max length && inline!
template<> struct format_impl<value_t::Table>
{
typedef detail::toml_default_type<value_t::Table>::type type;
2017-05-17 01:55:22 +00:00
std::size_t max_length;
format_impl() : max_length(80){}
format_impl(std::size_t mx) : max_length(mx){}
std::basic_string<toml::character>
2017-05-13 06:00:02 +00:00
operator()(const type& val)
2017-05-09 13:09:03 +00:00
{
2017-05-17 01:55:22 +00:00
std::basic_string<toml::character> retval;
for(const auto& item : val)
2017-05-09 13:09:03 +00:00
{
2017-05-17 01:55:22 +00:00
retval += item.first;
retval += " = ";
retval += format(item.second);
2017-05-09 13:09:03 +00:00
retval += '\n';
}
2017-05-17 01:55:22 +00:00
return retval;
2017-05-09 13:09:03 +00:00
}
};
template<typename traits, typename alloc>
2017-05-17 01:55:22 +00:00
std::basic_string<toml::character, traits, alloc>
2017-05-09 13:09:03 +00:00
format(const value& v)
{
switch(v.type())
{
2017-05-13 06:00:02 +00:00
case value_t::Boolean : return format_impl<value_t::Boolean >{}(v.template cast<value_t::Boolean >());
case value_t::Integer : return format_impl<value_t::Integer >{}(v.template cast<value_t::Integer >());
case value_t::Float : return format_impl<value_t::Float >{}(v.template cast<value_t::Float >());
case value_t::String : return format_impl<value_t::String >{}(v.template cast<value_t::String >());
case value_t::Datetime: return format_impl<value_t::Datetime>{}(v.template cast<value_t::Datetime>());
case value_t::Array : return format_impl<value_t::Array >{}(v.template cast<value_t::Array >());
case value_t::Table : return format_impl<value_t::Table >{}(v.template cast<value_t::Table >());
2017-05-17 01:55:22 +00:00
case value_t::Empty : throw std::runtime_error("toml::format: empty value");
case value_t::Unknown : throw std::runtime_error("toml::format: unknown value");
default: throw std::logic_error("toml::format: unknown enum value");
2017-05-09 13:09:03 +00:00
}
}
2017-05-09 15:43:13 +00:00
template<typename traits, typename alloc>
2017-05-17 01:55:22 +00:00
std::basic_string<toml::character, traits, alloc>
format(const value& v, std::size_t inl)
2017-05-09 13:09:03 +00:00
{
2017-05-17 01:55:22 +00:00
switch(v.type())
{
case value_t::Boolean : return format_impl<value_t::Boolean >{}(v.template cast<value_t::Boolean>());
case value_t::Integer : return format_impl<value_t::Integer >{}(v.template cast<value_t::Integer>());
case value_t::Float : return format_impl<value_t::Float >{}(v.template cast<value_t::Float>());
case value_t::String : return format_impl<value_t::String >{inl}(v.template cast<value_t::String>());
case value_t::Datetime: return format_impl<value_t::Datetime>{}(v.template cast<value_t::Datetime>());
case value_t::Array : return format_impl<value_t::Array >{inl}(v.template cast<value_t::Array>());
case value_t::Table : return format_impl<value_t::Table >{inl}(v.template cast<value_t::Table>());
case value_t::Empty : throw std::runtime_error("toml::format: empty value");
case value_t::Unknown : throw std::runtime_error("toml::format: unknown value");
default: throw std::logic_error("toml::format: unknown enum value");
}
}
template<typename traits, typename alloc>
std::basic_string<toml::character, traits, alloc>
format(std::basic_string<toml::character, traits, alloc> key, const value& val)
{
std::basic_string<toml::character, traits, alloc> retval(std::move(key));
2017-05-09 13:09:03 +00:00
retval += " = ";
retval += format(val);
return retval;
}
2017-05-17 01:55:22 +00:00
template<typename traits, typename alloc>
std::basic_string<toml::character, traits, alloc>
format(std::basic_string<toml::character, traits, alloc> key, const value& val, std::size_t mk)
{
std::basic_string<toml::character, traits, alloc> retval(std::move(key));
retval += " = ";
retval += format(val, mk);
return retval;
}
2017-05-09 15:43:13 +00:00
// ----------------------------- stream operators -----------------------------
namespace detail
{
template<typename T = std::size_t>
struct inline_limit
{
static_assert(std::is_same<T, std::size_t>::value, "do not instantiate this");
static const int index;
T limit;
inline_limit() = default;
~inline_limit() = default;
2017-05-17 01:55:22 +00:00
constexpr inline_limit(T i): limit(i){}
2017-05-09 15:43:13 +00:00
constexpr operator T() const {return limit;}
static void callback(std::ios_base::event ev, std::ios_base& ios, int idx)
{
void*& info = ios.pword(idx);
switch (ev)
{
case std::ios_base::erase_event:
{
delete static_cast<std::size_t*>(info);
break;
}
case std::ios_base::copyfmt_event:
{
info = new std::size_t(*static_cast<std::size_t*>(info));
break;
}
case std::ios_base::imbue_event:
{
break;
}
}
}
};
template<typename T>
const int inline_limit<T>::index = std::ios_base::xalloc();
} //detail
2017-05-17 01:55:22 +00:00
template<typename sizeT, typename traits = std::char_traits<toml::character>>
std::basic_ostream<toml::character, traits>&
operator<<(std::basic_ostream<toml::character, traits>& os,
2017-05-09 15:43:13 +00:00
const detail::inline_limit<sizeT>& inl)
{
void*& info = os.pword(detail::inline_limit<sizeT>::index);
if(!os.bad())
{
if(info == nullptr)
{
os.register_callback(detail::inline_limit<sizeT>::callback,
detail::inline_limit<sizeT>::index);
info = new std::size_t(inl.limit);
}
else
{
*static_cast<std::size_t*>(info) = inl.limit;
}
}
return os;
}
2017-05-17 01:55:22 +00:00
constexpr static detail::inline_limit<std::size_t> forceinline(
std::numeric_limits<std::size_t>::max());
2017-05-09 15:43:13 +00:00
inline detail::inline_limit<std::size_t> make_inline(std::size_t sz)
{
return detail::inline_limit<std::size_t>(sz);
}
2017-05-17 01:55:22 +00:00
template<typename T, typename traits = std::char_traits<toml::character>>
std::basic_ostream<toml::character, traits>&
operator<<(std::basic_ostream<toml::character, traits>& os,
2017-05-09 15:43:13 +00:00
const toml::value& v)
{
std::size_t* info =
2017-05-17 01:55:22 +00:00
static_cast<std::size_t*>(os.pword(detail::inline_limit<std::size_t>::index));
2017-05-09 15:43:13 +00:00
return os << (info == nullptr ? toml::format(v) : toml::format(v, *info));
}
2017-05-09 13:09:03 +00:00
}
#endif // TOML11_FORMAT