-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfig.cpp
170 lines (140 loc) · 3.79 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//============================================================================
// Name : TvGuideFetch
// Author : John Saunders
// Copyright : Copyright (c) 2009 John Saunders
// Description : Fetch Australian XMLTV guide data from oztivo
//============================================================================
#include <iostream>
#include <fstream>
#include "Config.hpp"
//////////////////////////////////////////////////////////////////////////////
// Config - singleton used for accessing the grabber configuration
//////////////////////////////////////////////////////////////////////////////
Config::Config()
: m_configPath("tvguide_config.xml")
{
}
Config::~Config()
{
}
// Return an instance of the Config singleton.
Config& Config::instance()
{
static Config s_instance;
return s_instance;
}
// Set the path to the configuration file.
bool Config::setConfigPath(const std::string& path, bool dontLoad)
{
m_configPath = path;
return dontLoad || loadConfig();
}
// Fetch the value of a specified configuration item.
bool Config::getItem(const std::string& item, std::string& value)
{
if (!loadConfig())
{
return false;
}
XmlNav nav(m_configDoc);
if (nav.gotoNode(item))
{
value = nav.getAllChars();
return true;
}
return false;
}
// Set the value of a specified configuration item.
bool Config::setItem(const std::string& item, const std::string& value)
{
if (!loadConfig())
{
return false;
}
XmlNav nav(m_configDoc);
if (nav.gotoNode(item))
{
nav.setAllChars(value.c_str());
return true;
}
return false;
}
// Fetch the value of a list of items.
bool Config::getItemList(const std::string& parent, const std::string& item, std::vector<std::string>& values)
{
if (!loadConfig())
{
return false;
}
XmlNav nav(m_configDoc);
if (nav.gotoNode(parent))
{
std::string itemName(item);
values.clear();
bool nodeSuccess = nav.gotoFirstChildNode();
while (nodeSuccess)
{
if (itemName == nav.getNodeName())
{
values.push_back(nav.getAllChars());
}
nodeSuccess = nav.gotoNextSiblingNode();
}
return true;
}
return false;
}
// Return an XML navigation object for browsing the config file.
XmlNav Config::getConfigNav()
{
return XmlNav(m_configDoc);
}
// Register a default configuration.
void Config::registerDefaultConfig(const std::string& name, const std::string& config)
{
m_defaultConfigs[name] = config;
}
// Load a default configuration.
bool Config::loadDefaultConfig(const std::string& listingsType)
{
MapOfConfigs::const_iterator iter = m_defaultConfigs.find(listingsType);
if (iter != m_defaultConfigs.end())
{
return m_configDoc.parse((*iter).second, true);
}
return false;
}
// Load the configuration from a file if not already loaded.
bool Config::loadConfig()
{
if (!m_configDoc.isLoaded())
{
std::ifstream in;
in.open(m_configPath.c_str(), std::ios_base::in);
if (in.is_open())
{
bool result = m_configDoc.parse(in, true);
in.close();
return result;
}
return false;
}
return true;
}
// Write the configuration to the config path.
bool Config::writeConfig()
{
XmlNav nav(m_configDoc);
std::ofstream out;
out.open(m_configPath.c_str(), std::ios_base::out | std::ios_base::trunc);
if (out.is_open())
{
out << "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" << std::endl
<< "<!-- TvGuideFetch configuration -->" << std::endl
<< nav << std::endl;
bool result = !out.bad();
out.close();
return result;
}
return false;
}