33 #include <boost/filesystem/convenience.hpp> 43 #include "boost/version.hpp" 44 #include "boost/filesystem/config.hpp" 45 #if BOOST_VERSION <= 104600 || BOOST_FILESYSTEM_VERSION < 3 47 namespace filesystem {
48 path
absolute(
const path& p) {
return complete(p); }
61 using boost::regex_match;
62 using boost::regex_search;
70 const string PolicyFile::EXT_PAF(
".paf");
71 const string PolicyFile::EXT_XML(
".xml");
73 const regex PolicyFile::SPACE_RE(
"^\\s*$");
74 const regex PolicyFile::COMMENT(
"^\\s*#");
75 const regex PolicyFile::CONTENTID(
"^\\s*#\\s*<\\?cfg\\s+\\w+(\\s+\\w+)*\\s*\\?>", regex::icase);
81 PolicyFile::PolicyFile(
const SupportedFormats::Ptr& fmts)
82 : PolicySource(fmts), Persistable(), _file(PolicyParserFactory::UNRECOGNIZED), _format(), _pfact() {}
84 PolicyFile::PolicyFile(
const string& filepath,
const SupportedFormats::Ptr& fmts)
85 : PolicySource(fmts), Persistable(), _file(filepath), _format(), _pfact() {}
87 PolicyFile::PolicyFile(
const char* filepath,
const SupportedFormats::Ptr& fmts)
88 : PolicySource(fmts), Persistable(), _file(filepath), _format(), _pfact() {}
90 PolicyFile::PolicyFile(
const fs::path& filepath,
const SupportedFormats::Ptr& fmts)
91 : PolicySource(fmts), Persistable(), _file(filepath), _format(), _pfact() {}
93 PolicyFile::PolicyFile(
const string& filepath,
const PolicyParserFactory::Ptr& parserFactory)
94 : PolicySource(), Persistable(), _file(filepath), _format(), _pfact(parserFactory) {
95 if (!_pfact.get()) _format = _pfact->getFormatName();
98 PolicyFile::PolicyFile(
const fs::path& filepath,
const PolicyParserFactory::Ptr& parserFactory)
99 : PolicySource(), Persistable(), _file(filepath), _format(), _pfact(parserFactory) {
100 if (!_pfact.get()) _format = _pfact->getFormatName();
103 PolicyFile::PolicyFile(
const string& filepath,
const fs::path& reposDir,
const SupportedFormats::Ptr& fmts)
104 : PolicySource(fmts), Persistable(), _file(filepath), _format(), _pfact() {
105 if (!_file.has_root_path() && !reposDir.empty()) _file = reposDir / _file;
108 PolicyFile::PolicyFile(
const fs::path& filepath,
const fs::path& reposDir,
const SupportedFormats::Ptr& fmts)
109 : PolicySource(fmts), Persistable(), _file(filepath), _format(), _pfact() {
110 if (!_file.has_root_path() && !reposDir.empty()) _file = reposDir / _file;
113 PolicyFile::PolicyFile(
const string& filepath,
const fs::path& reposDir,
114 const PolicyParserFactory::Ptr& parserFactory)
115 : PolicySource(), Persistable(), _file(filepath), _format(), _pfact(parserFactory) {
116 if (!_file.has_root_path() && !reposDir.empty()) _file = reposDir / _file;
117 if (!_pfact.get()) _format = _pfact->getFormatName();
120 PolicyFile::PolicyFile(
const fs::path& filepath,
const fs::path& reposDir,
121 const PolicyParserFactory::Ptr& parserFactory)
122 : PolicySource(), Persistable(), _file(filepath), _format(), _pfact(parserFactory) {
123 if (!_file.has_root_path() && !reposDir.empty()) _file = reposDir / _file;
124 if (!_pfact.get()) _format = _pfact->getFormatName();
133 const string& PolicyFile::getFormatName() {
134 if (_format.size() != 0)
return _format;
135 if (_file.empty())
return PolicyParserFactory::UNRECOGNIZED;
138 string ext = fs::extension(_file);
140 if (ext == EXT_PAF) {
141 if (_formats->supports(PAFParserFactory::FORMAT_NAME))
142 return cacheName(PAFParserFactory::FORMAT_NAME);
143 }
else if (ext == EXT_XML) {
144 return cacheName(
"XML");
149 if (fs::exists(_file)) {
150 ifstream is(_file.string().c_str());
153 "failure opening Policy file: " +
fs::absolute(_file).string());
158 while (is.good() && (regex_match(line, SPACE_RE) ||
159 (regex_search(line, COMMENT) && !regex_search(line, COMMENT)))) {
164 "failure reading Policy file: " +
fs::absolute(_file).string());
166 (regex_match(line, SPACE_RE) || (regex_search(line, COMMENT) && !regex_search(line, COMMENT)))) {
168 return PAFParserFactory::FORMAT_NAME;
171 return cacheName(_formats->recognizeType(line));
174 return PolicyParserFactory::UNRECOGNIZED;
184 void PolicyFile::load(Policy& policy)
const {
185 PolicyParserFactory::Ptr pfactory = _pfact;
186 if (!pfactory.get()) {
187 const string& fmtname = getFormatName();
188 if (fmtname.empty())
throw LSST_EXCEPT(ParserError,
"Unknown Policy format: " + _file.string());
190 pfactory = _formats->getFactory(fmtname);
195 ifstream
fs(_file.string().c_str());
definition of the PolicyParser class
definition of the PolicyFile class
a class for creating PAFParser objects
definition of Policy parsing exceptions
Reports errors in external input/output operations.
definition of the PAFParserFactory class
A base class for image defects.
path absolute(const path &p)
def line(points, frame=None, origin=afwImage.PARENT, symbs=False, ctype=None, size=0.5)
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
definition of Policy-specific exceptions classes