Skip to content

Commit 181925f

Browse files
Move from poco::file to std::filesystem
1 parent fca7b75 commit 181925f

16 files changed

+76
-65
lines changed

Framework/Kernel/inc/MantidKernel/BinaryFile.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88

99
#include "MantidKernel/DllConfig.h"
1010

11-
#include <Poco/File.h>
12-
#include <Poco/Path.h>
11+
#include <filesystem>
1312
#include <fstream>
1413
#include <memory>
1514
#include <string>
@@ -49,7 +48,7 @@ template <typename T> class DLLExport BinaryFile {
4948

5049
//------------------------------------------------------------------------------------
5150
/// Constructor - open a file
52-
BinaryFile(std::string filename) { this->open(filename); }
51+
BinaryFile(const std::string &filename) { this->open(filename); }
5352

5453
/// Destructor, close the file if needed
5554
~BinaryFile() { this->close(); }
@@ -63,7 +62,7 @@ template <typename T> class DLLExport BinaryFile {
6362
* */
6463
void open(const std::string &filename) {
6564
this->handle.reset(nullptr);
66-
if (!Poco::File(filename).exists()) {
65+
if (!std::filesystem::exists(filename)) {
6766
std::stringstream msg;
6867
msg << "BinaryFile::open: File " << filename << " was not found.";
6968
throw std::invalid_argument("File does not exist.");

Framework/Kernel/src/AttenuationProfile.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
#include "MantidKernel/ConfigService.h"
99
#include "MantidKernel/Exception.h"
1010
#include "MantidKernel/Material.h"
11-
#include <Poco/File.h>
1211
#include <Poco/Path.h>
12+
#include <filesystem>
1313
#include <fstream>
1414

1515
namespace Mantid::Kernel {
@@ -37,7 +37,7 @@ AttenuationProfile::AttenuationProfile(const std::string &inputFileName, const s
3737

3838
if (!searchPath.empty()) {
3939
inputFilePath = Poco::Path(Poco::Path(searchPath).parent(), inputFileName);
40-
if (Poco::File(inputFilePath).exists()) {
40+
if (std::filesystem::exists(inputFilePath.toString())) {
4141
useSearchDirectories = false;
4242
}
4343
}

Framework/Kernel/src/DirectoryValidator.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
#include "MantidKernel/DirectoryValidator.h"
88
#include "MantidKernel/IValidator.h"
99
#include <Poco/Exception.h>
10-
#include <Poco/File.h>
1110
#include <Poco/Path.h>
11+
#include <filesystem>
1212
#include <memory>
1313

1414
namespace Mantid::Kernel {
@@ -44,12 +44,14 @@ std::string DirectoryValidator::checkValidity(const std::string &value) const {
4444
// If the path is required to exist check it is there
4545
if (m_testExist) {
4646
try {
47-
if (value.empty() || !Poco::File(value).exists())
47+
if (value.empty() || !std::filesystem::exists(value))
4848
return "Directory \"" + value + "\" not found";
49-
if (!Poco::File(value).isDirectory())
49+
if (!std::filesystem::is_directory(value))
5050
return "Directory \"" + value + "\" specified is actually a file";
5151
} catch (Poco::FileException &) {
5252
return "Error accessing directory \"" + value + "\"";
53+
} catch (const std::filesystem::filesystem_error &) {
54+
return "Error accessing directory \"" + value + "\"";
5355
}
5456
}
5557

Framework/Kernel/src/FileDescriptor.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
#include "MantidKernel/FileDescriptor.h"
88
#include "MantidKernel/Strings.h"
99

10-
#include <Poco/File.h>
1110
#include <Poco/Path.h>
11+
#include <filesystem>
1212

1313
#include <stdexcept>
1414

@@ -131,7 +131,7 @@ FileDescriptor::FileDescriptor(const std::string &filename) : m_filename(), m_ex
131131
if (filename.empty()) {
132132
throw std::invalid_argument("FileDescriptor() - Empty filename '" + filename + "'");
133133
}
134-
if (!Poco::File(filename).exists()) {
134+
if (!std::filesystem::exists(filename)) {
135135
throw std::invalid_argument("FileDescriptor() - File '" + filename + "' does not exist");
136136
}
137137
initialize(filename);

Framework/Kernel/src/FileValidator.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
// SPDX - License - Identifier: GPL - 3.0 +
77
#include "MantidKernel/FileValidator.h"
88
#include "MantidKernel/Logger.h"
9-
#include <Poco/File.h>
109
#include <Poco/Path.h>
1110
#include <boost/algorithm/string/case_conv.hpp>
11+
#include <filesystem>
1212
#include <fstream>
1313
#include <memory>
1414
#include <sstream>
@@ -81,11 +81,11 @@ std::string FileValidator::checkValidity(const std::string &value) const {
8181
}
8282

8383
// If the file is required to exist check it is there
84-
if (m_testExist && (value.empty() || !Poco::File(value).exists())) {
84+
if (m_testExist && (value.empty() || !std::filesystem::exists(value))) {
8585
return "File \"" + abspath + "\" not found";
8686
}
8787

88-
if (m_testExist && (Poco::File(value).exists())) {
88+
if (m_testExist && (std::filesystem::exists(value))) {
8989
std::ifstream in;
9090
in.open(value.c_str());
9191
if (!in) {

Framework/Kernel/src/NexusDescriptor.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include <nexus/NeXusException.hpp>
1212
// clang-format on
1313

14-
#include <Poco/File.h>
14+
#include <filesystem>
1515
#include <Poco/Path.h>
1616

1717
#include <algorithm>
@@ -115,7 +115,7 @@ NexusDescriptor::NexusDescriptor(const std::string &filename, const bool init)
115115
if (filename.empty()) {
116116
throw std::invalid_argument("NexusDescriptor() - Empty filename '" + filename + "'");
117117
}
118-
if (!Poco::File(filename).exists()) {
118+
if (!std::filesystem::exists(filename)) {
119119
throw std::invalid_argument("NexusDescriptor() - File '" + filename + "' does not exist");
120120
}
121121

Framework/Kernel/test/BinaryFileTest.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
using namespace Mantid::Kernel;
1616

17-
using std::cout;
1817
using std::runtime_error;
1918
using std::size_t;
2019
using std::vector;
@@ -67,7 +66,7 @@ class BinaryFileTest : public CxxTest::TestSuite {
6766
MakeDummyFile(dummy_file, 3);
6867
TS_ASSERT_THROWS(file.open(dummy_file), const std::runtime_error &);
6968
file.close();
70-
Poco::File(dummy_file).remove();
69+
std::filesystem::remove(dummy_file);
7170
}
7271

7372
void testOpen() {
@@ -90,7 +89,7 @@ class BinaryFileTest : public CxxTest::TestSuite {
9089
TS_ASSERT_EQUALS(data.at(num - 1).pid, 39);
9190

9291
file.close();
93-
Poco::File(dummy_file).remove();
92+
std::filesystem::remove(dummy_file);
9493
}
9594

9695
void testLoadAllIntoVector() {
@@ -111,7 +110,7 @@ class BinaryFileTest : public CxxTest::TestSuite {
111110
TS_ASSERT_EQUALS(data.at(num - 1).tof, 38);
112111
TS_ASSERT_EQUALS(data.at(num - 1).pid, 39);
113112
file.close();
114-
Poco::File(dummy_file).remove();
113+
std::filesystem::remove(dummy_file);
115114
}
116115

117116
void testLoadInBlocks() {
@@ -142,7 +141,7 @@ class BinaryFileTest : public CxxTest::TestSuite {
142141
TS_ASSERT_EQUALS(data[9].tof, 38);
143142
TS_ASSERT_EQUALS(data[9].pid, 39);
144143
file.close();
145-
Poco::File(dummy_file).remove();
144+
std::filesystem::remove(dummy_file);
146145
}
147146

148147
void testLoadBlockAt() {
@@ -169,7 +168,7 @@ class BinaryFileTest : public CxxTest::TestSuite {
169168
loaded_size = file.loadBlock(data.get(), block_size);
170169
TS_ASSERT_EQUALS(loaded_size, 5);
171170
file.close();
172-
Poco::File(dummy_file).remove();
171+
std::filesystem::remove(dummy_file);
173172
}
174173

175174
void testCallingDestructorOnUnitializedObject() { BinaryFile<DasEvent> file2; }

Framework/Kernel/test/ChecksumHelperTest.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
#include "MantidKernel/ChecksumHelper.h"
1010
#include "MantidKernel/System.h"
1111

12-
#include <Poco/File.h>
1312
#include <cxxtest/TestSuite.h>
13+
#include <filesystem>
1414
#include <fstream>
1515

1616
using namespace Mantid::Kernel;
@@ -42,7 +42,7 @@ class ChecksumHelperTest : public CxxTest::TestSuite {
4242
std::string response = ChecksumHelper::sha1FromFile(filename, false);
4343
TSM_ASSERT_EQUALS("The calculated SHA-1 hash is not as expected", "363cbe9c113b8bcba9e0aa94dbe45e67856ff26b",
4444
response);
45-
Poco::File(filename).remove();
45+
std::filesystem::remove(filename);
4646
}
4747

4848
void testGitSha1FromFile() {
@@ -54,7 +54,7 @@ class ChecksumHelperTest : public CxxTest::TestSuite {
5454
std::string response = ChecksumHelper::gitSha1FromFile(filename);
5555
TSM_ASSERT_EQUALS("The calculated git-hash is not as expected", "db46957d5afdb266b4b3321f3ce2b8887f190ff5",
5656
response);
57-
Poco::File(filename).remove();
57+
std::filesystem::remove(filename);
5858
}
5959

6060
void testGitSha1FromFileWithLinuxLineEndings() {
@@ -67,7 +67,7 @@ class ChecksumHelperTest : public CxxTest::TestSuite {
6767
std::string response = ChecksumHelper::gitSha1FromFile(filename);
6868
TSM_ASSERT_EQUALS("The calculated git-hash is not as expected", "7e78655a4e48aa2fbd4a3f1aec4043009e342e31",
6969
response);
70-
Poco::File(filename).remove();
70+
std::filesystem::remove(filename);
7171
}
7272

7373
void testGitSha1FromFileWithWindowsLineEndingsFirstConvertsToLF() {
@@ -80,7 +80,7 @@ class ChecksumHelperTest : public CxxTest::TestSuite {
8080
std::string response = ChecksumHelper::gitSha1FromFile(filename);
8181
TSM_ASSERT_EQUALS("The calculated git-hash is not as expected", "23dcaeaefce51ed7cae98f6420f67e0ba0e2058a",
8282
response);
83-
Poco::File(filename).remove();
83+
std::filesystem::remove(filename);
8484
}
8585

8686
void testGitSha1FromFileWithOldStyleMacLineEndingsDoesNotConvertToLF() {
@@ -93,7 +93,7 @@ class ChecksumHelperTest : public CxxTest::TestSuite {
9393
std::string response = ChecksumHelper::gitSha1FromFile(filename);
9494
TSM_ASSERT_EQUALS("The calculated git-hash is not as expected", "7b7e77332c1610df14fd26476d1601a22a34f11f",
9595
response);
96-
Poco::File(filename).remove();
96+
std::filesystem::remove(filename);
9797
}
9898

9999
void createFile(const std::string &fileName, const std::string &data) {

Framework/Kernel/test/DirectoryValidatorTest.h

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
#include "MantidKernel/DirectoryValidator.h"
1212
#include <Poco/File.h>
13+
#include <filesystem>
14+
#include <fstream>
1315

1416
using namespace Mantid::Kernel;
1517

@@ -24,10 +26,12 @@ class DirectoryValidatorTest : public CxxTest::TestSuite {
2426
void testFailsOnAFile() {
2527
DirectoryValidator v(true);
2628
std::string ThisIsAFile("directoryvalidatortestfile.txt");
27-
Poco::File txt_file(ThisIsAFile);
28-
txt_file.createFile();
29+
30+
std::filesystem::path txt_file(ThisIsAFile);
31+
std::ofstream handle(txt_file);
32+
handle.close();
2933
TS_ASSERT_EQUALS(v.isValid(ThisIsAFile), "Directory \"" + ThisIsAFile + "\" specified is actually a file");
30-
txt_file.remove();
34+
std::filesystem::remove(txt_file);
3135
}
3236

3337
void testPassesOnNonexistentDirectoryIfYouSaySoForSomeReason() {
@@ -38,10 +42,10 @@ class DirectoryValidatorTest : public CxxTest::TestSuite {
3842

3943
void testPassesOnExistingDirectory() {
4044
std::string TestDir("./MyTestFolder");
41-
Poco::File dir(TestDir);
42-
dir.createDirectory();
45+
std::filesystem::path dir(TestDir);
46+
TS_ASSERT(std::filesystem::create_directory(dir));
4347
DirectoryValidator v(true);
4448
TS_ASSERT_EQUALS(v.isValid(TestDir), "");
45-
dir.remove(); // clean up your folder
49+
std::filesystem::remove(dir); // clean up your folder
4650
}
4751
};

Framework/Kernel/test/FileDescriptorTest.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
#include "MantidKernel/ConfigService.h"
1313
#include "MantidKernel/FileDescriptor.h"
1414

15-
#include <Poco/File.h>
1615
#include <Poco/Path.h>
16+
#include <filesystem>
1717

1818
using Mantid::Kernel::FileDescriptor;
1919

@@ -30,16 +30,16 @@ class FileDescriptorTest : public CxxTest::TestSuite {
3030
const auto &dataPaths = cfg.getDataSearchDirs();
3131
for (const auto &dataPath : dataPaths) {
3232
Poco::Path nxsPath(dataPath, "CNCS_7860_event.nxs");
33-
if (Poco::File(nxsPath).exists())
33+
if (std::filesystem::exists(nxsPath.toString()))
3434
m_testNexusPath = nxsPath.toString();
3535
Poco::Path nonNxsPath(dataPath, "CSP79590.raw");
36-
if (Poco::File(nonNxsPath).exists())
36+
if (std::filesystem::exists(nonNxsPath.toString()))
3737
m_testNonNexusPath = nonNxsPath.toString();
3838
Poco::Path asciiPath(dataPath, "AsciiExample.txt");
39-
if (Poco::File(asciiPath).exists())
39+
if (std::filesystem::exists(asciiPath.toString()))
4040
m_testAsciiPath = asciiPath.toString();
4141
Poco::Path emptyFilePath(dataPath, "emptyFile.txt");
42-
if (Poco::File(emptyFilePath).exists())
42+
if (std::filesystem::exists(emptyFilePath.toString()))
4343
m_emptyFilePath = emptyFilePath.toString();
4444

4545
if (!m_testNexusPath.empty() && !m_testNonNexusPath.empty() && !m_testAsciiPath.empty() &&

0 commit comments

Comments
 (0)