From 0bb0452de794d3e8c95c7c23f1dc06e8b2df4322 Mon Sep 17 00:00:00 2001 From: jeiea Date: Tue, 27 Jun 2017 00:21:13 +0900 Subject: [PATCH 1/2] Fix piping as text on Windows --- guetzli/guetzli.cc | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/guetzli/guetzli.cc b/guetzli/guetzli.cc index fb6cd0a9..423e056d 100644 --- a/guetzli/guetzli.cc +++ b/guetzli/guetzli.cc @@ -28,6 +28,10 @@ #include "guetzli/processor.h" #include "guetzli/quality.h" #include "guetzli/stats.h" +#if _WIN32 | _WIN64 +#include +#include +#endif namespace { @@ -160,6 +164,12 @@ std::string ReadFileOrDie(const char* filename) { exit(1); } +#if _WIN32 | _WIN64 + if (read_from_stdin) { + _setmode(_fileno(stdin), _O_BINARY); + } +#endif + std::string result; off_t buffer_size = 8192; @@ -196,6 +206,12 @@ void WriteFileOrDie(const char* filename, const std::string& contents) { perror("Can't open output file for writing"); exit(1); } + +#if _WIN32 | _WIN64 + if (write_to_stdout) { + _setmode(_fileno(stdout), _O_BINARY); + } +#endif if (fwrite(contents.data(), 1, contents.size(), f) != contents.size()) { perror("fwrite"); exit(1); @@ -204,6 +220,11 @@ void WriteFileOrDie(const char* filename, const std::string& contents) { perror("fclose"); exit(1); } +#if _WIN32 | _WIN64 + if (write_to_stdout) { + _setmode(_fileno(stdout), _O_TEXT); + } +#endif } void TerminateHandler() { From 58c34b1e5217f23e5627c28203f6d098e2b3661b Mon Sep 17 00:00:00 2001 From: jeiea Date: Thu, 29 Jun 2017 10:36:20 +0900 Subject: [PATCH 2/2] Fix exception occuring --- guetzli/guetzli.cc | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/guetzli/guetzli.cc b/guetzli/guetzli.cc index 423e056d..b1228da6 100644 --- a/guetzli/guetzli.cc +++ b/guetzli/guetzli.cc @@ -209,7 +209,7 @@ void WriteFileOrDie(const char* filename, const std::string& contents) { #if _WIN32 | _WIN64 if (write_to_stdout) { - _setmode(_fileno(stdout), _O_BINARY); + _setmode(_fileno(f), _O_BINARY); } #endif if (fwrite(contents.data(), 1, contents.size(), f) != contents.size()) { @@ -220,11 +220,6 @@ void WriteFileOrDie(const char* filename, const std::string& contents) { perror("fclose"); exit(1); } -#if _WIN32 | _WIN64 - if (write_to_stdout) { - _setmode(_fileno(stdout), _O_TEXT); - } -#endif } void TerminateHandler() {