UCommon
keydata.h
Go to the documentation of this file.
1 // Copyright (C) 2006-2014 David Sugar, Tycho Softworks.
2 //
3 // This file is part of GNU uCommon C++.
4 //
5 // GNU uCommon C++ is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published
7 // by the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // GNU uCommon C++ is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>.
17 
30 #ifndef _UCOMMON_KEYDATA_H_
31 #define _UCOMMON_KEYDATA_H_
32 
33 #ifndef _UCOMMON_CONFIG_H_
34 #include <ucommon/platform.h>
35 #endif
36 
37 #ifndef _UCOMMON_LINKED_H_
38 #include <ucommon/linked.h>
39 #endif
40 
41 #ifndef _UCOMMON_MEMORY_H_
42 #include <ucommon/memory.h>
43 #endif
44 
45 namespace ucommon {
46 
47 class keyfile;
48 
57 class __EXPORT keydata : public OrderedObject
58 {
59 private:
60  friend class keyfile;
61  OrderedIndex index;
63  keydata(keyfile *file, const char *id);
64  const char *name;
65  keyfile *root;
66 
67 public:
73  class __LOCAL keyvalue : public OrderedObject
74  {
75  private:
76  friend class keydata;
77  friend class keyfile;
78  keyvalue(keyfile *allocator, keydata *section, const char *key, const char *data);
79  public:
80  const char *id;
81  const char *value;
82  };
83 
84  friend class keyvalue;
85 
91  const char *get(const char *id) const;
92 
98  inline const char *operator()(const char *id) const
99  {return get(id);}
100 
108  void set(const char *id, const char *value);
109 
115  void clear(const char *id);
116 
121  inline const char *get(void) const
122  {return name;}
123 
128  inline keyvalue *begin(void) const
129  {return (keyvalue *)index.begin();}
130 
135  inline keyvalue *end(void) const
136  {return (keyvalue*)index.end();}
137 
142 };
143 
150 class __EXPORT keyfile : public memalloc
151 {
152 private:
153  friend class keydata;
154  OrderedIndex index;
155  keydata *defaults;
156  int errcode;
157 
158 protected:
159  keydata *create(const char *section);
160 
161 #ifdef _MSWINDOWS_
162  void load(HKEY root, keydata *section = NULL, const char *path = NULL);
163  bool save(HKEY root, keydata *section = NULL, const char *path = NULL);
164 #endif
165 
166 public:
171  keyfile(size_t pagesize = 0);
172 
178  keyfile(const char *path, size_t pagesize = 0);
179 
180  keyfile(const keyfile &copy, size_t pagesize = 0);
181 
188  void load(const char *path);
189 
195  bool save(const char *path);
196 
201  void load(const keyfile *source);
202 
207  void load(const keydata *source);
208 
212  void release(void);
213 
219  keydata *get(const char *section) const;
220 
221  inline keydata *operator()(const char *section) const
222  {return get(section);}
223 
224  inline keydata *operator[](const char *section) const
225  {return get(section);}
226 
231  inline keydata *get(void) const
232  {return defaults;}
233 
238  inline keydata *begin(void) const
239  {return (keydata *)index.begin();}
240 
245  inline keydata *end(void) const
246  {return (keydata *)index.end();}
247 
252 
253  inline int err(void) const
254  {return errcode;}
255 };
256 
257 } // namespace ucommon
258 
259 #endif
LinkedObject * end(void) const
Return last object in list for iterators.
Definition: linked.h:279
Linked objects, lists, templates, and containers.
keydata * end(void) const
Get last keydata object, for iterative examinations.
Definition: keydata.h:245
Common namespace for all ucommon objects.
A linked object base class for ordered objects.
Definition: linked.h:309
keyvalue * begin(void) const
Get first value object, for iterative examinations.
Definition: keydata.h:128
A smart pointer template for iterating linked lists.
Definition: linked.h:1292
A memory protocol pager for private heap manager.
Definition: memory.h:60
void release(SharedAccess &object)
Convenience function to unlock shared object through it's protocol.
Definition: access.h:260
Various miscellaneous platform specific headers and defines.
keyvalue * end(void) const
Get last value object, for iterative examinations.
Definition: keydata.h:135
Private heaps, pools, and associations.
An index container for maintaining an ordered list of objects.
Definition: linked.h:179
LinkedObject * begin(void) const
Return first object in list for iterators.
Definition: linked.h:272
const char * operator()(const char *id) const
Lookup a key value by it's id.
Definition: keydata.h:98
Traditional keypair config file parsing class.
Definition: keydata.h:150
keydata * begin(void) const
Get first keydata object, for iterative examinations.
Definition: keydata.h:238
Data keys parsed from a keyfile.
Definition: keydata.h:57
linked_pointer< keyvalue > iterator
Convenience typedef for iterative pointer.
Definition: keydata.h:141
linked_pointer< keydata > iterator
Convenience typedef for iterative pointer.
Definition: keydata.h:251
Access standard files through character protocol.
Definition: file.h:59
ObjectProtocol * copy(ObjectProtocol *object)
Convenience function to access object copy.
Definition: object.h:479
A key value set is used for iterative access.
Definition: keydata.h:73