1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
|
/// inotify cron configuration implementation
/**
* \file incroncfg.cpp
*
* incron configuration
*
* Copyright (C) 2007, 2008 Lukas Jelinek, <lukas@aiken.cz>
*
* This program is free software; you can use it, redistribute
* it and/or modify it under the terms of the GNU General Public
* License, version 2 (see LICENSE-GPL).
*
*/
#include <fstream>
#include <sstream>
#include <cstring>
#include "incroncfg.h"
#define INCRON_CFG_DEFAULT "/etc/incron.conf"
typedef std::map<std::string, std::string> CFG_MAP;
typedef CFG_MAP::iterator CFG_ITER;
CFG_MAP IncronCfg::m_values;
CFG_MAP IncronCfg::m_defaults;
void IncronCfg::Init()
{
m_defaults.insert(CFG_MAP::value_type("system_table_dir", "/etc/incron.d"));
m_defaults.insert(CFG_MAP::value_type("user_table_dir", "/var/spool/incron"));
m_defaults.insert(CFG_MAP::value_type("allowed_users", "/etc/incron.allow"));
m_defaults.insert(CFG_MAP::value_type("denied_users", "/etc/incron.deny"));
m_defaults.insert(CFG_MAP::value_type("lockfile_dir", "/var/run"));
m_defaults.insert(CFG_MAP::value_type("lockfile_name", "incrond"));
m_defaults.insert(CFG_MAP::value_type("editor", ""));
}
void IncronCfg::Load(const std::string& rPath)
{
char s[1024];
std::ifstream is(rPath.c_str());
if (is.is_open()) {
while (!is.eof() && !is.fail()) {
is.getline(s, 1023);
std::string key, val;
if (ParseLine(s, key, val)) {
m_values.insert(CFG_MAP::value_type(key, val));
}
}
is.close();
return;
}
if (rPath == INCRON_CFG_DEFAULT)
return;
is.open(INCRON_CFG_DEFAULT);
if (is.is_open()) {
while (!is.eof() && !is.fail()) {
is.getline(s, 1023);
std::string key, val;
if (ParseLine(s, key, val)) {
m_values.insert(CFG_MAP::value_type(key, val));
}
}
is.close();
}
}
bool IncronCfg::GetValue(const std::string& rKey, std::string& rVal)
{
CFG_ITER it = m_values.find(rKey);
if (it != m_values.end()) {
rVal = (*it).second;
return true;
}
it = m_defaults.find(rKey);
if (it != m_defaults.end()) {
rVal = (*it).second;
return true;
}
return false;
}
bool IncronCfg::GetValue(const std::string& rKey, int& rVal)
{
std::string s;
if (GetValue(rKey, s)) {
if (sscanf(s.c_str(), "%i", &rVal) == 1)
return true;
}
return false;
}
bool IncronCfg::GetValue(const std::string& rKey, unsigned& rVal)
{
std::string s;
if (GetValue(rKey, s)) {
if (sscanf(s.c_str(), "%u", &rVal) == 1)
return true;
}
return false;
}
bool IncronCfg::GetValue(const std::string& rKey, bool& rVal)
{
std::string s;
if (GetValue(rKey, s)) {
size_t len = (size_t) s.length();
for (size_t i = 0; i < len; i++) {
s[i] = (char) tolower(s[i]);
}
rVal = (s == "1" || s == "true" || s == "yes" || s == "on" || s == "enable" || s == "enabled");
return true;
}
return false;
}
std::string IncronCfg::BuildPath(const std::string& rPath, const std::string& rName)
{
if (rPath.rfind('/') == rPath.length() - 1)
return rPath + rName;
return rPath + "/" + rName;
}
bool IncronCfg::ParseLine(const char* s, std::string& rKey, std::string& rVal)
{
// CAUTION: This code hasn't been optimized. It may be slow.
char key[1024], val[1024];
if (IsComment(s))
return false;
std::istringstream ss(s);
ss.get(key, 1023, '=');
if (ss.fail())
return false;
ss.get(val, 1023);
if (ss.fail())
return false;
rKey = key;
rVal = val;
std::string::size_type a = rKey.find_first_not_of(" \t");
std::string::size_type b = rKey.find_last_not_of(" \t");
if (a == std::string::npos || b == std::string::npos)
return false;
rKey = rKey.substr(a, b-a+1);
a = rVal.find_first_not_of(" \t=");
b = rVal.find_last_not_of(" \t");
if (a == std::string::npos || b == std::string::npos) {
rVal = "";
}
else {
rVal = rVal.substr(a, b-a+1);
}
return true;
}
bool IncronCfg::IsComment(const char* s)
{
char* sx = strchr(s, '#');
if (sx == NULL)
return false;
size_t len = sx - s;
for (size_t i = 0; i < len; i++) {
if (!(s[i] == ' ' || s[i] == '\t'))
return false;
}
return true;
}
|