44#define G_ACCESS "GApplicationPars::operator[](std::string&)"
45#define G_AT "GApplicationPar& GApplicationPars::at(int&)"
46#define G_APPEND "GApplicationPars::append(GApplicationPar&)"
47#define G_INSERT1 "GApplicationPar& GApplicationPars::insert(int&, "\
49#define G_INSERT2 "GApplicationPar& GApplicationPars::insert(std::string&, "\
51#define G_REMOVE1 "GApplicationPars::remove(int&)"
52#define G_REMOVE2 "GApplicationPars::remove(std::string&)"
53#define G_EXTEND "GApplicationPars::extend(GApplicationPars&)"
54#define G_LOAD1 "GApplicationPars::load(GFilename&)"
55#define G_LOAD2 "GApplicationPars::load(GFilename&, "\
56 "std::vector<std::string>&)"
57#define G_SAVE "GApplicationPars::save(GFilename&)"
58#define G_OUTPATH "GApplicationPars::outpath(std::string&)"
59#define G_READ "GApplicationPars::read(std::string&)"
60#define G_WRITE "GApplicationPars::write(std::string&)"
61#define G_PARSE "GApplicationPars::parse()"
115 const std::vector<std::string>& args)
121 load(filename, args);
208 std::string msg =
"Parameter \""+name+
"\" has not been found in "
209 "parameter file. Please specify a valid parameter "
235 std::string msg =
"Parameter \""+name+
"\" has not been found in "
236 "parameter file. Please specify a valid parameter "
290 #if defined(G_RANGE_CHECK)
291 if (index < 0 || index >=
size()) {
312 #if defined(G_RANGE_CHECK)
313 if (index < 0 || index >=
size()) {
341 "Attempt to append parameter with name \""+par.
name()+
"\" in"
342 " parameter container, but a parameter with the same name exists"
343 " already at index "+
gammalib::str(inx)+
" in the container.\n"
344 "Every parameter in the parameter container needs a unique name.";
357 std::string line =
parline(parameter, &start, &stop);
380 append(
GApplicationPar(
"clobber",
"b",
"h",
"yes",
"",
"",
"Overwrite existing output files with new output files?"));
402 #if defined(G_RANGE_CHECK)
409 if (index < 0 || index >=
size()) {
419 "Attempt to insert parameter with name \""+par.
name()+
"\" in"
421 ", but a parameter with the same name exists already at index "+
423 "Every parameter in the parameter container needs a unique name.";
436 std::string line =
parline(parameter, &start, &stop);
440 int line_number =
m_line[index];
449 for (
int i = index+1; i <
size(); ++i) {
479 std::string msg =
"Parameter \""+name+
"\" has not been found in "
480 "parameter file. Please specify a valid parameter "
486 return (
insert(index, par));
503 #if defined(G_RANGE_CHECK)
504 if (index < 0 || index >=
size()) {
519 for (
int i = index; i <
size(); ++i) {
545 std::string msg =
"Parameter \""+name+
"\" has not been found in "
546 "parameter file. Please specify a valid parameter "
577 int num = pars.
size();
583 for (
int i = 0; i < num; ++i) {
589 "Attempt to append parameter with name \""+pars[i].name()+
590 "\" to parameter container, but a parameter with the same "
592 "the container. Every parameter in the parameter container "
593 "needs to have a unique name.";
679 std::string msg =
"Parameter file \""+filename.
url()+
"\" not "
680 "found. Please specify an existing parameter "
711 const std::vector<std::string>& args)
717 for (
int i = 1; i < args.size(); ++i) {
720 size_t pos = args[i].find(
"=");
721 if (pos == std::string::npos) {
722 std::string msg =
"No \"=\" symbol found for command line "
723 "parameter \""+args[i]+
"\". Please specify a "
724 "valid command line parameter of the form "
728 std::string name = args[i].substr(0, pos);
729 std::string value = args[i].substr(pos+1);
730 if (name.length() < 1) {
731 std::string msg =
"No parameter name found for before the \"=\" "
732 "symbol for command line parameter \""+args[i]+
733 "\". Please specify a valid command line "
734 "parameter of the form \"name=value\".";
740 std::string msg =
"No parameter with name \""+name+
"\" found. "
741 "Please specify an existing command line "
748 (*this)[name].value(value);
751 std::string msg =
"Assignment of command line parameter \""+
752 args[i]+
"\" failed.";
757 if ((*
this)[name].mode() ==
"q") {
758 (*this)[name].mode(
"h");
760 else if ((*
this)[name].mode() ==
"ql") {
761 (*this)[name].mode(
"hl");
763 else if ((*
this)[name].mode() ==
"lq") {
764 (*this)[name].mode(
"lh");
786 if (path.size() == 0) {
787 std::string msg =
"Parameter file \""+filename+
"\" not found. Please "
788 "make sure that the PFILES environment variable is "
825 for (
int i = 0; i < n_parfile; ++i, ++istring) {
830 for (
int i = 0; i < n_pars; ++i) {
832 int i_start = istring + 1;
833 int i_end = i_start + n_par;
835 par.
pickle(std::vector<std::string>(
string.begin()+i_start,
836 string.begin()+i_end));
842 for (
int i = 0; i < n_line; ++i, ++istring) {
847 for (
int i = 0; i < n_vstart; ++i, ++istring) {
852 for (
int i = 0; i < n_vstop; ++i, ++istring) {
857 m_mode =
string[istring++];
873 std::vector<std::string> string;
883 for (
int i = 0; i <
m_parfile.size(); ++i) {
888 for (
int i = 0; i <
m_pars.size(); ++i) {
889 std::vector<std::string> par =
m_pars[i].pickle();
891 for (
int k = 0; k < par.size(); ++k) {
892 string.push_back(par[k]);
897 for (
int i = 0; i <
m_line.size(); ++i) {
902 for (
int i = 0; i <
m_vstart.size(); ++i) {
907 for (
int i = 0; i <
m_vstop.size(); ++i) {
935 result.append(
"=== GApplicationPars ===");
938 for (
int i = 0; i <
size(); ++i) {
1029 char* ptr = std::getenv(
"PFILES");
1033 std::string pfiles = ptr;
1037 for (
int i = 0; i < dirs.size(); ++i) {
1040 std::string fname = dirs[i] +
"/" + filename;
1043 if (access(fname.c_str(), R_OK) == 0) {
1055 uid_t uid = geteuid();
1056 struct passwd* pw = getpwuid(uid);
1058 std::string fname = std::string(pw->pw_dir) +
"/pfiles/" + filename;
1059 if (access(fname.c_str(), R_OK) == 0) {
1067 if (path.size() == 0) {
1068 ptr = std::getenv(
"GAMMALIB");
1070 std::string fname = std::string(ptr) +
"/syspfiles/" + filename;
1071 if (access(fname.c_str(), R_OK) == 0) {
1079 #ifdef PACKAGE_PREFIX
1080 if (path.size() == 0) {
1081 std::string fname = std::string(PACKAGE_PREFIX) +
"/syspfiles/" +
1083 if (access(fname.c_str(), R_OK) == 0) {
1117 char* ptr = std::getenv(
"PFILES");
1121 std::string pfiles = ptr;
1125 for (
int i = 0; i < dirs.size(); ++i) {
1128 std::string fname = dirs[i] +
"/" + filename;
1131 if (access(fname.c_str(), R_OK) == 0) {
1143 uid_t uid = geteuid();
1144 struct passwd* pw = getpwuid(uid);
1146 std::string fname = std::string(pw->pw_dir) +
"/pfiles/" + filename;
1147 if (access(fname.c_str(), R_OK) == 0) {
1176 if (access(fname.c_str(), R_OK) == 0) {
1207 char* ptr = std::getenv(
"PFILES");
1211 std::string pfiles = ptr;
1215 for (
int i = 0; i < dirs.size(); ++i) {
1218 if (access(dirs[i].c_str(), W_OK) == 0) {
1219 path = dirs[i] +
"/" + filename;
1228 if (path.size() == 0) {
1231 uid_t uid = geteuid();
1232 gid_t gid = getegid();
1233 struct passwd* pw = getpwuid(uid);
1235 std::string msg =
"Unable to determine users home directory.";
1240 path = std::string(pw->pw_dir) +
"/pfiles";
1243 if (access(path.c_str(), F_OK) != 0) {
1244 if (mkdir(path.c_str(),
1245 S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) != 0) {
1246 std::string msg =
"Unable to create \""+path+
"\".";
1252 else if (access(path.c_str(), W_OK) != 0) {
1253 if (chown(path.c_str(), uid, gid) != 0 ||
1255 S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) != 0) {
1256 std::string msg =
"Could not make \""+path+
"\" write "
1257 "accessible for writing of the applications "
1264 path = path +
"/" + filename;
1291 #pragma omp critical(GApplicationsPars_io)
1298 #if defined(G_LOCK_PARFILE)
1300 lock.l_type = F_RDLCK;
1301 lock.l_whence = SEEK_SET;
1304 lock.l_pid = getpid();
1306 if ((fd = open(filename.c_str(), O_RDONLY)) == -1) {
1307 std::string msg =
"Could not open parameter file \""+filename+
1311 #if defined(G_CHECK_LOCK_PARFILE)
1312 if (fcntl(fd, F_SETLKW, &lock) == -1) {
1313 std::string msg =
"Could not get a lock on the parameter file \""+
1318 fcntl(fd, F_SETLKW, &lock);
1323 FILE* fptr = fopen(filename.c_str(),
"r");
1325 std::string msg =
"Could not open the parameter file \""+
1326 filename+
"\" for reading.";
1331 while (fgets(line, n, fptr) != NULL) {
1339 #if defined(G_LOCK_PARFILE)
1341 lock.l_type = F_UNLCK;
1342 #if defined(G_CHECK_LOCK_PARFILE)
1343 if (fcntl(fd, F_SETLK, &lock) == -1) {
1344 std::string msg =
"Could not unlock the parameter file \""+
1349 fcntl(fd, F_SETLK, &lock);
1378 #pragma omp critical(GApplicationsPars_io)
1383 #if defined(G_LOCK_PARFILE)
1385 lock.l_type = F_WRLCK;
1386 lock.l_whence = SEEK_SET;
1389 lock.l_pid = getpid();
1391 if ((fd = open(filename.c_str(), O_WRONLY)) != -1) {
1392 #if defined(G_CHECK_LOCK_PARFILE)
1393 if (fcntl(fd, F_SETLKW, &lock) == -1) {
1394 std::string msg =
"Could not get a lock on the parameter file \""+
1399 fcntl(fd, F_SETLKW, &lock);
1405 FILE* fptr = fopen(filename.c_str(),
"w");
1407 std::string msg =
"Could not open the parameter file \""+
1408 filename+
"\" for writing.";
1413 #if defined(G_LOCK_PARFILE)
1415 if ((fd = open(filename.c_str(), O_WRONLY)) != -1) {
1416 #if defined(G_CHECK_LOCK_PARFILE)
1417 if (fcntl(fd, F_SETLKW, &lock) == -1) {
1419 std::string msg =
"Could not get a lock on the parameter "
1420 "file \""+filename+
"\".";
1424 fcntl(fd, F_SETLKW, &lock);
1431 for (
int i = 0; i <
m_parfile.size(); ++i) {
1432 fprintf(fptr,
"%s",
m_parfile[i].c_str());
1439 #if defined(G_LOCK_PARFILE)
1441 lock.l_type = F_UNLCK;
1442 #if defined(G_CHECK_LOCK_PARFILE)
1443 if (fcntl(fd, F_SETLK, &lock) == -1) {
1444 std::string msg =
"Could not unlock the parameter file \""+
1449 fcntl(fd, F_SETLK, &lock);
1481 for (
int i = 0; i <
m_parfile.size(); ++i) {
1487 if (line.length() == 1 && line[0] ==
'\n')
1491 if (line.length() == 0 || line[0] ==
'#') {
1496 std::string fields[7];
1499 size_t end = line.length() - 1;
1503 for (
size_t pos = 0; pos < line.length(); ++pos) {
1506 if (line[pos] ==
'"') {
1515 if (line[pos] ==
',' || pos == end) {
1534 std::string msg =
"Quotes are not balanced in the following line "
1535 "of the parameter file: "+
1537 "correct the parameter file.";
1543 std::string msg =
"Number of fields ("+
gammalib::str(index)+
") "
1544 "does not correspond to the expected number of "
1545 "7 fields in the following line of the parameter "
1547 "Please correct the parameter file.";
1553 std::string msg =
"Redefinition of parameter name \""+fields[0]+
1554 "\" in the following line of the parameter "
1556 "Please correct the parameter file.";
1563 fields[3], fields[4], fields[5],
1570 std::string msg =
"Error \""+std::string(e.
what())+
"\" encountered "
1571 "in the following line of the parameter file: "+
1573 "Please correct the parameter file.";
1578 if (fields[0] ==
"mode") {
1579 if (fields[3] !=
"h" && fields[3] !=
"q" &&
1580 fields[3] !=
"hl" && fields[3] !=
"ql" &&
1581 fields[3] !=
"lh" && fields[3] !=
"lq") {
1582 std::string msg =
"Mode parameter has invalid value \""+
1583 fields[3]+
"\" in the following line of the "
1586 "Please correct the parameter file.";
1595 for (
int i = 0; i <
m_pars.size(); ++i) {
1596 if (
m_pars[i].mode() ==
"a") {
1618 for (
int i = 0; i <
m_pars.size(); ++i) {
1651 pars.
read(filename);
1657 for (
int i = 0; i <
m_pars.size(); ++i) {
1667 if ((
m_pars[i].type() == pars[inx].type()) &&
1668 pars[inx].is_learn() &&
1669 m_pars[i].m_value != pars[inx].m_value) {
1670 m_pars[i].m_value = pars[inx].m_value;
1671 m_pars[i].m_status = pars[inx].m_status;
1672 m_pars[i].m_update =
true;
1699 for (
int i = 0; i <
size(); ++i) {
1700 if (
m_pars[i].name() == name) {
1730 line.append(par.
name()+
", ");
1731 line.append(par.
type()+
", ");
1732 line.append(par.
mode()+
",");
1733 *start = line.length();
1734 line.append(par.
value()+
",");
1735 *stop = line.length();
1736 line.append(par.
min()+
",");
1737 line.append(par.
max()+
",");
1738 line.append(
"\""+par.
prompt()+
"\"\n");
Application parameter container class definition.
Exception handler interface definition.
Filename class interface definition.
Application parameter class.
void type(const std::string &type)
Set parameter type.
void mode(const std::string &mode)
Set parameter mode.
const std::string & max(void) const
Returns parameter maximum.
void value(const std::string &value)
Set parameter value.
void pickle(const std::vector< std::string > &string)
Set class from pickled string vector.
const std::string & name(void) const
Returns parameter name.
const std::string & prompt(void) const
Returns parameter prompt.
const std::string & min(void) const
Returns parameter minimum.
Application parameter container class.
void reserve(const int &num)
Reserves space for parameters in container.
virtual ~GApplicationPars(void)
Destructor.
std::vector< GApplicationPar > m_pars
Parameters.
void append_standard(void)
Append standard parameters to container.
GApplicationPar & at(const int &index)
Returns reference to parameter.
GApplicationPar & append(const GApplicationPar &par)
Append parameter to container.
void copy_members(const GApplicationPars &pars)
Copy class members.
std::vector< size_t > m_vstop
Column of value stop.
std::vector< std::string > m_parfile
Parameter file lines.
void free_members(void)
Delete class members.
GApplicationPar & operator[](const int &index)
Returns reference to parameter.
GApplicationPars(void)
Void constructor.
bool is_empty(void) const
Signals if there are no parameters in container.
void load(const GFilename &filename)
Load parameters.
void save(const GFilename &filename)
Save parameters.
void clear(void)
Clear parameter container.
int get_index(const std::string &name) const
Return parameter index by name.
std::string parline(GApplicationPar &par, size_t *start, size_t *stop) const
Return parameter file line for a specific parameter.
bool contains(const std::string &name) const
Check parameter exists.
void remove(const int &index)
Remove parameter from container.
void extend(const GApplicationPars &pars)
Append parameter container.
GApplicationPar & insert(const int &index, const GApplicationPar &par)
Insert parameter into container.
void init_members(void)
Initialise class members.
std::string pfiles_path(const std::string &filename) const
Return path to parfile in $PFILES or $HOME/pfiles folder.
std::string syspfiles_path(const std::string &filename) const
Return path to parfile in m_syspfiles folder.
void update(void)
Update parameter file.
std::string print(const GChatter &chatter=NORMAL) const
Print parameters.
GApplicationPars & operator=(const GApplicationPars &pars)
Assignment operator.
std::vector< std::string > pickle(void) const
Return pickled string vector.
std::string m_mode
Effective mode.
std::vector< int > m_line
Line number of parameter.
void synchronise(const std::string &filename)
Synchronise parameter file with the parameter values in a another parameter file.
void parse(void)
Parse parameter file.
void read(const std::string &filename)
Read parameter file.
void write(const std::string &filename) const
Write parameter file.
std::string inpath(const std::string &filename) const
Determine filepath for parameter file input.
std::vector< size_t > m_vstart
Column of value start.
std::string outpath(const std::string &filename) const
Determine filepath for parameter file output.
GApplicationPars * clone(void) const
Clone parameter container.
std::string m_syspfiles
Optional location of syspfiles.
int size(void) const
Return number of parameters in container.
virtual const char * what() const
Exception message.
std::string url(void) const
Return Uniform Resource Locator (URL)
std::string str(const unsigned short int &value)
Convert unsigned short integer value into string.
int toint(const std::string &arg)
Convert string into integer value.
std::string strip_whitespace(const std::string &arg)
Strip leading and trailing whitespace from string.
std::vector< std::string > split(const std::string &s, const std::string &sep)
Split string.
std::string strip_chars(const std::string &arg, const std::string &chars)
Strip leading and trailing character from string.