File size: 1,628 Bytes
90219c5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | //
// cputws.cpp
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Defines _cputws(), which writes a wide string directly to the console.
//
#include <conio.h>
#include <errno.h>
#include <corecrt_internal_lowio.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
// Writes the given string directly to the console. No newline is appended.
//
// Returns 0 on success; nonzero on failure.
extern "C" int __cdecl _cputws(wchar_t const* string)
{
_VALIDATE_CLEAR_OSSERR_RETURN((string != nullptr), EINVAL, -1);
if (__dcrt_lowio_ensure_console_output_initialized() == FALSE)
return -1;
// Write string to console file handle:
ptrdiff_t length = wcslen(string);
__acrt_lock(__acrt_conio_lock);
int result = 0;
__try
{
while (length > 0)
{
static size_t const max_write_bytes = 65535;
static size_t const max_write_wchars = max_write_bytes / sizeof(wchar_t);
DWORD const wchars_to_write = length > max_write_wchars
? max_write_wchars
: static_cast<DWORD>(length);
DWORD wchars_written;
if (__dcrt_write_console(
string,
wchars_to_write,
&wchars_written) == FALSE)
{
result = -1;
__leave;
}
string += wchars_to_write;
length -= wchars_to_write;
}
}
__finally
{
__acrt_unlock(__acrt_conio_lock);
}
return result;
}
|