filesystem.cpp
1 /*
2  * This file is part of CasADi.
3  *
4  * CasADi -- A symbolic framework for dynamic optimization.
5  * Copyright (C) 2010-2023 Joel Andersson, Joris Gillis, Moritz Diehl,
6  * KU Leuven. All rights reserved.
7  * Copyright (C) 2011-2014 Greg Horn
8  *
9  * CasADi is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 3 of the License, or (at your option) any later version.
13  *
14  * CasADi is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with CasADi; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  *
23  */
24 
25 
26 #include "filesystem_impl.hpp"
27 
28 namespace casadi {
29 
30 std::map<std::string, Filesystem::Plugin> Filesystem::solvers_;
31 
32 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
33 std::mutex Filesystem::mutex_solvers_;
34 #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
35 
36 const std::string Filesystem::infix_ = "filesystem";
37 
38 bool Filesystem::is_directory(const std::string& path) {
40  return Filesystem::getPlugin("ghc").exposed.is_directory(path);
41 }
42 
43 bool Filesystem::remove(const std::string& path) {
45  return Filesystem::getPlugin("ghc").exposed.remove(path);
46 }
47 
48 casadi_int Filesystem::remove_all(const std::string& path) {
50  return Filesystem::getPlugin("ghc").exposed.remove_all(path);
51 }
52 
53 bool Filesystem::has_parent_path(const std::string& path) {
55  return Filesystem::getPlugin("ghc").exposed.has_parent_path(path);
56 }
57 
58 std::string Filesystem::parent_path(const std::string& path) {
60  return Filesystem::getPlugin("ghc").exposed.parent_path(path);
61 }
62 
63 bool Filesystem::create_directories(const std::string& path) {
65  return Filesystem::getPlugin("ghc").exposed.create_directories(path);
66 }
67 
68 std::vector<std::string> Filesystem::iterate_directory_names(const std::string& path) {
70  return Filesystem::getPlugin("ghc").exposed.iterate_directory_names(path);
71 }
72 
73 std::string Filesystem::filename(const std::string& path) {
75  return Filesystem::getPlugin("ghc").exposed.filename(path);
76 }
77 
78 std::string Filesystem::absolute(const std::string& path) {
80  return Filesystem::getPlugin("ghc").exposed.absolute(path);
81 }
82 
84  return Filesystem::has_plugin("ghc");
85 }
86 
88  casadi_assert(Filesystem::is_enabled(),
89  "This action requires advanced filesystem access. Compile CasADi with WITH_GC=ON.");
90 }
91 
92 bool has_filesystem(const std::string& name) {
93  return Filesystem::has_plugin(name);
94 }
95 
96 void load_filesystem(const std::string& name) {
98 }
99 
100 std::string doc_filesystem(const std::string& name) {
101  return Filesystem::getPlugin(name).doc;
102 }
103 
104 
106  if (has_parent_path(filename)) {
107  std::string dir = parent_path(filename);
108  if (!is_directory(dir)) {
109  return create_directories(dir);
110  }
111  }
112  return true;
113 }
114 
115 void Filesystem::open(std::ofstream& stream, const std::string& filename,
116  std::ios_base::openmode mode) {
117  if (is_enabled()) {
118  casadi_assert(ensure_directory_exists(filename),
119  "Unable to create the required directory for '" + filename + "'.");
120  }
121  stream.open(filename.c_str(), mode);
122  if (is_enabled()) {
123  casadi_assert(stream.good(),
124  "Error opening stream '" + filename + "'.");
125  } else {
126  casadi_assert(stream.good(),
127  "Error opening stream '" + filename + "'. "
128  "Does the directory exits? "
129  "Note that CasADi needs to be compiled with WITH_GC=ON "
130  "for directories to be automatically created");
131  }
132 }
133 
134 std::ofstream* Filesystem::ofstream_ptr(const std::string& path,
135  std::ios_base::openmode mode) {
136  std::ofstream* ret = new std::ofstream();
137  Filesystem::open(*ret, path, mode);
138  return ret;
139 }
140 
141 } // namespace casadi
static bool has_parent_path(const std::string &path)
Definition: filesystem.cpp:53
static casadi_int remove_all(const std::string &path)
Definition: filesystem.cpp:48
static std::string absolute(const std::string &path)
Definition: filesystem.cpp:78
static const std::string infix_
Infix.
static std::string parent_path(const std::string &path)
Definition: filesystem.cpp:58
static std::string filename(const std::string &path)
Definition: filesystem.cpp:73
static std::map< std::string, Plugin > solvers_
Collection of solvers.
static bool is_directory(const std::string &path)
Definition: filesystem.cpp:38
static bool create_directories(const std::string &path)
Definition: filesystem.cpp:63
static bool ensure_directory_exists(const std::string &path)
Definition: filesystem.cpp:105
static bool is_enabled()
Definition: filesystem.cpp:83
static bool remove(const std::string &path)
Definition: filesystem.cpp:43
static void open(std::ofstream &, const std::string &path, std::ios_base::openmode mode=std::ios_base::out)
Definition: filesystem.cpp:115
static std::vector< std::string > iterate_directory_names(const std::string &path)
Definition: filesystem.cpp:68
static std::ofstream * ofstream_ptr(const std::string &path, std::ios_base::openmode mode=std::ios_base::out)
Definition: filesystem.cpp:134
static void assert_enabled()
Definition: filesystem.cpp:87
static bool has_plugin(const std::string &pname, bool verbose=false)
Check if a plugin is available or can be loaded.
static Plugin & getPlugin(const std::string &pname)
Load and get the creator function.
static Plugin load_plugin(const std::string &pname, bool register_plugin=true, bool needs_lock=true)
Load a plugin dynamically.
The casadi namespace.
Definition: archiver.cpp:28
void load_filesystem(const std::string &name)
Explicitly load a plugin dynamically.
Definition: filesystem.cpp:96
bool has_filesystem(const std::string &name)
Check if a particular plugin is available.
Definition: filesystem.cpp:92
std::string doc_filesystem(const std::string &name)
Get the documentation string for a plugin.
Definition: filesystem.cpp:100
std::vector< casadi_int > path(const std::vector< casadi_int > &map, casadi_int i_start)
std::string filename(const std::string &path)
Definition: ghc.cpp:55