2
* Copyright (C) 2003 Fabien Chereau
4
* This program is free software; you can redistribute it and/or
5
* modify it under the terms of the GNU General Public License
6
* as published by the Free Software Foundation; either version 2
7
* of the License, or (at your option) any later version.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
// Class which parses an init file
20
// C++ warper for the iniparser free library from N.Devillard
22
#ifndef _INIT_PARSER_H_
23
#define _INIT_PARSER_H_
32
typedef struct _dictionary_ dictionary;
35
//! Parses .ini configuration files.
36
//! This class is used to parse and modify ini style configuration files.
37
//! Settings in an ini file are separated into sections, and in each section are
38
//! specified by a key.
39
//! It is a C++ wrapper on the C library iniparser, by Nicolas Devillard
43
//! Create the parser object. Note you will need to call
44
//! load() before using the get() functions.
46
virtual ~InitParser();
48
//! Load the config file (the parsing operation)
49
//! @param file_name the path to the .ini file to be loaded.
50
void load(const QString& file_name);
52
//! Save the current config state.
53
//! @param file_name the path to the .ini file to be written
54
void save(const QString& file_name) const;
56
//! Get a string for a setting in the InitParser object.
57
//! If the requested setting cannot be found a warning message will be printed on standard error.
58
//! @param key is a compound key consisting of the section and
59
//! section-key separated by a colon (:), e.g. "sectionname:keyname".
60
//! @return the value for the specified key, or an empty string if the key is not found.
61
string get_str(const string& key) const;
63
//! Get a string for a setting in the InitParser object.
64
//! If the requested setting cannot be found a warning message will be printed on standard error.
65
//! @param section the section in which the required setting is to be found.
66
//! @param key is the key which identifes the required setting with the specified section.
67
//! @return the value for the specified key, or an empty string if the key is not found.
68
string get_str(const string& section, const string& key) const;
70
//! Get a string for a setting in the InitParser object, using a default value if the
71
//! requested setting is not found in the ini file.
72
//! If the requested setting cannot be found a warning message will be printed on standard error.
73
//! @param section the section in which the required setting is to be found.
74
//! @param key is the key which identifes the required setting with the specified section.
75
//! @param def the default value which is used if the requested setting is not found.
76
//! @return the value for the specified key, or the value of def if the key is not found.
77
string get_str(const string& section, const string& key, const string& def) const;
79
//! Get an integer value for a setting in the InitParser object.
80
//! If the requested setting cannot be found a warning message will be printed on standard error.
81
//! @param key is a compound key consisting of the section and
82
//! section-key separated by a colon (:), e.g. "sectionname:keyname".
83
//! @return the value for the specified key, or 0 if the key is not found.
84
int get_int(const string& key) const;
86
//! Get an integer value for a setting in the InitParser object.
87
//! If the requested setting cannot be found a warning message will be printed on standard error.
88
//! @param section the section in which the required setting is to be found.
89
//! @param key is the key which identifes the required setting with the specified section.
90
//! @return the value for the specified key, or 0 if the key is not found.
91
int get_int(const string& section, const string& key) const;
93
//! Get an integer value for a setting in the InitParser object, using a default value if the
94
//! requested setting is not found in the ini file.
95
//! If the requested setting cannot be found a warning message will be printed on standard error.
96
//! @param section the section in which the required setting is to be found.
97
//! @param key is the key which identifes the required setting with the specified section.
98
//! @param def the default value which is used if the requested setting is not found.
99
//! @return the value for the specified key, or the value of def if the key is not found.
100
int get_int(const string& section, const string& key, int def) const;
102
//! Get a double value for a setting in the InitParser object.
103
//! If the requested setting cannot be found a warning message will be printed on standard error.
104
//! @param key is a compound key consisting of the section and
105
//! section-key separated by a colon (:), e.g. "sectionname:keyname".
106
//! @return the value for the specified key, or 0.0 if the key is not found.
107
double get_double(const string& key) const;
109
//! Get a double value for a setting in the InitParser object.
110
//! If the requested setting cannot be found a warning message will be printed on standard error.
111
//! @param section the section in which the required setting is to be found.
112
//! @param key is the key which identifes the required setting with the specified section.
113
//! @return the value for the specified key, or 0.0 if the key is not found.
114
double get_double(const string& section, const string& key) const;
116
//! Get an double value for a setting in the InitParser object, using a default value if the
117
//! requested setting is not found in the ini file.
118
//! If the requested setting cannot be found a warning message will be printed on standard error.
119
//! @param section the section in which the required setting is to be found.
120
//! @param key is the key which identifes the required setting with the specified section.
121
//! @param def the default value which is used if the requested setting is not found.
122
//! @return the value for the specified key, or the value of def if the key is not found.
123
double get_double(const string& section, const string& key, double def) const;
125
//! Get a boolean value for a setting in the InitParser object.
126
//! If the requested setting cannot be found a warning message will be printed on standard error.
127
//! @param key is a compound key consisting of the section and
128
//! section-key separated by a colon (:), e.g. "sectionname:keyname".
129
//! @return the value for the specified key, or 0 (false) if the key is not found.
130
bool get_boolean(const string& key) const;
132
//! Get a boolean value for a setting in the InitParser object.
133
//! If the requested setting cannot be found a warning message will be printed on standard error.
134
//! @param section the section in which the required setting is to be found.
135
//! @param key is the key which identifes the required setting with the specified section.
136
//! @return the value for the specified key, or 0 (false) if the key is not found.
137
bool get_boolean(const string& section, const string& key) const;
139
//! Get an boolean value for a setting in the InitParser object, using a default value if the
140
//! requested setting is not found in the ini file.
141
//! If the requested setting cannot be found a warning message will be printed on standard error.
142
//! @param section the section in which the required setting is to be found.
143
//! @param key is the key which identifes the required setting with the specified section.
144
//! @param def the default value which is used if the requested setting is not found.
145
//! @return the value for the specified key, or the value of def if the key is not found.
146
bool get_boolean(const string& section, const string& key, bool def) const;
148
//! Set the value of a specified setting with the provided value.
149
//! @param key is a compound key consisting of the section and
150
//! section-key separated by a colon (:), e.g. "sectionname:keyname".
151
//! @param val the new value to use for the specified key.
152
//! @return if the entry cannot be found -1 is returned and the entry is created. Else 0
154
int set_str(const string& key, const QString& val);
156
//! Set the value of a specified setting with the provided value.
157
//! @param key is a compound key consisting of the section and
158
//! section-key separated by a colon (:), e.g. "sectionname:keyname".
159
//! @param val the new value to use for the specified key.
160
//! @return if the entry cannot be found -1 is returned and the entry is created. Else 0
162
int set_int(const string& key, int val);
164
//! Set the value of a specified setting with the provided value.
165
//! @param key is a compound key consisting of the section and
166
//! section-key separated by a colon (:), e.g. "sectionname:keyname".
167
//! @param val the new value to use for the specified key.
168
//! @return if the entry cannot be found -1 is returned and the entry is created. Else 0
170
int set_double(const string& key, double val);
172
//! Set the value of a specified setting with the provided value.
173
//! @param key is a compound key consisting of the section and
174
//! section-key separated by a colon (:), e.g. "sectionname:keyname".
175
//! @param val the new value to use for the specified key.
176
//! @return if the entry cannot be found -1 is returned and the entry is created. Else 0
178
int set_boolean(const string& key, bool val);
180
//! Get the number of sections in the InitParser object.
181
//! @return the nuumber of sections found.
182
int get_nsec(void) const;
184
//! Get the name of the section according to it's position in the InitParser object.
185
//! TODO: starting at 0 or 1?
186
//! @return the name of the requested section.
187
string get_secname(int n) const;
189
//! Determine if a setting exists in an InitParser object.
190
//! @return 1 if the entry exists, 0 otherwise
191
int find_entry(const string& entry) const;
194
// Check if the key is in the form section:key and if yes create the section in the dictionnary
195
// if it doesn't exist.
196
void make_section_from_key(const string& key);
198
void free_dico(void); // Unalloc memory
199
dictionary * dico; // The dictionnary containing the parsed data
202
#endif // _INIT_PARSER_H_