summaryrefslogtreecommitdiffstats
path: root/osdep
diff options
context:
space:
mode:
authorMartin Herkt <lachs0r@srsfckn.biz>2013-12-28 04:44:12 +0100
committerMartin Herkt <lachs0r@srsfckn.biz>2014-01-02 05:30:49 +0100
commitfd89a7598820f5b5a7a4b7029e31dd100cefbb96 (patch)
tree7db3cbec05acd535693affb8be0c2cc2298b8976 /osdep
parentb04b48fc10dabb1d1b26ff519f55dcafb320985e (diff)
downloadmpv-fd89a7598820f5b5a7a4b7029e31dd100cefbb96.tar.bz2
mpv-fd89a7598820f5b5a7a4b7029e31dd100cefbb96.tar.xz
osdep/io, mp_vfprintf: split out console detection
Diffstat (limited to 'osdep')
-rw-r--r--osdep/io.c85
1 files changed, 43 insertions, 42 deletions
diff --git a/osdep/io.c b/osdep/io.c
index 19f0be1108..2e2603eb7f 100644
--- a/osdep/io.c
+++ b/osdep/io.c
@@ -113,56 +113,57 @@ int mp_stat(const char *path, struct stat *buf)
return res;
}
-static int mp_vfprintf(FILE *stream, const char *format, va_list args)
+static int mp_check_console(HANDLE *wstream)
{
- int done = 0;
+ if (wstream != INVALID_HANDLE_VALUE) {
+ unsigned int filetype = GetFileType(wstream);
- if (stream == stdout || stream == stderr)
- {
- HANDLE *wstream = GetStdHandle(stream == stdout ?
- STD_OUTPUT_HANDLE : STD_ERROR_HANDLE);
- if (wstream != INVALID_HANDLE_VALUE)
+ if (!((filetype == FILE_TYPE_UNKNOWN) &&
+ (GetLastError() != ERROR_SUCCESS)))
{
- // figure out whether we're writing to a console
- unsigned int filetype = GetFileType(wstream);
- if (!((filetype == FILE_TYPE_UNKNOWN) &&
- (GetLastError() != ERROR_SUCCESS)))
- {
- int isConsole;
- filetype &= ~(FILE_TYPE_REMOTE);
- if (filetype == FILE_TYPE_CHAR)
- {
- DWORD ConsoleMode;
- int ret = GetConsoleMode(wstream, &ConsoleMode);
- if (!ret && (GetLastError() == ERROR_INVALID_HANDLE))
- isConsole = 0;
- else
- isConsole = 1;
- }
- else
- isConsole = 0;
-
- if (isConsole)
- {
- int nchars = vsnprintf(NULL, 0, format, args) + 1;
- char *buf = talloc_array(NULL, char, nchars);
- if (buf)
- {
- vsnprintf(buf, nchars, format, args);
- wchar_t *out = mp_from_utf8(NULL, buf);
- size_t nchars = wcslen(out);
- talloc_free(buf);
- done = WriteConsoleW(wstream, out, nchars, NULL, NULL);
- talloc_free(out);
- }
+ filetype &= ~(FILE_TYPE_REMOTE);
+
+ if (filetype == FILE_TYPE_CHAR) {
+ DWORD ConsoleMode;
+ int ret = GetConsoleMode(wstream, &ConsoleMode);
+
+ if (!(!ret && (GetLastError() == ERROR_INVALID_HANDLE))) {
+ // This seems to be a console
+ return 1;
}
- else
- done = vfprintf(stream, format, args);
}
}
}
- else
+
+ return 0;
+}
+
+static int mp_vfprintf(FILE *stream, const char *format, va_list args)
+{
+ int done = 0;
+
+ HANDLE *wstream = INVALID_HANDLE_VALUE;
+
+ if (stream == stdout || stream == stderr) {
+ wstream = GetStdHandle(stream == stdout ?
+ STD_OUTPUT_HANDLE : STD_ERROR_HANDLE);
+ }
+
+ if (mp_check_console(wstream)) {
+ size_t len = vsnprintf(NULL, 0, format, args) + 1;
+ char *buf = talloc_array(NULL, char, len);
+
+ if (buf) {
+ vsnprintf(buf, len, format, args);
+ wchar_t *out = mp_from_utf8(NULL, buf);
+ size_t len = wcslen(out);
+ talloc_free(buf);
+ done = WriteConsoleW(wstream, out, len, NULL, NULL);
+ talloc_free(out);
+ }
+ } else {
done = vfprintf(stream, format, args);
+ }
return done;
}