Switch from std::[w]stringstream/std::cout to [W]StringBuilder/fwrite

Remove all references to the iostream and sstream headers from code in the
src directory.
This commit is contained in:
Ryan Prichard 2016-01-17 00:40:28 -06:00
parent cf76665cfe
commit ede3244c26
8 changed files with 70 additions and 71 deletions

View File

@ -26,7 +26,6 @@
#include <string.h>
#include <stdint.h>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
@ -34,6 +33,7 @@
#include "../shared/AgentMsg.h"
#include "../shared/Buffer.h"
#include "../shared/DebugClient.h"
#include "../shared/StringBuilder.h"
#include "../shared/WindowsSecurity.h"
#include "../shared/WinptyAssert.h"
#include "../shared/c99_snprintf.h"
@ -248,11 +248,11 @@ NamedPipe *Agent::connectToNamedPipe(const std::wstring &pipeName)
// Returns a new server named pipe. It has not yet been connected.
NamedPipe *Agent::makeDataPipe(bool write)
{
std::wstringstream nameSS;
nameSS << L"\\\\.\\pipe\\winpty-data-"
<< (write ? L"conout-" : L"conin-")
<< m_genRandom.uniqueName();
const auto name = nameSS.str();
const auto name =
(WStringBuilder(128)
<< L"\\\\.\\pipe\\winpty-data-"
<< (write ? L"conout-" : L"conin-")
<< m_genRandom.uniqueName()).str_moved();
const DWORD openMode =
(write ? PIPE_ACCESS_OUTBOUND : PIPE_ACCESS_INBOUND)
| FILE_FLAG_FIRST_PIPE_INSTANCE

View File

@ -24,7 +24,6 @@
#include <string.h>
#include <algorithm>
#include <sstream>
#include <string>
#include "DebugShowInput.h"
@ -32,12 +31,15 @@
#include "DsrSender.h"
#include "Win32Console.h"
#include "../shared/DebugClient.h"
#include "../shared/StringBuilder.h"
#include "../shared/UnixCtrlChars.h"
#ifndef MAPVK_VK_TO_VSC
#define MAPVK_VK_TO_VSC 0
#endif
using namespace winpty_shared;
namespace {
struct MouseRecord {
@ -49,14 +51,13 @@ struct MouseRecord {
};
std::string MouseRecord::toString() const {
std::stringstream ss;
ss << "pos=" << std::dec << coord.X << "," << coord.Y
<< " flags=0x"
<< std::hex << flags;
StringBuilder sb(40);
sb << "pos=" << coord.X << ',' << coord.Y
<< " flags=0x" << hexOfInt(flags);
if (release) {
ss << " release";
sb << " release";
}
return ss.str();
return sb.str_moved();
}
const unsigned int kIncompleteEscapeTimeoutMs = 1000u;

View File

@ -25,12 +25,13 @@
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <sstream>
#include "../shared/StringBuilder.h"
#include "InputMap.h"
using namespace winpty_shared;
namespace {
struct Flag {
@ -65,7 +66,7 @@ static const Flag kMouseEventFlags[] = {
{ MOUSE_WHEELED, "Wheel" },
};
static void writeFlags(std::ostream &out, DWORD flags,
static void writeFlags(StringBuilder &out, DWORD flags,
const char *remainderName,
const Flag *table, size_t tableSize,
char pre, char sep, char post) {
@ -75,9 +76,9 @@ static void writeFlags(std::ostream &out, DWORD flags,
const Flag &f = table[i];
if ((f.value & flags) == f.value) {
if (!wroteSomething && pre != '\0') {
out.put(pre);
out << pre;
} else if (wroteSomething && sep != '\0') {
out.put(sep);
out << sep;
}
out << f.text;
wroteSomething = true;
@ -86,23 +87,20 @@ static void writeFlags(std::ostream &out, DWORD flags,
}
if (remaining != 0) {
if (!wroteSomething && pre != '\0') {
out.put(pre);
out << pre;
} else if (wroteSomething && sep != '\0') {
out.put(sep);
out << sep;
}
std::ios oldState(NULL);
oldState.copyfmt(out);
out << std::hex << remainderName << "(0x" << remaining << ")";
out.copyfmt(oldState);
out << remainderName << "(0x" << hexOfInt(remaining) << ')';
wroteSomething = true;
}
if (wroteSomething && post != '\0') {
out.put(post);
out << post;
}
}
template <size_t n>
static void writeFlags(std::ostream &out, DWORD flags,
static void writeFlags(StringBuilder &out, DWORD flags,
const char *remainderName,
const Flag (&table)[n],
char pre, char sep, char post) {
@ -112,25 +110,25 @@ static void writeFlags(std::ostream &out, DWORD flags,
} // anonymous namespace
std::string controlKeyStatePrefix(DWORD controlKeyState) {
std::stringstream ss;
writeFlags(ss, controlKeyState,
StringBuilder sb;
writeFlags(sb, controlKeyState,
"keyState", kControlKeyStates, '\0', '-', '-');
return ss.str();
return sb.str_moved();
}
std::string mouseEventToString(const MOUSE_EVENT_RECORD &mer) {
const uint16_t buttons = mer.dwButtonState & 0xFFFF;
const int16_t wheel = mer.dwButtonState >> 16;
std::stringstream ss;
ss << std::dec << "pos=" << mer.dwMousePosition.X << ','
<< mer.dwMousePosition.Y;
writeFlags(ss, mer.dwControlKeyState, "keyState", kControlKeyStates, ' ', ' ', '\0');
writeFlags(ss, mer.dwEventFlags, "flags", kMouseEventFlags, ' ', ' ', '\0');
writeFlags(ss, buttons, "buttons", kButtonStates, ' ', ' ', '\0');
StringBuilder sb;
sb << "pos=" << mer.dwMousePosition.X << ','
<< mer.dwMousePosition.Y;
writeFlags(sb, mer.dwControlKeyState, "keyState", kControlKeyStates, ' ', ' ', '\0');
writeFlags(sb, mer.dwEventFlags, "flags", kMouseEventFlags, ' ', ' ', '\0');
writeFlags(sb, buttons, "buttons", kButtonStates, ' ', ' ', '\0');
if (wheel != 0) {
ss << " wheel=" << std::dec << wheel;
sb << " wheel=" << wheel;
}
return ss.str();
return sb.str_moved();
}
void debugShowInput(bool enableMouse) {
@ -162,7 +160,7 @@ void debugShowInput(bool enableMouse) {
bool finished = false;
while (!finished &&
ReadConsoleInputW(conin, records, 32, &actual) && actual >= 1) {
std::stringstream ss;
StringBuilder sb;
for (DWORD i = 0; i < actual; ++i) {
const INPUT_RECORD &record = records[i];
if (record.EventType == KEY_EVENT) {
@ -172,9 +170,9 @@ void debugShowInput(bool enableMouse) {
ker.uChar.UnicodeChar,
static_cast<uint16_t>(ker.dwControlKeyState),
};
ss << "key: " << (ker.bKeyDown ? "dn" : "up")
<< " rpt=" << std::dec << ker.wRepeatCount
<< " scn=" << std::dec << ker.wVirtualScanCode
sb << "key: " << (ker.bKeyDown ? "dn" : "up")
<< " rpt=" << ker.wRepeatCount
<< " scn=" << ker.wVirtualScanCode
<< ' ' << key.toString() << '\n';
if ((ker.dwControlKeyState & LEFT_CTRL_PRESSED) &&
ker.wVirtualKeyCode == 'D') {
@ -183,24 +181,26 @@ void debugShowInput(bool enableMouse) {
}
} else if (record.EventType == MOUSE_EVENT) {
const MOUSE_EVENT_RECORD &mer = record.Event.MouseEvent;
ss << "mouse: " << mouseEventToString(mer).c_str() << '\n';
sb << "mouse: " << mouseEventToString(mer) << '\n';
} else if (record.EventType == WINDOW_BUFFER_SIZE_EVENT) {
const WINDOW_BUFFER_SIZE_RECORD &wbsr =
record.Event.WindowBufferSizeEvent;
ss << "buffer-resized: dwSize=("
<< std::dec << wbsr.dwSize.X << ','
<< wbsr.dwSize.Y << ")\n";
sb << "buffer-resized: dwSize=("
<< wbsr.dwSize.X << ','
<< wbsr.dwSize.Y << ")\n";
} else if (record.EventType == MENU_EVENT) {
const MENU_EVENT_RECORD &mer = record.Event.MenuEvent;
ss << "menu-event: commandId=0x"
<< std::hex << mer.dwCommandId << '\n';
sb << "menu-event: commandId=0x"
<< hexOfInt(mer.dwCommandId) << '\n';
} else if (record.EventType == FOCUS_EVENT) {
const FOCUS_EVENT_RECORD &fer = record.Event.FocusEvent;
ss << "focus: " << (fer.bSetFocus ? "gained" : "lost") << '\n';
sb << "focus: " << (fer.bSetFocus ? "gained" : "lost") << '\n';
}
}
std::cout << ss.str();
std::cout.flush();
const auto str = sb.str_moved();
fwrite(str.data(), 1, str.size(), stdout);
fflush(stdout);
}
SetConsoleMode(conin, origConsoleMode);
}

View File

@ -29,7 +29,6 @@
#include <memory>
#include <new>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
@ -38,6 +37,7 @@
#include "../shared/Buffer.h"
#include "../shared/DebugClient.h"
#include "../shared/GenRandom.h"
#include "../shared/StringBuilder.h"
#include "../shared/WindowsSecurity.h"
#include "BackgroundDesktop.h"
#include "Util.h"
@ -351,12 +351,12 @@ static bool shouldShowConsoleWindow() {
static OwnedHandle startAgentProcess(const std::wstring &desktopName,
const std::wstring &controlPipeName,
DWORD flags, int cols, int rows) {
std::wstring exePath = findAgentProgram();
std::wstringstream cmdlineStream;
cmdlineStream << L"\"" << exePath << L"\" "
<< controlPipeName << " "
<< flags << " " << cols << " " << rows;
std::wstring cmdline = cmdlineStream.str();
const std::wstring exePath = findAgentProgram();
const std::wstring cmdline =
(WStringBuilder(256)
<< L"\"" << exePath << L"\" "
<< controlPipeName << L' '
<< flags << L' ' << cols << L' ' << rows).str_moved();
// Start the agent.
auto desktopNameM = modifiableWString(desktopName);
@ -381,10 +381,10 @@ static OwnedHandle startAgentProcess(const std::wstring &desktopName,
&sui, &pi);
if (!success) {
const DWORD lastError = GetLastError();
std::wstringstream ss;
ss << "winpty-agent CreateProcess failed: cmdline='" << cmdline
<< "' err=0x" << std::hex << lastError;
auto errStr = ss.str();
const auto errStr =
(WStringBuilder(256)
<< L"winpty-agent CreateProcess failed: cmdline='" << cmdline
<< L"' err=0x" << whexOfInt(lastError)).str_moved();
trace("%ls", errStr.c_str());
throwWinptyException(WINPTY_ERROR_AGENT_CREATION_FAILED, errStr);
}

View File

@ -26,7 +26,6 @@
#include <string.h>
#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>

View File

@ -23,9 +23,8 @@
#include <stdint.h>
#include <string.h>
#include <sstream>
#include "DebugClient.h"
#include "StringBuilder.h"
namespace winpty_shared {
@ -120,19 +119,18 @@ std::wstring GenRandom::uniqueName() {
GetSystemTimeAsFileTime(&monotonicTime);
uint64_t monotonicTime64;
memcpy(&monotonicTime64, &monotonicTime, sizeof(uint64_t));
std::wstringstream ss;
ss << GetCurrentProcessId()
WStringBuilder sb(64);
sb << GetCurrentProcessId()
<< L'-' << InterlockedIncrement(&g_pipeCounter)
<< std::hex
<< L'-' << monotonicTime64;
<< L'-' << whexOfInt(monotonicTime64);
// It isn't clear to me how the crypto APIs would fail. It *probably*
// doesn't matter that much anyway? In principle, a predictable pipe name
// is subject to a local denial-of-service attack.
auto random = randomHexString(12);
if (!random.empty()) {
ss << '-' << random;
sb << L'-' << random;
}
return ss.str();
return sb.str_moved();
}
} // winpty_shared namespace

View File

@ -24,7 +24,6 @@
#include <string.h>
#include <array>
#include <sstream>
#include "DebugClient.h"
#include "OsModule.h"

View File

@ -77,6 +77,7 @@
'shared/GenRandom.h',
'shared/GenRandom.cc',
'shared/OsModule.h',
'shared/StringBuilder.h',
'shared/UnixCtrlChars.h',
'shared/WindowsSecurity.h',
'shared/WindowsSecurity.cc',
@ -114,6 +115,7 @@
'shared/DebugClient.cc',
'shared/GenRandom.h',
'shared/GenRandom.cc',
'shared/StringBuilder.h',
'shared/WindowsSecurity.h',
'shared/WindowsSecurity.cc',
'shared/c99_snprintf.h',