From 471599cb8d9d88de772005ac016b7920c3a5232e Mon Sep 17 00:00:00 2001 From: Gabriel Studer <gabriel.studer@unibas.ch> Date: Thu, 27 Feb 2025 17:35:21 +0100 Subject: [PATCH] mmcif writer: use semi-colon when writing multiline values --- modules/io/src/mol/star_writer.hh | 33 ++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/modules/io/src/mol/star_writer.hh b/modules/io/src/mol/star_writer.hh index 53923e957..fd1b094a6 100644 --- a/modules/io/src/mol/star_writer.hh +++ b/modules/io/src/mol/star_writer.hh @@ -125,11 +125,22 @@ public: // * loop_ (case insensitive) // * stop_ (case insensitive) // * global_ (case insensitive) + + bool is_multiline = false; bool needs_quotes = false; + // newline in string + for(char c: string_value) { + if(c == '\n') { + is_multiline = true; + break; + } + } + // space in string for(char c: string_value) { - if(isspace(c)) { + if(isspace(c) && c != '\n') { + // linebreaks evaluate to true in isspace but do not require quotes needs_quotes = true; break; } @@ -219,9 +230,16 @@ public: } } - if(needs_quotes) { + if(is_multiline && needs_quotes) { + value.value_ = "\n;\"" + string_value + "\"\n;\n"; + } + else if(needs_quotes) { value.value_ = "\"" + string_value + "\""; - } else { + } + else if(is_multiline) { + value.value_ = "\n;" + string_value + "\n;\n"; + } + else { value.value_ = string_value; } } @@ -315,9 +333,14 @@ public: desc_.ToStream(s); int desc_size = desc_.GetSize(); for(size_t i = 0; i < data_.size(); ++i) { - s << data_[i].GetValue(); + const String& v = data_[i].GetValue(); + s << v; + bool ends_with_newline = v.back() == '\n'; if((i+1) % desc_size == 0) { - s << std::endl; + // no need for newline if the last item ended with a newline anyways + if(!ends_with_newline) { + s << std::endl; + } } else { s << ' '; } -- GitLab