From a72857df0314e891c3bd48145493b465d9131a82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20=C5=9Aliwak?= Date: Wed, 16 Jun 2021 21:03:07 +0200 Subject: [PATCH] CommonIO: Replace readStandardInput() with readUntilEnd() with a configurable stream --- libsolutil/CommonIO.cpp | 7 +++---- libsolutil/CommonIO.h | 9 +++++---- solc/CommandLineInterface.cpp | 4 ++-- test/tools/afl_fuzzer.cpp | 2 +- test/tools/yulrun.cpp | 2 +- 5 files changed, 12 insertions(+), 12 deletions(-) diff --git a/libsolutil/CommonIO.cpp b/libsolutil/CommonIO.cpp index 3bab3657e..c87f7ceab 100644 --- a/libsolutil/CommonIO.cpp +++ b/libsolutil/CommonIO.cpp @@ -25,7 +25,6 @@ #include -#include #include #if defined(_WIN32) #include @@ -75,14 +74,14 @@ string solidity::util::readFileAsString(string const& _file) return readFile(_file); } -string solidity::util::readStandardInput() +string solidity::util::readUntilEnd(istream& _stdin) { string ret; - while (!cin.eof()) + while (!_stdin.eof()) { string tmp; // NOTE: this will read until EOF or NL - getline(cin, tmp); + getline(_stdin, tmp); ret.append(tmp); ret.append("\n"); } diff --git a/libsolutil/CommonIO.h b/libsolutil/CommonIO.h index 8538f0aa2..4288a8ea8 100644 --- a/libsolutil/CommonIO.h +++ b/libsolutil/CommonIO.h @@ -25,22 +25,23 @@ #pragma once #include +#include #include #include 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 exists but is not a regular file, it will throw NotAFile exception. /// If the file is empty, returns an empty string. std::string readFileAsString(std::string const& _file); -/// Retrieve and returns the contents of standard input (until EOF). -std::string readStandardInput(); +/// Retrieves and returns the whole content of the specified input stream (until EOF). +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(); /// Converts arbitrary value to string representation using std::stringstream. diff --git a/solc/CommandLineInterface.cpp b/solc/CommandLineInterface.cpp index a22d711f9..b98ce1b85 100644 --- a/solc/CommandLineInterface.cpp +++ b/solc/CommandLineInterface.cpp @@ -640,7 +640,7 @@ bool CommandLineInterface::readInputFilesAndConfigureRemappings() } if (addStdin) - m_fileReader.setSource(g_stdinFileName, readStandardInput()); + m_fileReader.setSource(g_stdinFileName, readUntilEnd(cin)); if (m_fileReader.sourceCodes().size() == 0) { @@ -1255,7 +1255,7 @@ bool CommandLineInterface::processInput() } string input; if (jsonFile.empty()) - input = readStandardInput(); + input = readUntilEnd(cin); else { try diff --git a/test/tools/afl_fuzzer.cpp b/test/tools/afl_fuzzer.cpp index ea4a5a828..af0939c2b 100644 --- a/test/tools/afl_fuzzer.cpp +++ b/test/tools/afl_fuzzer.cpp @@ -114,7 +114,7 @@ Allowed options)", { string input; if (inputFile.size() == 0) - input = readStandardInput(); + input = readUntilEnd(cin); else input = readFileAsString(inputFile); diff --git a/test/tools/yulrun.cpp b/test/tools/yulrun.cpp index 7ff7cfc9e..b5f8d60dd 100644 --- a/test/tools/yulrun.cpp +++ b/test/tools/yulrun.cpp @@ -155,7 +155,7 @@ Allowed options)", } } else - input = readStandardInput(); + input = readUntilEnd(cin); interpret(input); }