From 9a53d70539580d46180ebf5c0e062ab6ca02e13c Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Thu, 22 Jul 2004 18:21:21 +0000 Subject: [PATCH] Make printf output DOS style newlines on Windows. --- src/common/mm_io.cpp | 19 +++++++++++++++---- src/common/mm_io.h | 4 ++++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/common/mm_io.cpp b/src/common/mm_io.cpp index 35e1b4c11..454f55d29 100644 --- a/src/common/mm_io.cpp +++ b/src/common/mm_io.cpp @@ -69,11 +69,13 @@ mm_io_c::mm_io_c(const char *path, throw exception(); file_name = safestrdup(path); + dos_style_newlines = false; } mm_io_c::mm_io_c() { file_name = NULL; file = NULL; + dos_style_newlines = false; } mm_io_c::~mm_io_c() { @@ -175,11 +177,13 @@ mm_io_c::mm_io_c(const char *path, throw exception(); file_name = safestrdup(path); + dos_style_newlines = true; } mm_io_c::mm_io_c() { file_name = NULL; file = NULL; + dos_style_newlines = true; } mm_io_c::~mm_io_c() { @@ -543,9 +547,9 @@ int mm_io_c::printf(const char *fmt, ...) { va_list ap; - string new_fmt; + string new_fmt, s; char *new_string; - int len; + int len, pos; fix_format(fmt, new_fmt); va_start(ap, fmt); @@ -553,10 +557,17 @@ mm_io_c::printf(const char *fmt, new_string = (char *)safemalloc(len + 1); vsprintf(new_string, new_fmt.c_str(), ap); va_end(ap); - len = write(new_string, len); + s = new_string; safefree(new_string); + if (dos_style_newlines) { + pos = 0; + while ((pos = s.find('\n', pos)) >= 0) { + s.replace(pos, 1, "\n\r"); + pos += 2; + } + } - return len; + return write(s.c_str(), s.length()); } /* diff --git a/src/common/mm_io.h b/src/common/mm_io.h index 48379d526..c68ef641d 100644 --- a/src/common/mm_io.h +++ b/src/common/mm_io.h @@ -39,6 +39,7 @@ protected: char *file_name; void *file; stack positions; + bool dos_style_newlines; public: mm_io_c(); @@ -78,6 +79,9 @@ public: virtual int truncate(int64_t pos); virtual int printf(const char *fmt, ...); + virtual void use_dos_style_newlines(bool yes) { + dos_style_newlines = yes; + } }; class MTX_DLL_API mm_null_io_c: public mm_io_c {