• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright 2005 The Android Open Source Project
3 //
4 // Preferences file access.
5 //
6 #ifndef _SIM_PREFERENCES_H
7 #define _SIM_PREFERENCES_H
8 
9 #include "tinyxml.h"
10 
11 /*
12  * This class provides access to a preferences file.  It's possible to
13  * have more than one instance, though it's probably unwise to have multiple
14  * objects for the same file on disk.
15  *
16  * All value are stored as strings.  The class doesn't really try to
17  * enforce type safety, but it will complain if you try to do something
18  * nonsensical (like convert "foo" to an integer).
19  */
20 class Preferences {
21 public:
Preferences(void)22     Preferences(void) :
23         mpDoc(NULL), mDirty(false)
24         {}
~Preferences(void)25     ~Preferences(void) {
26         delete mpDoc;
27     }
28 
29     /* load all preferences from a file */
30     bool Load(const char* fileName);
31 
32     /* save all preferences to a file */
33     bool Save(const char* fileName);
34 
35     /* create new preferences set (use when file does not exist) */
36     bool Create(void);
37 
38     /*
39      * Retrieve a value from the preferences database.
40      *
41      * These return "false" if the value was not found or could not be
42      * converted to the expected type.  The value pointed to be the second
43      * arg is guaranteed to be left undisturbed in this case.
44      *
45      * The value set by GetString(const char*, char**) will be set to
46      * newly-allocated storage that must be freed with "delete[]".  This
47      * is done instead of returning a const char* because it's unclear
48      * what guarantees TinyXml makes wrt string lifetime (especially in
49      * a multithreaded environment).
50      */
51     bool GetBool(const char* name, bool* pVal) const;
52     bool GetInt(const char* name, int* pInt) const;
53     bool GetDouble(const char* name, double* pDouble) const;
54     bool GetString(const char* name, char** pVal) const;
55     bool GetString(const char* name, wxString& str) const;
56 
57     /*
58      * Set a value in the database.
59      */
60     void SetBool(const char* name, bool val);
61     void SetInt(const char* name, int val);
62     void SetDouble(const char* name, double val);
63     void SetString(const char* name, const char* val);
64 
65     /*
66      * Just test for existence.
67      */
68     bool Exists(const char* name) const;
69 
70     /*
71      * Remove a <pref> from the config file.
72      */
73     bool RemovePref(const char* name);
74 
75     /*
76      * Get the value of the "dirty" flag.
77      */
GetDirty(void)78     bool GetDirty(void) const { return mDirty; }
79 
80 private:
81     /* Implementation of getters */
82     bool _GetBool(TiXmlElement* pElem, bool* pVal) const;
83     bool _GetInt(TiXmlElement* pElem, int* pInt) const;
84     bool _GetDouble(TiXmlElement* pElem, double* pDouble) const;
85     bool _GetString(TiXmlElement* pElem, wxString& str) const;
86 
87     /* this can be used to generate some defaults */
88     void SetDefaults(void);
89 
90     /* locate the named preference */
91     TiXmlNode* _FindNode(const char* type, const char* name) const;
92     TiXmlNode* FindPref(const char* str) const;
93     /* like FindPref, but returns a TiXmlElement */
94     TiXmlElement* FindPrefElement(const char* str) const;
95     /* add a new preference entry */
96     TiXmlElement* AddPref(const char* str);
97     /* removes a node */
98     bool _RemoveNode(TiXmlNode* pNode);
99 
100     TiXmlDocument*  mpDoc;
101     bool            mDirty;
102 };
103 
104 #endif // _SIM_PREFERENCES_H
105