diff --git a/src/tl/tl/tlFileUtils.cc b/src/tl/tl/tlFileUtils.cc index 303b07b4f..8428e8e40 100644 --- a/src/tl/tl/tlFileUtils.cc +++ b/src/tl/tl/tlFileUtils.cc @@ -84,6 +84,10 @@ TL_PUBLIC void file_utils_force_windows () { s_mode = OS_Windows; } TL_PUBLIC void file_utils_force_linux () { s_mode = OS_Linux; } TL_PUBLIC void file_utils_force_reset () { s_mode = OS_Auto; } +const char *line_separator () +{ + return is_win () ? "\r\n" : "\n"; +} static bool is_drive (const std::string &part) { diff --git a/src/tl/tl/tlFileUtils.h b/src/tl/tl/tlFileUtils.h index 766ffc819..b6cb7ee4a 100644 --- a/src/tl/tl/tlFileUtils.h +++ b/src/tl/tl/tlFileUtils.h @@ -189,6 +189,11 @@ std::string TL_PUBLIC get_inst_path (); */ std::string TL_PUBLIC get_module_path (void *addr); +/** + * @brief Gets the line separator (CRLF on windows, LF on linux) + */ +const char * TL_PUBLIC line_separator (); + } #endif diff --git a/src/tl/tl/tlStream.cc b/src/tl/tl/tlStream.cc index 899dd5c5c..409c8cd29 100644 --- a/src/tl/tl/tlStream.cc +++ b/src/tl/tl/tlStream.cc @@ -769,11 +769,10 @@ OutputStream::put (const char *b, size_t n) ++b; --n; } else if (*b == '\n') { -#if defined(__WIN32) - put_raw ("\r\n", 2); -#else - put_raw ("\n", 1); -#endif + const char *ls = line_separator (); + while (*ls) { + put_raw (ls++, 1); + } ++b; --n; } else { diff --git a/src/tl/unit_tests/tlStreamTests.cc b/src/tl/unit_tests/tlStreamTests.cc index 1111c6608..4c398e206 100644 --- a/src/tl/unit_tests/tlStreamTests.cc +++ b/src/tl/unit_tests/tlStreamTests.cc @@ -22,6 +22,15 @@ #include "tlStream.h" #include "tlUnitTest.h" +#include "tlFileUtils.h" + +// Secret mode switchers for testing +namespace tl +{ +TL_PUBLIC void file_utils_force_windows (); +TL_PUBLIC void file_utils_force_linux (); +TL_PUBLIC void file_utils_force_reset (); +} TEST(InputPipe1) { @@ -58,18 +67,43 @@ TEST(TextOutputStream) EXPECT_EQ (s, "Hello, world!\nWith another line\n\r\r\nseparated by a LFCR and CRLF."); } - { - tl::OutputStream os (fn, tl::OutputStream::OM_Auto, true); - os << "Hello, world!\nWith another line\n\r\r\nseparated by a LFCR and CRLF."; - } + try { + + tl::file_utils_force_linux (); + + { + tl::OutputStream os (fn, tl::OutputStream::OM_Auto, true); + os << "Hello, world!\nWith another line\n\r\r\nseparated by a LFCR and CRLF."; + } - { tl::InputStream is (fn); std::string s = is.read_all (); -#if defined(__WIN32) - EXPECT_EQ (s, "Hello, world!\r\nWith another line\r\n\r\nseparated by a LFCR and CRLF."); -#else + EXPECT_EQ (s, "Hello, world!\nWith another line\n\nseparated by a LFCR and CRLF."); -#endif + tl::file_utils_force_reset (); + + } catch (...) { + tl::file_utils_force_reset (); + throw; + } + + try { + + tl::file_utils_force_windows (); + + { + tl::OutputStream os (fn, tl::OutputStream::OM_Auto, true); + os << "Hello, world!\nWith another line\n\r\r\nseparated by a LFCR and CRLF."; + } + + tl::InputStream is (fn); + std::string s = is.read_all (); + + EXPECT_EQ (s, "Hello, world!\r\nWith another line\r\n\r\nseparated by a LFCR and CRLF."); + tl::file_utils_force_reset (); + + } catch (...) { + tl::file_utils_force_reset (); + throw; } }