FLTK 1.3.0
Fl_Preferences.H
00001 //
00002 // "$Id: Fl_Preferences.H 7949 2010-12-05 00:38:16Z greg.ercolano $"
00003 //
00004 // Preferences .
00005 //
00006 // Copyright 2002-2010 by Matthias Melcher.
00007 //
00008 // This library is free software; you can redistribute it and/or
00009 // modify it under the terms of the GNU Library General Public
00010 // License as published by the Free Software Foundation; either
00011 // version 2 of the License, or (at your option) any later version.
00012 //
00013 // This library is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016 // Library General Public License for more details.
00017 //
00018 // You should have received a copy of the GNU Library General Public
00019 // License along with this library; if not, write to the Free Software
00020 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00021 // USA.
00022 //
00023 // Please report all bugs and problems on the following page:
00024 //
00025 //     http://www.fltk.org/str.php
00026 //
00027 
00028 /* \file
00029    Fl_Preferences class . */
00030 
00031 #ifndef Fl_Preferences_H
00032 #  define Fl_Preferences_H
00033 
00034 #  include <stdio.h>
00035 #  include "Fl_Export.H" 
00036 
00069 class FL_EXPORT Fl_Preferences {
00070 
00071 public: 
00075   enum Root { 
00076     SYSTEM=0,   
00077     USER        
00078   };
00079   
00087   typedef void *ID;
00088   
00089   static const char *newUUID();
00090 
00091   Fl_Preferences( Root root, const char *vendor, const char *application );
00092   Fl_Preferences( const char *path, const char *vendor, const char *application );
00093   Fl_Preferences( Fl_Preferences &parent, const char *group );
00094   Fl_Preferences( Fl_Preferences *parent, const char *group );
00095   Fl_Preferences( Fl_Preferences &parent, int groupIndex );
00096   Fl_Preferences( Fl_Preferences *parent, int groupIndex );
00097   Fl_Preferences(const Fl_Preferences&);
00098   Fl_Preferences( ID id );
00099   virtual ~Fl_Preferences();
00100   
00103   ID id() { return (ID)node; }
00104   
00107   static char remove(ID id_) { return ((Node*)id_)->remove(); }
00108 
00111   const char *name() { return node->name(); }
00112   
00115   const char *path() { return node->path(); }
00116   
00117   int groups();
00118   const char *group( int num_group );
00119   char groupExists( const char *key );
00120   char deleteGroup( const char *group );
00121   char deleteAllGroups();
00122 
00123   int entries();
00124   const char *entry( int index );
00125   char entryExists( const char *key );
00126   char deleteEntry( const char *entry );
00127   char deleteAllEntries();
00128   
00129   char clear();
00130 
00131   char set( const char *entry, int value );
00132   char set( const char *entry, float value );
00133   char set( const char *entry, float value, int precision );
00134   char set( const char *entry, double value );
00135   char set( const char *entry, double value, int precision );
00136   char set( const char *entry, const char *value );
00137   char set( const char *entry, const void *value, int size ); 
00138   
00139   char get( const char *entry, int &value, int defaultValue );
00140   char get( const char *entry, float &value,  float defaultValue );
00141   char get( const char *entry, double &value, double defaultValue );
00142   char get( const char *entry, char *&value,  const char *defaultValue );
00143   char get( const char *entry, char *value,   const char *defaultValue, int maxSize );
00144   char get( const char *entry, void *&value,  const void *defaultValue, int defaultSize );
00145   char get( const char *entry, void *value,   const void *defaultValue, int defaultSize, int maxSize );
00146 
00147   int size( const char *entry );
00148 
00149   char getUserdataPath( char *path, int pathlen );
00150 
00151   void flush();
00152 
00153   // char export( const char *filename, Type fileFormat );
00154   // char import( const char *filename );
00155   
00167   class FL_EXPORT Name {
00168 
00169     char *data_;
00170 
00171   public: 
00172     Name( unsigned int n );
00173     Name( const char *format, ... );
00174 
00179     operator const char *() { return data_; }
00180     ~Name();
00181   };
00182 
00184   struct Entry {
00185     char *name, *value;
00186   };
00187 
00188 private: 
00189   Fl_Preferences() : node(0), rootNode(0) { }
00190   Fl_Preferences &operator=(const Fl_Preferences&);
00191 
00192   static char nameBuffer[128];
00193   static char uuidBuffer[40];
00194   static Fl_Preferences *runtimePrefs;
00195 
00196   class RootNode;
00197   
00198   class FL_EXPORT Node {        // a node contains a list to all its entries 
00199                                 // and all means to manage the tree structure
00200     Node *child_, *next_;
00201     union {                     // these two are mutually exclusive
00202       Node *parent_;            // top_ bit clear
00203       RootNode *root_;          // top_ bit set
00204     };
00205     char *path_;
00206     Entry *entry_;
00207     int nEntry_, NEntry_;
00208     unsigned char dirty_:1;
00209     unsigned char top_:1;
00210     unsigned char indexed_:1;
00211     // indexing routines
00212     Node **index_;
00213     int nIndex_, NIndex_;
00214     void createIndex();
00215     void updateIndex();
00216     void deleteIndex();
00217   public:
00218     static int lastEntrySet;
00219   public:
00220     Node( const char *path );
00221     ~Node();
00222     // node methods
00223     int write( FILE *f );
00224     const char *name();
00225     const char *path() { return path_; }
00226     Node *find( const char *path );
00227     Node *search( const char *path, int offset=0 );
00228     Node *childNode( int ix );
00229     Node *addChild( const char *path );
00230     void setParent( Node *parent );
00231     Node *parent() { return top_?0L:parent_; }
00232     void setRoot(RootNode *r) { root_ = r; top_ = 1; }
00233     RootNode *findRoot();
00234     char remove();
00235     char dirty();
00236     void deleteAllChildren();
00237     // entry methods
00238     int nChildren();
00239     const char *child( int ix );
00240     void set( const char *name, const char *value );
00241     void set( const char *line );
00242     void add( const char *line );
00243     const char *get( const char *name );
00244     int getEntry( const char *name );
00245     char deleteEntry( const char *name );
00246     void deleteAllEntries();
00247     int nEntry() { return nEntry_; }
00248     Entry &entry(int i) { return entry_[i]; }
00249   };
00250   friend class Node;
00251 
00252   class FL_EXPORT RootNode {            // the root node manages file paths and basic reading and writing
00253     Fl_Preferences *prefs_;
00254     char *filename_;
00255     char *vendor_, *application_;
00256   public:
00257     RootNode( Fl_Preferences *, Root root, const char *vendor, const char *application );
00258     RootNode( Fl_Preferences *, const char *path, const char *vendor, const char *application );
00259     RootNode( Fl_Preferences * );
00260     ~RootNode();
00261     int read();
00262     int write();
00263     char getPath( char *path, int pathlen );
00264   };
00265   friend class RootNode;
00266 
00267 protected:
00268   Node *node;
00269   RootNode *rootNode;
00270 };
00271 
00272 #endif // !Fl_Preferences_H
00273 
00274 //
00275 // End of "$Id: Fl_Preferences.H 7949 2010-12-05 00:38:16Z greg.ercolano $".
00276 //