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>
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<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)

View File

@ -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.

View File

@ -56,7 +56,7 @@ static void help()
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<string> 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;
}

View File

@ -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;