00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046 #ifdef HAVE_YAML
00047
00048 #include <sstream>
00049
00050 extern "C" {
00051 #include <stdio.h>
00052 #include <string.h>
00053 #include <sys/errno.h>
00054 };
00055
00056 #include "cfg_yaml.h"
00057 #include "consmgr.h"
00058
00059
00060
00061
00062
00063 #if YAML_USING_SYCK
00064
00071 YAML *YAML::instance = NULL;
00072 #endif
00073
00074 YAML::YAML(const string &filename) : Config(filename)
00075 #if YAML_USING_SYCK
00076 , parser(NULL), filep(NULL), index(162)
00077 #endif
00078 {
00079 #if YAML_USING_SYCK
00080
00081 if (instance) {
00082 std::ostringstream error;
00083
00084 error << "Only one instance of class YAML may exist, and one "
00085 << "already exists at " << instance;
00086 throw ConfigException(error.str());
00087 }
00088 #endif
00089
00090 int pret;
00091
00092
00093 pret = pthread_mutex_init(&mutex, NULL);
00094 PTHREAD_CHECK_AND_THROW(pret, "mutex_init(mutex)");
00095
00096
00097 pret = pthread_mutex_lock(&mutex);
00098 PTHREAD_CHECK_AND_THROW(pret, "mutex_lock(mutex)");
00099
00100 #if YAML_USING_SYCK
00101
00102 parser = syck_new_parser();
00103 if (parser == NULL)
00104 throw ConfigException("Unable to generate a Parser from the Syck library.");
00105
00106
00107 YAML::instance = this;
00108
00109
00110
00111 syck_parser_handler(parser, YAML::node_handler);
00112 syck_parser_error_handler(parser, YAML::error_handler);
00113 syck_parser_bad_anchor_handler(parser, bad_anchor_handler);
00114
00115
00116
00117 filep = fopen(filename.c_str(), "r");
00118
00119
00120 if (filep == NULL)
00121 throw ConfigException("Unable to open input file '"+filename+"': "+string(strerror(errno)));
00122 syck_parser_file(parser, filep, NULL);
00123 #endif
00124
00125
00126 pret = pthread_mutex_unlock(&mutex);
00127 PTHREAD_CHECK_AND_THROW(pret, "mutex_unlock(mutex)");
00128 }
00129
00130 YAML::~YAML()
00131 {
00132 int pret;
00133
00134
00135 pret = pthread_mutex_lock(&mutex);
00136 PTHREAD_CHECK_AND_THROW(pret, "mutex_lock(mutex)");
00137
00138 #if YAML_USING_SYCK
00139
00140 syck_free_parser(parser);
00141 parser = NULL;
00142 #endif
00143
00144
00145 pret = pthread_mutex_unlock(&mutex);
00146 PTHREAD_CHECK_AND_THROW(pret, "mutex_unlock(mutex)");
00147 pret = pthread_mutex_destroy(&mutex);
00148 PTHREAD_CHECK_AND_THROW(pret, "mutex_destroy(mutex)");
00149 }
00150
00151
00152
00153
00154
00155 void
00156 YAML::load_entry(const string &name)
00157 {
00158 int pret;
00159
00160
00161 pret = pthread_mutex_lock(&mutex);
00162 PTHREAD_CHECK_AND_THROW(pret, "mutex_lock(mutex)");
00163
00164
00165
00166 intended = name;
00167
00168 #if 0
00169 clog << "keys are:" << endl;
00170 for (std::vector<std::string>::const_iterator ki = config_keys.begin() ;
00171 ki != config_keys.end() ; ki++) {
00172 clog << "\t" << *ki << endl;
00173 }
00174 #endif
00175
00176
00177
00178
00179
00180
00181 if (syck_parse(parser) == 0)
00182 throw ConfigUnfoundException(name);
00183
00184
00185 pret = pthread_mutex_unlock(&mutex);
00186 PTHREAD_CHECK_AND_THROW(pret, "mutex_unlock(mutex)");
00187
00188 return;
00189 }
00190
00191 #if YAML_USING_SYCK
00192
00193
00194
00195
00196
00197 SYMID
00198 YAML::node_handler(SyckParser *p, SyckNode *n)
00199 {
00200 return YAML::instance->import_node(n);
00201 }
00202
00203 void
00204 YAML::error_handler(SyckParser *p, char *str)
00205 {
00206 throw ConfigParseException(p->linect + 1, string(str));
00207 }
00208
00209 SyckNode *
00210 YAML::bad_anchor_handler(SyckParser *p, char *anchor)
00211 {
00212 clog << "In bad_anchor_handler, anchor: " << anchor << endl;
00213 return(NULL);
00214 }
00215
00216
00217
00218
00219
00220 SYMID
00221 YAML::import_node(SyckNode *n)
00222 {
00223 #ifdef CONFIGURATION_DEBUGGING
00224 clog << "Importing node, kind: ";
00225 #endif
00226 switch (n->kind) {
00227 case syck_map_kind:
00228 #ifdef CONFIGURATION_DEBUGGING
00229 clog << "map (style: " << ((n->data.pairs->style == map_inline) ? "inline" : "none")
00230 << "), " << n->data.pairs->idx << " elements (capacity " << n->data.pairs->capa << ")";
00231 for (short i = 0 ; i < syck_map_count(n) ; i++) {
00232 string key = scalars[n->data.pairs->keys[i]];
00233 string val;
00234 if (scalars.find(n->data.pairs->values[i]) == scalars.end()) {
00235 std::ostringstream foo;
00236 foo << "(SYMID " << n->data.pairs->values[i]
00237 << "; not a scalar)";
00238 val = foo.str();
00239 } else
00240 val = scalars[n->data.pairs->values[i]];
00241 clog << endl << " " << key << ": " << val;
00242 }
00243 #endif
00244
00245
00246
00247
00248
00249
00250 for (short i = 0 ; i < syck_map_count(n) ; i++) {
00251 string key = scalars[syck_map_read(n, map_key, i)];
00252
00253
00254
00255 if (maps.find(syck_map_read(n, map_value, i)) != maps.end()) {
00256 #ifdef CONFIGURATION_DEBUGGING
00257 clog << endl << "Got an element name: '" << key << "', ";
00258 #endif
00259 if (key == intended) {
00260 load_from_map(syck_map_read(n, map_value, i));
00261 #ifdef CONFIGURATION_DEBUGGING
00262 clog << "loaded the values from the map!" << endl;
00263 } else {
00264 clog << "don't want it.";
00265 #endif
00266 }
00267 }
00268 }
00269
00270
00271 {
00272 SyckNode *mapnode = syck_new_map(0,0);
00273 syck_map_empty(mapnode);
00274 syck_map_update(mapnode, n);
00275
00276 n->id = index++;
00277 maps.insert(std::pair<SYMID,SyckNode*>(n->id,mapnode));
00278 }
00279 break;
00280 case syck_seq_kind:
00281 #ifdef CONFIGURATION_DEBUGGING
00282 clog << "seq (style: " << ((n->data.list->style == seq_inline) ? "inline" : "none")
00283 << "), " << syck_seq_count(n) << " elements (capacity " << n->data.list->capa << ")";
00284 #endif
00285 break;
00286 case syck_str_kind:
00287 #ifdef CONFIGURATION_DEBUGGING
00288 clog << "str (" << n->type_id << ", '" << n->data.str->ptr << "' (" << n->data.str->len << " bytes))";
00289 #endif
00290 n->id = index++;
00291 scalars.insert(std::pair<SYMID,string>(n->id, string(n->data.str->ptr)));
00292 break;
00293 }
00294 #ifdef CONFIGURATION_DEBUGGING
00295 clog << endl;
00296 #endif
00297 return(n->id);
00298 }
00299
00300
00301 void
00302 YAML::load_from_map(SYMID mapid)
00303 {
00304 #ifdef CONFIGURATION_DEBUGGING
00305 clog << "Loading from map at SYMID " << mapid << endl;
00306 #endif
00307
00308 SyckNode *n = maps[mapid];
00309
00310
00311
00312
00313
00314 #ifdef CONFIGURATION_DEBUGGING
00315 clog << "See a map with " << syck_map_count(n) << " elements:" << endl;
00316 #endif
00317
00318 for (short i = 0 ; i < syck_map_count(n) ; i++) {
00319 string key = scalars[syck_map_read(n, map_key, i)];
00320 string value = scalars[syck_map_read(n, map_value, i)];
00321 #ifdef CONFIGURATION_DEBUGGING
00322 clog << "\t" << key << "\t:\t" << value << endl;
00323 #endif
00324 this->insert(std::pair<string,string>(key,value));
00325 }
00326 return;
00327 }
00328 #endif
00329
00330 #endif