More consistent handling of Windows/Linux switch in output stream and unit tests.

This commit is contained in:
Matthias Koefferlein 2019-11-21 21:57:49 +01:00
parent 72a3528e55
commit 215ce8812b
4 changed files with 56 additions and 14 deletions

View File

@ -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)
{

View File

@ -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

View File

@ -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 {

View File

@ -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;
}
}