FLTK
Fl_Preferences.H
1 //
2 // Preferences implementation for the Fast Light Tool Kit (FLTK).
3 //
4 // Copyright 2002-2010 by Matthias Melcher.
5 //
6 // This library is free software. Distribution and use rights are outlined in
7 // the file "COPYING" which should have been included with this file. If this
8 // file is missing or damaged, see the license at:
9 //
10 // https://www.fltk.org/COPYING.php
11 //
12 // Please see the following page on how to report bugs and issues:
13 //
14 // https://www.fltk.org/bugs.php
15 //
16 
17 /* \file
18  Fl_Preferences class . */
19 
20 #ifndef Fl_Preferences_H
21 # define Fl_Preferences_H
22 
23 # include <stdio.h>
24 # include "Fl_Export.H"
25 
63 class FL_EXPORT Fl_Preferences {
64 
65 public:
69  enum Root {
70  SYSTEM = 0,
71  USER,
72  ROOT_MASK = 0xFF,
73  CORE = 0x100,
74  CORE_SYSTEM = CORE|SYSTEM,
75  CORE_USER = CORE|USER
76  };
77 
85  typedef void *ID;
86 
87  static const char *newUUID();
88 
93  static const unsigned int NONE = 0x0000;
95  static const unsigned int USER_READ_OK = 0x0001;
97  static const unsigned int USER_WRITE_OK = 0x0002;
99  static const unsigned int USER_OK = USER_READ_OK | USER_WRITE_OK;
101  static const unsigned int SYSTEM_READ_OK = 0x0004;
103  static const unsigned int SYSTEM_WRITE_OK = 0x0008;
105  static const unsigned int SYSTEM_OK = SYSTEM_READ_OK | SYSTEM_WRITE_OK;
107  static const unsigned int APP_OK = SYSTEM_OK | USER_OK;
109  static const unsigned int CORE_READ_OK = 0x0010;
111  static const unsigned int CORE_WRITE_OK = 0x0020;
113  static const unsigned int CORE_OK = CORE_READ_OK | CORE_WRITE_OK;
115  static const unsigned int ALL_READ_OK = USER_READ_OK | SYSTEM_READ_OK | CORE_READ_OK;
117  static const unsigned int ALL_WRITE_OK = USER_WRITE_OK | SYSTEM_WRITE_OK | CORE_WRITE_OK;
119  static const unsigned int ALL = ALL_READ_OK | ALL_WRITE_OK;
120 
121  static void file_access(unsigned int flags);
122  static unsigned int file_access();
123 
124  Fl_Preferences( Root root, const char *vendor, const char *application );
125  Fl_Preferences( const char *path, const char *vendor, const char *application );
126  Fl_Preferences( Fl_Preferences &parent, const char *group );
127  Fl_Preferences( Fl_Preferences *parent, const char *group );
128  Fl_Preferences( Fl_Preferences &parent, int groupIndex );
129  Fl_Preferences( Fl_Preferences *parent, int groupIndex );
131  Fl_Preferences( ID id );
132  virtual ~Fl_Preferences();
133 
136  ID id() { return (ID)node; }
137 
140  static char remove(ID id_) { return ((Node*)id_)->remove(); }
141 
144  const char *name() { return node->name(); }
145 
148  const char *path() { return node->path(); }
149 
150  int groups();
151  const char *group( int num_group );
152  char groupExists( const char *key );
153  char deleteGroup( const char *group );
154  char deleteAllGroups();
155 
156  int entries();
157  const char *entry( int index );
158  char entryExists( const char *key );
159  char deleteEntry( const char *entry );
160  char deleteAllEntries();
161 
162  char clear();
163 
164  char set( const char *entry, int value );
165  char set( const char *entry, float value );
166  char set( const char *entry, float value, int precision );
167  char set( const char *entry, double value );
168  char set( const char *entry, double value, int precision );
169  char set( const char *entry, const char *value );
170  char set( const char *entry, const void *value, int size );
171 
172  char get( const char *entry, int &value, int defaultValue );
173  char get( const char *entry, float &value, float defaultValue );
174  char get( const char *entry, double &value, double defaultValue );
175  char get( const char *entry, char *&value, const char *defaultValue );
176  char get( const char *entry, char *value, const char *defaultValue, int maxSize );
177  char get( const char *entry, void *&value, const void *defaultValue, int defaultSize );
178  char get( const char *entry, void *value, const void *defaultValue, int defaultSize, int maxSize );
179 
180  int size( const char *entry );
181 
182  char getUserdataPath( char *path, int pathlen );
183 
184  void flush();
185 
186  // char export( const char *filename, Type fileFormat );
187  // char import( const char *filename );
188 
201  class FL_EXPORT Name {
202 
203  char *data_;
204 
205  public:
206  Name( unsigned int n );
207  Name( const char *format, ... );
208 
213  operator const char *() { return data_; }
214  ~Name();
215  };
216 
218  struct Entry {
219  char *name, *value;
220  };
221 
222 private:
223  Fl_Preferences() : node(0), rootNode(0) { }
224  Fl_Preferences &operator=(const Fl_Preferences&);
225 
226  static char nameBuffer[128];
227  static char uuidBuffer[40];
228  static Fl_Preferences *runtimePrefs;
229  static unsigned int fileAccess_;
230 
231 public: // older Sun compilers need this (public definition of the following classes)
232  class RootNode;
233 
234  class FL_EXPORT Node { // a node contains a list to all its entries
235  // and all means to manage the tree structure
236  Node *child_, *next_;
237  union { // these two are mutually exclusive
238  Node *parent_; // top_ bit clear
239  RootNode *root_; // top_ bit set
240  };
241  char *path_;
242  Entry *entry_;
243  int nEntry_, NEntry_;
244  unsigned char dirty_:1;
245  unsigned char top_:1;
246  unsigned char indexed_:1;
247  // indexing routines
248  Node **index_;
249  int nIndex_, NIndex_;
250  void createIndex();
251  void updateIndex();
252  void deleteIndex();
253  public:
254  static int lastEntrySet;
255  public:
256  Node( const char *path );
257  ~Node();
258  // node methods
259  int write( FILE *f );
260  const char *name();
261  const char *path() { return path_; }
262  Node *find( const char *path );
263  Node *search( const char *path, int offset=0 );
264  Node *childNode( int ix );
265  Node *addChild( const char *path );
266  void setParent( Node *parent );
267  Node *parent() { return top_?0L:parent_; }
268  void setRoot(RootNode *r) { root_ = r; top_ = 1; }
269  RootNode *findRoot();
270  char remove();
271  char dirty();
272  void clearDirtyFlags();
273  void deleteAllChildren();
274  // entry methods
275  int nChildren();
276  const char *child( int ix );
277  void set( const char *name, const char *value );
278  void set( const char *line );
279  void add( const char *line );
280  const char *get( const char *name );
281  int getEntry( const char *name );
282  char deleteEntry( const char *name );
283  void deleteAllEntries();
284  int nEntry() { return nEntry_; }
285  Entry &entry(int i) { return entry_[i]; }
286  };
287  friend class Node;
288 
289  class FL_EXPORT RootNode { // the root node manages file paths and basic reading and writing
290  Fl_Preferences *prefs_;
291  char *filename_;
292  char *vendor_, *application_;
293  Root root_;
294  public:
295  RootNode( Fl_Preferences *, Root root, const char *vendor, const char *application );
296  RootNode( Fl_Preferences *, const char *path, const char *vendor, const char *application );
298  ~RootNode();
299  int read();
300  int write();
301  char getPath( char *path, int pathlen );
302  };
303  friend class RootNode;
304 
305 protected:
306  Node *node;
307  RootNode *rootNode;
308 };
309 
310 #endif // !Fl_Preferences_H
Definition: Fl_Preferences.H:234
Definition: Fl_Preferences.H:218
const char * path()
Return the full path to this entry.
Definition: Fl_Preferences.H:148
Root
Define the scope of the preferences.
Definition: Fl_Preferences.H:69
const char * name()
Return the name of this entry.
Definition: Fl_Preferences.H:144
&#39;Name&#39; provides a simple method to create numerical or more complex procedural names for entries and ...
Definition: Fl_Preferences.H:201
ID id()
Return an ID that can later be reused to open more references to this dataset.
Definition: Fl_Preferences.H:136
Fl_Preferences provides methods to store user settings between application starts.
Definition: Fl_Preferences.H:63
void * ID
Every Fl_Preferences-Group has a uniqe ID.
Definition: Fl_Preferences.H:85
Definition: Fl_Preferences.H:289
Preferences apply only to the current user.
Definition: Fl_Preferences.H:71