LSSTApplications  10.0+286,10.0+36,10.0+46,10.0-2-g4f67435,10.1+152,10.1+37,11.0,11.0+1,11.0-1-g47edd16,11.0-1-g60db491,11.0-1-g7418c06,11.0-2-g04d2804,11.0-2-g68503cd,11.0-2-g818369d,11.0-2-gb8b8ce7
LSSTDataManagementBasePackage
Public Member Functions | Protected Types | Static Protected Member Functions | Protected Attributes | List of all members
lsst::meas::algorithms::shapelet::ConfigFile Class Reference

#include <ConfigFile.h>

Public Member Functions

 ConfigFile ()
 
 ConfigFile (const std::string fileName, const std::string delimiter="=", const std::string comment="#", const std::string include="+", const std::string sentry="EndConfigFile")
 
void load (const std::string fileName)
 
void load (const std::string fileName, const std::string delimiter, const std::string comment="", const std::string include="", const std::string sentry="")
 
void read (std::istream &is)
 
void append (const std::string &s)
 
void write (std::ostream &os) const
 
void writeAsComment (std::ostream &os) const
 
ConvertibleStringgetNoCheck (const std::string &key)
 
ConvertibleString get (const std::string &key) const
 
ConvertibleStringoperator[] (const std::string &key)
 
ConvertibleString operator[] (const std::string &key) const
 
template<typename T >
read (const std::string &key) const
 
template<typename T >
read (const std::string &key, const T &value) const
 
template<typename T >
bool readInto (T &var, const std::string &key) const
 
template<typename T >
bool readInto (T &var, const std::string &key, const T &value) const
 
std::string getstr (const std::string key) const throw (const char*)
 
std::string getstr (const std::string key, const std::string defval)
 
template<typename T >
void add (std::string key, const T &value)
 
void remove (const std::string &key)
 
bool keyExists (const std::string &key) const
 
std::string getDelimiter () const
 
std::string getComment () const
 
std::string getInclude () const
 
std::string getSentry () const
 
std::string setDelimiter (const std::string &s)
 
std::string setComment (const std::string &s)
 
std::string setInclude (const std::string &s)
 
std::string setSentry (const std::string &s)
 
size_t size ()
 

Protected Types

typedef std::map< std::string,
ConvertibleString >::iterator 
MapIt
 
typedef std::map< std::string,
ConvertibleString >
::const_iterator 
MapCIt
 

Static Protected Member Functions

static void trim (std::string &s)
 

Protected Attributes

std::string _delimiter
 
std::string _comment
 
std::string _include
 
std::string _sentry
 
std::map< std::string,
ConvertibleString
_contents
 

Detailed Description

Definition at line 239 of file ConfigFile.h.

Member Typedef Documentation

typedef std::map<std::string,ConvertibleString>::const_iterator lsst::meas::algorithms::shapelet::ConfigFile::MapCIt
protected

Definition at line 338 of file ConfigFile.h.

typedef std::map<std::string,ConvertibleString>::iterator lsst::meas::algorithms::shapelet::ConfigFile::MapIt
protected

Definition at line 337 of file ConfigFile.h.

Constructor & Destructor Documentation

lsst::meas::algorithms::shapelet::ConfigFile::ConfigFile ( )

Definition at line 9 of file ConfigFile.cc.

9  :
10  _delimiter("="), _comment("#"), _include("+"), _sentry("EndConfigFile")
11 {
12  // Construct an empty ConfigFile
13 }
lsst::meas::algorithms::shapelet::ConfigFile::ConfigFile ( const std::string  fileName,
const std::string  delimiter = "=",
const std::string  comment = "#",
const std::string  include = "+",
const std::string  sentry = "EndConfigFile" 
)

Definition at line 15 of file ConfigFile.cc.

17  :
18  _delimiter(delimiter), _comment(comment), _include(inc), _sentry(sentry)
19 {
20  // Construct a ConfigFile, getting keys and values from given file
21 
22  std::ifstream in( fileName.c_str() );
23 
24  if( !in ) {
25 #ifdef NOTHROW
26  std::cerr<<"File not found: "<<fileName<<std::endl;
27  exit(1);
28 #else
29  throw FileNotFoundError(fileName);
30 #endif
31  }
32 
33  in >> (*this);
34 }

Member Function Documentation

template<typename T >
void lsst::meas::algorithms::shapelet::ConfigFile::add ( std::string  key,
const T &  value 
)
inline

Definition at line 459 of file ConfigFile.h.

460  {
461  // Add a key with given value
462  trim(key);
463  _contents[key] = value;
464  }
std::map< std::string, ConvertibleString > _contents
Definition: ConfigFile.h:334
void lsst::meas::algorithms::shapelet::ConfigFile::append ( const std::string &  s)
inline

Definition at line 268 of file ConfigFile.h.

269  { std::istringstream ss(s); read(ss); }
ConvertibleString lsst::meas::algorithms::shapelet::ConfigFile::get ( const std::string &  key) const

Definition at line 79 of file ConfigFile.cc.

80 {
81  std::string key2 = key;
82  trim(key2);
83  MapCIt p = _contents.find(key2);
84  if (p == _contents.end()) {
85 #ifdef NOTHROW
86  std::cerr<<"Key not found: "<<key2<<std::endl;
87  exit(1); return key;
88 #else
89  throw ParameterException(
90  "ConfigFile error: key "+key2+" not found");
91 #endif
92  } else {
93  return p->second;
94  }
95 }
std::map< std::string, ConvertibleString > _contents
Definition: ConfigFile.h:334
std::map< std::string, ConvertibleString >::const_iterator MapCIt
Definition: ConfigFile.h:338
std::string lsst::meas::algorithms::shapelet::ConfigFile::getComment ( ) const
inline

Definition at line 311 of file ConfigFile.h.

311 { return _comment; }
std::string lsst::meas::algorithms::shapelet::ConfigFile::getDelimiter ( ) const
inline

Definition at line 310 of file ConfigFile.h.

std::string lsst::meas::algorithms::shapelet::ConfigFile::getInclude ( ) const
inline

Definition at line 312 of file ConfigFile.h.

312 { return _include; }
ConvertibleString & lsst::meas::algorithms::shapelet::ConfigFile::getNoCheck ( const std::string &  key)

Definition at line 72 of file ConfigFile.cc.

73 {
74  std::string key2 = key;
75  trim(key2);
76  return _contents[key2];
77 }
std::map< std::string, ConvertibleString > _contents
Definition: ConfigFile.h:334
std::string lsst::meas::algorithms::shapelet::ConfigFile::getSentry ( ) const
inline

Definition at line 313 of file ConfigFile.h.

313 { return _sentry; }
std::string lsst::meas::algorithms::shapelet::ConfigFile::getstr ( const std::string  key) const
throw (const char *
)

Definition at line 101 of file ConfigFile.cc.

101  {
102  MapCIt p = _contents.find(key);
103 
104  if (p == _contents.end()) {
105  std::stringstream err;
106  err<<"ConfigFile error: key '"<<key<<"' not found";
107  throw err.str().c_str();
108  }
109 
110  std::string val = get(key);
111  return val;
112 }
bool val
std::map< std::string, ConvertibleString > _contents
Definition: ConfigFile.h:334
std::map< std::string, ConvertibleString >::const_iterator MapCIt
Definition: ConfigFile.h:338
std::string lsst::meas::algorithms::shapelet::ConfigFile::getstr ( const std::string  key,
const std::string  defval 
)

Definition at line 114 of file ConfigFile.cc.

116  {
117  MapCIt p = _contents.find(key);
118 
119  std::string val;
120  if (p == _contents.end()) {
121  val = defval;
122  } else {
123  val = get(key);
124  }
125  return val;
126 }
bool val
std::map< std::string, ConvertibleString > _contents
Definition: ConfigFile.h:334
std::map< std::string, ConvertibleString >::const_iterator MapCIt
Definition: ConfigFile.h:338
bool lsst::meas::algorithms::shapelet::ConfigFile::keyExists ( const std::string &  key) const

Definition at line 139 of file ConfigFile.cc.

140 {
141  // Indicate whether key is found
142  MapCIt p = _contents.find( key );
143  return ( p != _contents.end() );
144 }
std::map< std::string, ConvertibleString > _contents
Definition: ConfigFile.h:334
std::map< std::string, ConvertibleString >::const_iterator MapCIt
Definition: ConfigFile.h:338
void lsst::meas::algorithms::shapelet::ConfigFile::load ( const std::string  fileName)
inline

Definition at line 254 of file ConfigFile.h.

255  { std::ifstream fs(fileName.c_str()); read(fs); }
void lsst::meas::algorithms::shapelet::ConfigFile::load ( const std::string  fileName,
const std::string  delimiter,
const std::string  comment = "",
const std::string  include = "",
const std::string  sentry = "" 
)

Definition at line 36 of file ConfigFile.cc.

39 {
40  // Construct a ConfigFile, getting keys and values from given file
41 
42  // the old values
43  std::string delimiter1 = _delimiter;
44  std::string comment1 = _comment;
45  std::string inc1 = _include;
46  std::string sentry1 = _sentry;
47 
48  if (delimiter != "") _delimiter = delimiter;
49  if (comment != "") _comment = comment;
50  if (inc != "") _include = inc;
51  if (sentry != "") _sentry = sentry;
52 
53  std::ifstream in( fileName.c_str() );
54 
55  if( !in ) {
56 #ifdef NOTHROW
57  std::cerr<<"File not found: "<<fileName<<std::endl;
58  exit(1);
59 #else
60  throw FileNotFoundError(fileName);
61 #endif
62  }
63 
64  in >> (*this);
65 
66  _delimiter = delimiter1;
67  _comment = comment1;
68  _include = inc1;
69  _sentry = sentry1;
70 }
ConvertibleString& lsst::meas::algorithms::shapelet::ConfigFile::operator[] ( const std::string &  key)
inline

Definition at line 279 of file ConfigFile.h.

280  { return getNoCheck(key); }
ConvertibleString & getNoCheck(const std::string &key)
Definition: ConfigFile.cc:72
ConvertibleString lsst::meas::algorithms::shapelet::ConfigFile::operator[] ( const std::string &  key) const
inline

Definition at line 281 of file ConfigFile.h.

282  { return get(key); }
void lsst::meas::algorithms::shapelet::ConfigFile::read ( std::istream &  is)

Definition at line 178 of file ConfigFile.cc.

179 {
180  // Load a ConfigFile from is
181  // Read in keys and values, keeping internal whitespace
182  const std::string& delim = _delimiter; // separator
183  const std::string& comm = _comment; // comment
184  const std::string& inc = _include; // include directive
185  const std::string& sentry = _sentry; // end of file sentry
186  const std::string::size_type skip = delim.size(); // length of separator
187 
188  std::string nextLine = "";
189  // might need to read ahead to see where value ends
190 
191  while( is || nextLine.size() > 0 ) {
192  // Read an entire line at a time
193  std::string line;
194  if( nextLine.size() > 0 ) {
195  line = nextLine; // we read ahead; use it now
196  nextLine = "";
197  } else {
198  std::getline( is, line );
199  }
200 
201  // Ignore comments
202  line = line.substr( 0, line.find(comm) );
203 
204  // Remove leading and trailing whitespace
205  trim(line);
206 
207  // If line is blank, go on to next line.
208  if (line.size() == 0) continue;
209 
210  // Check for include directive (only at start of line)
211  if (line.find(inc) == 0) {
212  line.erase(0,inc.size());
213  std::stringstream ss(line);
214  std::string fileName;
215  ss >> fileName;
216  load(fileName);
217  // implcitly skip the rest of the line.
218  continue;
219  }
220 
221  // Check for end of file sentry
222  if( sentry != "" && line.find(sentry) != std::string::npos ) return;
223 
224  // Parse the line if it contains a delimiter
225  std::string::size_type delimPos = line.find( delim );
226  if( delimPos < std::string::npos ) {
227  // Extract the key
228  std::string key = line.substr( 0, delimPos );
229  line.replace( 0, delimPos+skip, "" );
230 
231  // See if value continues on the next line
232  // Stop at blank line, next line with a key, end of stream,
233  // or end of file sentry
234  bool terminate = false;
235  while( !terminate && is ) {
236  std::getline( is, nextLine );
237  terminate = true;
238 
239  std::string nextLineCopy = nextLine;
240  ConfigFile::trim(nextLineCopy);
241  if( nextLineCopy == "" ) continue;
242 
243  nextLine = nextLine.substr( 0, nextLine.find(comm) );
244  if( nextLine.find(delim) != std::string::npos )
245  continue;
246  if( sentry != "" && nextLine.find(sentry) != std::string::npos )
247  continue;
248 
249  nextLineCopy = nextLine;
250  ConfigFile::trim(nextLineCopy);
251  if( nextLineCopy != "" ) line += "\n";
252  line += nextLine;
253  terminate = false;
254  }
255 
256  // Store key and value
257  ConfigFile::trim(key);
258  ConfigFile::trim(line);
259  _contents[key] = line; // overwrites if key is repeated
260  }
261  }
262 }
void load(const std::string fileName)
Definition: ConfigFile.h:254
std::map< std::string, ConvertibleString > _contents
Definition: ConfigFile.h:334
template<typename T >
T lsst::meas::algorithms::shapelet::ConfigFile::read ( const std::string &  key) const
inline

Definition at line 347 of file ConfigFile.h.

348  {
349  // Read the value corresponding to key
350  std::string key2 = key;
351  trim(key2);
352  MapCIt p = _contents.find(key2);
353  if(p == _contents.end()) {
354 #ifdef NOTHROW
355  std::cerr<<"Key not found: "<<key2<<std::endl;
356  exit(1);
357 #else
358  throw ParameterException(
359  "ConfigFile error: key "+key2+" not found");
360 #endif
361  }
362  T ret;
363  try {
364  ret = p->second;
365  } catch (ParameterException& e) {
366  xdbg<<"Caught ParameterException: \n"<<e.what()<<std::endl;
367  throw ParameterException(
368  "ConfigFile error: Could not convert entry for key " +
369  key2 +
370  " to given type.\nCaught error from ConvertibleString: \n" +
371  e.what());
372  }
373  return ret;
374  }
std::map< std::string, ConvertibleString > _contents
Definition: ConfigFile.h:334
std::map< std::string, ConvertibleString >::const_iterator MapCIt
Definition: ConfigFile.h:338
#define xdbg
Definition: dbg.h:70
template<typename T >
T lsst::meas::algorithms::shapelet::ConfigFile::read ( const std::string &  key,
const T &  value 
) const
inline

Definition at line 378 of file ConfigFile.h.

379  {
380  // Return the value corresponding to key or given default value
381  // if key is not found
382  std::string key2 = key;
383  trim(key2);
384  MapCIt p = _contents.find(key2);
385  if(p == _contents.end()) {
386  return value;
387  } else {
388  T ret;
389  try {
390  ret = p->second;
391  } catch (ParameterException& e) {
392  xdbg<<"Caught ParameterException: \n"<<e.what()<<std::endl;
393  throw ParameterException(
394  "ConfigFile error: Could not convert entry for key " +
395  key2 +
396  " to given type.\nCaught error from ConvertibleString: \n" +
397  e.what());
398  }
399  return ret;
400  }
401  }
std::map< std::string, ConvertibleString > _contents
Definition: ConfigFile.h:334
std::map< std::string, ConvertibleString >::const_iterator MapCIt
Definition: ConfigFile.h:338
#define xdbg
Definition: dbg.h:70
template<typename T >
bool lsst::meas::algorithms::shapelet::ConfigFile::readInto ( T &  var,
const std::string &  key 
) const
inline

Definition at line 405 of file ConfigFile.h.

406  {
407  // Get the value corresponding to key and store in var
408  // Return true if key is found
409  // Otherwise leave var untouched
410  std::string key2 = key;
411  trim(key2);
412  MapCIt p = _contents.find(key2);
413  bool isFound = ( p != _contents.end() );
414  if(isFound) {
415  try {
416  var = p->second;
417  } catch (ParameterException& e) {
418  xdbg<<"Caught ParameterException: \n"<<e.what()<<std::endl;
419  throw ParameterException(
420  "ConfigFile error: Could not convert entry for key " +
421  key2 +
422  " to given type.\nCaught error from ConvertibleString: \n" +
423  e.what());
424  }
425  }
426  return isFound;
427  }
std::map< std::string, ConvertibleString > _contents
Definition: ConfigFile.h:334
std::map< std::string, ConvertibleString >::const_iterator MapCIt
Definition: ConfigFile.h:338
#define xdbg
Definition: dbg.h:70
template<typename T >
bool lsst::meas::algorithms::shapelet::ConfigFile::readInto ( T &  var,
const std::string &  key,
const T &  value 
) const
inline

Definition at line 431 of file ConfigFile.h.

433  {
434  // Get the value corresponding to key and store in var
435  // Return true if key is found
436  // Otherwise set var to given default
437  std::string key2 = key;
438  trim(key2);
439  MapCIt p = _contents.find(key2);
440  bool isFound = ( p != _contents.end() );
441  if(isFound) {
442  try {
443  var = p->second;
444  } catch (ParameterException& e) {
445  xdbg<<"Caught ParameterException: \n"<<e.what()<<std::endl;
446  throw ParameterException(
447  "ConfigFile error: Could not convert entry for key " +
448  key2 +
449  " to given type.\nCaught error from ConvertibleString: \n" +
450  e.what());
451  }
452  } else {
453  var = value;
454  }
455  return isFound;
456  }
std::map< std::string, ConvertibleString > _contents
Definition: ConfigFile.h:334
std::map< std::string, ConvertibleString >::const_iterator MapCIt
Definition: ConfigFile.h:338
#define xdbg
Definition: dbg.h:70
void lsst::meas::algorithms::shapelet::ConfigFile::remove ( const std::string &  key)

Definition at line 131 of file ConfigFile.cc.

132 {
133  // Remove key and its value
134  _contents.erase( _contents.find( key ) );
135  return;
136 }
std::map< std::string, ConvertibleString > _contents
Definition: ConfigFile.h:334
std::string lsst::meas::algorithms::shapelet::ConfigFile::setComment ( const std::string &  s)
inline

Definition at line 317 of file ConfigFile.h.

318  { std::string old = _comment; _comment = s; return old; }
std::string lsst::meas::algorithms::shapelet::ConfigFile::setDelimiter ( const std::string &  s)
inline

Definition at line 315 of file ConfigFile.h.

316  { std::string old = _delimiter; _delimiter = s; return old; }
std::string lsst::meas::algorithms::shapelet::ConfigFile::setInclude ( const std::string &  s)
inline

Definition at line 319 of file ConfigFile.h.

320  { std::string old = _include; _include = s; return old; }
std::string lsst::meas::algorithms::shapelet::ConfigFile::setSentry ( const std::string &  s)
inline

Definition at line 321 of file ConfigFile.h.

322  { std::string old = _sentry; _sentry = s; return old; }
size_t lsst::meas::algorithms::shapelet::ConfigFile::size ( )
inline

Definition at line 324 of file ConfigFile.h.

324 { return _contents.size(); }
std::map< std::string, ConvertibleString > _contents
Definition: ConfigFile.h:334
void lsst::meas::algorithms::shapelet::ConfigFile::trim ( std::string &  s)
staticprotected

Definition at line 147 of file ConfigFile.cc.

148 {
149  // Remove leading and trailing whitespace
150  std::string whiteSpace = " \n\t\v\r\f";
151  s.erase( 0, s.find_first_not_of(whiteSpace) );
152  s.erase( s.find_last_not_of(whiteSpace) + 1);
153 }
void lsst::meas::algorithms::shapelet::ConfigFile::write ( std::ostream &  os) const

Definition at line 156 of file ConfigFile.cc.

157 {
158  // Save a ConfigFile to os
159  for(MapCIt p = _contents.begin(); p != _contents.end(); ++p ) {
160  os << p->first << " " << _delimiter << " ";
161  os << p->second << std::endl;
162  }
163 }
std::map< std::string, ConvertibleString > _contents
Definition: ConfigFile.h:334
std::map< std::string, ConvertibleString >::const_iterator MapCIt
Definition: ConfigFile.h:338
void lsst::meas::algorithms::shapelet::ConfigFile::writeAsComment ( std::ostream &  os) const

Definition at line 165 of file ConfigFile.cc.

166 {
167  // Save a ConfigFile to os
168  for(MapCIt p = _contents.begin(); p != _contents.end(); ++p ) {
169  std::string f = p->first;
170  std::string s = p->second;
171  std::replace(f.begin(),f.end(),'\n',' ');
172  std::replace(s.begin(),s.end(),'\n',' ');
173  os << _comment << " " << f << " " << _delimiter << " ";
174  os << s << std::endl;
175  }
176 }
std::map< std::string, ConvertibleString > _contents
Definition: ConfigFile.h:334
std::map< std::string, ConvertibleString >::const_iterator MapCIt
Definition: ConfigFile.h:338

Member Data Documentation

std::string lsst::meas::algorithms::shapelet::ConfigFile::_comment
protected

Definition at line 331 of file ConfigFile.h.

std::map<std::string,ConvertibleString> lsst::meas::algorithms::shapelet::ConfigFile::_contents
protected

Definition at line 334 of file ConfigFile.h.

std::string lsst::meas::algorithms::shapelet::ConfigFile::_delimiter
protected

Definition at line 330 of file ConfigFile.h.

std::string lsst::meas::algorithms::shapelet::ConfigFile::_include
protected

Definition at line 332 of file ConfigFile.h.

std::string lsst::meas::algorithms::shapelet::ConfigFile::_sentry
protected

Definition at line 333 of file ConfigFile.h.


The documentation for this class was generated from the following files: