Bredbandskollen CLI  1.2
Asynchronous network task engine
taskconfig.h
1 // Copyright (c) 2018 IIS (The Internet Foundation in Sweden)
2 // Written by Göran Andersson <initgoran@gmail.com>
3 
4 #pragma once
5 
6 #include <map>
7 #include <set>
8 #include <string>
9 #include <stdexcept>
10 #include <iostream>
11 #include <fstream>
12 
14 class BadTaskConfig : public std::exception {
15 public:
16  BadTaskConfig(const std::string &msg = "cannot read config file") :
17  err_msg(msg) {
18  }
19  const char *what() const noexcept override;
20  BadTaskConfig(const BadTaskConfig &old) : err_msg(old.err_msg) {
21  }
22  ~BadTaskConfig() noexcept override;
23  std::string err_msg;
24 };
25 
44 class TaskConfig {
45 public:
48 
50  TaskConfig(std::istream &cfg_stream);
51 
53  TaskConfig(const std::string &cfg_text);
54 
56  void add(const std::string &key, const std::string &val);
57 
59  void set(const std::string &key, const std::string &val) {
60  the_config.erase(key);
61  add(key, val);
62  }
63 
65  void erase(const std::string &key) {
66  the_config.erase(key);
67  }
68 
70  void setDefault(const std::string &key, const std::string &val) {
71  if (the_config.find(key) == the_config.end())
72  add(key, val);
73  }
74 
76  void addLine(const std::string &line);
77 
79  std::multimap<std::string, std::string>::iterator begin() {
80  return the_config.begin();
81  }
82 
84  std::multimap<std::string, std::string>::iterator end() {
85  return the_config.end();
86  }
87 
89  std::multimap<std::string, std::string>::const_iterator begin() const {
90  return the_config.begin();
91  }
92 
94  std::multimap<std::string, std::string>::const_iterator end() const {
95  return the_config.end();
96  }
97 
99  void workerAttributes(const std::set<std::string> &attrs);
100 
102  static TaskConfig load(const std::string &filename);
103 
105  const std::multimap<std::string, std::string> &cfg() const {
106  return the_config;
107  }
108 
110  std::string value(const std::string &key) const;
111 
113  bool hasKey(const std::string &key) const {
114  return the_config.find(key) != the_config.end();
115  }
116 
118  std::pair<std::multimap<std::string, std::string>::const_iterator,
119  std::multimap<std::string, std::string>::const_iterator>
120  range(const std::string &key) const {
121  return the_config.equal_range(key);
122  }
123 
129  void openlog(std::ofstream &logger, bool append = false) const;
130 
136  std::set<std::string>
137  parseList(const std::string &category = "whitelist") const;
138 
140  void parseArgs(int &argc, char **&argv);
141 
144  std::map<std::string, std::string>
145  parseKeyVal(const std::string &category = "user") const;
146 
148  bool saveJsonToFile(const std::string &filename);
149 
154  static TaskConfig loadJsonFromFile(const std::string &filename);
155 
156 private:
157  void _load(std::istream &cfg_stream);
158  std::multimap<std::string, std::string> the_config;
159 };
160 
161 std::ostream &operator<<(std::ostream &out, const TaskConfig &tc);
Exception thrown on syntax errors in task config.
Definition: taskconfig.h:14
Read configuration from file or string.
Definition: taskconfig.h:44
bool hasKey(const std::string &key) const
Return true if key exists, otherwise false:
Definition: taskconfig.h:113
void erase(const std::string &key)
Remove value(s) of a directive.
Definition: taskconfig.h:65
std::string value(const std::string &key) const
Return value of last occurence of key, or empty string.
Definition: taskconfig.cpp:31
std::multimap< std::string, std::string >::iterator begin()
Start iterator to loop over the config.
Definition: taskconfig.h:79
void setDefault(const std::string &key, const std::string &val)
Set value of a directive unless already set.
Definition: taskconfig.h:70
bool saveJsonToFile(const std::string &filename)
Store contents as a JSON object. Return false on failure.
Definition: taskconfig.cpp:153
std::multimap< std::string, std::string >::iterator end()
End iterator to loop over the config.
Definition: taskconfig.h:84
std::set< std::string > parseList(const std::string &category="whitelist") const
Split config value into non-blank strings.
Definition: taskconfig.cpp:99
std::pair< std::multimap< std::string, std::string >::const_iterator, std::multimap< std::string, std::string >::const_iterator > range(const std::string &key) const
Return a range of the key/value paris for the given key.
Definition: taskconfig.h:120
void add(const std::string &key, const std::string &val)
Add a directive to the config.
Definition: taskconfig.cpp:52
static TaskConfig load(const std::string &filename)
Read config from file.
Definition: taskconfig.cpp:26
std::multimap< std::string, std::string >::const_iterator end() const
End const iterator to loop over the config.
Definition: taskconfig.h:94
void addLine(const std::string &line)
Incrementally add to the config.
Definition: taskconfig.cpp:56
const std::multimap< std::string, std::string > & cfg() const
Return the parsed configuration.
Definition: taskconfig.h:105
TaskConfig()
Empty configuration.
Definition: taskconfig.h:47
void set(const std::string &key, const std::string &val)
Replace value(s) of a directive with a new one.
Definition: taskconfig.h:59
void parseArgs(int &argc, char **&argv)
Parse command line arguments starting with "--":
Definition: taskconfig.cpp:111
void workerAttributes(const std::set< std::string > &attrs)
Make a set of directives available to worker processes.
Definition: taskconfig.cpp:72
std::map< std::string, std::string > parseKeyVal(const std::string &category="user") const
Definition: taskconfig.cpp:138
static TaskConfig loadJsonFromFile(const std::string &filename)
Load key/value pairs from JSON object.
Definition: taskconfig.cpp:171
void openlog(std::ofstream &logger, bool append=false) const
Log to the file specified by the logfile directive.
Definition: taskconfig.cpp:39
std::multimap< std::string, std::string >::const_iterator begin() const
Start const iterator to loop over the config.
Definition: taskconfig.h:89