- Notifications
You must be signed in to change notification settings - Fork 8.5k
/
Copy pathmisc.cpp
83 lines (70 loc) · 2.78 KB
/
misc.cpp
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
#include"precomp.h"
#include"misc.h"
#include<til/unicode.h>
#include"dbcs.h"
#include"../types/inc/GlyphWidth.hpp"
#include"../interactivity/inc/ServiceLocator.hpp"
#pragma hdrstop
#defineCHAR_NULL ((char)0)
using Microsoft::Console::Interactivity::ServiceLocator;
WCHAR CharToWchar(_In_reads_(cch) const char* const pch, const UINT cch)
{
constauto& gci = ServiceLocator::LocateGlobals().getConsoleInformation();
auto wc = L'\0';
FAIL_FAST_IF(!(IsDBCSLeadByteConsole(*pch, &gci.OutputCPInfo) || cch == 1));
ConvertOutputToUnicode(gci.OutputCP, pch, cch, &wc, 1);
return wc;
}
voidSetConsoleCPInfo(const BOOL fOutput)
{
auto& gci = ServiceLocator::LocateGlobals().getConsoleInformation();
if (fOutput)
{
if (!GetCPInfo(gci.OutputCP, &gci.OutputCPInfo))
{
gci.OutputCPInfo.LeadByte[0] = 0;
}
}
else
{
if (!GetCPInfo(gci.CP, &gci.CPInfo))
{
gci.CPInfo.LeadByte[0] = 0;
}
}
}
// Routine Description:
// - Converts unicode characters to ANSI given a destination codepage
// Arguments:
// - uiCodePage - codepage for use in conversion
// - pwchSource - unicode string to convert
// - cchSource - length of pwchSource in characters
// - pchTarget - pointer to destination buffer to receive converted ANSI string
// - cchTarget - size of destination buffer in characters
// Return Value:
// - Returns the number characters written to pchTarget, or 0 on failure
intConvertToOem(const UINT uiCodePage,
_In_reads_(cchSource) const WCHAR* const pwchSource,
const UINT cchSource,
_Out_writes_(cchTarget) CHAR* const pchTarget,
const UINT cchTarget) noexcept
{
FAIL_FAST_IF(!(pwchSource != (LPWSTR)pchTarget));
// clang-format off
#pragma prefast(suppress: __WARNING_W2A_BEST_FIT, "WC_NO_BEST_FIT_CHARS doesn't work in many codepages. Retain old behavior.")
// clang-format on
returnLOG_IF_WIN32_BOOL_FALSE(WideCharToMultiByte(uiCodePage, 0, pwchSource, cchSource, pchTarget, cchTarget, nullptr, nullptr));
}
// Output data is always translated via the ansi codepage so glyph translation works.
intConvertOutputToUnicode(_In_ UINT uiCodePage,
_In_reads_(cchSource) const CHAR* const pchSource,
_In_ UINT cchSource,
_Out_writes_(cchTarget) WCHAR* pwchTarget,
_In_ UINT cchTarget) noexcept
{
FAIL_FAST_IF(!(cchTarget > 0));
pwchTarget[0] = L'\0';
returnMultiByteToWideChar(uiCodePage, MB_USEGLYPHCHARS, pchSource, cchSource, pwchTarget, cchTarget);
}