5#ifndef GEMMI_TO_CIF_HPP_
6#define GEMMI_TO_CIF_HPP_
61 std::string
str()
const {
70 s +=
"align_pairs=" + std::to_string(
align_pairs) +
",";
72 s +=
"align_loops=" + std::to_string(
align_loops) +
",";
83 explicit BufOstream(std::ostream& os_) : os(os_), ptr(buf) {}
86 os.write(buf, ptr - buf);
89 void write(
const char* s,
size_t len) {
90 constexpr int margin =
sizeof(buf) - 512;
91 if (ptr - buf + len >
margin) {
98 std::memcpy(ptr, s, len);
102 write(s.c_str(), s.size());
109 std::memset(ptr,
' ', n);
124 for (
size_t pos = 0, end = 0; end != std::string::npos; pos = end + 1) {
125 end = value.find(
"\r\n", pos);
126 size_t len = (end == std::string::npos ? value.size() : end) - pos;
127 os.
write(value.c_str() + pos, len);
138 if (name.size() + value.size() > 120) {
142 if (name.size() <
options.align_pairs)
154 for (
size_t i = 0;
i != loop.
tags.size(); ++
i)
159 os.
write(
"loop_", 5);
160 for (
const std::string& tag : loop.
tags) {
170 for (
const std::string& val : loop.
values) {
177 w = std::min(w, (
size_t)
options.align_loops);
182 for (
const std::string& val : loop.
values) {
190 if (col !=
ncol - 1) {
211 os.
write(
"save_", 5);
216 os.
write(
"save_\n", 6);
227inline bool should_be_separated_(
const Item& a,
const Item& b) {
233 auto adot = a.pair[0].find(
'.');
234 if (
adot == std::string::npos)
236 auto bdot = b.pair[0].find(
'.');
243 os.
write(
"data_", 5);
248 const Item* prev =
nullptr;
252 if (prev && !
options.compact && should_be_separated_(*prev, item)) {
267 for (
const Block& block :
doc.blocks) {
std::ostream with buffering.
void operator<<(const std::string &s)
void write(const char *s, size_t len)
BufOstream(std::ostream &os_)
void write_cif_block_to_stream(std::ostream &os_, const Block &block, WriteOptions options=WriteOptions())
bool is_text_field(const std::string &val)
void write_out_loop(BufOstream &os, const Loop &loop, WriteOptions options)
void write_out_pair(BufOstream &os, const std::string &name, const std::string &value, WriteOptions options)
void write_out_item(BufOstream &os, const Item &item, WriteOptions options)
void write_cif_to_stream(std::ostream &os, const Document &doc, WriteOptions options=WriteOptions())
void write_text_field(BufOstream &os, const std::string &value)
std::vector< Item > items
std::vector< std::string > tags
std::vector< std::string > values
WriteOptions(Style style)
bool compact
no blank lines between categories, only between blocks
bool misuse_hash
put '#' (empty comments) before/after categories
std::uint16_t align_pairs
width reserved for tags in pairs (e.g. 34 = value starts at 35th column)
bool prefer_pairs
write single-row loops as pairs
std::uint16_t align_loops
if non-zero, determines max width of each column in a loop and aligns all values to this width; the w...