27 #include "cif++/file.hpp" 28 #include "cif++/gzio.hpp" 34 void file::set_validator(
const validator *v)
37 for (
auto &db : *
this)
41 bool file::is_valid()
const 43 if (m_validator ==
nullptr)
44 std::runtime_error(
"No validator loaded explicitly, cannot continue");
48 result =
d.is_valid() and result;
51 result = validate_links();
58 if (m_validator ==
nullptr)
61 std::cerr <<
"No dictionary loaded explicitly, loading default" << std::endl;
66 bool result = not empty();
69 result =
d.is_valid() and result;
72 result = validate_links();
77 bool file::validate_links()
const 79 if (m_validator ==
nullptr)
80 std::runtime_error(
"No validator loaded explicitly, cannot continue");
84 for (
auto &db : *
this)
85 result = db.validate_links() and result;
90 void file::load_dictionary()
94 auto *audit_conform = front().get(
"audit_conform");
95 if (audit_conform and not audit_conform->empty())
97 std::string name = audit_conform->front().get<std::string>(
"dict_name");
99 if (name ==
"mmcif_pdbx_v50")
100 name =
"mmcif_pdbx.dic";
102 if (not name.empty())
106 load_dictionary(name);
108 catch (
const std::exception &ex)
111 std::cerr <<
"Failed to load dictionary " << std::quoted(name) <<
": " << ex.what() << std::endl;
121 void file::load_dictionary(std::string_view name)
123 set_validator(&validator_factory::instance()[name]);
126 bool file::contains(std::string_view name)
const 128 return std::find_if(begin(), end(), [name](
const datablock &db) {
return iequals(db.name(), name); }) != end();
131 datablock &file::operator[](std::string_view name)
133 auto i = std::find_if(begin(), end(), [name](
const datablock &
c)
134 {
return iequals(c.name(), name); });
143 const datablock &file::operator[](std::string_view name)
const 145 static const datablock s_empty;
146 auto i = std::find_if(begin(), end(), [name](
const datablock &c)
147 {
return iequals(c.name(), name); });
148 return i == end() ? s_empty : *
i;
151 std::tuple<file::iterator, bool> file::emplace(std::string_view name)
164 auto n = std::next(
i);
165 splice(begin(), *
this,
i,
n);
176 auto &db = emplace_front(name);
177 db.set_validator(m_validator);
180 return std::make_tuple(begin(), is_new);
183 void file::load(
const std::filesystem::path &p)
187 gzio::ifstream
in(p);
188 if (not
in.is_open())
189 throw std::runtime_error(
"Could not open file " + p.string());
193 catch (
const std::exception &)
195 throw_with_nested(std::runtime_error(
"Error reading file " + p.string()));
201 auto saved = m_validator;
202 set_validator(
nullptr);
207 if (saved !=
nullptr)
208 set_validator(saved);
213 void file::save(
const std::filesystem::path &p)
const 215 gzio::ofstream outFile(p);
219 void file::save(std::ostream &os)
const 224 for (
auto &db : *
this)
bool iequals(std::string_view a, std::string_view b)
basic_istream< char, std::char_traits< char > > istream