~vcs-imports-ii/znc/master

« back to all changes in this revision

Viewing changes to Config.h

  • Committer: Uli Schlachter
  • Date: 2011-04-01 19:43:05 UTC
  • Revision ID: git-v1:70c7745899c659c5b85ea592cb221208a0a6cad0
Overhaul the config parsing

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>

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2004-2011  See the AUTHORS file for details.
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or modify it
 
5
 * under the terms of the GNU General Public License version 2 as published
 
6
 * by the Free Software Foundation.
 
7
 */
 
8
 
 
9
#ifndef CONFIG_H
 
10
#define CONFIG_H
 
11
 
 
12
#include "ZNCString.h"
 
13
#include "FileUtils.h"
 
14
 
 
15
class CConfig {
 
16
public:
 
17
        struct CConfigEntry {
 
18
                CConfigEntry();
 
19
                CConfigEntry(const CConfig& Config);
 
20
                CConfigEntry(const CConfigEntry& other);
 
21
                ~CConfigEntry();
 
22
                CConfigEntry& operator=(const CConfigEntry& other);
 
23
 
 
24
                CConfig* m_pSubConfig;
 
25
        };
 
26
 
 
27
        typedef map<CString, VCString> EntryMap;
 
28
        typedef map<CString, CConfigEntry> SubConfig;
 
29
        typedef map<CString, SubConfig> SubConfigMap;
 
30
 
 
31
        typedef EntryMap::const_iterator EntryMapIterator;
 
32
        typedef SubConfigMap::const_iterator SubConfigMapIterator;
 
33
 
 
34
        EntryMapIterator BeginEntries() const {
 
35
                return m_ConfigEntries.begin();
 
36
        }
 
37
        EntryMapIterator EndEntries() const {
 
38
                return m_ConfigEntries.end();
 
39
        }
 
40
 
 
41
        SubConfigMapIterator BeginSubConfigs() const {
 
42
                return m_SubConfigs.begin();
 
43
        }
 
44
        SubConfigMapIterator EndSubConfigs() const {
 
45
                return m_SubConfigs.end();
 
46
        }
 
47
 
 
48
        bool FindStringVector(const CString& sName, VCString& vsList) {
 
49
                EntryMap::iterator it = m_ConfigEntries.find(sName);
 
50
                vsList.clear();
 
51
                if (it == m_ConfigEntries.end())
 
52
                        return false;
 
53
                vsList = it->second;
 
54
                m_ConfigEntries.erase(it);
 
55
                return true;
 
56
        }
 
57
 
 
58
        bool FindStringEntry(const CString& sName, CString& sRes) {
 
59
                EntryMap::iterator it = m_ConfigEntries.find(sName);
 
60
                sRes.clear();
 
61
                if (it == m_ConfigEntries.end() || it->second.empty())
 
62
                        return false;
 
63
                sRes = it->second.front();
 
64
                it->second.erase(it->second.begin());
 
65
                if (it->second.empty())
 
66
                        m_ConfigEntries.erase(it);
 
67
                return true;
 
68
        }
 
69
 
 
70
        bool FindSubConfig(const CString& sName, SubConfig& Config) {
 
71
                SubConfigMap::iterator it = m_SubConfigs.find(sName);
 
72
                if (it == m_SubConfigs.end()) {
 
73
                        Config.clear();
 
74
                        return false;
 
75
                }
 
76
                Config = it->second;
 
77
                m_SubConfigs.erase(it);
 
78
                return true;
 
79
        }
 
80
 
 
81
        bool empty() const {
 
82
                return m_ConfigEntries.empty() && m_SubConfigs.empty();
 
83
        }
 
84
 
 
85
        bool Parse(CFile& file, CString& sErrorMsg);
 
86
 
 
87
private:
 
88
        EntryMap m_ConfigEntries;
 
89
        SubConfigMap m_SubConfigs;
 
90
};
 
91
 
 
92
#endif // !CONFIG_H