00001 /* -*- Mode: CC; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 5 -*- */ 00002 00003 /* 00004 * The PreferenceHandler class provides an easy mechanism to store and 00005 * retrieve key/value pairs of different types (bool, int, float and string). 00006 */ 00007 00008 /* 00009 -----BEGIN PGP SIGNED MESSAGE----- 00010 Hash: SHA1 00011 00012 Created by Daniel Aarno on Mon Oct 13 2003. 00013 Copyright (c) 2004 by Daniel Aarno - <macbishop@users.sf.net> 00014 M. Sc. in Electrical Engineering - http://www.e.kth.se/~e98_aar 00015 00016 All Rights Reserved 00017 ATTRIBUTION ASSURANCE LICENSE (adapted from the original BSD license) 00018 Redistribution and use in source and binary forms, with or without 00019 modification, are permitted provided that the conditions below are met. 00020 These conditions require a modest attribution to (the 00021 "Author"), who hopes that its promotional value may help justify the 00022 thousands of dollars in otherwise billable time invested in writing 00023 this and other freely available, open-source software. 00024 00025 1. Redistributions of source code, in whole or part and with or without 00026 modification (the "Code"), must prominently display this GPG-signed 00027 text in verifiable form. 00028 2. Redistributions of the Code in binary form must be accompanied by 00029 this GPG-signed text in any documentation and, each time the resulting 00030 executable program or a program dependent thereon is launched, a 00031 prominent display (e.g., splash screen or banner text) of the Author's 00032 attribution information, which includes: 00033 (a) Name ("Daniel Aarno"), 00034 (b) Professional identification ("M. Sc. in Electrical Engineering"), and 00035 (c) URL ("http://www.e.kth.se/~e98_aar"). 00036 3. Neither the name nor any trademark of the Author may be used to 00037 endorse or promote products derived from this software without specific 00038 prior written permission. 00039 4. Users are entirely responsible, to the exclusion of the Author and 00040 any other persons, for compliance with (1) regulations set by owners or 00041 administrators of employed equipment, (2) licensing terms of any other 00042 software, and (3) local regulations regarding use, including those 00043 regarding import, export, and use of encryption software. 00044 00045 THIS FREE SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND 00046 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00047 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 00048 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 00049 EVENT SHALL THE AUTHOR OR ANY CONTRIBUTOR BE LIABLE FOR 00050 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00051 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 00052 EFFECTS OF UNAUTHORIZED OR MALICIOUS NETWORK ACCESS; 00053 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00054 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 00055 AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00056 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00057 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 00058 IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00059 -----BEGIN PGP SIGNATURE----- 00060 Version: GnuPG v1.2.4 (GNU/Linux) 00061 00062 iD8DBQFB+7iBi6ECThHTSIkRAuH8AKC1JQmsD5L+fRFOW9qIciicWvbgigCeIREb 00063 BnQOCM2kl3Is+rZ2Cz1iy1E= 00064 =qw2o 00065 -----END PGP SIGNATURE----- 00066 */ 00067 00068 #ifndef _PREFERENCE_HANDLER_ 00069 #define _PREFERENCE_HANDLER_ 00070 00071 #include <string> 00072 #include <unistd.h> 00073 #include <sys/types.h> 00074 #include <errno.h> 00075 #include <libxml/xmlmemory.h> 00076 #include <libxml/parser.h> 00077 00078 enum { 00079 PREFERENCE_HANDLER_PARSE_ERROR = 1, 00080 PREFERENCE_HANDLER_NO_KEY_ERROR 00081 }; 00082 00119 class PreferenceHandler 00120 { 00121 private: 00122 std::string path; 00123 xmlDocPtr doc; 00124 int error; 00125 bool modified; 00126 00127 static PreferenceHandler* stdPH; 00128 00129 public: 00138 PreferenceHandler(std::string path); 00139 00143 ~PreferenceHandler(); 00144 00148 int getError() { return error; } 00149 00156 bool boolForKey(const std::string &key); 00157 bool boolForKey(const char* key); 00158 00164 float floatForKey(const std::string &key); 00165 float floatForKey(const char* key); 00166 00172 int intForKey(const std::string &key); 00173 int intForKey(const char* key); 00174 00179 std::string stringForKey(const std::string &key); 00180 std::string stringForKey(const char* key); 00181 00187 void setBoolForKey(const bool b, const std::string &key); 00188 void setBoolForKey(const bool b, const char* key); 00189 00195 void setFloatForKey(const float f, const std::string &key); 00196 void setFloatForKey(const float f, const char* key); 00197 00203 void setIntForKey(const int i, const std::string &key); 00204 void setIntForKey(const int i, const char* key); 00205 00212 void setStringForKey(const std::string &str, const std::string &key); 00213 void setStringForKey(const char* str, const char* key); 00214 00226 bool synchronize(); 00227 00235 static void Init(std::string path); 00236 00239 static void Clean(); 00246 static PreferenceHandler* StandardPreferenceHandler(); 00247 00254 static void ExpandTildeInPath(std::string &path); 00255 00256 private: 00257 xmlNodePtr getNodeForKey(const std::string &key); 00258 xmlNodePtr addNewKey(const std::string &key); 00259 }; 00260 00261 #endif //_PREFERENCE_HANDLER_
1.4.7