examples: Change stdin and stdout to binary mode

On windows text mode for these plays with newline escape sequences, breaking the ContextStream encoding.

Fixes: #12
This commit is contained in:
Ben Clayton 2020-01-17 19:01:13 +00:00 committed by Ben Clayton
parent 96b25aafe0
commit ced82a0501

View File

@ -24,11 +24,20 @@
#include <mutex>
#include <unordered_set>
#ifdef _MSC_VER
#define OS_WINDOWS 1
#endif
// Uncomment the line below and change <path-to-log-file> to a file path to
// write all DAP communications to the given path.
//
// #define LOG_TO_FILE "<path-to-log-file>"
#ifdef OS_WINDOWS
#include <fcntl.h> // _O_BINARY
#include <io.h> // _setmode
#endif // OS_WINDOWS
namespace {
// sourceContent holds the synthetic file source.
@ -148,6 +157,13 @@ void Event::fire() {
// main() entry point to the DAP server.
int main(int, char* []) {
#ifdef OS_WINDOWS
// Change stdin & stdout from text mode to binary mode.
// This ensures sequences of \r\n are not changed to \n.
_setmode(_fileno(stdin), _O_BINARY);
_setmode(_fileno(stdout), _O_BINARY);
#endif // OS_WINDOWS
std::shared_ptr<dap::Writer> log;
#ifdef LOG_TO_FILE
log = dap::file(LOG_TO_FILE);