Make reading input in wrapper more robust.
We read the size of the message now instead of getting everything between quotes.
This commit is contained in:
parent
d746b8c266
commit
7ac7bd2edb
|
@ -19,6 +19,7 @@
|
||||||
#include <experimental/filesystem>
|
#include <experimental/filesystem>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
|
#include <cstring>
|
||||||
#include <sys/wait.h>
|
#include <sys/wait.h>
|
||||||
|
|
||||||
using std::string;
|
using std::string;
|
||||||
|
@ -31,36 +32,34 @@ namespace fs = std::experimental::filesystem;
|
||||||
|
|
||||||
const string read_input()
|
const string read_input()
|
||||||
{
|
{
|
||||||
|
// Read message length.
|
||||||
|
uint32_t length;
|
||||||
|
char buffer[4];
|
||||||
|
|
||||||
|
cin.read(buffer, sizeof(uint32_t));
|
||||||
|
std::memcpy(&length, buffer, 4);
|
||||||
|
|
||||||
|
// Ignore quotes.
|
||||||
|
length -= 2;
|
||||||
|
cin.ignore(1);
|
||||||
|
|
||||||
|
// Read message.
|
||||||
string input;
|
string input;
|
||||||
char c;
|
char c;
|
||||||
|
|
||||||
bool start = false;
|
for (; length > 0; --length)
|
||||||
while (cin.read(&c, 1).good())
|
|
||||||
{
|
{
|
||||||
if (!start)
|
cin.read(&c, 1);
|
||||||
{
|
input += c;
|
||||||
if (c == '"')
|
|
||||||
{
|
|
||||||
start = true;
|
|
||||||
}
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (c != '"')
|
|
||||||
{
|
|
||||||
input += c;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return input;
|
return input;
|
||||||
}
|
}
|
||||||
|
|
||||||
void send_message(const string &message)
|
void send_message(const string &message)
|
||||||
{
|
{
|
||||||
uint32_t length = message.length() + 2;
|
uint32_t length = message.length() + 2;
|
||||||
cout.write(reinterpret_cast<const char*>(&length), sizeof(length));
|
cout.write(reinterpret_cast<const char*>(&length), sizeof(uint32_t));
|
||||||
cout << '"' << message << '"';
|
cout << '"' << message << '"';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue