From 6f2865228cb02f0ba0b58990a9d3006dbe2692c6 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Wed, 18 Oct 2017 12:54:47 +0100 Subject: Add readStandardInput helper --- libdevcore/CommonIO.cpp | 14 ++++++++++++++ libdevcore/CommonIO.h | 3 +++ 2 files changed, 17 insertions(+) (limited to 'libdevcore') diff --git a/libdevcore/CommonIO.cpp b/libdevcore/CommonIO.cpp index eb7af83e..8c7e08f6 100644 --- a/libdevcore/CommonIO.cpp +++ b/libdevcore/CommonIO.cpp @@ -66,6 +66,20 @@ string dev::readFileAsString(string const& _file) return readFile(_file); } +string dev::readStandardInput() +{ + string ret; + while (!cin.eof()) + { + string tmp; + // NOTE: this will read until EOF or NL + getline(cin, tmp); + ret.append(tmp); + ret.append("\n"); + } + return ret; +} + void dev::writeFile(std::string const& _file, bytesConstRef _data, bool _writeDeleteRename) { namespace fs = boost::filesystem; diff --git a/libdevcore/CommonIO.h b/libdevcore/CommonIO.h index 0c702818..33769ec3 100644 --- a/libdevcore/CommonIO.h +++ b/libdevcore/CommonIO.h @@ -34,6 +34,9 @@ namespace dev /// If the file doesn't exist or isn't readable, returns an empty container / bytes. std::string readFileAsString(std::string const& _file); +/// Retrieve and returns the contents of standard input (until EOF). +std::string readStandardInput(); + /// Write the given binary data into the given file, replacing the file if it pre-exists. /// Throws exception on error. /// @param _writeDeleteRename useful not to lose any data: If set, first writes to another file in -- cgit v1.2.3