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 46 namespace boost {
namespace filesystem {
64 using boost::regex_match;
65 using boost::regex_search;
71 const string PolicyFile::EXT_PAF(
".paf");
72 const string PolicyFile::EXT_XML(
".xml");
74 const regex PolicyFile::SPACE_RE(
"^\\s*$");
75 const regex PolicyFile::COMMENT(
"^\\s*#");
77 PolicyFile::CONTENTID(
"^\\s*#\\s*<\\?cfg\\s+\\w+(\\s+\\w+)*\\s*\\?>",
84 PolicyFile::PolicyFile(
const SupportedFormats::Ptr& fmts)
85 : PolicySource(fmts), Persistable(),
86 _file(PolicyParserFactory::UNRECOGNIZED), _format(), _pfact()
89 PolicyFile::PolicyFile(
const string& filepath,
90 const SupportedFormats::Ptr& fmts)
91 : PolicySource(fmts), Persistable(), _file(filepath), _format(), _pfact()
94 PolicyFile::PolicyFile(
const char *filepath,
95 const SupportedFormats::Ptr& fmts)
96 : PolicySource(fmts), Persistable(), _file(filepath), _format(), _pfact()
99 PolicyFile::PolicyFile(
const fs::path& filepath,
100 const SupportedFormats::Ptr& fmts)
101 : PolicySource(fmts), Persistable(), _file(filepath), _format(), _pfact()
104 PolicyFile::PolicyFile(
const string& filepath,
105 const PolicyParserFactory::Ptr& parserFactory)
106 : PolicySource(), Persistable(),
107 _file(filepath), _format(), _pfact(parserFactory)
109 if (! _pfact.get()) _format = _pfact->getFormatName();
112 PolicyFile::PolicyFile(
const fs::path& filepath,
113 const PolicyParserFactory::Ptr& parserFactory)
114 : PolicySource(), Persistable(),
115 _file(filepath), _format(), _pfact(parserFactory)
117 if (! _pfact.get()) _format = _pfact->getFormatName();
120 PolicyFile::PolicyFile(
const string& filepath,
const fs::path& reposDir,
121 const SupportedFormats::Ptr& fmts)
122 : PolicySource(fmts), Persistable(), _file(filepath), _format(), _pfact()
124 if (! _file.has_root_path() && ! reposDir.empty())
125 _file = reposDir / _file;
128 PolicyFile::PolicyFile(
const fs::path& filepath,
const fs::path& reposDir,
129 const SupportedFormats::Ptr& fmts)
130 : PolicySource(fmts), Persistable(), _file(filepath), _format(), _pfact()
132 if (! _file.has_root_path() && ! reposDir.empty())
133 _file = reposDir / _file;
136 PolicyFile::PolicyFile(
const string& filepath,
const fs::path& reposDir,
137 const PolicyParserFactory::Ptr& parserFactory)
138 : PolicySource(), Persistable(),
139 _file(filepath), _format(), _pfact(parserFactory)
141 if (! _file.has_root_path() && ! reposDir.empty())
142 _file = reposDir / _file;
143 if (! _pfact.get()) _format = _pfact->getFormatName();
146 PolicyFile::PolicyFile(
const fs::path& filepath,
const fs::path& reposDir,
147 const PolicyParserFactory::Ptr& parserFactory)
148 : PolicySource(), Persistable(),
149 _file(filepath), _format(), _pfact(parserFactory)
151 if (! _file.has_root_path() && ! reposDir.empty())
152 _file = reposDir / _file;
153 if (! _pfact.get()) _format = _pfact->getFormatName();
162 const string& PolicyFile::getFormatName() {
163 if (_format.size() != 0)
return _format;
164 if (_file.empty())
return PolicyParserFactory::UNRECOGNIZED;
167 string ext = fs::extension(_file);
169 if (ext == EXT_PAF) {
170 if (_formats->supports(PAFParserFactory::FORMAT_NAME))
171 return cacheName(PAFParserFactory::FORMAT_NAME);
173 else if (ext == EXT_XML) {
174 return cacheName(
"XML");
179 if (fs::exists(_file)) {
180 ifstream is(_file.string().c_str());
183 "failure opening Policy file: " 190 (regex_match(line, SPACE_RE) ||
191 (regex_search(line, COMMENT) && !regex_search(line, COMMENT))))
196 "failure reading Policy file: " 199 (regex_match(line, SPACE_RE) ||
200 (regex_search(line, COMMENT) && !regex_search(line, COMMENT))))
203 return PAFParserFactory::FORMAT_NAME;
206 return cacheName(_formats->recognizeType(line));
209 return PolicyParserFactory::UNRECOGNIZED;
219 void PolicyFile::load(Policy& policy)
const {
221 PolicyParserFactory::Ptr pfactory = _pfact;
222 if (! pfactory.get()) {
223 const string& fmtname = getFormatName();
225 throw LSST_EXCEPT(ParserError,
"Unknown Policy format: " + _file.string());
227 pfactory = _formats->getFactory(fmtname);
232 ifstream
fs(_file.string().c_str());
235 "failure opening Policy file: " 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