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