Skip to content

Commit cb1a0f0

Browse files
committed
readFileAsString(): Accept path as boost::filesystem::path instead of string
1 parent 790d08f commit cb1a0f0

File tree

9 files changed

+25
-27
lines changed

9 files changed

+25
-27
lines changed

libsolidity/interface/FileReader.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ ReadCallback::Result FileReader::readFile(string const& _kind, string const& _so
7878
return ReadCallback::Result{false, "Not a valid file."};
7979

8080
// NOTE: we ignore the FileNotFound exception as we manually check above
81-
auto contents = readFileAsString(canonicalPath.string());
81+
auto contents = readFileAsString(canonicalPath);
8282
m_sourceCodes[_sourceUnitName] = contents;
8383
return ReadCallback::Result{true, contents};
8484
}

libsolutil/CommonIO.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
#include <libsolutil/CommonIO.h>
2424
#include <libsolutil/Assertions.h>
2525

26-
#include <boost/filesystem.hpp>
27-
2826
#include <fstream>
2927
#if defined(_WIN32)
3028
#include <windows.h>
@@ -40,20 +38,20 @@ namespace
4038
{
4139

4240
template <typename T>
43-
inline T readFile(std::string const& _file)
41+
inline T readFile(boost::filesystem::path const& _file)
4442
{
45-
assertThrow(boost::filesystem::exists(_file), FileNotFound, _file);
43+
assertThrow(boost::filesystem::exists(_file), FileNotFound, _file.string());
4644

4745
// ifstream does not always fail when the path leads to a directory. Instead it might succeed
4846
// with tellg() returning a nonsensical value so that std::length_error gets raised in resize().
49-
assertThrow(boost::filesystem::is_regular_file(_file), NotAFile, _file);
47+
assertThrow(boost::filesystem::is_regular_file(_file), NotAFile, _file.string());
5048

5149
T ret;
5250
size_t const c_elementSize = sizeof(typename T::value_type);
53-
std::ifstream is(_file, std::ifstream::binary);
51+
std::ifstream is(_file.string(), std::ifstream::binary);
5452

5553
// Technically, this can still fail even though we checked above because FS content can change at any time.
56-
assertThrow(is, FileNotFound, _file);
54+
assertThrow(is, FileNotFound, _file.string());
5755

5856
// get length of file:
5957
is.seekg(0, is.end);
@@ -69,7 +67,7 @@ inline T readFile(std::string const& _file)
6967

7068
}
7169

72-
string solidity::util::readFileAsString(string const& _file)
70+
string solidity::util::readFileAsString(boost::filesystem::path const& _file)
7371
{
7472
return readFile<string>(_file);
7573
}

libsolutil/CommonIO.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424

2525
#pragma once
2626

27+
#include <boost/filesystem.hpp>
28+
2729
#include <libsolutil/Common.h>
2830
#include <iostream>
2931
#include <sstream>
@@ -36,7 +38,7 @@ namespace solidity::util
3638
/// If the file doesn't exist, it will throw a FileNotFound exception.
3739
/// If the file exists but is not a regular file, it will throw NotAFile exception.
3840
/// If the file is empty, returns an empty string.
39-
std::string readFileAsString(std::string const& _file);
41+
std::string readFileAsString(boost::filesystem::path const& _file);
4042

4143
/// Retrieves and returns the whole content of the specified input stream (until EOF).
4244
std::string readUntilEnd(std::istream& _stdin);

solc/CommandLineInterface.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ bool CommandLineInterface::readInputFiles()
430430
}
431431

432432
// NOTE: we ignore the FileNotFound exception as we manually check above
433-
string fileContent = readFileAsString(infile.string());
433+
string fileContent = readFileAsString(infile);
434434
if (m_options.input.mode == InputMode::StandardJson)
435435
{
436436
solAssert(!m_standardJsonInput.has_value(), "");

test/TestCaseReader.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ pair<SourceMap, size_t> TestCaseReader::parseSourcesAndSettingsWithLineNumber(is
168168
if (!fs::exists(externalSourceFullPath))
169169
BOOST_THROW_EXCEPTION(runtime_error("External Source '" + externalSourceTarget.string() + "' not found."));
170170
else
171-
externalSourceContent = util::readFileAsString(externalSourceFullPath.string());
171+
externalSourceContent = util::readFileAsString(externalSourceFullPath);
172172

173173
if (sources.count(externalSourceName))
174174
BOOST_THROW_EXCEPTION(runtime_error("Multiple definitions of test source \"" + externalSourceName + "\"."));

test/libsolutil/CommonIO.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ BOOST_AUTO_TEST_CASE(readFileAsString_regular_file)
4444
TemporaryDirectory tempDir("common-io-test-");
4545
createFileWithContent(tempDir.path() / "test.txt", "ABC\ndef\n");
4646

47-
BOOST_TEST(readFileAsString((tempDir.path() / "test.txt").string()) == "ABC\ndef\n");
47+
BOOST_TEST(readFileAsString(tempDir.path() / "test.txt") == "ABC\ndef\n");
4848
}
4949

5050
BOOST_AUTO_TEST_CASE(readFileAsString_directory)
5151
{
5252
TemporaryDirectory tempDir("common-io-test-");
53-
BOOST_CHECK_THROW(readFileAsString(tempDir.path().string()), NotAFile);
53+
BOOST_CHECK_THROW(readFileAsString(tempDir.path()), NotAFile);
5454
}
5555

5656
BOOST_AUTO_TEST_CASE(readFileAsString_symlink)
@@ -61,7 +61,7 @@ BOOST_AUTO_TEST_CASE(readFileAsString_symlink)
6161
if (!createSymlinkIfSupportedByFilesystem("test.txt", tempDir.path() / "symlink.txt"))
6262
return;
6363

64-
BOOST_TEST(readFileAsString((tempDir.path() / "symlink.txt").string()) == "ABC\ndef\n");
64+
BOOST_TEST(readFileAsString(tempDir.path() / "symlink.txt") == "ABC\ndef\n");
6565
}
6666

6767
BOOST_AUTO_TEST_SUITE_END()

tools/solidityUpgrade/SourceUpgrade.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ bool SourceUpgrade::readInputFiles()
421421
if (m_args.count(g_argInputFile))
422422
for (string path: m_args[g_argInputFile].as<vector<string>>())
423423
{
424-
auto infile = boost::filesystem::path(path);
424+
boost::filesystem::path infile = path;
425425
if (!boost::filesystem::exists(infile))
426426
{
427427
if (!ignoreMissing)
@@ -448,8 +448,7 @@ bool SourceUpgrade::readInputFiles()
448448
continue;
449449
}
450450

451-
m_sourceCodes[infile.generic_string()] = readFileAsString(infile.string());
452-
path = boost::filesystem::canonical(infile).string();
451+
m_sourceCodes[infile.generic_string()] = readFileAsString(infile);
453452
}
454453

455454
if (m_sourceCodes.size() == 0)
@@ -484,8 +483,8 @@ ReadCallback::Callback SourceUpgrade::fileReader()
484483
{
485484
try
486485
{
487-
auto path = boost::filesystem::path(_path);
488-
auto canonicalPath = boost::filesystem::weakly_canonical(path);
486+
boost::filesystem::path path = _path;
487+
boost::filesystem::path canonicalPath = boost::filesystem::weakly_canonical(path);
489488
bool isAllowed = false;
490489
for (auto const& allowedDir: m_allowedDirectories)
491490
{
@@ -508,7 +507,7 @@ ReadCallback::Callback SourceUpgrade::fileReader()
508507
if (!boost::filesystem::is_regular_file(canonicalPath))
509508
return ReadCallback::Result{false, "Not a valid file."};
510509

511-
auto contents = readFileAsString(canonicalPath.string());
510+
string contents = readFileAsString(canonicalPath);
512511
m_sourceCodes[path.generic_string()] = contents;
513512
return ReadCallback::Result{true, contents};
514513
}

tools/yulPhaser/Phaser.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@
3535
#include <libsolutil/CommonData.h>
3636
#include <libsolutil/CommonIO.h>
3737

38-
#include <boost/filesystem.hpp>
39-
4038
#include <iostream>
4139

4240
using namespace std;
@@ -405,12 +403,12 @@ vector<Program> ProgramFactory::build(Options const& _options)
405403
return inputPrograms;
406404
}
407405

408-
CharStream ProgramFactory::loadSource(string const& _sourcePath)
406+
CharStream ProgramFactory::loadSource(boost::filesystem::path const& _sourcePath)
409407
{
410-
assertThrow(boost::filesystem::exists(_sourcePath), MissingFile, "Source file does not exist: " + _sourcePath);
408+
assertThrow(boost::filesystem::exists(_sourcePath), MissingFile, "Source file does not exist: " + _sourcePath.string());
411409

412410
string sourceCode = readFileAsString(_sourcePath);
413-
return CharStream(sourceCode, _sourcePath);
411+
return CharStream(sourceCode, _sourcePath.string());
414412
}
415413

416414
void Phaser::main(int _argc, char** _argv)

tools/yulPhaser/Phaser.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <tools/yulPhaser/AlgorithmRunner.h>
2626
#include <tools/yulPhaser/GeneticAlgorithms.h>
2727

28+
#include <boost/filesystem.hpp>
2829
#include <boost/program_options.hpp>
2930

3031
#include <cstddef>
@@ -242,7 +243,7 @@ class ProgramFactory
242243
static std::vector<Program> build(Options const& _options);
243244

244245
private:
245-
static langutil::CharStream loadSource(std::string const& _sourcePath);
246+
static langutil::CharStream loadSource(boost::filesystem::path const& _sourcePath);
246247
};
247248

248249
/**

0 commit comments

Comments
 (0)