VirtualBox

source: kBuild/trunk/src/lib/maybe_con_fwrite.c@ 2940

Last change on this file since 2940 was 2906, checked in by bird, 8 years ago

More stdout console buffering tricks on windows.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.2 KB
Line 
1/* $Id: maybe_con_fwrite.c 2906 2016-09-09 22:15:57Z bird $ */
2/** @file
3 * maybe_con_write - Optimized console output on windows.
4 */
5
6/*
7 * Copyright (c) 2016 knut st. osmundsen <[email protected]>
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25 * IN THE SOFTWARE.
26 *
27 * Alternatively, the content of this file may be used under the terms of the
28 * GPL version 2 or later, or LGPL version 2.1 or later.
29 */
30
31
32/*********************************************************************************************************************************
33* Header Files *
34*********************************************************************************************************************************/
35#ifdef KBUILD_OS_WINDOWS
36# include <windows.h>
37#endif
38#include <errno.h>
39#include <stdio.h>
40#ifdef _MSC_VER
41# include <io.h>
42# include <conio.h>
43#endif
44
45
46
47/**
48 * Drop-in fwrite replacement for optimizing console output on windows.
49 *
50 *
51 * @returns Units written; 0 & errno on failure.
52 * @param pvBuf What to write.
53 * @param cbUnit How much to write in each unit.
54 * @param cUnits How many units to write.
55 * @param pFile The file to write to.
56 */
57size_t maybe_con_fwrite(void const *pvBuf, size_t cbUnit, size_t cUnits, FILE *pFile)
58{
59#ifdef KBUILD_OS_WINDOWS
60 /*
61 * If it's a TTY, do our own conversion to wide char and
62 * call WriteConsoleW directly.
63 */
64 if ( cbUnit > 0
65 && cUnits > 0
66 && (pFile == stdout || pFile == stderr))
67 {
68 int fd = fileno(pFile);
69 if (fd >= 0)
70 {
71 if (isatty(fd))
72 {
73 HANDLE hCon = (HANDLE)_get_osfhandle(fd);
74 if ( hCon != INVALID_HANDLE_VALUE
75 && hCon != NULL)
76 {
77 size_t cbToWrite = cbUnit * cUnits;
78 size_t cwcTmp = cbToWrite * 2 + 16;
79 wchar_t *pawcTmp = (wchar_t *)malloc(cwcTmp * sizeof(wchar_t));
80 if (pawcTmp)
81 {
82 int cwcToWrite;
83 static UINT s_uConsoleCp = 0;
84 if (s_uConsoleCp == 0)
85 s_uConsoleCp = GetConsoleCP();
86
87 cwcToWrite = MultiByteToWideChar(s_uConsoleCp, 0 /*dwFlags*/, pvBuf, (int)cbToWrite, pawcTmp, (int)(cwcTmp - 1));
88 if (cwcToWrite > 0)
89 {
90 int rc;
91 pawcTmp[cwcToWrite] = '\0';
92
93 /* Let the CRT do the rest. At least the Visual C++ 2010 CRT
94 sources indicates _cputws will do the right thing we want. */
95 fflush(pFile);
96 rc = _cputws(pawcTmp);
97 free(pawcTmp);
98 if (rc >= 0)
99 return cUnits;
100 return 0;
101 }
102 free(pawcTmp);
103 }
104 }
105 }
106 }
107 }
108#endif
109
110 /*
111 * Semi regular write handling.
112 */
113 return fwrite(pvBuf, cbUnit, cUnits, pFile);
114}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette