~ubuntu-branches/debian/squeeze/stellarium/squeeze

« back to all changes in this revision

Viewing changes to src/stelutils/InitParser.hpp

  • Committer: Bazaar Package Importer
  • Author(s): Cédric Delfosse
  • Date: 2008-05-19 21:28:23 UTC
  • mfrom: (3.1.5 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080519212823-m5nfiuntxstxzxj7
Tags: 0.9.1-4
Add libxcursor-dev, libxfixes-dev, libxinerama-dev, libqt4-opengl-dev to
build-deps (Closes: #479906)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2003 Fabien Chereau
 
3
 *
 
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.
 
8
 *
 
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.
 
13
 *
 
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.
 
17
 */
 
18
 
 
19
// Class which parses an init file
 
20
// C++ warper for the iniparser free library from N.Devillard
 
21
 
 
22
#ifndef _INIT_PARSER_H_
 
23
#define _INIT_PARSER_H_
 
24
 
 
25
#include <string>
 
26
#include <iostream>
 
27
#include <QString>
 
28
 
 
29
using namespace std;
 
30
 
 
31
// Predeclaration
 
32
typedef struct _dictionary_ dictionary;
 
33
 
 
34
//! @class InitParser 
 
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
 
40
class InitParser
 
41
{
 
42
public:
 
43
        //! Create the parser object. Note you will need to call
 
44
        //! load() before using the get() functions.
 
45
        InitParser();
 
46
        virtual ~InitParser();
 
47
 
 
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);
 
51
 
 
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;
 
55
 
 
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;
 
62
        
 
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;
 
69
        
 
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;
 
78
 
 
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;
 
85
        
 
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;
 
92
        
 
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;
 
101
 
 
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;
 
108
        
 
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;
 
115
        
 
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;
 
124
 
 
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;
 
131
        
 
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;
 
138
        
 
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;
 
147
        
 
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 
 
153
        //! is returned.
 
154
        int set_str(const string& key, const QString& val);
 
155
        
 
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 
 
161
        //! is returned.
 
162
        int set_int(const string& key, int val);
 
163
        
 
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 
 
169
        //! is returned.
 
170
        int set_double(const string& key, double val);
 
171
        
 
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 
 
177
        //! is returned.
 
178
        int set_boolean(const string& key, bool val);
 
179
        
 
180
        //! Get the number of sections in the InitParser object.
 
181
        //! @return the nuumber of sections found.
 
182
        int get_nsec(void) const;
 
183
        
 
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;
 
188
        
 
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;
 
192
 
 
193
private:
 
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);
 
197
 
 
198
        void free_dico(void);   // Unalloc memory
 
199
        dictionary * dico;              // The dictionnary containing the parsed data
 
200
};
 
201
 
 
202
#endif // _INIT_PARSER_H_