TODO: Optimize - combine ReadConsoleData/ReadConsoleInfo
| 3582 | |
| 3583 | // TODO: Optimize - combine ReadConsoleData/ReadConsoleInfo |
| 3584 | void static CorrectDBCSCursorPosition(HANDLE ahConOut, CONSOLE_SCREEN_BUFFER_INFO& csbi) |
| 3585 | { |
| 3586 | if (csbi.dwSize.X <= 0 || csbi.dwCursorPosition.X <= 0) |
| 3587 | return; |
| 3588 | |
| 3589 | // Issue 577: Chinese display error on Chinese |
| 3590 | // -- GetConsoleScreenBufferInfo returns DBCS cursor coordinates, but we require wchar_t !!! |
| 3591 | if (!IsConsoleDoubleCellCP() |
| 3592 | // gh-1057: In NON DBCS systems there are cursor problems too (Win10 stable build 15063) |
| 3593 | && !NeedLegacyCursorCorrection()) |
| 3594 | return; |
| 3595 | |
| 3596 | // The correction process |
| 3597 | { |
| 3598 | CHAR Chars[200]; |
| 3599 | LONG cchMax = countof(Chars); |
| 3600 | LPSTR pChars = (csbi.dwCursorPosition.X <= cchMax) ? Chars : (LPSTR)calloc(csbi.dwCursorPosition.X, sizeof(*pChars)); |
| 3601 | if (pChars) |
| 3602 | cchMax = csbi.dwCursorPosition.X; |
| 3603 | else |
| 3604 | pChars = Chars; // memory allocation fail? try part of line? |
| 3605 | COORD crRead = {0, csbi.dwCursorPosition.Y}; |
| 3606 | // gh-1501: Windows 10 CJK version set COMMON_LVB_*_BYTE even for SOME non-ASCII characters (e.g. Greek "Α", Russian "Я", etc.) |
| 3607 | #if 0 |
| 3608 | //120830 - DBCS uses 2 cells per hieroglyph |
| 3609 | // TODO: Optimize |
| 3610 | DWORD nRead = 0; |
| 3611 | BOOL bRead = NeedLegacyCursorCorrection() ? FALSE : ReadConsoleOutputCharacterA(ahConOut, pChars, cchMax, crRead, &nRead); |
| 3612 | // ReadConsoleOutputCharacterA may return "???" instead of DBCS codes in Non-DBCS systems |
| 3613 | if (bRead && (nRead == cchMax)) |
| 3614 | { |
| 3615 | _ASSERTE(nRead==cchMax); |
| 3616 | int nXShift = 0; |
| 3617 | LPSTR p = pChars, pEnd = pChars+nRead; |
| 3618 | while (p < pEnd) |
| 3619 | { |
| 3620 | if (IsDBCSLeadByte(*p)) |
| 3621 | { |
| 3622 | nXShift++; |
| 3623 | p++; |
| 3624 | _ASSERTE(p < pEnd); |
| 3625 | } |
| 3626 | p++; |
| 3627 | } |
| 3628 | _ASSERTE(nXShift <= csbi.dwCursorPosition.X); |
| 3629 | csbi.dwCursorPosition.X = std::max(0,(csbi.dwCursorPosition.X - nXShift)); |
| 3630 | } |
| 3631 | else if (IsWin10() && (NeedLegacyCursorCorrection() || (GetConsoleOutputCP() == CP_UTF8))) |
| 3632 | #endif |
| 3633 | { |
| 3634 | // Temporary workaround for conhost bug! |
| 3635 | CHAR_INFO CharsEx[200]; |
| 3636 | CHAR_INFO* pCharsEx = (cchMax <= countof(CharsEx)) ? CharsEx |
| 3637 | : (CHAR_INFO*)calloc(cchMax, sizeof(*pCharsEx)); |
| 3638 | if (pCharsEx) |
| 3639 | { |
| 3640 | COORD bufSize = {MakeShort(cchMax), 1}; COORD bufCoord = {0,0}; |
| 3641 | SMALL_RECT rgn = MakeSmallRect(0, csbi.dwCursorPosition.Y, cchMax-1, csbi.dwCursorPosition.Y); |
no test coverage detected