PageRenderTime 25ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/fltk/FL/Fl_Preferences.H

http://luafltk.googlecode.com/
C Header | 288 lines | 145 code | 41 blank | 102 comment | 0 complexity | 92d2a7d68280b48b4f4ab15f039ecc60 MD5 | raw file
Possible License(s): LGPL-2.0, LGPL-3.0, 0BSD
  1. //
  2. // "$Id: Fl_Preferences.H 7415 2010-04-03 13:03:43Z manolo $"
  3. //
  4. // Preferences .
  5. //
  6. // Copyright 2002-2009 by Matthias Melcher.
  7. //
  8. // This library is free software; you can redistribute it and/or
  9. // modify it under the terms of the GNU Library General Public
  10. // License as published by the Free Software Foundation; either
  11. // version 2 of the License, or (at your option) any later version.
  12. //
  13. // This library is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. // Library General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Library General Public
  19. // License along with this library; if not, write to the Free Software
  20. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  21. // USA.
  22. //
  23. // Please report all bugs and problems on the following page:
  24. //
  25. // http://www.fltk.org/str.php
  26. //
  27. /* \file
  28. Fl_Preferences class . */
  29. #ifndef Fl_Preferences_H
  30. # define Fl_Preferences_H
  31. # include <stdio.h>
  32. # include "Fl_Export.H"
  33. /**
  34. \brief Fl_Preferences provides methods to store user
  35. settings between application starts.
  36. It is similar to the
  37. Registry on WIN32 and Preferences on MacOS, and provides a
  38. simple configuration mechanism for UNIX.
  39. Fl_Preferences uses a hierarchy to store data. It
  40. bundles similar data into groups and manages entries into those
  41. groups as name/value pairs.
  42. Preferences are stored in text files that can be edited
  43. manually. The file format is easy to read and relatively
  44. forgiving. Preferences files are the same on all platforms. User
  45. comments in preference files are preserved. Filenames are unique
  46. for each application by using a vendor/application naming
  47. scheme. The user must provide default values for all entries to
  48. ensure proper operation should preferences be corrupted or not
  49. yet exist.
  50. Entries can be of any length. However, the size of each
  51. preferences file should be kept small for performance
  52. reasons. One application can have multiple preferences files.
  53. Extensive binary data however should be stored in separate
  54. files: see getUserdataPath().
  55. \note Starting with FLTK 1.3, preference databases are expected to
  56. be in utf8 encoding. Previous databases were stored in the
  57. current chracter set or code page which renders them incompatible
  58. for text entries using international characters.
  59. */
  60. class FL_EXPORT Fl_Preferences
  61. {
  62. public:
  63. /**
  64. Define the scope of the preferences.
  65. */
  66. enum Root {
  67. SYSTEM=0, ///< Preferences are used system-wide
  68. USER ///< Preferences apply only to the current user
  69. };
  70. /**
  71. Every Fl_Preferences-Group has a uniqe ID.
  72. ID's can be retrieved from an Fl_Preferences-Group and can then be used
  73. to create more Fl_Preference references to the same data set, as long as the
  74. database remains open.
  75. */
  76. typedef void *ID;
  77. static const char *newUUID();
  78. Fl_Preferences( Root root, const char *vendor, const char *application );
  79. Fl_Preferences( const char *path, const char *vendor, const char *application );
  80. Fl_Preferences( Fl_Preferences &parent, const char *group );
  81. Fl_Preferences( Fl_Preferences *parent, const char *group );
  82. Fl_Preferences( Fl_Preferences &parent, int groupIndex );
  83. Fl_Preferences( Fl_Preferences *parent, int groupIndex );
  84. Fl_Preferences(const Fl_Preferences&);
  85. Fl_Preferences( ID id );
  86. ~Fl_Preferences();
  87. /** Return an ID that can later be reused to open more references to this dataset.
  88. */
  89. ID id() { return (ID)node; }
  90. /** Remove the group with this ID from a database.
  91. */
  92. static char remove(ID id_) { return ((Node*)id_)->remove(); }
  93. /** Return the name of this entry.
  94. */
  95. const char *name() { return node->name(); }
  96. /** Return the the full path to this entry.
  97. */
  98. const char *path() { return node->path(); }
  99. int groups();
  100. const char *group( int num_group );
  101. char groupExists( const char *key );
  102. char deleteGroup( const char *group );
  103. char deleteAllGroups();
  104. int entries();
  105. const char *entry( int index );
  106. char entryExists( const char *key );
  107. char deleteEntry( const char *entry );
  108. char deleteAllEntries();
  109. char clear();
  110. char set( const char *entry, int value );
  111. char set( const char *entry, float value );
  112. char set( const char *entry, float value, int precision );
  113. char set( const char *entry, double value );
  114. char set( const char *entry, double value, int precision );
  115. char set( const char *entry, const char *value );
  116. char set( const char *entry, const void *value, int size );
  117. char get( const char *entry, int &value, int defaultValue );
  118. char get( const char *entry, float &value, float defaultValue );
  119. char get( const char *entry, double &value, double defaultValue );
  120. char get( const char *entry, char *&value, const char *defaultValue );
  121. char get( const char *entry, char *value, const char *defaultValue, int maxSize );
  122. char get( const char *entry, void *&value, const void *defaultValue, int defaultSize );
  123. char get( const char *entry, void *value, const void *defaultValue, int defaultSize, int maxSize );
  124. int size( const char *entry );
  125. char getUserdataPath( char *path, int pathlen );
  126. void flush();
  127. // char export( const char *filename, Type fileFormat );
  128. // char import( const char *filename );
  129. char copyTo(class Fl_Tree*);
  130. /**
  131. 'Name' provides a simple method to create numerical or more complex
  132. procedural names for entries and groups on the fly.
  133. Example: prefs.set(Fl_Preferences::Name("File%d",i),file[i]);.
  134. See test/preferences.cxx as a sample for writing arrays into preferences.<p>
  135. 'Name' is actually implemented as a class inside Fl_Preferences. It casts
  136. into const char* and gets automatically destroyed after the enclosing call
  137. ends.
  138. */
  139. class FL_EXPORT Name {
  140. char *data_;
  141. public:
  142. Name( unsigned int n );
  143. Name( const char *format, ... );
  144. /**
  145. Return the Name as a "C" string.
  146. \internal
  147. */
  148. operator const char *() { return data_; }
  149. ~Name();
  150. };
  151. /** \internal An entry associates a preference name to its corresponding value */
  152. struct Entry
  153. {
  154. char *name, *value;
  155. };
  156. private:
  157. Fl_Preferences() : node(0), rootNode(0) { }
  158. Fl_Preferences &operator=(const Fl_Preferences&);
  159. static char nameBuffer[128];
  160. static char uuidBuffer[40];
  161. static Fl_Preferences *runtimePrefs;
  162. class RootNode;
  163. class FL_EXPORT Node // a node contains a list to all its entries
  164. { // and all means to manage the tree structure
  165. Node *child_, *next_;
  166. union { // these two are mutually exclusive
  167. Node *parent_; // top_ bit clear
  168. RootNode *root_; // top_ bit set
  169. };
  170. char *path_;
  171. Entry *entry_;
  172. int nEntry_, NEntry_;
  173. unsigned char dirty_:1;
  174. unsigned char top_:1;
  175. unsigned char indexed_:1;
  176. // indexing routines
  177. Node **index_;
  178. int nIndex_, NIndex_;
  179. void createIndex();
  180. void updateIndex();
  181. void deleteIndex();
  182. public:
  183. static int lastEntrySet;
  184. public:
  185. Node( const char *path );
  186. ~Node();
  187. char copyTo(class Fl_Tree*, class Fl_Tree_Item*);
  188. // node methods
  189. int write( FILE *f );
  190. const char *name();
  191. const char *path() { return path_; }
  192. Node *find( const char *path );
  193. Node *search( const char *path, int offset=0 );
  194. Node *childNode( int ix );
  195. Node *addChild( const char *path );
  196. void setParent( Node *parent );
  197. Node *parent() { return top_?0L:parent_; }
  198. void setRoot(RootNode *r) { root_ = r; top_ = 1; }
  199. RootNode *findRoot();
  200. char remove();
  201. char dirty();
  202. void deleteAllChildren();
  203. // entry methods
  204. int nChildren();
  205. const char *child( int ix );
  206. void set( const char *name, const char *value );
  207. void set( const char *line );
  208. void add( const char *line );
  209. const char *get( const char *name );
  210. int getEntry( const char *name );
  211. char deleteEntry( const char *name );
  212. void deleteAllEntries();
  213. int nEntry() { return nEntry_; }
  214. Entry &entry(int i) { return entry_[i]; }
  215. };
  216. friend class Node;
  217. class FL_EXPORT RootNode // the root node manages file paths and basic reading and writing
  218. {
  219. Fl_Preferences *prefs_;
  220. char *filename_;
  221. char *vendor_, *application_;
  222. public:
  223. RootNode( Fl_Preferences *, Root root, const char *vendor, const char *application );
  224. RootNode( Fl_Preferences *, const char *path, const char *vendor, const char *application );
  225. RootNode( Fl_Preferences * );
  226. ~RootNode();
  227. int read();
  228. int write();
  229. char getPath( char *path, int pathlen );
  230. };
  231. friend class RootNode;
  232. protected:
  233. Node *node;
  234. RootNode *rootNode;
  235. };
  236. #endif // !Fl_Preferences_H
  237. //
  238. // End of "$Id: Fl_Preferences.H 7415 2010-04-03 13:03:43Z manolo $".
  239. //