Subversion Repositories public

Rev

Rev 69 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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