Merge pull request #3098 from ethereum/cli-cleanup

Cleanup some file/io reader in devcore
This commit is contained in:
chriseth 2017-10-19 13:39:28 +02:00 committed by GitHub
commit b96602122c
4 changed files with 44 additions and 40 deletions

View File

@ -39,7 +39,7 @@ namespace
{ {
template <typename _T> template <typename _T>
inline _T contentsGeneric(std::string const& _file) inline _T readFile(std::string const& _file)
{ {
_T ret; _T ret;
size_t const c_elementSize = sizeof(typename _T::value_type); 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<string>(_file); return readFile<string>(_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) void dev::writeFile(std::string const& _file, bytesConstRef _data, bool _writeDeleteRename)

View File

@ -32,7 +32,10 @@ namespace dev
/// Retrieve and returns the contents of the given file as a std::string. /// 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. /// 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. /// Write the given binary data into the given file, replacing the file if it pre-exists.
/// Throws exception on error. /// Throws exception on error.

View File

@ -56,7 +56,7 @@ static void help()
static void version() static void version()
{ {
cout << "LLLC, the Lovely Little Language Compiler " << endl; cout << "LLLC, the Lovely Little Language Compiler" << endl;
cout << "Version: " << VersionString << endl; cout << "Version: " << VersionString << endl;
exit(0); exit(0);
} }
@ -118,39 +118,39 @@ int main(int argc, char** argv)
string src; string src;
if (infile.empty()) if (infile.empty())
{ src = readStandardInput();
string s;
while (!cin.eof())
{
getline(cin, s);
src.append(s);
}
}
else else
src = contentsString(infile); src = readFileAsString(infile);
vector<string> errors; vector<string> errors;
if (src.empty()) if (src.empty())
{
errors.push_back("Empty file."); errors.push_back("Empty file.");
}
else if (mode == Disassemble) else if (mode == Disassemble)
{ {
cout << disassemble(fromHex(src)) << endl; cout << disassemble(fromHex(src)) << endl;
} }
else if (mode == Binary || mode == Hex) 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) if (mode == Hex)
cout << toHex(bs) << endl; cout << toHex(bs) << endl;
else if (mode == Binary) else if (mode == Binary)
cout.write((char const*)bs.data(), bs.size()); cout.write((char const*)bs.data(), bs.size());
} }
else if (mode == ParseTree) else if (mode == ParseTree)
{
cout << parseLLL(src) << endl; cout << parseLLL(src) << endl;
}
else if (mode == Assembly) 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) for (auto const& i: errors)
cerr << i << endl; cerr << i << endl;
if ( errors.size() ) if (errors.size())
return 1; return 1;
return 0; return 0;
} }

View File

@ -424,20 +424,13 @@ void CommandLineInterface::readInputFilesAndConfigureRemappings()
continue; continue;
} }
m_sourceCodes[infile.string()] = dev::contentsString(infile.string()); m_sourceCodes[infile.string()] = dev::readFileAsString(infile.string());
path = boost::filesystem::canonical(infile).string(); path = boost::filesystem::canonical(infile).string();
} }
m_allowedDirectories.push_back(boost::filesystem::path(path).remove_filename()); m_allowedDirectories.push_back(boost::filesystem::path(path).remove_filename());
} }
if (addStdin) if (addStdin)
{ m_sourceCodes[g_stdinFileName] = dev::readStandardInput();
string s;
while (!cin.eof())
{
getline(cin, s);
m_sourceCodes[g_stdinFileName].append(s + '\n');
}
}
} }
bool CommandLineInterface::parseLibraryOption(string const& _input) bool CommandLineInterface::parseLibraryOption(string const& _input)
@ -447,7 +440,7 @@ bool CommandLineInterface::parseLibraryOption(string const& _input)
try try
{ {
if (fs::is_regular_file(_input)) if (fs::is_regular_file(_input))
data = contentsString(_input); data = readFileAsString(_input);
} }
catch (fs::filesystem_error const&) catch (fs::filesystem_error const&)
{ {
@ -698,7 +691,7 @@ bool CommandLineInterface::processInput()
return ReadCallback::Result{false, "Not a valid file."}; return ReadCallback::Result{false, "Not a valid file."};
else else
{ {
auto contents = dev::contentsString(canonicalPath.string()); auto contents = dev::readFileAsString(canonicalPath.string());
m_sourceCodes[path.string()] = contents; m_sourceCodes[path.string()] = contents;
return ReadCallback::Result{true, contents}; return ReadCallback::Result{true, contents};
} }
@ -731,13 +724,7 @@ bool CommandLineInterface::processInput()
if (m_args.count(g_argStandardJSON)) if (m_args.count(g_argStandardJSON))
{ {
string input; string input = dev::readStandardInput();
while (!cin.eof())
{
string tmp;
getline(cin, tmp);
input.append(tmp + "\n");
}
StandardCompiler compiler(fileReader); StandardCompiler compiler(fileReader);
cout << compiler.compile(input) << endl; cout << compiler.compile(input) << endl;
return true; return true;