From 5927e15bc78cc13134672ebfb83e3b6143204cae Mon Sep 17 00:00:00 2001 From: James Zern Date: Fri, 29 Aug 2014 19:07:17 -0700 Subject: [PATCH] example_util: add ExUtilSetBinaryMode use it in dwebp when dealing with 'stdout' Change-Id: I8b8a0b0de9e73731e913ac3c83b5e2b33c693175 --- examples/dwebp.c | 14 +------------- examples/example_util.c | 16 +++++++++++++++- examples/example_util.h | 5 +++++ 3 files changed, 21 insertions(+), 14 deletions(-) diff --git a/examples/dwebp.c b/examples/dwebp.c index a37b05ed..fe80dfe3 100644 --- a/examples/dwebp.c +++ b/examples/dwebp.c @@ -38,11 +38,6 @@ #include #endif -#if defined(_WIN32) -#include // for _O_BINARY -#include // for _setmode() -#endif - #include "webp/decode.h" #include "./example_util.h" #include "./stopwatch.h" @@ -482,15 +477,8 @@ static int SaveOutput(const WebPDecBuffer* const buffer, needs_open_file = (format != PNG); #endif -#if defined(_WIN32) - if (use_stdout && _setmode(_fileno(stdout), _O_BINARY) == -1) { - fprintf(stderr, "Failed to reopen stdout in O_BINARY mode.\n"); - return -1; - } -#endif - if (needs_open_file) { - fout = use_stdout ? stdout : fopen(out_file, "wb"); + fout = use_stdout ? ExUtilSetBinaryMode(stdout) : fopen(out_file, "wb"); if (fout == NULL) { fprintf(stderr, "Error opening output file %s\n", out_file); return 0; diff --git a/examples/example_util.c b/examples/example_util.c index b77b816a..4443f83d 100644 --- a/examples/example_util.c +++ b/examples/example_util.c @@ -11,6 +11,11 @@ // #include "./example_util.h" + +#if defined(_WIN32) +#include // for _O_BINARY +#include // for _setmode() +#endif #include #include #include @@ -21,9 +26,18 @@ // ----------------------------------------------------------------------------- // File I/O -static const size_t kBlockSize = 16384; // default initial size +FILE* ExUtilSetBinaryMode(FILE* file) { +#if defined(_WIN32) + if (_setmode(_fileno(file), _O_BINARY) == -1) { + fprintf(stderr, "Failed to reopen file in O_BINARY mode.\n"); + return NULL; + } +#endif + return file; +} int ExUtilReadFromStdin(const uint8_t** data, size_t* data_size) { + static const size_t kBlockSize = 16384; // default initial size size_t max_size = 0; size_t size = 0; uint8_t* input = NULL; diff --git a/examples/example_util.h b/examples/example_util.h index bf90c35d..4ef703da 100644 --- a/examples/example_util.h +++ b/examples/example_util.h @@ -13,12 +13,17 @@ #ifndef WEBP_EXAMPLES_EXAMPLE_UTIL_H_ #define WEBP_EXAMPLES_EXAMPLE_UTIL_H_ +#include #include "webp/decode.h" #ifdef __cplusplus extern "C" { #endif +// Reopen file in binary (O_BINARY) mode. +// Returns 'file' on success, NULL otherwise. +FILE* ExUtilSetBinaryMode(FILE* file); + // Allocates storage for entire file 'file_name' and returns contents and size // in 'data' and 'data_size'. Returns 1 on success, 0 otherwise. '*data' should // be deleted using free().