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