config-file.hpp
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
23 #ifndef NDN_CONFIG_FILE_HPP
24 #define NDN_CONFIG_FILE_HPP
25 
26 #include <string>
27 #include <map>
28 #include <exception>
29 
30 namespace ndn {
31 
36 class ConfigFile {
37 public:
41  ConfigFile();
42 
43  class Error : public std::exception {
44  public:
45  Error(const std::string& errorMessage) throw()
46  : errorMessage_(errorMessage)
47  {
48  }
49 
50  virtual ~Error() throw();
51 
52  std::string
53  Msg() const { return errorMessage_; }
54 
55  virtual const char*
56  what() const throw() { return errorMessage_.c_str(); }
57 
58  private:
59  const std::string errorMessage_;
60  };
61 
68  std::string
69  get(const std::string& key, const std::string& defaultValue) const
70  {
71  std::map<std::string, std::string>::const_iterator found = config_.find(key);
72  if (found != config_.end())
73  return found->second;
74  else
75  return defaultValue;
76  }
77 
82  const std::string&
83  getPath() const { return path_; }
84 
89  const std::map<std::string, std::string>&
90  getParsedConfiguration() const { return config_; }
91 
92 private:
102  static std::string
103  findConfigFile();
104 
108  void
109  parse();
110 
111  std::string path_;
112  std::map<std::string, std::string> config_;
113 };
114 
115 }
116 
117 #endif
Copyright (C) 2013-2016 Regents of the University of California.
Definition: common.hpp:36
const std::string & getPath() const
Get the path of the configuration file.
Definition: config-file.hpp:83
Definition: config-file.hpp:43
const std::map< std::string, std::string > & getParsedConfiguration() const
Get the configuration key/value pairs.
Definition: config-file.hpp:90
A ConfigFile locates, opens, and parses a library configuration file, and holds the values for the ap...
Definition: config-file.hpp:36
ConfigFile()
Locate, open, and parse a library configuration file.
Definition: config-file.cpp:33