Statistics
| Branch: | Tag: | Revision:

dvbd / configfile.cpp @ bfdb7446

History | View | Annotate | Download (1.75 KB)

1
/*
2
  Copyright 2003 John Knottenbelt
3
  
4
  This program is free software; you can redistribute it and/or modify
5
  it under the terms of the GNU General Public License as published by
6
  the Free Software Foundation; either version 2 of the License, or
7
  (at your option) any later version.
8
 
9
  This program is distributed in the hope that it will be useful,
10
  but WITHOUT ANY WARRANTY; without even the implied warranty of
11
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
  GNU General Public License for more details.
13
 
14
  You should have received a copy of the GNU General Public License
15
  along with this program; if not, write to the Free Software
16
  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
17
*/
18

    
19
#include "configfile.h"
20
#include "stringutil.h"
21
#include <fstream>
22
#include <iostream>
23

    
24
ConfigFile::ConfigFile()
25
{
26
}
27

    
28
void ConfigFile::clear()
29
{
30
  data.clear();
31
}
32

    
33
bool ConfigFile::read(const std::string &file)
34
{
35
  std::ifstream input(file.c_str());
36
  if (!input)
37
    return false;
38

    
39
  std::string line;
40
  int lineno = 0;
41

    
42
  while (getline(input, line)) {
43
    lineno++;
44
    line = stripTrailingWS(stripLeadingWS(stripLineComment(line)));
45

    
46
    if (line == "")
47
      continue;
48

    
49
    std::string::size_type pos = line.find('=');
50
    if (pos == std::string::npos) {
51
      std::cerr << "Warning: error reading configuration file, no '=' found on line " 
52
                << lineno << std::endl;
53
      continue;
54
    }
55

    
56
    std::string key = stripTrailingWS(line.substr(0, pos));
57
    std::string value = stripLeadingWS(line.substr(pos + 1));
58

    
59
    data[key] = value;
60
  }
61

    
62
  input.close();
63
  return true;
64
}
65

    
66
void ConfigFile::set(const std::string &key, const std::string &value)
67
{
68
  data[key] = value;
69
}
70

    
71
std::string ConfigFile::get(const std::string &key) 
72
{
73
  return data[key];
74
}