UCommon
|
00001 // Copyright (C) 2006-2010 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 keydata *create(const char *section); 00159 00160 public: 00165 keyfile(size_t pagesize = 0); 00166 00172 keyfile(const char *path, size_t pagesize = 0); 00173 00180 void load(const char *path); 00181 00185 void release(void); 00186 00192 keydata *get(const char *section) const; 00193 00194 inline keydata *operator()(const char *section) const 00195 {return get(section);}; 00196 00197 inline keydata *operator[](const char *section) const 00198 {return get(section);}; 00199 00204 inline keydata *get(void) const 00205 {return defaults;}; 00206 00211 inline keydata *begin(void) const 00212 {return (keydata *)index.begin();}; 00213 00218 inline keydata *end(void) const 00219 {return (keydata *)index.end();}; 00220 00224 typedef linked_pointer<keydata> iterator; 00225 00226 inline int err(void) 00227 {return errcode;} 00228 }; 00229 00230 END_NAMESPACE 00231 00232 #endif