Change readFile/readFileAsString to throw FileNotFound exception

This commit is contained in:
Alex Beregszaszi 2020-10-31 01:04:53 +00:00
parent be74479517
commit 3c7f642227
6 changed files with 37 additions and 7 deletions

View File

@ -47,8 +47,7 @@ inline T readFile(std::string const& _file)
T ret;
size_t const c_elementSize = sizeof(typename T::value_type);
std::ifstream is(_file, std::ifstream::binary);
if (!is)
return ret;
assertThrow(is, FileNotFound, _file);
// get length of file:
is.seekg(0, is.end);

View File

@ -32,7 +32,8 @@ namespace solidity::util
{
/// 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, it will throw a FileNotFound 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).

View File

@ -48,7 +48,7 @@ private:
DEV_SIMPLE_EXCEPTION(InvalidAddress);
DEV_SIMPLE_EXCEPTION(BadHexCharacter);
DEV_SIMPLE_EXCEPTION(BadHexCase);
DEV_SIMPLE_EXCEPTION(FileError);
DEV_SIMPLE_EXCEPTION(FileNotFound);
DEV_SIMPLE_EXCEPTION(DataTooLong);
DEV_SIMPLE_EXCEPTION(StringTooLong);

View File

@ -86,6 +86,8 @@ using namespace solidity::langutil;
namespace po = boost::program_options;
DEV_SIMPLE_EXCEPTION(FileError);
namespace solidity::frontend
{
@ -614,6 +616,7 @@ bool CommandLineInterface::readInputFilesAndConfigureRemappings()
continue;
}
// NOTE: we ignore the FileNotFound exception as we manually check above
m_sourceCodes[infile.generic_string()] = readFileAsString(infile.string());
path = boost::filesystem::canonical(infile).string();
}
@ -643,6 +646,10 @@ bool CommandLineInterface::parseLibraryOption(string const& _input)
{
// Thrown e.g. if path is too long.
}
catch (FileNotFound const&)
{
// Should not happen if `fs::is_regular_file` is correct.
}
vector<string> libraries;
boost::split(libraries, data, boost::is_space() || boost::is_any_of(","), boost::token_compress_on);
@ -1146,6 +1153,7 @@ bool CommandLineInterface::processInput()
if (!boost::filesystem::is_regular_file(canonicalPath))
return ReadCallback::Result{false, "Not a valid file."};
// NOTE: we ignore the FileNotFound exception as we manually check above
auto contents = readFileAsString(canonicalPath.string());
m_sourceCodes[path.generic_string()] = contents;
return ReadCallback::Result{true, contents};
@ -1232,6 +1240,7 @@ bool CommandLineInterface::processInput()
if (jsonFile.empty())
input = readStandardInput();
else
// TODO: handle FileNotFound exception
input = readFileAsString(jsonFile);
StandardCompiler compiler(fileReader);
sout() << compiler.compile(std::move(input)) << endl;

View File

@ -20,6 +20,7 @@
*/
#include <libsolutil/CommonIO.h>
#include <libsolutil/Exceptions.h>
#include <liblangutil/ErrorReporter.h>
#include <liblangutil/Scanner.h>
#include <libyul/AsmAnalysis.h>
@ -243,8 +244,18 @@ Allowed options)",
}
string input;
try
{
input = readFileAsString(arguments["input-file"].as<string>());
}
catch (FileNotFound const& _exception)
{
cerr << "File not found:" << _exception.comment() << endl;
return 1;
}
if (arguments.count("input-file"))
YulOpti{}.runInteractive(readFileAsString(arguments["input-file"].as<string>()));
YulOpti{}.runInteractive(input);
else
cout << options;

View File

@ -35,6 +35,7 @@
#include <libsolutil/CommonIO.h>
#include <libsolutil/CommonData.h>
#include <libsolutil/Exceptions.h>
#include <boost/program_options.hpp>
@ -137,10 +138,19 @@ Allowed options)",
else
{
string input;
if (arguments.count("input-file"))
for (string path: arguments["input-file"].as<vector<string>>())
input += readFileAsString(path);
{
try
{
input += readFileAsString(path);
}
catch (FileNotFound const&)
{
cerr << "File not found: " << path << endl;
return 1;
}
}
else
input = readStandardInput();