-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.cpp
63 lines (52 loc) · 1.72 KB
/
config.cpp
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
//*****************************************************************************
//
// Copyright (C) 2005 Steve Connet. All rights reserved.
//
// Source File Name : config.cpp
// Author : Steve Connet
//
// File Overview : Implementation of the config object
//
//*****************************************************************************
#include "config.h"
#include "util.h"
#include <fstream>
#include <algorithm>
// reads the config file and parses each line
Config::Config(std::string const &filename)
{
std::string buf;
char *name;
char *value;
data.clear();
// open the file
std::ifstream infile(filename.c_str(), std::ios::in);
if(!infile)
{
std::cerr << "error opening " << filename << std::endl;
exit(-1);
}
// read each line in the file
while(std::getline(infile, buf))
{
// make sure we have valid data for this line
if(buf.length() && isalpha(buf[0]))
{
// convert to lower case
std::transform(buf.begin(), buf.end(), buf.begin(), utils::safetolower);
// find the equal (=) char
std::string::size_type pos = buf.find_first_of('=');
// capture the name and value
name = new char[buf.length()];
value = new char[buf.length()];
strcpy(name, buf.substr(0, pos).c_str());
strcpy(value, buf.substr(pos + 1).c_str());
// insert into the map
data[ utils::trimleft(utils::trimright(name)) ] =
utils::trimleft(utils::trimright(value));
delete []name;
delete []value;
}
}
infile.close();
}