FLTK 1.3.0
Fl_Preferences.H
00001 //
00002 // "$Id$"
00003 //
00004 // Preferences .
00005 //
00006 // Copyright 2002-2010 by Matthias Melcher.
00007 //
00008 // This library is free software. Distribution and use rights are outlined in
00009 // the file "COPYING" which should have been included with this file.  If this
00010 // file is missing or damaged, see the license at:
00011 //
00012 //     http://www.fltk.org/COPYING.php
00013 //
00014 // Please report all bugs and problems on the following page:
00015 //
00016 //     http://www.fltk.org/str.php
00017 //
00018 
00019 /* \file
00020    Fl_Preferences class . */
00021 
00022 #ifndef Fl_Preferences_H
00023 #  define Fl_Preferences_H
00024 
00025 #  include <stdio.h>
00026 #  include "Fl_Export.H" 
00027 
00060 class FL_EXPORT Fl_Preferences {
00061 
00062 public: 
00066   enum Root { 
00067     SYSTEM=0,   
00068     USER        
00069   };
00070   
00078   typedef void *ID;
00079   
00080   static const char *newUUID();
00081 
00082   Fl_Preferences( Root root, const char *vendor, const char *application );
00083   Fl_Preferences( const char *path, const char *vendor, const char *application );
00084   Fl_Preferences( Fl_Preferences &parent, const char *group );
00085   Fl_Preferences( Fl_Preferences *parent, const char *group );
00086   Fl_Preferences( Fl_Preferences &parent, int groupIndex );
00087   Fl_Preferences( Fl_Preferences *parent, int groupIndex );
00088   Fl_Preferences(const Fl_Preferences&);
00089   Fl_Preferences( ID id );
00090   virtual ~Fl_Preferences();
00091   
00094   ID id() { return (ID)node; }
00095   
00098   static char remove(ID id_) { return ((Node*)id_)->remove(); }
00099 
00102   const char *name() { return node->name(); }
00103   
00106   const char *path() { return node->path(); }
00107   
00108   int groups();
00109   const char *group( int num_group );
00110   char groupExists( const char *key );
00111   char deleteGroup( const char *group );
00112   char deleteAllGroups();
00113 
00114   int entries();
00115   const char *entry( int index );
00116   char entryExists( const char *key );
00117   char deleteEntry( const char *entry );
00118   char deleteAllEntries();
00119   
00120   char clear();
00121 
00122   char set( const char *entry, int value );
00123   char set( const char *entry, float value );
00124   char set( const char *entry, float value, int precision );
00125   char set( const char *entry, double value );
00126   char set( const char *entry, double value, int precision );
00127   char set( const char *entry, const char *value );
00128   char set( const char *entry, const void *value, int size ); 
00129   
00130   char get( const char *entry, int &value, int defaultValue );
00131   char get( const char *entry, float &value,  float defaultValue );
00132   char get( const char *entry, double &value, double defaultValue );
00133   char get( const char *entry, char *&value,  const char *defaultValue );
00134   char get( const char *entry, char *value,   const char *defaultValue, int maxSize );
00135   char get( const char *entry, void *&value,  const void *defaultValue, int defaultSize );
00136   char get( const char *entry, void *value,   const void *defaultValue, int defaultSize, int maxSize );
00137 
00138   int size( const char *entry );
00139 
00140   char getUserdataPath( char *path, int pathlen );
00141 
00142   void flush();
00143 
00144   // char export( const char *filename, Type fileFormat );
00145   // char import( const char *filename );
00146   
00159   class FL_EXPORT Name {
00160 
00161     char *data_;
00162 
00163   public: 
00164     Name( unsigned int n );
00165     Name( const char *format, ... );
00166 
00171     operator const char *() { return data_; }
00172     ~Name();
00173   };
00174 
00176   struct Entry {
00177     char *name, *value;
00178   };
00179 
00180 private: 
00181   Fl_Preferences() : node(0), rootNode(0) { }
00182   Fl_Preferences &operator=(const Fl_Preferences&);
00183 
00184   static char nameBuffer[128];
00185   static char uuidBuffer[40];
00186   static Fl_Preferences *runtimePrefs;
00187 
00188   class RootNode;
00189   
00190   class FL_EXPORT Node {        // a node contains a list to all its entries 
00191                                 // and all means to manage the tree structure
00192     Node *child_, *next_;
00193     union {                     // these two are mutually exclusive
00194       Node *parent_;            // top_ bit clear
00195       RootNode *root_;          // top_ bit set
00196     };
00197     char *path_;
00198     Entry *entry_;
00199     int nEntry_, NEntry_;
00200     unsigned char dirty_:1;
00201     unsigned char top_:1;
00202     unsigned char indexed_:1;
00203     // indexing routines
00204     Node **index_;
00205     int nIndex_, NIndex_;
00206     void createIndex();
00207     void updateIndex();
00208     void deleteIndex();
00209   public:
00210     static int lastEntrySet;
00211   public:
00212     Node( const char *path );
00213     ~Node();
00214     // node methods
00215     int write( FILE *f );
00216     const char *name();
00217     const char *path() { return path_; }
00218     Node *find( const char *path );
00219     Node *search( const char *path, int offset=0 );
00220     Node *childNode( int ix );
00221     Node *addChild( const char *path );
00222     void setParent( Node *parent );
00223     Node *parent() { return top_?0L:parent_; }
00224     void setRoot(RootNode *r) { root_ = r; top_ = 1; }
00225     RootNode *findRoot();
00226     char remove();
00227     char dirty();
00228     void deleteAllChildren();
00229     // entry methods
00230     int nChildren();
00231     const char *child( int ix );
00232     void set( const char *name, const char *value );
00233     void set( const char *line );
00234     void add( const char *line );
00235     const char *get( const char *name );
00236     int getEntry( const char *name );
00237     char deleteEntry( const char *name );
00238     void deleteAllEntries();
00239     int nEntry() { return nEntry_; }
00240     Entry &entry(int i) { return entry_[i]; }
00241   };
00242   friend class Node;
00243 
00244   class FL_EXPORT RootNode {            // the root node manages file paths and basic reading and writing
00245     Fl_Preferences *prefs_;
00246     char *filename_;
00247     char *vendor_, *application_;
00248   public:
00249     RootNode( Fl_Preferences *, Root root, const char *vendor, const char *application );
00250     RootNode( Fl_Preferences *, const char *path, const char *vendor, const char *application );
00251     RootNode( Fl_Preferences * );
00252     ~RootNode();
00253     int read();
00254     int write();
00255     char getPath( char *path, int pathlen );
00256   };
00257   friend class RootNode;
00258 
00259 protected:
00260   Node *node;
00261   RootNode *rootNode;
00262 };
00263 
00264 #endif // !Fl_Preferences_H
00265 
00266 //
00267 // End of "$Id$".
00268 //