forked from znc/znc
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This moves stuff to a two-step model. First, the new class CConfig reads the config file, parses it and creates a in-memory model of stuff. Only then do we actually go forward and apply the stuff. The upside of this is that some config errors are caught before we change anything on the running upside. Let's see how much stuff this broke... Signed-off-by: Uli Schlachter <psychon@znc.in>
- Loading branch information
Showing
13 changed files
with
1,018 additions
and
594 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,8 @@ Makefile | |
/modules/modpython/*.pyc | ||
/modules/*.pyc | ||
|
||
/test/ConfigTest | ||
|
||
# Compiled Object files | ||
*.o | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
/* | ||
* Copyright (C) 2004-2011 See the AUTHORS file for details. | ||
* | ||
* This program is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU General Public License version 2 as published | ||
* by the Free Software Foundation. | ||
*/ | ||
|
||
#include "Config.h" | ||
#include <stack> | ||
#include <sstream> | ||
|
||
struct ConfigStackEntry { | ||
CString sTag; | ||
CString sName; | ||
CConfig Config; | ||
|
||
ConfigStackEntry(const CString& Tag, const CString Name) { | ||
sTag = Tag; | ||
sName = Name; | ||
} | ||
}; | ||
|
||
CConfig::CConfigEntry::CConfigEntry() | ||
: m_pSubConfig(NULL) { | ||
} | ||
|
||
CConfig::CConfigEntry::CConfigEntry(const CConfig& Config) | ||
: m_pSubConfig(new CConfig(Config)) { | ||
} | ||
|
||
CConfig::CConfigEntry::CConfigEntry(const CConfigEntry& other) | ||
: m_pSubConfig(NULL) { | ||
if (other.m_pSubConfig) | ||
m_pSubConfig = new CConfig(*other.m_pSubConfig); | ||
} | ||
|
||
CConfig::CConfigEntry::~CConfigEntry() | ||
{ | ||
delete m_pSubConfig; | ||
} | ||
|
||
CConfig::CConfigEntry& CConfig::CConfigEntry::operator=(const CConfigEntry& other) { | ||
delete m_pSubConfig; | ||
if (other.m_pSubConfig) | ||
m_pSubConfig = new CConfig(*other.m_pSubConfig); | ||
else | ||
m_pSubConfig = NULL; | ||
return *this; | ||
} | ||
|
||
bool CConfig::Parse(CFile& file, CString& sErrorMsg) | ||
{ | ||
CString sLine; | ||
unsigned int uLineNum = 0; | ||
CConfig *pActiveConfig = this; | ||
std::stack<ConfigStackEntry> ConfigStack; | ||
bool bCommented = false; // support for /**/ style comments | ||
|
||
if (!file.Seek(0)) | ||
return "Could not seek to the beginning of the config."; | ||
|
||
while (file.ReadLine(sLine)) { | ||
uLineNum++; | ||
|
||
#define ERROR(arg) do { \ | ||
std::stringstream stream; \ | ||
stream << "Error on line " << uLineNum << ": " << arg; \ | ||
sErrorMsg = stream.str(); \ | ||
m_SubConfigs.clear(); \ | ||
m_ConfigEntries.clear(); \ | ||
return false; \ | ||
} while (0) | ||
|
||
// Remove all leading spaces and trailing line endings | ||
sLine.TrimLeft(); | ||
sLine.TrimRight("\r\n"); | ||
|
||
if ((sLine.empty()) || (sLine[0] == '#') || (sLine.Left(2) == "//")) { | ||
continue; | ||
} | ||
|
||
if (sLine.Left(2) == "/*") { | ||
if (sLine.Right(2) != "*/") { | ||
bCommented = true; | ||
} | ||
|
||
continue; | ||
} | ||
|
||
if (bCommented) { | ||
if (sLine.Right(2) == "*/") { | ||
bCommented = false; | ||
} | ||
|
||
continue; | ||
} | ||
|
||
if ((sLine.Left(1) == "<") && (sLine.Right(1) == ">")) { | ||
sLine.LeftChomp(); | ||
sLine.RightChomp(); | ||
sLine.Trim(); | ||
|
||
CString sTag = sLine.Token(0); | ||
CString sValue = sLine.Token(1, true); | ||
|
||
sTag.Trim(); | ||
sValue.Trim(); | ||
|
||
if (sTag.Left(1) == "/") { | ||
sTag = sTag.substr(1); | ||
|
||
if (!sValue.empty()) | ||
ERROR("Malformated closing tag. Expected \"</" << sTag << ">\"."); | ||
if (ConfigStack.empty()) | ||
ERROR("Closing tag \"" << sTag << "\" which is not open."); | ||
|
||
const struct ConfigStackEntry& entry = ConfigStack.top(); | ||
CConfig myConfig(entry.Config); | ||
CString sName(entry.sName); | ||
|
||
if (!sTag.Equals(entry.sTag)) | ||
ERROR("Closing tag \"" << sTag << "\" which is not open."); | ||
|
||
// This breaks entry | ||
ConfigStack.pop(); | ||
|
||
if (ConfigStack.empty()) | ||
pActiveConfig = this; | ||
else | ||
pActiveConfig = &ConfigStack.top().Config; | ||
|
||
SubConfig &conf = pActiveConfig->m_SubConfigs[sTag.AsLower()]; | ||
SubConfig::const_iterator it = conf.find(sName); | ||
|
||
if (it != conf.end()) | ||
ERROR("Duplicate entry for tag \"" << sTag << "\" name \"" << sName << "\"."); | ||
|
||
conf[sName] = CConfigEntry(myConfig); | ||
} else { | ||
if (sValue.empty()) | ||
ERROR("Empty block name at begin of block."); | ||
ConfigStack.push(ConfigStackEntry(sTag.AsLower(), sValue)); | ||
pActiveConfig = &ConfigStack.top().Config; | ||
} | ||
|
||
continue; | ||
} | ||
|
||
// If we have a regular line, figure out where it goes | ||
CString sName = sLine.Token(0, false, "="); | ||
CString sValue = sLine.Token(1, true, "="); | ||
|
||
// Only remove the first space, people might want | ||
// leading spaces (e.g. in the MOTD). | ||
if (sValue.Left(1) == " ") | ||
sValue.LeftChomp(); | ||
|
||
// We don't have any names with spaces, trim all | ||
// leading/trailing spaces. | ||
sName.Trim(); | ||
|
||
if (sName.empty() || sValue.empty()) | ||
ERROR("Malformed line"); | ||
|
||
CString sNameLower = sName.AsLower(); | ||
pActiveConfig->m_ConfigEntries[sNameLower].push_back(sValue); | ||
} | ||
|
||
if (bCommented) | ||
ERROR("Comment not closed at end of file."); | ||
|
||
if (!ConfigStack.empty()) { | ||
const CString& sTag = ConfigStack.top().sTag; | ||
ERROR("Not all tags are closed at the end of the file. Inner-most open tag is \"" << sTag << "\"."); | ||
} | ||
|
||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* Copyright (C) 2004-2011 See the AUTHORS file for details. | ||
* | ||
* This program is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU General Public License version 2 as published | ||
* by the Free Software Foundation. | ||
*/ | ||
|
||
#ifndef CONFIG_H | ||
#define CONFIG_H | ||
|
||
#include "ZNCString.h" | ||
#include "FileUtils.h" | ||
|
||
class CConfig { | ||
public: | ||
struct CConfigEntry { | ||
CConfigEntry(); | ||
CConfigEntry(const CConfig& Config); | ||
CConfigEntry(const CConfigEntry& other); | ||
~CConfigEntry(); | ||
CConfigEntry& operator=(const CConfigEntry& other); | ||
|
||
CConfig* m_pSubConfig; | ||
}; | ||
|
||
typedef map<CString, VCString> EntryMap; | ||
typedef map<CString, CConfigEntry> SubConfig; | ||
typedef map<CString, SubConfig> SubConfigMap; | ||
|
||
typedef EntryMap::const_iterator EntryMapIterator; | ||
typedef SubConfigMap::const_iterator SubConfigMapIterator; | ||
|
||
EntryMapIterator BeginEntries() const { | ||
return m_ConfigEntries.begin(); | ||
} | ||
EntryMapIterator EndEntries() const { | ||
return m_ConfigEntries.end(); | ||
} | ||
|
||
SubConfigMapIterator BeginSubConfigs() const { | ||
return m_SubConfigs.begin(); | ||
} | ||
SubConfigMapIterator EndSubConfigs() const { | ||
return m_SubConfigs.end(); | ||
} | ||
|
||
bool FindStringVector(const CString& sName, VCString& vsList) { | ||
EntryMap::iterator it = m_ConfigEntries.find(sName); | ||
vsList.clear(); | ||
if (it == m_ConfigEntries.end()) | ||
return false; | ||
vsList = it->second; | ||
m_ConfigEntries.erase(it); | ||
return true; | ||
} | ||
|
||
bool FindStringEntry(const CString& sName, CString& sRes) { | ||
EntryMap::iterator it = m_ConfigEntries.find(sName); | ||
sRes.clear(); | ||
if (it == m_ConfigEntries.end() || it->second.empty()) | ||
return false; | ||
sRes = it->second.front(); | ||
it->second.erase(it->second.begin()); | ||
if (it->second.empty()) | ||
m_ConfigEntries.erase(it); | ||
return true; | ||
} | ||
|
||
bool FindSubConfig(const CString& sName, SubConfig& Config) { | ||
SubConfigMap::iterator it = m_SubConfigs.find(sName); | ||
if (it == m_SubConfigs.end()) { | ||
Config.clear(); | ||
return false; | ||
} | ||
Config = it->second; | ||
m_SubConfigs.erase(it); | ||
return true; | ||
} | ||
|
||
bool empty() const { | ||
return m_ConfigEntries.empty() && m_SubConfigs.empty(); | ||
} | ||
|
||
bool Parse(CFile& file, CString& sErrorMsg); | ||
|
||
private: | ||
EntryMap m_ConfigEntries; | ||
SubConfigMap m_SubConfigs; | ||
}; | ||
|
||
#endif // !CONFIG_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.