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