Subversion Repositories public

Rev

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