mm_io_c::puts: add option for writing Unix-style newlines on Windows

This commit is contained in:
Moritz Bunkus 2021-07-06 20:17:02 +02:00
parent b283b8b2dd
commit 133fca6093
No known key found for this signature in database
GPG Key ID: 74AF00ADF2E32C85

View File

@ -96,9 +96,13 @@ mm_io_c::setFilePointer2(int64_t offset,
size_t
mm_io_c::puts(const std::string &s) {
#if defined(SYS_WINDOWS)
bool is_windows = true;
static std::optional<bool> s_use_unix_newlines;
if (!s_use_unix_newlines.has_value())
s_use_unix_newlines = mtx::sys::get_environment_variable("MTX_ALWAYS_USE_UNIX_NEWLINES") == "1";
bool use_unix_newlines = *s_use_unix_newlines;
#else
bool is_windows = false;
bool use_unix_newlines = true;
#endif
size_t num_written = 0;
const char *cs = s.c_str();
@ -106,8 +110,8 @@ mm_io_c::puts(const std::string &s) {
int cur_pos;
for (cur_pos = 0; cs[cur_pos] != 0; ++cur_pos) {
bool keep_char = is_windows || ('\r' != cs[cur_pos]) || ('\n' != cs[cur_pos + 1]);
bool insert_cr = is_windows && ('\n' == cs[cur_pos]) && ((0 == cur_pos) || ('\r' != cs[cur_pos - 1]));
bool keep_char = !use_unix_newlines || ('\r' != cs[cur_pos]) || ('\n' != cs[cur_pos + 1]);
bool insert_cr = !use_unix_newlines && ('\n' == cs[cur_pos]) && ((0 == cur_pos) || ('\r' != cs[cur_pos - 1]));
if (keep_char && !insert_cr)
continue;