Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Move from poco::file to std::filesystem - Kernel #37870

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions Framework/Kernel/inc/MantidKernel/BinaryFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@

#include "MantidKernel/DllConfig.h"

#include <Poco/File.h>
#include <Poco/Path.h>
#include <filesystem>
#include <fstream>
#include <memory>
#include <string>
Expand Down Expand Up @@ -49,7 +48,7 @@ template <typename T> class DLLExport BinaryFile {

//------------------------------------------------------------------------------------
/// Constructor - open a file
BinaryFile(std::string filename) { this->open(filename); }
BinaryFile(const std::string &filename) { this->open(filename); }

/// Destructor, close the file if needed
~BinaryFile() { this->close(); }
Expand All @@ -63,7 +62,7 @@ template <typename T> class DLLExport BinaryFile {
* */
void open(const std::string &filename) {
this->handle.reset(nullptr);
if (!Poco::File(filename).exists()) {
if (!std::filesystem::exists(filename)) {
std::stringstream msg;
msg << "BinaryFile::open: File " << filename << " was not found.";
throw std::invalid_argument("File does not exist.");
Expand Down
4 changes: 2 additions & 2 deletions Framework/Kernel/src/AttenuationProfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
#include "MantidKernel/ConfigService.h"
#include "MantidKernel/Exception.h"
#include "MantidKernel/Material.h"
#include <Poco/File.h>
#include <Poco/Path.h>
#include <filesystem>
#include <fstream>

namespace Mantid::Kernel {
Expand Down Expand Up @@ -37,7 +37,7 @@ AttenuationProfile::AttenuationProfile(const std::string &inputFileName, const s

if (!searchPath.empty()) {
inputFilePath = Poco::Path(Poco::Path(searchPath).parent(), inputFileName);
if (Poco::File(inputFilePath).exists()) {
if (std::filesystem::exists(inputFilePath.toString())) {
useSearchDirectories = false;
}
}
Expand Down
8 changes: 5 additions & 3 deletions Framework/Kernel/src/DirectoryValidator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
#include "MantidKernel/DirectoryValidator.h"
#include "MantidKernel/IValidator.h"
#include <Poco/Exception.h>
#include <Poco/File.h>
#include <Poco/Path.h>
#include <filesystem>
#include <memory>

namespace Mantid::Kernel {
Expand Down Expand Up @@ -44,12 +44,14 @@ std::string DirectoryValidator::checkValidity(const std::string &value) const {
// If the path is required to exist check it is there
if (m_testExist) {
try {
if (value.empty() || !Poco::File(value).exists())
if (value.empty() || !std::filesystem::exists(value))
return "Directory \"" + value + "\" not found";
if (!Poco::File(value).isDirectory())
if (!std::filesystem::is_directory(value))
return "Directory \"" + value + "\" specified is actually a file";
} catch (Poco::FileException &) {
return "Error accessing directory \"" + value + "\"";
} catch (const std::filesystem::filesystem_error &) {
return "Error accessing directory \"" + value + "\"";
}
}

Expand Down
10 changes: 4 additions & 6 deletions Framework/Kernel/src/FileDescriptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
#include "MantidKernel/FileDescriptor.h"
#include "MantidKernel/Strings.h"

#include <Poco/File.h>
#include <Poco/Path.h>

#include <filesystem>
#include <stdexcept>

namespace Mantid::Kernel {
Expand Down Expand Up @@ -82,7 +80,7 @@ bool FileDescriptor::isAscii(std::istream &data, const size_t nbytes) {
bool FileDescriptor::isAscii(FILE *file, const size_t nbytes) {
// read the data and reset the seek index back to the beginning
auto dataArray = new char[nbytes];
char *pend = &dataArray[fread(dataArray, 1, nbytes, file)];
const char *pend = &dataArray[fread(dataArray, 1, nbytes, file)];
int retval = fseek(file, 0, SEEK_SET);
if (retval < 0)
throw std::runtime_error("FileDescriptor::isAscii - Cannot change position "
Expand Down Expand Up @@ -131,7 +129,7 @@ FileDescriptor::FileDescriptor(const std::string &filename) : m_filename(), m_ex
if (filename.empty()) {
throw std::invalid_argument("FileDescriptor() - Empty filename '" + filename + "'");
}
if (!Poco::File(filename).exists()) {
if (!std::filesystem::exists(filename)) {
throw std::invalid_argument("FileDescriptor() - File '" + filename + "' does not exist");
}
initialize(filename);
Expand Down Expand Up @@ -177,7 +175,7 @@ bool FileDescriptor::isXML() const { return (this->isAscii() && this->extension(
*/
void FileDescriptor::initialize(const std::string &filename) {
m_filename = filename;
m_extension = Mantid::Kernel::Strings::toLower("." + Poco::Path(filename).getExtension());
m_extension = Mantid::Kernel::Strings::toLower(std::filesystem::path(filename).extension().string());

m_file.open(m_filename.c_str(), std::ios::in | std::ios::binary);
if (!m_file)
Expand Down
12 changes: 6 additions & 6 deletions Framework/Kernel/src/FileValidator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
// SPDX - License - Identifier: GPL - 3.0 +
#include "MantidKernel/FileValidator.h"
#include "MantidKernel/Logger.h"
#include <Poco/File.h>
#include <Poco/Path.h>
#include <boost/algorithm/string/case_conv.hpp>
#include <filesystem>
#include <fstream>
#include <memory>
#include <sstream>
Expand Down Expand Up @@ -75,17 +75,17 @@ std::string FileValidator::checkValidity(const std::string &value) const {
// create a variable for the absolute path to be used in error messages
std::string abspath(value);
if (!value.empty()) {
Poco::Path path(value);
if (path.isAbsolute())
abspath = path.toString();
std::filesystem::path path(value);
if (path.is_absolute())
abspath = std::filesystem::absolute(path).string();
}

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

if (m_testExist && (Poco::File(value).exists())) {
if (m_testExist && (std::filesystem::exists(value))) {
std::ifstream in;
in.open(value.c_str());
if (!in) {
Expand Down
8 changes: 3 additions & 5 deletions Framework/Kernel/src/NexusDescriptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@
#include <nexus/NeXusException.hpp>
// clang-format on

#include <Poco/File.h>
#include <Poco/Path.h>

#include <algorithm>
#include <cstring>
#include <filesystem>
#include <string>

namespace Mantid::Kernel {
Expand Down Expand Up @@ -115,7 +113,7 @@ NexusDescriptor::NexusDescriptor(const std::string &filename, const bool init)
if (filename.empty()) {
throw std::invalid_argument("NexusDescriptor() - Empty filename '" + filename + "'");
}
if (!Poco::File(filename).exists()) {
if (!std::filesystem::exists(filename)) {
throw std::invalid_argument("NexusDescriptor() - File '" + filename + "' does not exist");
}

Expand Down Expand Up @@ -211,7 +209,7 @@ bool NexusDescriptor::classTypeExists(const std::string &classType) const {
*/
void NexusDescriptor::initialize(const std::string &filename) {
m_filename = filename;
m_extension = "." + Poco::Path(filename).getExtension();
m_extension = std::filesystem::path(filename).extension().string();

m_file = std::make_unique<::NeXus::File>(this->filename());

Expand Down
2 changes: 2 additions & 0 deletions Framework/Kernel/test/AttenuationProfileTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class AttenuationProfileTest : public CxxTest::TestSuite {

void testLoadAttenuationFile() {
std::string path = Mantid::Kernel::ConfigService::Instance().getFullPath("AttenuationProfile.DAT", false, 0);
TS_ASSERT(!path.empty());
auto profile = AttenuationProfile(path, "");
TS_ASSERT_THROWS_NOTHING(auto profile = AttenuationProfile(path, ""));
}

Expand Down
11 changes: 5 additions & 6 deletions Framework/Kernel/test/BinaryFileTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

using namespace Mantid::Kernel;

using std::cout;
using std::runtime_error;
using std::size_t;
using std::vector;
Expand Down Expand Up @@ -67,7 +66,7 @@ class BinaryFileTest : public CxxTest::TestSuite {
MakeDummyFile(dummy_file, 3);
TS_ASSERT_THROWS(file.open(dummy_file), const std::runtime_error &);
file.close();
Poco::File(dummy_file).remove();
std::filesystem::remove(dummy_file);
}

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

file.close();
Poco::File(dummy_file).remove();
std::filesystem::remove(dummy_file);
}

void testLoadAllIntoVector() {
Expand All @@ -111,7 +110,7 @@ class BinaryFileTest : public CxxTest::TestSuite {
TS_ASSERT_EQUALS(data.at(num - 1).tof, 38);
TS_ASSERT_EQUALS(data.at(num - 1).pid, 39);
file.close();
Poco::File(dummy_file).remove();
std::filesystem::remove(dummy_file);
}

void testLoadInBlocks() {
Expand Down Expand Up @@ -142,7 +141,7 @@ class BinaryFileTest : public CxxTest::TestSuite {
TS_ASSERT_EQUALS(data[9].tof, 38);
TS_ASSERT_EQUALS(data[9].pid, 39);
file.close();
Poco::File(dummy_file).remove();
std::filesystem::remove(dummy_file);
}

void testLoadBlockAt() {
Expand All @@ -169,7 +168,7 @@ class BinaryFileTest : public CxxTest::TestSuite {
loaded_size = file.loadBlock(data.get(), block_size);
TS_ASSERT_EQUALS(loaded_size, 5);
file.close();
Poco::File(dummy_file).remove();
std::filesystem::remove(dummy_file);
}

void testCallingDestructorOnUnitializedObject() { BinaryFile<DasEvent> file2; }
Expand Down
12 changes: 6 additions & 6 deletions Framework/Kernel/test/ChecksumHelperTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
#include "MantidKernel/ChecksumHelper.h"
#include "MantidKernel/System.h"

#include <Poco/File.h>
#include <cxxtest/TestSuite.h>
#include <filesystem>
#include <fstream>

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

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

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

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

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

void createFile(const std::string &fileName, const std::string &data) {
Expand Down
17 changes: 10 additions & 7 deletions Framework/Kernel/test/DirectoryValidatorTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
#include <cxxtest/TestSuite.h>

#include "MantidKernel/DirectoryValidator.h"
#include <Poco/File.h>
#include <filesystem>
#include <fstream>

using namespace Mantid::Kernel;

Expand All @@ -24,10 +25,12 @@ class DirectoryValidatorTest : public CxxTest::TestSuite {
void testFailsOnAFile() {
DirectoryValidator v(true);
std::string ThisIsAFile("directoryvalidatortestfile.txt");
Poco::File txt_file(ThisIsAFile);
txt_file.createFile();

std::filesystem::path txt_file(ThisIsAFile);
std::ofstream handle(txt_file);
handle.close();
TS_ASSERT_EQUALS(v.isValid(ThisIsAFile), "Directory \"" + ThisIsAFile + "\" specified is actually a file");
txt_file.remove();
std::filesystem::remove(txt_file);
}

void testPassesOnNonexistentDirectoryIfYouSaySoForSomeReason() {
Expand All @@ -38,10 +41,10 @@ class DirectoryValidatorTest : public CxxTest::TestSuite {

void testPassesOnExistingDirectory() {
std::string TestDir("./MyTestFolder");
Poco::File dir(TestDir);
dir.createDirectory();
std::filesystem::path dir(TestDir);
TS_ASSERT(std::filesystem::create_directory(dir));
DirectoryValidator v(true);
TS_ASSERT_EQUALS(v.isValid(TestDir), "");
dir.remove(); // clean up your folder
std::filesystem::remove(dir); // clean up your folder
}
};
27 changes: 13 additions & 14 deletions Framework/Kernel/test/FileDescriptorTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
#include "MantidKernel/ConfigService.h"
#include "MantidKernel/FileDescriptor.h"

#include <Poco/File.h>
#include <Poco/Path.h>
#include <filesystem>

using Mantid::Kernel::FileDescriptor;

Expand All @@ -29,18 +28,18 @@ class FileDescriptorTest : public CxxTest::TestSuite {
cfg.reset();
const auto &dataPaths = cfg.getDataSearchDirs();
for (const auto &dataPath : dataPaths) {
Poco::Path nxsPath(dataPath, "CNCS_7860_event.nxs");
if (Poco::File(nxsPath).exists())
m_testNexusPath = nxsPath.toString();
Poco::Path nonNxsPath(dataPath, "CSP79590.raw");
if (Poco::File(nonNxsPath).exists())
m_testNonNexusPath = nonNxsPath.toString();
Poco::Path asciiPath(dataPath, "AsciiExample.txt");
if (Poco::File(asciiPath).exists())
m_testAsciiPath = asciiPath.toString();
Poco::Path emptyFilePath(dataPath, "emptyFile.txt");
if (Poco::File(emptyFilePath).exists())
m_emptyFilePath = emptyFilePath.toString();
const auto nxsPath = std::filesystem::path(dataPath) / "CNCS_7860_event.nxs";
if (std::filesystem::exists(nxsPath))
m_testNexusPath = nxsPath.string();
const auto nonNxsPath = std::filesystem::path(dataPath) / "CSP79590.raw";
if (std::filesystem::exists(nonNxsPath))
m_testNonNexusPath = nonNxsPath.string();
const auto asciiPath = std::filesystem::path(dataPath) / "AsciiExample.txt";
if (std::filesystem::exists(asciiPath))
m_testAsciiPath = asciiPath.string();
const auto emptyFilePath = std::filesystem::path(dataPath) / "emptyFile.txt";
if (std::filesystem::exists(emptyFilePath))
m_emptyFilePath = emptyFilePath.string();

if (!m_testNexusPath.empty() && !m_testNonNexusPath.empty() && !m_testAsciiPath.empty() &&
!m_emptyFilePath.empty())
Expand Down
Loading
Loading