00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef _FL_CONFIG_H_
00023 #define _FL_CONFIG_H_
00024
00025 #include "Enumerations.h"
00026 #include "Fl_Util.h"
00027 #include "Fl_String.h"
00028 #include "Fl_Color.h"
00029 #include "Fl_Ptr_List.h"
00030 #include "Fl_Map.h"
00031
00032 #include <string.h>
00033 #include <stdio.h>
00034 #include <stdlib.h>
00035
00036 #ifdef _WIN32_WCE
00037 #include "x.h"
00038 #endif
00039
00044
00048 enum ConfErrors {
00049 CONF_SUCCESS = 0,
00050 CONF_ERR_FILE,
00051 CONF_ERR_SECTION,
00052 CONF_ERR_KEY,
00053 CONF_ERR_MEMORY,
00054 CONF_ERR_NOVALUE,
00055 };
00056
00058 typedef Fl_Ptr_List Fl_Config_Sections;
00059
00061 typedef Fl_String_String_Map Fl_Config_Lines;
00062
00065 class Fl_Config;
00066
00072 class FL_API Fl_Config_Section
00073 {
00074 friend class Fl_Config;
00075 public:
00076 Fl_Config_Section(const Fl_String &name, const Fl_String &path, Fl_Config_Section *par);
00077 virtual ~Fl_Config_Section();
00078
00082 virtual void clear();
00083
00087 Fl_Config_Section *parent() const { return m_parent; }
00088
00093 const Fl_String &name() const { return m_name; }
00094
00099 const Fl_String &path() const { return m_path; }
00100
00104 const Fl_Config_Lines &lines() const { return m_lines; }
00105
00109 Fl_Config_Lines &lines() { return m_lines; }
00110
00114 const Fl_Config_Sections §ions() const { return m_sections; }
00115
00119 Fl_Config_Sections §ions() { return m_sections; }
00120
00126 Fl_Config_Section *find(const char *section_name, bool recursive=false) const;
00127
00128 protected:
00129 Fl_Config_Section *m_parent;
00130 Fl_String m_name, m_path;
00131
00132 Fl_Config_Lines m_lines;
00133 Fl_Config_Sections m_sections;
00134
00135 void write_section(int indent, FILE *fp) const;
00136
00137 void add_entry(const Fl_String &key, const Fl_String &value);
00138 bool remove_entry(const Fl_String &key);
00139 Fl_String *find_entry(const char *key) const;
00140 };
00141
00149 class FL_API Fl_Config : public Fl_Config_Section {
00150 public:
00151
00155 enum ConfigType {
00156 USER=1,
00157 SYSTEM
00158 };
00159
00177 Fl_Config(const char *vendor, const char *application, int mode=USER);
00178
00186 Fl_Config(const char *filename, bool readfile=true, bool createfile=true);
00187
00191 virtual ~Fl_Config();
00192
00209 static char *find_config_file(const char *filename, bool create=true, int mode=USER);
00210
00211
00217 bool read_file(bool create = true);
00218
00224 bool flush();
00225
00227 const Fl_String &filename() const { return m_filename; }
00229 void filename(const char *filename) { m_filename = filename; }
00231 void filename(const Fl_String &filename) { m_filename = filename; }
00232
00234 const Fl_String &vendor() const { return m_vendor; }
00236 void vendor(const char *vendor) { m_vendor = vendor; }
00238 void vendor(const Fl_String &vendor) { m_vendor = vendor; }
00239
00241 const Fl_String &application() const { return m_app; }
00243 void application(const char *app) { m_app = app; }
00245 void application(const Fl_String &app) { m_app = app; }
00246
00252 bool is_changed() const { return m_changed; }
00253
00258 void set_changed() { m_changed = true; }
00259
00263 int error() const { return m_error; }
00264
00268 void reset_error() { m_error = 0; }
00269
00273 const char *strerror() const { return Fl_Config::strerror(m_error); }
00274
00278 static const char *strerror(int errnum);
00279
00287 Fl_Config_Section *create_section(const char *path) { Fl_String tmp(path); return create_section(tmp); }
00288
00296 Fl_Config_Section *create_section(const Fl_String &path);
00297
00306 Fl_Config_Section *find_section(const char *path, bool perfect_match=true) const;
00307
00311 Fl_Config_Sections *section_list(const char *secpath) const { Fl_Config_Section *s=find_section(secpath); return s ? (&s->sections()) : 0; }
00312
00316 Fl_Config_Lines *line_list(const char *secpath) const { Fl_Config_Section *s=find_section(secpath); return s ? (&s->lines()) : 0; }
00317
00323 void set_section(const char *secpath) { set_section(create_section(secpath)); }
00324
00328 void set_section(Fl_Config_Section *sec) { m_cur_sec = sec; }
00329
00334 void remove_key(const char *section, const char *key);
00335
00340 void remove_sec(const char *section);
00341
00342
00352 int read(const char *key, Fl_String &ret, const char *def_value) { return _read_string(m_cur_sec, key, ret, def_value); }
00353
00364 int read(const char *key, char *ret, const char *def_value, int size) { return _read_string(m_cur_sec, key, ret, def_value, size); }
00365
00376 int read(const char *key, char *&ret, const char *def_value=0) { return _read_string(m_cur_sec, key, ret, def_value); }
00377
00387 int read(const char *key, long &ret, long def_value=0) { return _read_long(m_cur_sec, key, ret, def_value); }
00388
00398 int read(const char *key, int &ret, int def_value=0) { return _read_int(m_cur_sec, key, ret, def_value); }
00399
00409 int read(const char *key, float &ret, float def_value=0) { return _read_float(m_cur_sec, key, ret, def_value); }
00410
00420 int read(const char *key, double &ret, double def_value=0) { return _read_double(m_cur_sec, key, ret, def_value); }
00421
00431 int read(const char *key, bool &ret, bool def_value=0) { return _read_bool(m_cur_sec, key, ret, def_value); }
00432
00442 int read(const char *key, Fl_Color &ret, Fl_Color def_value=0) { return _read_color(m_cur_sec, key, ret, def_value); }
00443
00444
00453 int write(const char *key, const Fl_String &value) { return _write_string(m_cur_sec, key, value); }
00454
00463 int write(const char *key, const char *value) { return _write_string(m_cur_sec, key, value); }
00464
00473 int write(const char *key, const long value) { return _write_long(m_cur_sec, key, value); }
00474
00483 int write(const char *key, const int value) { return _write_int(m_cur_sec, key, value); }
00484
00493 int write(const char *key, const float value) { return _write_float(m_cur_sec, key, value); }
00494
00503 int write(const char *key, const double value) { return _write_double(m_cur_sec, key, value); }
00504
00513 int write(const char *key, const bool value) { return _write_bool(m_cur_sec, key, value); }
00514
00523 int write(const char *key, const Fl_Color value) { return _write_color(m_cur_sec, key, value); }
00524
00525
00536 int get(const char *section, const char *key, Fl_String &ret, const char *def_value) { return _read_string(find_section(section), key, ret, def_value); }
00537
00548 int get(const char *section, const char *key, char *ret, const char *def_value, int size) { return _read_string(find_section(section), key, ret, def_value, size); }
00549
00561 int get(const char *section, const char *key, char *&ret, const char *def_value=0) { return _read_string(find_section(section), key, ret, def_value); }
00562
00573 int get(const char *section, const char *key, long &ret, long def_value=0) { return _read_long(find_section(section), key, ret, def_value); }
00574
00585 int get(const char *section, const char *key, int &ret, int def_value=0) { return _read_int(find_section(section), key, ret, def_value); }
00586
00597 int get(const char *section, const char *key, float &ret, float def_value=0) { return _read_float(find_section(section), key, ret, def_value); }
00598
00609 int get(const char *section, const char *key, double &ret, double def_value=0) { return _read_double(find_section(section), key, ret, def_value); }
00610
00621 int get(const char *section, const char *key, bool &ret, bool def_value=0) { return _read_bool(find_section(section), key, ret, def_value); }
00622
00633 int get(const char *section, const char *key, Fl_Color &ret, Fl_Color def_value=0) { return _read_color(find_section(section), key, ret, def_value); }
00634
00635
00645 int set(const char *section, const char *key, const Fl_String &value) { return _write_string(create_section(section), key, value); }
00646
00656 int set(const char *section, const char *key, const char *value) { return _write_string(create_section(section), key, value); }
00657
00667 int set(const char *section, const char *key, const long value) { return _write_long(create_section(section), key, value); }
00668
00678 int set(const char *section, const char *key, const int value) { return _write_int(create_section(section), key, value); }
00679
00689 int set(const char *section, const char *key, const float value) { return _write_float(create_section(section), key, value); }
00690
00700 int set(const char *section, const char *key, const bool value) { return _write_double(create_section(section), key, value); }
00701
00711 int set(const char *section, const char *key, const Fl_Color value) { return _write_color(create_section(section), key, value); }
00712
00713 private:
00714 int m_error;
00715 Fl_String m_filename;
00716 Fl_String m_vendor, m_app;
00717
00718 Fl_Config_Section *m_cur_sec;
00719 bool m_changed;
00720
00721 int _read_string(Fl_Config_Section *s, const char *key, Fl_String &ret, const char *def_value);
00722 int _read_string(Fl_Config_Section *s, const char *key, char *ret, const char *def_value, int size);
00723 int _read_string(Fl_Config_Section *s, const char *key, char *&ret, const char *def_value);
00724 int _read_long (Fl_Config_Section *s, const char *key, long &ret, long def_value);
00725 int _read_int (Fl_Config_Section *s, const char *key, int &ret, int def_value);
00726 int _read_float (Fl_Config_Section *s, const char *key, float &ret, float def_value);
00727 int _read_double(Fl_Config_Section *s, const char *key, double &ret, double def_value);
00728 int _read_bool (Fl_Config_Section *s, const char *key, bool &ret, bool def_value);
00729 int _read_color (Fl_Config_Section *s, const char *key, Fl_Color &ret, Fl_Color def_value);
00730
00731 int _write_string(Fl_Config_Section *s, const char *key, const Fl_String &value);
00732 int _write_string(Fl_Config_Section *s, const char *key, const char *value);
00733 int _write_long (Fl_Config_Section *s, const char *key, const long value);
00734 int _write_int (Fl_Config_Section *s, const char *key, const int value);
00735 int _write_float (Fl_Config_Section *s, const char *key, const float value);
00736 int _write_double(Fl_Config_Section *s, const char *key, const double value);
00737 int _write_bool (Fl_Config_Section *s, const char *key, const bool value);
00738 int _write_color (Fl_Config_Section *s, const char *key, const Fl_Color value);
00739 };
00740
00741
00742 static inline const char* fl_find_config_file(const char *filename, bool create=true) {
00743 return Fl_Config::find_config_file(filename, create, Fl_Config::USER);
00744 }
00745
00746 #endif