From d267c1a77de20be82116acc574ac85bcfb29d377 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Atgeirr=20Fl=C3=B8=20Rasmussen?= Date: Thu, 6 Apr 2017 12:14:06 +0200 Subject: [PATCH] Create ensureDirectoryExists() free function. --- CMakeLists_files.cmake | 2 ++ opm/simulators/ensureDirectoryExists.cpp | 45 ++++++++++++++++++++++++ opm/simulators/ensureDirectoryExists.hpp | 40 +++++++++++++++++++++ 3 files changed, 87 insertions(+) create mode 100644 opm/simulators/ensureDirectoryExists.cpp create mode 100644 opm/simulators/ensureDirectoryExists.hpp diff --git a/CMakeLists_files.cmake b/CMakeLists_files.cmake index 45b8d3414d..2392fa88da 100644 --- a/CMakeLists_files.cmake +++ b/CMakeLists_files.cmake @@ -63,6 +63,7 @@ list (APPEND MAIN_SOURCE_FILES opm/polymer/TransportSolverTwophasePolymer.cpp opm/polymer/fullyimplicit/FullyImplicitCompressiblePolymerSolver.cpp opm/polymer/fullyimplicit/PolymerPropsAd.cpp + opm/simulators/ensureDirectoryExists.cpp opm/simulators/SimulatorCompressibleTwophase.cpp opm/simulators/SimulatorIncompTwophase.cpp opm/simulators/WellSwitchingLogger.cpp @@ -258,6 +259,7 @@ list (APPEND PUBLIC_HEADER_FILES opm/polymer/fullyimplicit/SimulatorFullyImplicitBlackoilPolymer.hpp opm/polymer/fullyimplicit/SimulatorFullyImplicitBlackoilPolymer_impl.hpp opm/polymer/fullyimplicit/WellStateFullyImplicitBlackoilPolymer.hpp + opm/simulators/ensureDirectoryExists.hpp opm/simulators/ParallelFileMerger.hpp opm/simulators/SimulatorCompressibleTwophase.hpp opm/simulators/SimulatorIncompTwophase.hpp diff --git a/opm/simulators/ensureDirectoryExists.cpp b/opm/simulators/ensureDirectoryExists.cpp new file mode 100644 index 0000000000..0e89917281 --- /dev/null +++ b/opm/simulators/ensureDirectoryExists.cpp @@ -0,0 +1,45 @@ +/* + Copyright 2017 SINTEF Digital, Mathematics and Cybernetics. + Copyright 2017 Statoil ASA. + + This file is part of the Open Porous Media project (OPM). + + OPM is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OPM is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with OPM. If not, see . +*/ + +#include +#include + +namespace Opm +{ + + /// The directory pointed to by 'dirpath' will be created if it + /// does not already exist. Will throw an exception if this cannot + /// be done. + void ensureDirectoryExists(const boost::filesystem::path& dirpath) + { + if (dirpath != ".") { // Do not try to create the current directory. + if (!is_directory(dirpath)) { + try { + create_directories(dirpath); + } + catch (...) { + OPM_THROW(std::runtime_error, "Creating directories failed: " << dirpath); + } + } + } + } + + +} // namespace Opm diff --git a/opm/simulators/ensureDirectoryExists.hpp b/opm/simulators/ensureDirectoryExists.hpp new file mode 100644 index 0000000000..5b22b961f0 --- /dev/null +++ b/opm/simulators/ensureDirectoryExists.hpp @@ -0,0 +1,40 @@ +/* + Copyright 2017 SINTEF Digital, Mathematics and Cybernetics. + Copyright 2017 Statoil ASA. + + This file is part of the Open Porous Media project (OPM). + + OPM is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OPM is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with OPM. If not, see . +*/ + +#ifndef OPM_ENSUREDIRECTORYEXISTS_HEADER_INCLUDED +#define OPM_ENSUREDIRECTORYEXISTS_HEADER_INCLUDED + +#include + +namespace Opm +{ + + /// The directory pointed to by 'dirpath' will be created if it + /// does not already exist. Will throw an exception if this cannot + /// be done. + /// + /// Note that std::string can be passed to this functions, as they + /// can be implicitly converted to boost::filesystem::path objects. + void ensureDirectoryExists(const boost::filesystem::path& dirpath); + + +} // namespace Opm + +#endif // OPM_ENSUREDIRECTORYEXISTS_HEADER_INCLUDED