forked from cnangel/python-libconfig
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpylibconfig.cc
275 lines (228 loc) · 7.96 KB
/
pylibconfig.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#include <boost/python.hpp>
#include <libconfig.h++>
#include <string>
using namespace boost::python;
using namespace libconfig;
class pyConfig
{
public:
pyConfig ()
{
config = new Config ();
}
~pyConfig ()
{
delete config;
}
void read ( FILE * stream )
{ config->read ( stream ); }
void write ( FILE * stream )
{ config->write ( stream ); }
void readFile ( const char * filename )
{ config->readFile ( filename ); }
void writeFile ( const char * filename )
{ config->writeFile ( filename ); }
bool getAutoConvert ()
{ return config->getAutoConvert (); }
void setAutoConvert ( bool flag )
{ config->setAutoConvert ( flag ); }
bool exists ( const char * path )
{ return config->exists ( path ); }
tuple value ( const char * path )
{
std::string v_string;
if ( config->lookupValue ( path, v_string ) )
return make_tuple ( v_string.c_str(), true );
unsigned int v_uint;
if ( config->lookupValue ( path, v_uint ) )
return make_tuple ( v_uint, true );
int v_int;
if ( config->lookupValue ( path, v_int ) )
return make_tuple ( v_int, true );
bool v_bool;
if ( config->lookupValue ( path, v_bool ) )
return make_tuple ( v_bool, true );
unsigned long long v_ulonglong;
if ( config->lookupValue ( path, v_ulonglong ) )
return make_tuple ( v_ulonglong, true );
long long v_longlong;
if ( config->lookupValue ( path, v_longlong ) )
return make_tuple ( v_longlong, true );
float v_float;
if ( config->lookupValue ( path, v_float ) )
return make_tuple ( v_float, true );
double v_double;
if ( config->lookupValue ( path, v_double ) )
return make_tuple ( v_double, true );
return make_tuple ( "", false );
}
list children_root ()
{
list result;
int length = config->getRoot ().getLength ();
for ( int index = 0; index < length; index++ )
{
result.append ( config->getRoot () [ index ].getPath ().c_str () );
}
return result;
}
list children ( const char * path )
{
list result;
try {
list result;
int length = config->lookup ( path ).getLength ();
for ( int index = 0; index < length; index++ )
{
result.append ( config->lookup ( path ) [ index ].getPath ().c_str () );
}
return result;
}
catch ( SettingNotFoundException & e )
{
return result;
}
}
void remove ( const char * path, const char * name )
{
config->lookup ( path ).remove ( name );
}
void addBoolean ( const char * path, const char * name )
{
config->lookup ( path ).add ( name, libconfig::Setting::TypeBoolean );
}
void addBigInteger ( const char * path, const char * name )
{
config->lookup ( path ).add ( name, libconfig::Setting::TypeInt64 );
}
void addInteger ( const char * path, const char * name )
{
config->lookup ( path ).add ( name, libconfig::Setting::TypeInt );
}
void addFloat ( const char * path, const char * name )
{
config->lookup ( path ).add ( name, libconfig::Setting::TypeFloat );
}
void addString ( const char * path, const char * name )
{
config->lookup ( path ).add ( name, libconfig::Setting::TypeString );
}
void addGroup ( const char * path, const char * name )
{
config->lookup ( path ).add ( name, libconfig::Setting::TypeGroup );
}
void addList ( const char * path, const char * name )
{
config->lookup ( path ).add ( name, libconfig::Setting::TypeList );
}
void addArray_str ( const char * path, const char * name )
{
config->lookup ( path ).add ( name, libconfig::Setting::TypeArray );
}
void addArray ( const char * path )
{
config->lookup ( path ).add ( libconfig::Setting::TypeArray );
}
void appendToList_int ( const char * path, int value )
{
config->lookup ( path ).add ( libconfig::Setting::TypeInt ) = value;
}
void appendToList_bool ( const char * path, bool value)
{
config->lookup ( path ).add ( libconfig::Setting::TypeBoolean ) = value;
}
void appendToList_float ( const char * path, float value )
{
config->lookup ( path ).add ( libconfig::Setting::TypeFloat ) = value;
}
void appendToList_double ( const char * path, double value )
{
config->lookup ( path ).add ( libconfig::Setting::TypeFloat ) = value;
}
void appendToList_longlong ( const char * path, long long value )
{
config->lookup ( path ).add ( libconfig::Setting::TypeInt64 ) = value;
}
void appendToList_long( const char * path, long value )
{
config->lookup ( path ).add ( libconfig::Setting::TypeInt ) = value;
}
void appendToList_str ( const char * path, const char * value )
{
config->lookup ( path ).add ( libconfig::Setting::TypeString ) = value;
}
void appendToList_Group ( const char * path )
{
config->lookup ( path ).add ( libconfig::Setting::TypeGroup );
}
void setValue_bool ( const char * path, bool value )
{
config->lookup ( path ) = value;
}
void setValue_int ( const char * path, int value )
{
config->lookup ( path ) = value;
}
void setValue_long ( const char * path, long value )
{
config->lookup ( path ) = value;
}
void setValue_longlong ( const char * path, long long value )
{
config->lookup ( path ) = value;
}
void setValue_float ( const char * path, float value )
{
config->lookup ( path ) = value;
}
void setValue_double ( const char * path, double value )
{
config->lookup ( path ) = value;
}
void setValue_str ( const char * path, std::string value )
{
config->lookup ( path ) = value;
}
private:
Config * config;
};
BOOST_PYTHON_MODULE ( pylibconfig )
{
class_<pyConfig>("Config")
.def("read", &pyConfig::read )
.def("write", &pyConfig::write )
.def("readFile", &pyConfig::readFile )
.def("writeFile", &pyConfig::writeFile )
.def("getAutoConvert", &pyConfig::getAutoConvert )
.def("setAutoConvert", &pyConfig::setAutoConvert )
.def("exists", &pyConfig::exists )
.def("children", &pyConfig::children )
.def("children", &pyConfig::children_root )
.def("value", &pyConfig::value )
.def("remove", &pyConfig::remove )
.def("addString", &pyConfig::addString )
.def("addBoolean", &pyConfig::addBoolean )
.def("addBigInteger", &pyConfig::addBigInteger )
.def("addInteger", &pyConfig::addInteger )
.def("addFloat", &pyConfig::addFloat )
.def("addGroup", &pyConfig::addGroup )
.def("addList", &pyConfig::addList )
.def("addArray", &pyConfig::addArray )
.def("addArray", &pyConfig::addArray_str )
.def("setValue", &pyConfig::setValue_bool )
.def("setValue", &pyConfig::setValue_float )
.def("setValue", &pyConfig::setValue_double )
.def("setValue", &pyConfig::setValue_longlong )
.def("setValue", &pyConfig::setValue_long )
.def("setValue", &pyConfig::setValue_int )
.def("setValue", &pyConfig::setValue_str )
.def("appendToList", &pyConfig::appendToList_Group )
.def("appendToList", &pyConfig::appendToList_bool )
.def("appendToList", &pyConfig::appendToList_float )
.def("appendToList", &pyConfig::appendToList_double)
.def("appendToList", &pyConfig::appendToList_longlong )
.def("appendToList", &pyConfig::appendToList_long )
.def("appendToList", &pyConfig::appendToList_int )
.def("appendToList", &pyConfig::appendToList_str)
;
}