summaryrefslogtreecommitdiff
path: root/src/configreader.cpp
blob: 6ba9453f75267c7b1db14205ce1aabf2015d2eb5 (plain)
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
/*       +------------------------------------+
 *       | Inspire Internet Relay Chat Daemon |
 *       +------------------------------------+
 *
 *  InspIRCd: (C) 2002-2010 InspIRCd Development Team
 * See: http://www.inspircd.org/wiki/index.php/Credits
 *
 * This program is free but copyrighted software; see
 *            the file COPYING for details.
 *
 * ---------------------------------------------------
 */

#include "services.h"

ConfigReader::ConfigReader() : data(&Config.config_data), errorlog(new std::ostringstream(std::stringstream::in | std::stringstream::out)), privatehash(false), error(CONF_NO_ERROR)
{
}

ConfigReader::~ConfigReader()
{
	if (this->errorlog)
		delete this->errorlog;
	if (this->privatehash)
		delete this->data;
}

ConfigReader::ConfigReader(const Anope::string &filename) : data(new ConfigDataHash), errorlog(new std::ostringstream(std::stringstream::in | std::stringstream::out)), privatehash(true), error(CONF_NO_ERROR)
{
	Config.ClearStack();
}

Anope::string ConfigReader::ReadValue(const Anope::string &tag, const Anope::string &name, const Anope::string &default_value, int index, bool allow_linefeeds)
{
	/* Don't need to strlcpy() tag and name anymore, ReadConf() takes const char* */
	Anope::string result;

	if (!Config.ConfValue(*this->data, tag, name, default_value, index, result, allow_linefeeds))
		this->error = CONF_VALUE_NOT_FOUND;

	return result;
}

Anope::string ConfigReader::ReadValue(const Anope::string &tag, const Anope::string &name, int index, bool allow_linefeeds)
{
	return ReadValue(tag, name, "", index, allow_linefeeds);
}

bool ConfigReader::ReadFlag(const Anope::string &tag, const Anope::string &name, const Anope::string &default_value, int index)
{
	return Config.ConfValueBool(*this->data, tag, name, default_value, index);
}

bool ConfigReader::ReadFlag(const Anope::string &tag, const Anope::string &name, int index)
{
	return ReadFlag(tag, name, "", index);
}

int ConfigReader::ReadInteger(const Anope::string &tag, const Anope::string &name, const Anope::string &default_value, int index, bool need_positive)
{
	int result;

	if (!Config.ConfValueInteger(*this->data, tag, name, default_value, index, result))
	{
		this->error = CONF_VALUE_NOT_FOUND;
		return 0;
	}

	if (need_positive && result < 0)
	{
		this->error = CONF_INT_NEGATIVE;
		return 0;
	}

	return result;
}

int ConfigReader::ReadInteger(const Anope::string &tag, const Anope::string &name, int index, bool need_positive)
{
	return ReadInteger(tag, name, "", index, need_positive);
}

long ConfigReader::GetError()
{
	long olderr = this->error;
	this->error = 0;
	return olderr;
}

void ConfigReader::DumpErrors(bool bail)
{
	Config.ReportConfigError(this->errorlog->str(), bail);
}

int ConfigReader::Enumerate(const Anope::string &tag) const
{
	return Config.ConfValueEnum(*this->data, tag);
}

int ConfigReader::EnumerateValues(const Anope::string &tag, int index)
{
	return Config.ConfVarEnum(*this->data, tag, index);
}

bool ConfigReader::Verify()
{
	return this->readerror;
}