Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <map>
#include <utility>
#include <algorithm>
// trim from start
static inline std::string &ltrim(std::string &s) {
s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
return s;
}
// trim from end
static inline std::string &rtrim(std::string &s) {
s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
return s;
}
// trim from both ends
static inline std::string &trim(std::string &s) {
return ltrim(rtrim(s));
}
typedef std::map<std::string, std::string> t_dict;
const std::string configPath = "pokus.conf";
class Config {
private:
t_dict dict;
public:
Config(void) {setDefaults();};
void setDefaults(void);
void set(std::string key, std::string value);
std::string get(std::string key) {return dict[key];};
void print(void);
};
void Config::setDefaults(void) {
dict["format_battery"] = "^s[left;#aabbaa;Bat ]^s[left;#dddddd;%.1s %d%%]";
dict["format_cpu"] = "^s[left;#aabbaa;CPU ]^g[left;150;14;%d;100;#888888;#991111;cpu]";
dict["format_date"] = "^s[right;#dddddd;%s]";
dict["format_essid"] = "^s[left;#aabbaa;ESSID]^s[left;#dddddd; %s]";
dict["format_ip"] = "^s[left;#aabbaa;IP]^s[left;#dddddd; %s]";
dict["format_network_down"] = "^s[left;#aabbaa;Down ]^g[left;100;14;%d;5000;#888888;#00af00;down]";
dict["format_network_up"] = "^s[left;#aabbaa;Up ]^g[left;100;14;%d;2500;#888888;#af0000;up]";
dict["format_ram"] = "^s[left;#aabbaa;RAM ]^p[left;100;8;0;%ld;%ld;#888888;#771144]";
dict["format_volume"] = "^s[left;#aabbaa;Vol ]^s[left;#dddddd;%d%%]";
dict["eth0_path_up"] = "/sys/class/net/eth0/statistics/tx_bytes";
dict["eth0_path_down"] = "/sys/class/net/eth0/statistics/rx_bytes";
dict["battery_path_full"] = "/sys/class/power_supply/BAT0/energy_full";
dict["battery_path_now"] = "/sys/class/power_supply/BAT0/energy_now";
dict["battery_path_state"] = "/sys/class/power_supply/BAT0/status";
dict["battery_critical_percent"] = "6";
dict["battery_critical_action"] = "systemctl hibernate";
}
void Config::set(std::string key, std::string value) {
if (dict.count(key) == 0) {
std::cerr << "Ignoring invalid config key: " << key << std::endl;
} else {
dict[key] = value;
}
}
void Config::print(void) {
std::cout << "Map size: " << dict.size() << std::endl;
for( t_dict::iterator ii=dict.begin(); ii!=dict.end(); ii++) {
std::cout << "'" << (*ii).first << "' : '" << (*ii).second << "'" << std::endl;
}
}
void parseConfig(Config &config, std::string path) {
std::ifstream infile(path.c_str());
if (infile.good()) {
std::string line;
while (getline(infile, line)) {
std::istringstream is_line(line);
trim(line);
// ignore commented lines
line = line.substr(0, line.find("#"));
line = line.substr(0, line.find("//"));
// ignore empty lines
if (line.empty())
continue;
unsigned sep = line.find("=");
std::string key = line.substr(0, sep);
std::string value = line.substr(sep + 1);
config.set(trim(key), trim(value));
}
} else {
std::cerr << "Error opening config file: " << path << std::endl;
throw 1;
}
}
int main(int, char**) {
Config config = Config();
parseConfig(config, configPath);
config.print();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment