Reference documentation for deal.II version GIT relicensing-837-gaf73e64026 2024-06-15 22:40:02+00:00
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
Loading...
Searching...
No Matches
path_search.cc
Go to the documentation of this file.
1// ------------------------------------------------------------------------
2//
3// SPDX-License-Identifier: LGPL-2.1-or-later
4// Copyright (C) 2005 - 2023 by the deal.II authors
5//
6// This file is part of the deal.II library.
7//
8// Part of the source code is dual licensed under Apache-2.0 WITH
9// LLVM-exception OR LGPL-2.1-or-later. Detailed license information
10// governing the source code and code contributions can be found in
11// LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II.
12//
13// ------------------------------------------------------------------------
14
15
19
20#include <algorithm>
21#include <cstdio>
22#include <iostream>
23
25
26
27std::map<std::string, std::vector<std::string>> PathSearch::path_lists;
28std::map<std::string, std::vector<std::string>> PathSearch::suffix_lists;
29std::string PathSearch::empty("");
30
31void
33{
34 std::vector<std::string> v;
35 v.emplace_back();
36 path_lists.insert(map_type(std::string("PARAMETER"), v));
37
38 /*
39 * TODO: re-enable some sensible default paths. Maier, 2012
40 */
41 path_lists.insert(map_type(std::string("MESH"), v));
42
43 v.clear();
44 v.emplace_back();
45 v.emplace_back(".prm");
46 suffix_lists.insert(map_type(std::string("PARAMETER"), v));
47
48 /*
49 * TODO: "Would require linking with the deal.II libraries"? This .cc
50 * file gets compiled into the library... maier, 2012
51 */
52 // We cannot use the GridIn class
53 // to query the formats, since this
54 // would require linking with the
55 // deal.II libraries.
56 v.clear();
57 v.emplace_back();
58 v.emplace_back(".inp");
59 v.emplace_back(".xda");
60 v.emplace_back(".dbmesh");
61 v.emplace_back(".dat");
62 v.emplace_back(".plt");
63 v.emplace_back(".nc");
64 v.emplace_back(".msh");
65 suffix_lists.insert(map_type(std::string("MESH"), v));
66}
67
68std::vector<std::string> &
69PathSearch::get_path_list(const std::string &cls)
70{
71 if (path_lists.empty())
73
74 // Modified by Luca Heltai. If a class is not there, add it
75 if (path_lists.count(cls) == 0)
77
78 // Assert(path_lists.count(cls) != 0, ExcNoClass(cls));
79 Assert(path_lists.count(cls) != 0, ExcInternalError());
80
81 return path_lists.find(cls)->second;
82}
83
84
85std::vector<std::string> &
86PathSearch::get_suffix_list(const std::string &cls)
87{
88 // This is redundant. The constructor should have already called the
89 // add_path function with the path_list bit...
90
91 // Modified by Luca Heltai. If a class is not there, add it
92 if (suffix_lists.count(cls) == 0)
94
95 // Assert(suffix_lists.count(cls) != 0, ExcNoClass(cls));
97
98 return suffix_lists.find(cls)->second;
99}
100
101
102PathSearch::PathSearch(const std::string &cls, const unsigned int debug)
103 : cls(cls)
104 , my_path_list(get_path_list(cls))
105 , my_suffix_list(get_suffix_list(cls))
106 , debug(debug)
107{}
108
109
110std::string
111PathSearch::find(const std::string &filename,
112 const std::string &suffix,
113 const char *open_mode)
114{
115 std::vector<std::string>::const_iterator path;
116 const std::vector<std::string>::const_iterator endp = my_path_list.end();
117
118 std::string real_name;
119
120 if (debug > 2)
121 deallog << "PathSearch[" << cls << "] " << my_path_list.size()
122 << " directories " << std::endl;
123
124 // Try to open file in the various directories we have
125 for (path = my_path_list.begin(); path != endp; ++path)
126 {
127 // see if the file exists as given, i.e., with
128 // the whole filename specified, including (possibly)
129 // the suffix
130 {
131 real_name = *path + filename;
132 if (debug > 1)
133 deallog << "PathSearch[" << cls << "] trying " << real_name
134 << std::endl;
135 FILE *fp = fopen(real_name.c_str(), open_mode);
136 if (fp != nullptr)
137 {
138 if (debug > 0)
139 deallog << "PathSearch[" << cls << "] opened " << real_name
140 << std::endl;
141 fclose(fp);
142 return real_name;
143 }
144 }
145
146 // try again with the suffix appended, unless there is
147 // no suffix
148 if (!suffix.empty())
149 {
150 real_name = *path + filename + suffix;
151 if (debug > 1)
152 deallog << "PathSearch[" << cls << "] trying " << real_name
153 << std::endl;
154 FILE *fp = fopen(real_name.c_str(), open_mode);
155 if (fp != nullptr)
156 {
157 if (debug > 0)
158 deallog << "PathSearch[" << cls << "] opened " << real_name
159 << std::endl;
160 fclose(fp);
161 return real_name;
162 }
163 }
164 }
165 AssertThrow(false, ExcFileNotFound(filename, cls));
166 return std::string("");
167}
168
169std::string
170PathSearch::find(const std::string &filename, const char *open_mode)
171{
172 std::vector<std::string>::const_iterator suffix;
173 const std::vector<std::string>::const_iterator ends = my_suffix_list.end();
174
175 if (debug > 2)
176 deallog << "PathSearch[" << cls << "] " << my_path_list.size()
177 << " directories " << my_suffix_list.size() << " suffixes"
178 << std::endl;
179
180 for (suffix = my_suffix_list.begin(); suffix != ends; ++suffix)
181 {
182 try
183 {
184 return find(filename, *suffix, open_mode);
185 }
186 catch (ExcFileNotFound &)
187 {
188 continue;
189 }
190 }
191 AssertThrow(false, ExcFileNotFound(filename, cls));
192 return std::string("");
193}
194
195
196void
197PathSearch::add_class(const std::string &cls)
198{
199 // Make sure standard classes are
200 // initialized first
201 if (path_lists.empty())
203 // Add empty path and empty suffix
204 // for new class
205 std::vector<std::string> v;
206 v.emplace_back();
207 path_lists.insert(map_type(cls, v));
208 suffix_lists.insert(map_type(cls, v));
209}
210
211
212void
213PathSearch::add_path(const std::string &path, Position pos)
214{
215 if (pos == back)
216 my_path_list.push_back(path);
217 else if (pos == front)
218 my_path_list.insert(my_path_list.begin(), path);
219 else if (pos == after_none)
220 {
221 std::vector<std::string>::iterator i =
222 std::find(my_path_list.begin(), my_path_list.end(), empty);
223 if (i != my_path_list.end())
224 ++i;
225 my_path_list.insert(i, path);
226 }
227}
228
229
230void
231PathSearch::add_suffix(const std::string &suffix, Position pos)
232{
233 if (pos == back)
234 my_suffix_list.push_back(suffix);
235 else if (pos == front)
236 my_suffix_list.insert(my_suffix_list.begin(), suffix);
237 else if (pos == after_none)
238 {
239 std::vector<std::string>::iterator i =
240 std::find(my_suffix_list.begin(), my_suffix_list.end(), empty);
241 if (i != my_suffix_list.end())
242 ++i;
243 my_suffix_list.insert(i, suffix);
244 }
245}
246
247
248
std::string find(const std::string &filename, const char *open_mode="r")
static std::vector< std::string > & get_path_list(const std::string &cls)
static std::vector< std::string > & get_suffix_list(const std::string &cls)
static std::map< std::string, std::vector< std::string > > path_lists
const std::string cls
void add_path(const std::string &path, Position pos=back)
std::vector< std::string > & my_suffix_list
static std::map< std::string, std::vector< std::string > > suffix_lists
static void add_class(const std::string &cls)
const unsigned int debug
std::vector< std::string > & my_path_list
@ back
Add new item at end of list.
Definition path_search.h:91
@ after_none
Add in path list after empty element.
Definition path_search.h:95
@ front
Add new item at front of list.
Definition path_search.h:93
void add_suffix(const std::string &suffix, Position pos=back)
std::map< std::string, std::vector< std::string > >::value_type map_type
PathSearch(const std::string &cls, const unsigned int debug=0)
static std::string empty
static void initialize_classes()
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:502
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:503
static ::ExceptionBase & ExcFileNotFound(std::string arg1, std::string arg2)
#define Assert(cond, exc)
static ::ExceptionBase & ExcInternalError()
#define AssertThrow(cond, exc)
LogStream deallog
Definition logstream.cc:36