2002-04-30 00:57:31 +04:00
|
|
|
//
|
2005-02-25 00:55:12 +03:00
|
|
|
// "$Id$"
|
2002-04-30 00:57:31 +04:00
|
|
|
//
|
2008-09-16 11:26:22 +04:00
|
|
|
// Preferences .
|
2002-04-30 00:57:31 +04:00
|
|
|
//
|
2009-01-01 19:11:32 +03:00
|
|
|
// Copyright 2002-2009 by Matthias Melcher.
|
2002-04-30 00:57:31 +04:00
|
|
|
//
|
|
|
|
// This library is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Library General Public
|
|
|
|
// License as published by the Free Software Foundation; either
|
|
|
|
// version 2 of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// Library General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Library General Public
|
|
|
|
// License along with this library; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
// USA.
|
|
|
|
//
|
2005-04-16 04:13:17 +04:00
|
|
|
// Please report all bugs and problems on the following page:
|
|
|
|
//
|
|
|
|
// http://www.fltk.org/str.php
|
2002-04-30 00:57:31 +04:00
|
|
|
//
|
|
|
|
|
2008-10-15 17:46:06 +04:00
|
|
|
/* \file
|
2008-09-16 11:26:22 +04:00
|
|
|
Fl_Preferences class . */
|
2008-04-08 04:09:16 +04:00
|
|
|
|
2002-04-30 00:57:31 +04:00
|
|
|
#ifndef Fl_Preferences_H
|
|
|
|
# define Fl_Preferences_H
|
|
|
|
|
2009-04-01 18:10:58 +04:00
|
|
|
# include <stdio.h>
|
2002-08-14 20:19:48 +04:00
|
|
|
# include "Fl_Export.H"
|
2002-04-30 00:57:31 +04:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2008-10-19 13:37:21 +04:00
|
|
|
\brief Fl_Preferences provides methods to store user
|
|
|
|
settings between application starts.
|
|
|
|
|
|
|
|
It is similar to the
|
2008-09-15 00:00:03 +04:00
|
|
|
Registry on WIN32 and Preferences on MacOS, and provides a
|
|
|
|
simple configuration mechanism for UNIX.
|
|
|
|
|
2008-09-15 23:21:20 +04:00
|
|
|
Fl_Preferences uses a hierarchy to store data. It
|
2008-09-15 00:00:03 +04:00
|
|
|
bundles similar data into groups and manages entries into those
|
|
|
|
groups as name/value pairs.
|
|
|
|
|
|
|
|
Preferences are stored in text files that can be edited
|
|
|
|
manually. The file format is easy to read and relatively
|
|
|
|
forgiving. Preferences files are the same on all platforms. User
|
|
|
|
comments in preference files are preserved. Filenames are unique
|
|
|
|
for each application by using a vendor/application naming
|
|
|
|
scheme. The user must provide default values for all entries to
|
|
|
|
ensure proper operation should preferences be corrupted or not
|
|
|
|
yet exist.
|
|
|
|
|
|
|
|
Entries can be of any length. However, the size of each
|
2010-01-12 11:48:55 +03:00
|
|
|
preferences file should be kept small for performance
|
2008-09-15 00:00:03 +04:00
|
|
|
reasons. One application can have multiple preferences files.
|
|
|
|
Extensive binary data however should be stored in separate
|
2008-10-19 13:37:21 +04:00
|
|
|
files: see getUserdataPath().
|
2010-01-24 03:16:33 +03:00
|
|
|
|
2008-10-19 13:37:21 +04:00
|
|
|
\note Starting with FLTK 1.3, preference databases are expected to
|
|
|
|
be in utf8 encoding. Previous databases were stored in the
|
|
|
|
current chracter set or code page which renders them incompatible
|
|
|
|
for text entries using international characters.
|
2002-04-30 00:57:31 +04:00
|
|
|
*/
|
2002-08-14 20:19:48 +04:00
|
|
|
class FL_EXPORT Fl_Preferences
|
2002-04-30 00:57:31 +04:00
|
|
|
{
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
2008-04-08 04:09:16 +04:00
|
|
|
/**
|
2008-09-15 00:00:03 +04:00
|
|
|
Define the scope of the preferences.
|
2008-04-08 04:09:16 +04:00
|
|
|
*/
|
|
|
|
enum Root {
|
2008-09-03 00:25:52 +04:00
|
|
|
SYSTEM=0, ///< Preferences are used system-wide
|
2008-04-08 04:09:16 +04:00
|
|
|
USER ///< Preferences apply only to the current user
|
|
|
|
};
|
2009-12-27 23:14:04 +03:00
|
|
|
|
2009-12-29 01:26:48 +03:00
|
|
|
/**
|
|
|
|
Every Fl_Preferences-Group has a uniqe ID.
|
|
|
|
|
|
|
|
ID's can be retrieved from an Fl_Preferences-Group and can then be used
|
|
|
|
to create more Fl_Preference references to the same data set, as long as the
|
2010-04-03 17:03:43 +04:00
|
|
|
database remains open.
|
2009-12-29 01:26:48 +03:00
|
|
|
*/
|
|
|
|
typedef void *ID;
|
|
|
|
|
2009-12-27 23:14:04 +03:00
|
|
|
static const char *newUUID();
|
2002-04-30 00:57:31 +04:00
|
|
|
|
2002-08-14 20:19:48 +04:00
|
|
|
Fl_Preferences( Root root, const char *vendor, const char *application );
|
2002-08-27 07:03:37 +04:00
|
|
|
Fl_Preferences( const char *path, const char *vendor, const char *application );
|
2008-04-08 04:09:16 +04:00
|
|
|
Fl_Preferences( Fl_Preferences &parent, const char *group );
|
2009-12-27 23:14:04 +03:00
|
|
|
Fl_Preferences( Fl_Preferences *parent, const char *group );
|
|
|
|
Fl_Preferences( Fl_Preferences &parent, int groupIndex );
|
|
|
|
Fl_Preferences( Fl_Preferences *parent, int groupIndex );
|
2010-01-24 03:16:33 +03:00
|
|
|
Fl_Preferences(const Fl_Preferences&);
|
2009-12-29 01:26:48 +03:00
|
|
|
Fl_Preferences( ID id );
|
2002-08-14 20:19:48 +04:00
|
|
|
~Fl_Preferences();
|
2009-12-29 01:26:48 +03:00
|
|
|
|
2009-12-29 02:10:11 +03:00
|
|
|
/** Return an ID that can later be reused to open more references to this dataset.
|
|
|
|
*/
|
|
|
|
ID id() { return (ID)node; }
|
2010-01-24 03:16:33 +03:00
|
|
|
|
2010-04-03 17:03:43 +04:00
|
|
|
/** Remove the group with this ID from a database.
|
2010-01-24 03:16:33 +03:00
|
|
|
*/
|
2010-04-03 13:14:03 +04:00
|
|
|
static char remove(ID id_) { return ((Node*)id_)->remove(); }
|
2002-04-30 00:57:31 +04:00
|
|
|
|
2009-12-27 23:14:04 +03:00
|
|
|
/** Return the name of this entry.
|
|
|
|
*/
|
|
|
|
const char *name() { return node->name(); }
|
|
|
|
|
|
|
|
/** Return the the full path to this entry.
|
|
|
|
*/
|
|
|
|
const char *path() { return node->path(); }
|
|
|
|
|
2002-08-14 20:19:48 +04:00
|
|
|
int groups();
|
2008-09-18 02:25:45 +04:00
|
|
|
const char *group( int num_group );
|
|
|
|
char groupExists( const char *key );
|
2002-08-14 20:19:48 +04:00
|
|
|
char deleteGroup( const char *group );
|
2010-01-12 11:48:55 +03:00
|
|
|
char deleteAllGroups();
|
2002-04-30 00:57:31 +04:00
|
|
|
|
2002-08-14 20:19:48 +04:00
|
|
|
int entries();
|
2008-04-08 04:09:16 +04:00
|
|
|
const char *entry( int index );
|
2008-09-18 02:25:45 +04:00
|
|
|
char entryExists( const char *key );
|
2002-08-14 20:19:48 +04:00
|
|
|
char deleteEntry( const char *entry );
|
2010-01-12 11:48:55 +03:00
|
|
|
char deleteAllEntries();
|
|
|
|
|
|
|
|
char clear();
|
2002-04-30 00:57:31 +04:00
|
|
|
|
2002-08-14 20:19:48 +04:00
|
|
|
char set( const char *entry, int value );
|
|
|
|
char set( const char *entry, float value );
|
2006-08-18 11:29:09 +04:00
|
|
|
char set( const char *entry, float value, int precision );
|
2002-08-14 20:19:48 +04:00
|
|
|
char set( const char *entry, double value );
|
2006-08-18 11:29:09 +04:00
|
|
|
char set( const char *entry, double value, int precision );
|
2002-08-14 20:19:48 +04:00
|
|
|
char set( const char *entry, const char *value );
|
|
|
|
char set( const char *entry, const void *value, int size );
|
2008-10-31 19:30:07 +03:00
|
|
|
|
|
|
|
char get( const char *entry, int &value, int defaultValue );
|
2002-08-14 20:19:48 +04:00
|
|
|
char get( const char *entry, float &value, float defaultValue );
|
|
|
|
char get( const char *entry, double &value, double defaultValue );
|
|
|
|
char get( const char *entry, char *&value, const char *defaultValue );
|
|
|
|
char get( const char *entry, char *value, const char *defaultValue, int maxSize );
|
|
|
|
char get( const char *entry, void *&value, const void *defaultValue, int defaultSize );
|
|
|
|
char get( const char *entry, void *value, const void *defaultValue, int defaultSize, int maxSize );
|
2008-04-08 04:09:16 +04:00
|
|
|
|
2002-08-14 20:19:48 +04:00
|
|
|
int size( const char *entry );
|
2002-04-30 00:57:31 +04:00
|
|
|
|
2002-08-14 20:19:48 +04:00
|
|
|
char getUserdataPath( char *path, int pathlen );
|
2008-04-08 04:09:16 +04:00
|
|
|
|
2002-08-14 20:19:48 +04:00
|
|
|
void flush();
|
2002-04-30 00:57:31 +04:00
|
|
|
|
2002-08-14 20:19:48 +04:00
|
|
|
// char export( const char *filename, Type fileFormat );
|
|
|
|
// char import( const char *filename );
|
2010-01-11 01:37:56 +03:00
|
|
|
|
|
|
|
char copyTo(class Fl_Tree*);
|
2002-04-30 00:57:31 +04:00
|
|
|
|
2008-04-08 04:09:16 +04:00
|
|
|
/**
|
2008-09-15 00:00:03 +04:00
|
|
|
'Name' provides a simple method to create numerical or more complex
|
|
|
|
procedural names for entries and groups on the fly.
|
|
|
|
|
2008-09-15 23:21:20 +04:00
|
|
|
Example: prefs.set(Fl_Preferences::Name("File%d",i),file[i]);.
|
2008-09-15 00:00:03 +04:00
|
|
|
|
2008-09-15 23:21:20 +04:00
|
|
|
See test/preferences.cxx as a sample for writing arrays into preferences.<p>
|
2008-09-15 00:00:03 +04:00
|
|
|
'Name' is actually implemented as a class inside Fl_Preferences. It casts
|
2008-09-15 23:21:20 +04:00
|
|
|
into const char* and gets automatically destroyed after the enclosing call
|
2008-09-15 00:00:03 +04:00
|
|
|
ends.
|
2008-04-08 04:09:16 +04:00
|
|
|
*/
|
2003-01-21 17:51:54 +03:00
|
|
|
class FL_EXPORT Name {
|
2008-04-08 04:09:16 +04:00
|
|
|
|
2002-05-01 02:25:18 +04:00
|
|
|
char *data_;
|
2008-04-08 04:09:16 +04:00
|
|
|
|
2002-05-01 02:25:18 +04:00
|
|
|
public:
|
2008-04-08 04:09:16 +04:00
|
|
|
|
2002-08-14 20:19:48 +04:00
|
|
|
Name( unsigned int n );
|
|
|
|
Name( const char *format, ... );
|
2008-04-08 04:09:16 +04:00
|
|
|
|
|
|
|
/**
|
2008-10-31 19:30:07 +03:00
|
|
|
Return the Name as a "C" string.
|
2008-09-15 00:00:03 +04:00
|
|
|
\internal
|
2008-04-08 04:09:16 +04:00
|
|
|
*/
|
2002-08-14 20:19:48 +04:00
|
|
|
operator const char *() { return data_; }
|
|
|
|
~Name();
|
2002-05-01 02:25:18 +04:00
|
|
|
};
|
2009-04-18 22:52:59 +04:00
|
|
|
|
|
|
|
/** \internal An entry associates a preference name to its corresponding value */
|
2002-04-30 00:57:31 +04:00
|
|
|
struct Entry
|
|
|
|
{
|
|
|
|
char *name, *value;
|
|
|
|
};
|
|
|
|
|
2002-05-04 00:30:19 +04:00
|
|
|
private:
|
|
|
|
|
2010-01-24 03:16:33 +03:00
|
|
|
Fl_Preferences() : node(0), rootNode(0) { }
|
2005-07-26 11:59:01 +04:00
|
|
|
Fl_Preferences &operator=(const Fl_Preferences&);
|
|
|
|
|
2002-05-04 00:30:19 +04:00
|
|
|
static char nameBuffer[128];
|
2009-12-27 23:14:04 +03:00
|
|
|
static char uuidBuffer[40];
|
2010-01-24 03:16:33 +03:00
|
|
|
static Fl_Preferences *runtimePrefs;
|
2002-05-04 00:30:19 +04:00
|
|
|
|
2009-12-29 01:26:48 +03:00
|
|
|
class RootNode;
|
|
|
|
|
2003-01-21 17:51:54 +03:00
|
|
|
class FL_EXPORT Node // a node contains a list to all its entries
|
2002-04-30 00:57:31 +04:00
|
|
|
{ // and all means to manage the tree structure
|
2009-12-29 01:26:48 +03:00
|
|
|
Node *child_, *next_;
|
|
|
|
union { // these two are mutually exclusive
|
|
|
|
Node *parent_; // top_ bit clear
|
|
|
|
RootNode *root_; // top_ bit set
|
|
|
|
};
|
2002-04-30 00:57:31 +04:00
|
|
|
char *path_;
|
2010-02-21 00:14:47 +03:00
|
|
|
Entry *entry_;
|
|
|
|
int nEntry_, NEntry_;
|
2010-01-07 19:02:44 +03:00
|
|
|
unsigned char dirty_:1;
|
|
|
|
unsigned char top_:1;
|
2010-02-21 00:14:47 +03:00
|
|
|
unsigned char indexed_:1;
|
|
|
|
// indexing routines
|
|
|
|
Node **index_;
|
|
|
|
int nIndex_, NIndex_;
|
|
|
|
void createIndex();
|
|
|
|
void updateIndex();
|
|
|
|
void deleteIndex();
|
|
|
|
public:
|
|
|
|
static int lastEntrySet;
|
2002-04-30 00:57:31 +04:00
|
|
|
public:
|
|
|
|
Node( const char *path );
|
|
|
|
~Node();
|
2010-01-11 01:37:56 +03:00
|
|
|
char copyTo(class Fl_Tree*, class Fl_Tree_Item*);
|
2002-04-30 00:57:31 +04:00
|
|
|
// node methods
|
|
|
|
int write( FILE *f );
|
2009-12-27 23:14:04 +03:00
|
|
|
const char *name();
|
|
|
|
const char *path() { return path_; }
|
2002-04-30 00:57:31 +04:00
|
|
|
Node *find( const char *path );
|
2002-09-06 00:44:36 +04:00
|
|
|
Node *search( const char *path, int offset=0 );
|
2010-01-11 01:37:56 +03:00
|
|
|
Node *childNode( int ix );
|
2002-04-30 00:57:31 +04:00
|
|
|
Node *addChild( const char *path );
|
|
|
|
void setParent( Node *parent );
|
2009-12-29 02:10:11 +03:00
|
|
|
Node *parent() { return top_?0L:parent_; }
|
2009-12-29 01:26:48 +03:00
|
|
|
void setRoot(RootNode *r) { root_ = r; top_ = 1; }
|
|
|
|
RootNode *findRoot();
|
2002-04-30 00:57:31 +04:00
|
|
|
char remove();
|
|
|
|
char dirty();
|
2010-01-12 11:48:55 +03:00
|
|
|
void deleteAllChildren();
|
2002-04-30 00:57:31 +04:00
|
|
|
// entry methods
|
|
|
|
int nChildren();
|
|
|
|
const char *child( int ix );
|
|
|
|
void set( const char *name, const char *value );
|
|
|
|
void set( const char *line );
|
|
|
|
void add( const char *line );
|
|
|
|
const char *get( const char *name );
|
|
|
|
int getEntry( const char *name );
|
|
|
|
char deleteEntry( const char *name );
|
2010-01-12 11:48:55 +03:00
|
|
|
void deleteAllEntries();
|
2010-02-21 00:14:47 +03:00
|
|
|
int nEntry() { return nEntry_; }
|
|
|
|
Entry &entry(int i) { return entry_[i]; }
|
2002-04-30 00:57:31 +04:00
|
|
|
};
|
|
|
|
friend class Node;
|
|
|
|
|
2003-01-21 17:51:54 +03:00
|
|
|
class FL_EXPORT RootNode // the root node manages file paths and basic reading and writing
|
2002-04-30 00:57:31 +04:00
|
|
|
{
|
|
|
|
Fl_Preferences *prefs_;
|
|
|
|
char *filename_;
|
|
|
|
char *vendor_, *application_;
|
|
|
|
public:
|
2002-05-04 00:30:19 +04:00
|
|
|
RootNode( Fl_Preferences *, Root root, const char *vendor, const char *application );
|
2002-08-27 07:03:37 +04:00
|
|
|
RootNode( Fl_Preferences *, const char *path, const char *vendor, const char *application );
|
2010-01-24 03:16:33 +03:00
|
|
|
RootNode( Fl_Preferences * );
|
2002-04-30 00:57:31 +04:00
|
|
|
~RootNode();
|
|
|
|
int read();
|
|
|
|
int write();
|
2002-04-30 22:11:49 +04:00
|
|
|
char getPath( char *path, int pathlen );
|
2002-04-30 00:57:31 +04:00
|
|
|
};
|
|
|
|
friend class RootNode;
|
|
|
|
|
2010-01-24 03:16:33 +03:00
|
|
|
protected:
|
|
|
|
|
2002-04-30 00:57:31 +04:00
|
|
|
Node *node;
|
|
|
|
RootNode *rootNode;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#endif // !Fl_Preferences_H
|
|
|
|
|
|
|
|
//
|
2005-02-25 00:55:12 +03:00
|
|
|
// End of "$Id$".
|
2002-04-30 00:57:31 +04:00
|
|
|
//
|