varconf 1.0.3
Configuration library for the Worldforge system.
config.h
1/*
2 * config.h - interface for configuration class
3 * Copyright (C) 2001, Stefanus Du Toit, Joseph Zupko
4 * (C) 2002-2006 Alistair Riddoch
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 * Contact: Joseph Zupko
21 * jaz147@psu.edu
22 *
23 * 189 Reese St.
24 * Old Forge, PA 18518
25 */
26
27#ifndef VARCONF_CONFIG_H
28#define VARCONF_CONFIG_H
29
30#include "varconf_defs.h"
31#include "parse_error.h"
32#include "variable.h"
33
34#include <sigc++/trackable.h>
35#include <sigc++/signal.h>
36
37#include <iostream>
38#include <map>
39#include <string>
40
41namespace varconf {
42
43typedef std::map< std::string, Variable > sec_map;
44typedef std::map< std::string, sec_map> conf_map;
45typedef std::map< char, std::pair<std::string, bool> > parameter_map;
46
47class VARCONF_API Config : virtual protected sigc::trackable {
48public:
49 // Allows use as a singleton, if desired.
50 static Config* inst();
51
52 Config() = default;
53
54 // New Config object, but deep-copies the m_conf and m_par_lookup of existing,
55 // passed Config object.
56 Config(const Config & conf);
57
58 virtual ~Config();
59
60 VARCONF_API friend std::ostream & operator <<(std::ostream & out, Config & conf);
61 VARCONF_API friend std::istream & operator >>(std::istream & in, Config & conf);
62 VARCONF_API friend bool operator ==(const Config & one, const Config & two);
63
64 // Converts all nonalphanumeric characters in str except ``-'' and ``_'' to
65 // ``_''; converts caps in str to lower-case.
66 static void clean(std::string & str);
67
68 // Returns true if specified key exists under specified section.
69 bool find(const std::string & section, const std::string & key = "") const;
70
71 // Returns true if specified key exists under specified section and is
72 // successfully deleted.
73 bool erase(const std::string & section, const std::string & key = "");
74
75 // Writes to the specified output stream.
76 // Why isn't this protected?
77 bool writeToStream(std::ostream & out, Scope scope_mask) const;
78
79 // Gets, sets conf info based on options passed via command line.
80 int getCmdline(int argc, char** argv, Scope scope = INSTANCE);
81
82 // Gets, stores a name/value pair from the environment variable with
83 // name == prefix.
84 // prefix is case-sensitive!
85 void getEnv(const std::string & prefix, Scope scope = INSTANCE);
86
87 // Writes conf map to specified file. Returns true on success.
88 bool writeToFile(const std::string & filename,
89 Scope scopeMask = (Scope)(GLOBAL | USER | INSTANCE)) const;
90
91 // Reads contents of specified file and set into conf map. Returns
92 // true on success.
93 bool readFromFile(const std::string & filename, Scope scope = USER);
94
95 // Ensures specified filestream is properly formatted.
96 // Why isn't this protected?
97 void parseStream(std::istream & in, Scope scope);
98
99 // Wrapper for find(section)
100 bool findSection(const std::string & section) const;
101
102 // Wrapper for find(section, key)
103 bool findItem(const std::string & section, const std::string & key) const;
104
105 // Returns value of specified section.
106 const sec_map & getSection(const std::string & section);
107
108 // Returns value of specified key under specified section.
109 Variable getItem(const std::string & section, const std::string & key) const;
110
111 // Set the short name for a given long name to be used with short args.
112 void setParameterLookup(char s_name, const std::string & l_name,
113 bool value = false);
114
115 // If key isn't null, clean() section and key and set variable.
116 void setItem(const std::string & section, const std::string & key,
117 const Variable & item, Scope scope = INSTANCE);
118
119 // Accessor for the contained sections.
120 const conf_map& getSections() const;
121
122 sigc::signal<void> sig;
123 sigc::signal<void, const char*> sige;
124 sigc::signal<void, const std::string&, const std::string&> sigv;
125 sigc::signal<void, const std::string&, const std::string&, Config&> sigsv;
126 // libsigc++ signals; in order: standard callback signal, error signal,
127 // verbose callback signal and "super-verbose" callback signal.
128
129private:
130 static Config* m_instance;
131 conf_map m_conf;
132 parameter_map m_par_lookup;
133};
134
135
136VARCONF_API std::ostream & operator <<(std::ostream & out, Config & conf);
137VARCONF_API std::istream & operator >>(std::istream & in, Config & conf);
138VARCONF_API bool operator ==(const Config & one, const Config & two);
139
140} // namespace varconf
141
142#endif // VARCONF_CONFIG_H