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 
46 namespace ucommon {
47 
48 class keyfile;
49 
58 class __EXPORT keydata : public OrderedObject
59 {
60 private:
61  friend class keyfile;
62 
63  OrderedIndex index;
64  const char *name;
65  keyfile *root;
66 
68  keydata(keyfile *file, const char *id);
69  __DELETE_COPY(keydata);
70 
71 public:
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 
114  void set(const char *id, const char *value);
115 
121  void clear(const char *id);
122 
127  inline const char *get(void) const
128  {return name;}
129 
134  inline keyvalue *begin(void) const
135  {return (keyvalue *)index.begin();}
136 
141  inline keyvalue *end(void) const
142  {return (keyvalue*)index.end();}
143 
148 };
149 
156 class __EXPORT keyfile : public memalloc
157 {
158 private:
159  friend class keydata;
160  OrderedIndex index;
161  keydata *defaults;
162  int errcode;
163 
164 protected:
165  keydata *create(const char *section);
166 
167 #ifdef _MSWINDOWS_
168  void load(HKEY root, keydata *section = NULL, const char *path = NULL);
169  bool save(HKEY root, keydata *section = NULL, const char *path = NULL);
170 #endif
171 
172 public:
177  keyfile(size_t pagesize = 0);
178 
184  keyfile(const char *path, size_t pagesize = 0);
185 
186  keyfile(const keyfile &copy, size_t pagesize = 0);
187 
194  void load(const char *path);
195 
201  bool save(const char *path);
202 
207  void load(const keyfile *source);
208 
213  void load(const keydata *source);
214 
218  void release(void);
219 
225  keydata *get(const char *section) const;
226 
227  inline keydata *operator()(const char *section) const
228  {return get(section);}
229 
230  inline keydata *operator[](const char *section) const
231  {return get(section);}
232 
237  inline keydata *get(void) const
238  {return defaults;}
239 
244  inline keydata *begin(void) const
245  {return (keydata *)index.begin();}
246 
251  inline keydata *end(void) const
252  {return (keydata *)index.end();}
253 
258 
259  inline int err(void) const
260  {return errcode;}
261 
266  void assign(keyfile& source);
267 
268  inline keyfile& operator=(keyfile& source) {
269  assign(source);
270  return *this;
271  }
272 };
273 
274 } // namespace ucommon
275 
276 #endif
void release(SharedAccess &object)
Convenience function to unlock shared object through it's protocol.
Definition: access.h:280
const char * operator()(const char *id) const
Lookup a key value by it's id.
Definition: keydata.h:104
Various miscellaneous platform specific headers and defines.
Traditional keypair config file parsing class.
Definition: keydata.h:156
A linked object base class for ordered objects.
Definition: linked.h:319
LinkedObject * end(void) const
Return last object in list for iterators.
Definition: linked.h:285
ObjectProtocol * copy(ObjectProtocol *object)
Convenience function to access object copy.
Definition: object.h:510
Access standard files through character protocol.
Definition: file.h:60
Linked objects, lists, templates, and containers.
Private heaps, pools, and associations.
keydata * end(void) const
Get last keydata object, for iterative examinations.
Definition: keydata.h:251
An index container for maintaining an ordered list of objects.
Definition: linked.h:182
linked_pointer< keydata > iterator
Convenience typedef for iterative pointer.
Definition: keydata.h:257
keyvalue * end(void) const
Get last value object, for iterative examinations.
Definition: keydata.h:141
keydata * begin(void) const
Get first keydata object, for iterative examinations.
Definition: keydata.h:244
A key value set is used for iterative access.
Definition: keydata.h:77
Common namespace for all ucommon objects.
Definition: access.h:47
keyvalue * begin(void) const
Get first value object, for iterative examinations.
Definition: keydata.h:134
A smart pointer template for iterating linked lists.
Definition: linked.h:1348
A memory protocol pager for private heap manager.
Definition: memory.h:61
linked_pointer< keyvalue > iterator
Convenience typedef for iterative pointer.
Definition: keydata.h:147
LinkedObject * begin(void) const
Return first object in list for iterators.
Definition: linked.h:277
Data keys parsed from a keyfile.
Definition: keydata.h:58