FLTK 1.4.0
Loading...
Searching...
No Matches
Fl_Preferences.H
1//
2// Preferences implementation for the Fast Light Tool Kit (FLTK).
3//
4// Copyright 2002-2023 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# include "fl_attr.h"
26
27//class Fl_String;
28#if (FLTK_USE_STD)
29#include <string>
30#endif
31
124class FL_EXPORT Fl_Preferences {
125
126public:
130 enum Root {
131 UNKNOWN_ROOT_TYPE = -1,
132 SYSTEM = 0,
135 ROOT_MASK = 0x00FF,
136 CORE = 0x0100,
137 C_LOCALE = 0x1000,
138 CLEAR = 0x2000,
140 SYSTEM_L = SYSTEM | C_LOCALE,
141 USER_L = USER | C_LOCALE,
142 CORE_SYSTEM_L = CORE | SYSTEM_L,
143 CORE_USER_L = CORE | USER_L,
144 CORE_SYSTEM = CORE | SYSTEM,
145 CORE_USER = CORE | USER
146 };
147
155 typedef void *ID;
156
157 static const char *new_UUID();
158
163 static const unsigned int NONE = 0x0000;
165 static const unsigned int USER_READ_OK = 0x0001;
167 static const unsigned int USER_WRITE_OK = 0x0002;
169 static const unsigned int USER_OK = USER_READ_OK | USER_WRITE_OK;
171 static const unsigned int SYSTEM_READ_OK = 0x0004;
173 static const unsigned int SYSTEM_WRITE_OK = 0x0008;
175 static const unsigned int SYSTEM_OK = SYSTEM_READ_OK | SYSTEM_WRITE_OK;
177 static const unsigned int APP_OK = SYSTEM_OK | USER_OK;
179 static const unsigned int CORE_READ_OK = 0x0010;
181 static const unsigned int CORE_WRITE_OK = 0x0020;
183 static const unsigned int CORE_OK = CORE_READ_OK | CORE_WRITE_OK;
185 static const unsigned int ALL_READ_OK = USER_READ_OK | SYSTEM_READ_OK | CORE_READ_OK;
187 static const unsigned int ALL_WRITE_OK = USER_WRITE_OK | SYSTEM_WRITE_OK | CORE_WRITE_OK;
189 static const unsigned int ALL = ALL_READ_OK | ALL_WRITE_OK;
190
191 static void file_access(unsigned int flags);
192 static unsigned int file_access();
193 static Root filename( char *buffer, size_t buffer_size, Root root, const char *vendor, const char *application );
194
195 Fl_Preferences( Root root, const char *vendor, const char *application );
196 Fl_Preferences( const char *path, const char *vendor, const char *application, Root flags );
197 Fl_Preferences( Fl_Preferences &parent, const char *group );
198 Fl_Preferences( Fl_Preferences *parent, const char *group );
199 Fl_Preferences( Fl_Preferences &parent, int groupIndex );
200 Fl_Preferences( Fl_Preferences *parent, int groupIndex );
202 Fl_Preferences( ID id );
203 virtual ~Fl_Preferences();
204
205 FL_DEPRECATED("in 1.4.0 - use Fl_Preferences(path, vendor, application, flags) instead",
206 Fl_Preferences( const char *path, const char *vendor, const char *application ) );
207
208 Root filename( char *buffer, size_t buffer_size);
209
212 ID id() { return (ID)node; }
213
216 static char remove(ID id_) { return ((Node*)id_)->remove(); }
217
220 const char *name() { return node->name(); }
221
224 const char *path() { return node->path(); }
225
226 int groups();
227 const char *group( int num_group );
228 char group_exists( const char *key );
229 char delete_group( const char *group );
230 char delete_all_groups();
231
232 int entries();
233 const char *entry( int index );
234 char entry_exists( const char *key );
235 char delete_entry( const char *entry );
236 char delete_all_entries();
237
238 char clear();
239
240 char set( const char *entry, int value );
241 char set( const char *entry, float value );
242 char set( const char *entry, float value, int precision );
243 char set( const char *entry, double value );
244 char set( const char *entry, double value, int precision );
245 char set( const char *entry, const char *value );
246 char set( const char *entry, const void *value, int size );
247
248 char get( const char *entry, int &value, int defaultValue );
249 char get( const char *entry, float &value, float defaultValue );
250 char get( const char *entry, double &value, double defaultValue );
251 char get( const char *entry, char *&value, const char *defaultValue );
252 char get( const char *entry, char *value, const char *defaultValue, int maxSize );
253 char get( const char *entry, void *&value, const void *defaultValue, int defaultSize );
254 char get( const char *entry, void *value, const void *defaultValue, int defaultSize, int maxSize );
255 char get( const char *entry, void *value, const void *defaultValue, int defaultSize, int *size );
256
257// char set( const char *entry, const Fl_String &value );
258// char get( const char *entry, Fl_String &value, const Fl_String &defaultValue );
259
260#if (FLTK_USE_STD)
261 char set( const char *entry, const std::string &value );
262 char get( const char *entry, std::string &value, const std::string &defaultValue );
263#endif
264
265 int size( const char *entry );
266
267 char get_userdata_path( char *path, int pathlen );
268
269 int flush();
270
271 int dirty();
272
274 static const char *newUUID() { return new_UUID(); }
275 char groupExists( const char *key ) { return group_exists(key); }
276 char deleteGroup( const char *group ) { return delete_group(group); }
277 char deleteAllGroups() { return delete_all_groups(); }
278 char entryExists( const char *key ) { return entry_exists(key); }
279 char deleteEntry( const char *entry ) { return delete_entry(entry); }
280 char deleteAllEntries() { return delete_all_entries(); }
281 char getUserdataPath( char *path, int pathlen ) { return get_userdata_path(path, pathlen); }
296 class FL_EXPORT Name {
297
298 char *data_;
299
300 public:
301 Name( unsigned int n );
302 Name( const char *format, ... );
303
308 operator const char *() { return data_; }
309 ~Name();
310 };
311
313 struct Entry {
314 char *name, *value;
315 };
316
317private:
318 Fl_Preferences() : node(0), rootNode(0) { }
319 Fl_Preferences &operator=(const Fl_Preferences&);
320
321 static char nameBuffer[128];
322 static char uuidBuffer[40];
323 static Fl_Preferences *runtimePrefs;
324 static unsigned int fileAccess_;
325
326public: // older Sun compilers need this (public definition of the following classes)
327 class RootNode;
328
329 class FL_EXPORT Node { // a node contains a list to all its entries
330 // and all means to manage the tree structure
331 Node *first_child_, *next_;
332 union { // these two are mutually exclusive
333 Node *parent_; // top_ bit clear
334 RootNode *root_node_; // top_ bit set
335 };
336 char *path_;
337 Entry *entry_;
338 int nEntry_, NEntry_;
339 unsigned char dirty_:1;
340 unsigned char top_:1;
341 unsigned char indexed_:1;
342 // indexing routines
343 Node **index_;
344 int nIndex_, NIndex_;
345 void createIndex();
346 void updateIndex();
347 void deleteIndex();
348 public:
349 static int lastEntrySet;
350 public:
351 Node( const char *path );
352 ~Node();
353 // node methods
354 int write( FILE *f );
355 const char *name();
356 const char *path() { return path_; }
357 Node *find( const char *path );
358 Node *search( const char *path, int offset=0 );
359 Node *childNode( int ix );
360 Node *addChild( const char *path );
361 void setParent( Node *parent );
362 Node *parent() { return top_?0L:parent_; }
363 void setRoot(RootNode *r) { root_node_ = r; top_ = 1; }
364 RootNode *findRoot();
365 char remove();
366 char dirty();
367 void clearDirtyFlags();
368 void deleteAllChildren();
369 // entry methods
370 int nChildren();
371 const char *child( int ix );
372 void set( const char *name, const char *value );
373 void set( const char *line );
374 void add( const char *line );
375 const char *get( const char *name );
376 int getEntry( const char *name );
377 char deleteEntry( const char *name );
378 void deleteAllEntries();
379 int nEntry() { return nEntry_; }
380 Entry &entry(int i) { return entry_[i]; }
381 };
382 friend class Node;
383
384 class FL_EXPORT RootNode { // the root node manages file paths and basic reading and writing
385 Fl_Preferences *prefs_;
386 char *filename_;
387 char *vendor_, *application_;
388 Root root_type_;
389 public:
390 RootNode( Fl_Preferences *, Root root, const char *vendor, const char *application );
391 RootNode( Fl_Preferences *, const char *path, const char *vendor, const char *application, Root flags );
393 ~RootNode();
394 int read();
395 int write();
396 char getPath( char *path, int pathlen );
397 char *filename() { return filename_; }
398 Root root() { return root_type_; }
399 };
400 friend class RootNode;
401
402protected:
403 Node *node;
404 RootNode *rootNode;
405};
406
407#endif // !Fl_Preferences_H
'Name' provides a simple method to create numerical or more complex procedural names for entries and ...
Definition Fl_Preferences.H:296
Definition Fl_Preferences.H:329
Definition Fl_Preferences.H:384
Fl_Preferences store user settings between application starts.
Definition Fl_Preferences.H:124
Root
Define the scope of the preferences.
Definition Fl_Preferences.H:130
@ USER
Preferences apply only to the current user. Deprecated, see USER_L.
Definition Fl_Preferences.H:133
@ MEMORY
Returned if querying memory mapped preferences.
Definition Fl_Preferences.H:134
ID id()
Return an ID that can later be reused to open more references to this dataset.
Definition Fl_Preferences.H:212
const char * path()
Return the full path to this entry.
Definition Fl_Preferences.H:224
void * ID
Every Fl_Preferences-Group has a unique ID.
Definition Fl_Preferences.H:155
const char * name()
Return the name of this entry.
Definition Fl_Preferences.H:220
static char remove(ID id_)
Remove the group with this ID from a database.
Definition Fl_Preferences.H:216
This file defines compiler-specific macros.
#define FL_DEPRECATED(msg, func)
Enclosing a function or method in FL_DEPRECATED marks it as no longer recommended.
Definition fl_attr.h:57
Definition Fl_Preferences.H:313