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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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