/
config.cpp
99 lines (79 loc) · 2.07 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
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
#include <fstream>
#include <map>
#include <boost/filesystem.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/algorithm/string.hpp>
#include "functions.h"
#include "config.h"
bool is_number(const std::string& s)
{
std::string::const_iterator it = s.begin();
while (it != s.end() && std::isdigit(*it)) ++it;
return !s.empty() && it == s.end();
}
Configuration::Configuration() :
m_valid(false)
{
}
bool Configuration::Valid() const
{
return m_valid;
}
::Channels Configuration::Channels() const
{
return m_channels;
}
void Configuration::Valid(bool valid)
{
m_valid = valid;
}
void Configuration::Channels(const ::Channels& channels)
{
m_channels = channels;
}
ConfigurationFile::ConfigurationFile(const std::string& filename) :
m_filename(filename)
{
}
void ConfigurationFile::Channels(const ::Channels& channels)
{
m_configuration.Channels(channels);
}
Configuration ConfigurationFile::Read(const Provider& provider)
{
std::ifstream file(m_filename.c_str());
if (!file.good())
return Configuration();
std::map<int, std::string> channelMap;
std::string line;
while (std::getline(file, line))
{
std::vector<std::string> split;
boost::split(split, line, boost::is_any_of("="));
if (is_number(split[0]))
channelMap[boost::lexical_cast<int>(split[0])] = split[1];
}
::Channels channels;
::Channels allChannels = provider.GetChannels();
for (::Channels::iterator it = allChannels.begin(); it != allChannels.end(); ++it)
{
if (channelMap.find(it->Id()) != channelMap.end())
{
it->Name(channelMap[it->Id()]);
channels.push_back(*it);
}
}
m_configuration.Channels(channels);
m_configuration.Valid(true);
return m_configuration;
}
void ConfigurationFile::Write()
{
boost::filesystem::path fullpath(m_filename);
boost::filesystem::path directory = fullpath.parent_path();
boost::filesystem::create_directories(directory);
std::ofstream file(m_filename.c_str());
::Channels channels = m_configuration.Channels();
for (::Channels::iterator it = channels.begin(); it != channels.end(); ++it)
file << it->Id() << "=" << it->Name() << std::endl;
}