installed pty
[VSoRC/.git] / node_modules / node-pty / deps / winpty / src / agent / LargeConsoleRead.cc
1 // Copyright (c) 2015 Ryan Prichard
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to
5 // deal in the Software without restriction, including without limitation the
6 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7 // sell copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19 // IN THE SOFTWARE.
20
21 #include "LargeConsoleRead.h"
22
23 #include <stdlib.h>
24
25 #include "../shared/WindowsVersion.h"
26 #include "Scraper.h"
27 #include "Win32ConsoleBuffer.h"
28
29 LargeConsoleReadBuffer::LargeConsoleReadBuffer() :
30     m_rect(0, 0, 0, 0), m_rectWidth(0)
31 {
32 }
33
34 void largeConsoleRead(LargeConsoleReadBuffer &out,
35                       Win32ConsoleBuffer &buffer,
36                       const SmallRect &readArea,
37                       WORD attributesMask) {
38     ASSERT(readArea.Left >= 0 &&
39            readArea.Top >= 0 &&
40            readArea.Right >= readArea.Left &&
41            readArea.Bottom >= readArea.Top &&
42            readArea.width() <= MAX_CONSOLE_WIDTH);
43     const size_t count = readArea.width() * readArea.height();
44     if (out.m_data.size() < count) {
45         out.m_data.resize(count);
46     }
47     out.m_rect = readArea;
48     out.m_rectWidth = readArea.width();
49
50     static const bool useLargeReads = isAtLeastWindows8();
51     if (useLargeReads) {
52         buffer.read(readArea, out.m_data.data());
53     } else {
54         const int maxReadLines = std::max(1, MAX_CONSOLE_WIDTH / readArea.width());
55         int curLine = readArea.Top;
56         while (curLine <= readArea.Bottom) {
57             const SmallRect subReadArea(
58                 readArea.Left,
59                 curLine,
60                 readArea.width(),
61                 std::min(maxReadLines, readArea.Bottom + 1 - curLine));
62             buffer.read(subReadArea, out.lineDataMut(curLine));
63             curLine = subReadArea.Bottom + 1;
64         }
65     }
66     if (attributesMask != static_cast<WORD>(~0)) {
67         for (size_t i = 0; i < count; ++i) {
68             out.m_data[i].Attributes &= attributesMask;
69         }
70     }
71 }