diff --git a/libdevcore/CommonIO.cpp b/libdevcore/CommonIO.cpp index 5d47937b0..8c7e08f6b 100644 --- a/libdevcore/CommonIO.cpp +++ b/libdevcore/CommonIO.cpp @@ -39,7 +39,7 @@ namespace { template -inline _T contentsGeneric(std::string const& _file) +inline _T readFile(std::string const& _file) { _T ret; size_t const c_elementSize = sizeof(typename _T::value_type); @@ -61,9 +61,23 @@ inline _T contentsGeneric(std::string const& _file) } -string dev::contentsString(string const& _file) +string dev::readFileAsString(string const& _file) { - return contentsGeneric(_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) diff --git a/libdevcore/CommonIO.h b/libdevcore/CommonIO.h index d84362cfc..33769ec3d 100644 --- a/libdevcore/CommonIO.h +++ b/libdevcore/CommonIO.h @@ -32,7 +32,10 @@ namespace dev /// Retrieve and returns the contents of the given file as a std::string. /// If the file doesn't exist or isn't readable, returns an empty container / bytes. -std::string contentsString(std::string const& _file); +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. diff --git a/lllc/main.cpp b/lllc/main.cpp index 912ce16af..5679bc2b0 100644 --- a/lllc/main.cpp +++ b/lllc/main.cpp @@ -35,15 +35,15 @@ using namespace dev::solidity; using namespace dev::eth; static string const VersionString = - string(ETH_PROJECT_VERSION) + - (string(SOL_VERSION_PRERELEASE).empty() ? "" : "-" + string(SOL_VERSION_PRERELEASE)) + - (string(SOL_VERSION_BUILDINFO).empty() ? "" : "+" + string(SOL_VERSION_BUILDINFO)); + string(ETH_PROJECT_VERSION) + + (string(SOL_VERSION_PRERELEASE).empty() ? "" : "-" + string(SOL_VERSION_PRERELEASE)) + + (string(SOL_VERSION_BUILDINFO).empty() ? "" : "+" + string(SOL_VERSION_BUILDINFO)); static void help() { cout << "Usage lllc [OPTIONS] " << endl - << "Options:" << endl + << "Options:" << endl << " -b,--binary Parse, compile and assemble; output byte code in binary." << endl << " -x,--hex Parse, compile and assemble; output byte code in hex." << endl << " -a,--assembly Only parse and compile; show assembly." << endl @@ -51,12 +51,12 @@ static void help() << " -o,--optimise Turn on/off the optimiser; off by default." << endl << " -h,--help Show this help message and exit." << endl << " -V,--version Show the version and exit." << endl; - exit(0); + exit(0); } static void version() { - cout << "LLLC, the Lovely Little Language Compiler " << endl; + cout << "LLLC, the Lovely Little Language Compiler" << endl; cout << "Version: " << VersionString << endl; exit(0); } @@ -118,39 +118,39 @@ int main(int argc, char** argv) string src; if (infile.empty()) - { - string s; - while (!cin.eof()) - { - getline(cin, s); - src.append(s); - } - } + src = readStandardInput(); else - src = contentsString(infile); + src = readFileAsString(infile); vector errors; if (src.empty()) + { errors.push_back("Empty file."); + } else if (mode == Disassemble) { cout << disassemble(fromHex(src)) << endl; } else if (mode == Binary || mode == Hex) { - auto bs = compileLLL(src, optimise ? true : false, &errors, contentsString); + auto bs = compileLLL(src, optimise ? true : false, &errors, readFileAsString); if (mode == Hex) cout << toHex(bs) << endl; else if (mode == Binary) cout.write((char const*)bs.data(), bs.size()); } else if (mode == ParseTree) + { cout << parseLLL(src) << endl; + } else if (mode == Assembly) - cout << compileLLLToAsm(src, optimise ? true : false, &errors, contentsString) << endl; + { + cout << compileLLLToAsm(src, optimise ? true : false, &errors, readFileAsString) << endl; + } + for (auto const& i: errors) cerr << i << endl; - if ( errors.size() ) + if (errors.size()) return 1; return 0; } diff --git a/solc/CommandLineInterface.cpp b/solc/CommandLineInterface.cpp index 1686dc2e4..fe1461b4e 100644 --- a/solc/CommandLineInterface.cpp +++ b/solc/CommandLineInterface.cpp @@ -424,20 +424,13 @@ void CommandLineInterface::readInputFilesAndConfigureRemappings() continue; } - m_sourceCodes[infile.string()] = dev::contentsString(infile.string()); + m_sourceCodes[infile.string()] = dev::readFileAsString(infile.string()); path = boost::filesystem::canonical(infile).string(); } m_allowedDirectories.push_back(boost::filesystem::path(path).remove_filename()); } if (addStdin) - { - string s; - while (!cin.eof()) - { - getline(cin, s); - m_sourceCodes[g_stdinFileName].append(s + '\n'); - } - } + m_sourceCodes[g_stdinFileName] = dev::readStandardInput(); } bool CommandLineInterface::parseLibraryOption(string const& _input) @@ -447,7 +440,7 @@ bool CommandLineInterface::parseLibraryOption(string const& _input) try { if (fs::is_regular_file(_input)) - data = contentsString(_input); + data = readFileAsString(_input); } catch (fs::filesystem_error const&) { @@ -698,7 +691,7 @@ bool CommandLineInterface::processInput() return ReadCallback::Result{false, "Not a valid file."}; else { - auto contents = dev::contentsString(canonicalPath.string()); + auto contents = dev::readFileAsString(canonicalPath.string()); m_sourceCodes[path.string()] = contents; return ReadCallback::Result{true, contents}; } @@ -731,13 +724,7 @@ bool CommandLineInterface::processInput() if (m_args.count(g_argStandardJSON)) { - string input; - while (!cin.eof()) - { - string tmp; - getline(cin, tmp); - input.append(tmp + "\n"); - } + string input = dev::readStandardInput(); StandardCompiler compiler(fileReader); cout << compiler.compile(input) << endl; return true;