ucommon
|
00001 // Copyright (C) 2006-2014 David Sugar, Tycho Softworks. 00002 // 00003 // This file is part of GNU uCommon C++. 00004 // 00005 // GNU uCommon C++ is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU Lesser General Public License as published 00007 // by the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // GNU uCommon C++ is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU Lesser General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU Lesser General Public License 00016 // along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>. 00017 00030 #ifndef _UCOMMON_KEYDATA_H_ 00031 #define _UCOMMON_KEYDATA_H_ 00032 00033 #ifndef _UCOMMON_CONFIG_H_ 00034 #include <ucommon/platform.h> 00035 #endif 00036 00037 #ifndef _UCOMMON_LINKED_H_ 00038 #include <ucommon/linked.h> 00039 #endif 00040 00041 #ifndef _UCOMMON_MEMORY_H_ 00042 #include <ucommon/memory.h> 00043 #endif 00044 00045 namespace ucommon { 00046 00047 class keyfile; 00048 00057 class __EXPORT keydata : public OrderedObject 00058 { 00059 private: 00060 friend class keyfile; 00061 OrderedIndex index; 00062 keydata(keyfile *file); 00063 keydata(keyfile *file, const char *id); 00064 const char *name; 00065 keyfile *root; 00066 00067 public: 00073 class __LOCAL keyvalue : public OrderedObject 00074 { 00075 private: 00076 friend class keydata; 00077 friend class keyfile; 00078 keyvalue(keyfile *allocator, keydata *section, const char *key, const char *data); 00079 public: 00080 const char *id; 00081 const char *value; 00082 }; 00083 00084 friend class keyvalue; 00085 00091 const char *get(const char *id) const; 00092 00098 inline const char *operator()(const char *id) const 00099 {return get(id);} 00100 00108 void set(const char *id, const char *value); 00109 00115 void clear(const char *id); 00116 00121 inline const char *get(void) const 00122 {return name;} 00123 00128 inline keyvalue *begin(void) const 00129 {return (keyvalue *)index.begin();} 00130 00135 inline keyvalue *end(void) const 00136 {return (keyvalue*)index.end();} 00137 00141 typedef linked_pointer<keyvalue> iterator; 00142 }; 00143 00150 class __EXPORT keyfile : public memalloc 00151 { 00152 private: 00153 friend class keydata; 00154 OrderedIndex index; 00155 keydata *defaults; 00156 int errcode; 00157 00158 protected: 00159 keydata *create(const char *section); 00160 00161 #ifdef _MSWINDOWS_ 00162 void load(HKEY root, keydata *section = NULL, const char *path = NULL); 00163 bool save(HKEY root, keydata *section = NULL, const char *path = NULL); 00164 #endif 00165 00166 public: 00171 keyfile(size_t pagesize = 0); 00172 00178 keyfile(const char *path, size_t pagesize = 0); 00179 00180 keyfile(const keyfile ©, size_t pagesize = 0); 00181 00188 void load(const char *path); 00189 00195 bool save(const char *path); 00196 00201 void load(const keyfile *source); 00202 00207 void load(const keydata *source); 00208 00212 void release(void); 00213 00219 keydata *get(const char *section) const; 00220 00221 inline keydata *operator()(const char *section) const 00222 {return get(section);} 00223 00224 inline keydata *operator[](const char *section) const 00225 {return get(section);} 00226 00231 inline keydata *get(void) const 00232 {return defaults;} 00233 00238 inline keydata *begin(void) const 00239 {return (keydata *)index.begin();} 00240 00245 inline keydata *end(void) const 00246 {return (keydata *)index.end();} 00247 00251 typedef linked_pointer<keydata> iterator; 00252 00253 inline int err(void) 00254 {return errcode;} 00255 }; 00256 00257 } // namespace ucommon 00258 00259 #endif