UCommon
keydata.h
Go to the documentation of this file.
1// Copyright (C) 2006-2014 David Sugar, Tycho Softworks.
2// Copyright (C) 2015 Cherokees of Idaho.
3//
4// This file is part of GNU uCommon C++.
5//
6// GNU uCommon C++ is free software: you can redistribute it and/or modify
7// it under the terms of the GNU Lesser General Public License as published
8// by the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10//
11// GNU uCommon C++ 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
14// GNU Lesser General Public License for more details.
15//
16// You should have received a copy of the GNU Lesser General Public License
17// along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>.
18
31#ifndef _UCOMMON_KEYDATA_H_
32#define _UCOMMON_KEYDATA_H_
33
34#ifndef _UCOMMON_CONFIG_H_
35#include <ucommon/platform.h>
36#endif
37
38#ifndef _UCOMMON_LINKED_H_
39#include <ucommon/linked.h>
40#endif
41
42#ifndef _UCOMMON_MEMORY_H_
43#include <ucommon/memory.h>
44#endif
45
46namespace ucommon {
47
48class keyfile;
49
58class __EXPORT keydata : public OrderedObject
59{
60private:
61 friend class keyfile;
62
63 OrderedIndex index;
64 const char *name;
65 keyfile *root;
66
67 keydata(keyfile *file);
68 keydata(keyfile *file, const char *id);
69 __DELETE_COPY(keydata);
70
71public:
77 class __LOCAL keyvalue : public OrderedObject
78 {
79 private:
80 friend class keydata;
81 friend class keyfile;
82 keyvalue(keyfile *allocator, keydata *section, const char *key, const char *data);
83 __DELETE_COPY(keyvalue);
84
85 public:
86 const char *id;
87 const char *value;
88 };
89
90 friend class keyvalue;
91
97 const char *get(const char *id) const;
98
104 inline const char *operator()(const char *id) const {
105 return get(id);
106 }
107
115 void set(const char *id, const char *value);
116
122 void clear(const char *id);
123
128 inline const char *get(void) const {
129 return name;
130 }
131
136 inline keyvalue *begin(void) const {
137 return (keyvalue *)index.begin();
138 }
139
144 inline keyvalue *end(void) const {
145 return (keyvalue*)index.end();
146 }
147
152};
153
160class __EXPORT keyfile : public memalloc
161{
162private:
163 friend class keydata;
164 OrderedIndex index;
165 keydata *defaults;
166 int errcode;
167
168protected:
169 keydata *create(const char *section);
170
171#ifdef _MSWINDOWS_
172 void load(HKEY root, keydata *section = NULL, const char *path = NULL);
173 bool save(HKEY root, keydata *section = NULL, const char *path = NULL);
174#endif
175
176public:
181 keyfile(size_t pagesize = 0);
182
188 keyfile(const char *path, size_t pagesize = 0);
189
190 keyfile(const keyfile &copy, size_t pagesize = 0);
191
198 void load(const char *path);
199
205 bool save(const char *path);
206
211 void load(const keyfile *source);
212
217 void load(const keydata *source);
218
222 void release(void);
223
229 keydata *get(const char *section) const;
230
231 inline keydata *operator()(const char *section) const {
232 return get(section);
233 }
234
235 inline keydata *operator[](const char *section) const {
236 return get(section);
237 }
238
243 inline keydata *get(void) const {
244 return defaults;
245 }
246
251 inline keydata *begin(void) const {
252 return (keydata *)index.begin();
253 }
254
259 inline keydata *end(void) const {
260 return (keydata *)index.end();
261 }
262
267
268 inline int err(void) const {
269 return errcode;
270 }
271
276 void assign(keyfile& source);
277
278 inline keyfile& operator=(keyfile& source) {
279 assign(source);
280 return *this;
281 }
282};
283
284} // namespace ucommon
285
286#endif
Private heaps, pools, and associations.
Linked objects, lists, templates, and containers.
Various miscellaneous platform specific headers and defines.
Common namespace for all ucommon objects.
Definition access.h:47
Data keys parsed from a keyfile.
Definition keydata.h:59
keyvalue * end(void) const
Get last value object, for iterative examinations.
Definition keydata.h:144
keyvalue * begin(void) const
Get first value object, for iterative examinations.
Definition keydata.h:136
const char * get(const char *id) const
Lookup a key value by it's id.
void clear(const char *id)
Remove a keyword id from the keydata structure.
const char * operator()(const char *id) const
Lookup a key value by it's id.
Definition keydata.h:104
linked_pointer< keyvalue > pointer
Convenience typedef for iterative pointer.
Definition keydata.h:151
const char * get(void) const
Get the name of this section.
Definition keydata.h:128
void set(const char *id, const char *value)
Set a keyword and value in the keydata structure.
A key value set is used for iterative access.
Definition keydata.h:78
Traditional keypair config file parsing class.
Definition keydata.h:161
keyfile(size_t pagesize=0)
Create an empty key file ready for loading.
void release(void)
Release and re-initialize keyfile.
bool save(const char *path)
Save (write) a set of config keys to dist.
keyfile(const char *path, size_t pagesize=0)
Create a key file object from an existing config file.
keydata * end(void) const
Get last keydata object, for iterative examinations.
Definition keydata.h:259
void assign(keyfile &source)
Assign foreign pager to us.
void load(const keydata *source)
Load a single set of keys.
keydata * get(const char *section) const
Get a keydata section name.
linked_pointer< keydata > pointer
Convenience typedef for iterative pointer.
Definition keydata.h:266
void load(const keyfile *source)
Load from an existing keyfile object.
keydata * get(void) const
Get the non-sectioned defaults if there are any.
Definition keydata.h:243
keydata * begin(void) const
Get first keydata object, for iterative examinations.
Definition keydata.h:251
void load(const char *path)
Load (overlay) another config file over the currently loaded one.
An index container for maintaining an ordered list of objects.
Definition linked.h:177
LinkedObject * begin(void) const
Return first object in list for iterators.
Definition linked.h:270
LinkedObject * end(void) const
Return last object in list for iterators.
Definition linked.h:278
A linked object base class for ordered objects.
Definition linked.h:313
A smart pointer template for iterating linked lists.
Definition linked.h:992
A memory protocol pager for private heap manager.
Definition memory.h:62