CommonIO: Replace readStandardInput() with readUntilEnd() with a configurable stream

This commit is contained in:
Kamil Śliwak 2021-06-16 21:03:07 +02:00
parent 98e1dee45f
commit a72857df03
5 changed files with 12 additions and 12 deletions

View File

@ -25,7 +25,6 @@
#include <boost/filesystem.hpp> #include <boost/filesystem.hpp>
#include <iostream>
#include <fstream> #include <fstream>
#if defined(_WIN32) #if defined(_WIN32)
#include <windows.h> #include <windows.h>
@ -75,14 +74,14 @@ string solidity::util::readFileAsString(string const& _file)
return readFile<string>(_file); return readFile<string>(_file);
} }
string solidity::util::readStandardInput() string solidity::util::readUntilEnd(istream& _stdin)
{ {
string ret; string ret;
while (!cin.eof()) while (!_stdin.eof())
{ {
string tmp; string tmp;
// NOTE: this will read until EOF or NL // NOTE: this will read until EOF or NL
getline(cin, tmp); getline(_stdin, tmp);
ret.append(tmp); ret.append(tmp);
ret.append("\n"); ret.append("\n");
} }

View File

@ -25,22 +25,23 @@
#pragma once #pragma once
#include <libsolutil/Common.h> #include <libsolutil/Common.h>
#include <iostream>
#include <sstream> #include <sstream>
#include <string> #include <string>
namespace solidity::util namespace solidity::util
{ {
/// Retrieve and returns the contents of the given file as a std::string. /// Retrieves and returns the contents of the given file as a std::string.
/// If the file doesn't exist, it will throw a FileNotFound exception. /// If the file doesn't exist, it will throw a FileNotFound exception.
/// If the file exists but is not a regular file, it will throw NotAFile exception. /// If the file exists but is not a regular file, it will throw NotAFile exception.
/// If the file is empty, returns an empty string. /// If the file is empty, returns an empty string.
std::string readFileAsString(std::string const& _file); std::string readFileAsString(std::string const& _file);
/// Retrieve and returns the contents of standard input (until EOF). /// Retrieves and returns the whole content of the specified input stream (until EOF).
std::string readStandardInput(); std::string readUntilEnd(std::istream& _stdin);
/// Retrieve and returns a character from standard input (without waiting for EOL). /// Retrieves and returns a character from standard input (without waiting for EOL).
int readStandardInputChar(); int readStandardInputChar();
/// Converts arbitrary value to string representation using std::stringstream. /// Converts arbitrary value to string representation using std::stringstream.

View File

@ -640,7 +640,7 @@ bool CommandLineInterface::readInputFilesAndConfigureRemappings()
} }
if (addStdin) if (addStdin)
m_fileReader.setSource(g_stdinFileName, readStandardInput()); m_fileReader.setSource(g_stdinFileName, readUntilEnd(cin));
if (m_fileReader.sourceCodes().size() == 0) if (m_fileReader.sourceCodes().size() == 0)
{ {
@ -1255,7 +1255,7 @@ bool CommandLineInterface::processInput()
} }
string input; string input;
if (jsonFile.empty()) if (jsonFile.empty())
input = readStandardInput(); input = readUntilEnd(cin);
else else
{ {
try try

View File

@ -114,7 +114,7 @@ Allowed options)",
{ {
string input; string input;
if (inputFile.size() == 0) if (inputFile.size() == 0)
input = readStandardInput(); input = readUntilEnd(cin);
else else
input = readFileAsString(inputFile); input = readFileAsString(inputFile);

View File

@ -155,7 +155,7 @@ Allowed options)",
} }
} }
else else
input = readStandardInput(); input = readUntilEnd(cin);
interpret(input); interpret(input);
} }