1 /* defines the currently used bootstrap config file */
2 #include "osrfConfig.h"
4 osrfConfig* __osrfConfigDefault = NULL;
7 void osrfConfigSetDefaultConfig(osrfConfig* cfg) {
8 if(cfg) __osrfConfigDefault = cfg;
11 void osrfConfigFree(osrfConfig* cfg) {
13 jsonObjectFree(cfg->config);
14 free(cfg->configContext);
20 int osrfConfigHasDefaultConfig() {
21 return ( __osrfConfigDefault != NULL );
25 void osrfConfigCleanup() {
26 osrfConfigFree(__osrfConfigDefault);
27 __osrfConfigDefault = NULL;
31 void osrfConfigReplaceConfig(osrfConfig* cfg, const jsonObject* obj) {
32 if(!cfg || !obj) return;
33 jsonObjectFree(cfg->config);
34 cfg->config = jsonObjectClone(obj);
37 osrfConfig* osrfConfigInit(char* configFile, char* configContext) {
38 if(!configFile) return NULL;
40 osrfConfigFree(__osrfConfigDefault);
42 osrfConfig* cfg = safe_malloc(sizeof(osrfConfig));
43 if(configContext) cfg->configContext = strdup(configContext);
44 else cfg->configContext = NULL;
46 xmlDocPtr doc = xmlParseFile(configFile);
48 warning_handler( "Unable to parse XML config file %s", configFile);
52 cfg->config = xmlDocToJSON(doc);
56 warning_handler("xmlDocToJSON failed for config %s", configFile);
63 char* osrfConfigGetValue(osrfConfig* cfg, char* path, ...) {
65 if(!path) return NULL;
66 if(!cfg) cfg = __osrfConfigDefault;
67 if(!cfg) { warning_handler("No Confif object!"); return NULL; }
69 VA_LIST_TO_STRING(path);
74 if(cfg->configContext) {
75 obj = jsonObjectFindPath( cfg->config, "//%s%s", cfg->configContext, VA_BUF);
76 if(obj) val = jsonObjectToSimpleString(jsonObjectGetIndex(obj, 0));
79 obj = jsonObjectFindPath( cfg->config, VA_BUF);
80 if(obj) val = jsonObjectToSimpleString(obj);
88 int osrfConfigGetValueList(osrfConfig* cfg, osrfStringArray* arr, char* path, ...) {
90 if(!arr || !path) return 0;
91 if(!cfg) cfg = __osrfConfigDefault;
92 if(!cfg) { return warning_handler("No Config object!"); }
94 VA_LIST_TO_STRING(path);
97 if(cfg->configContext) {
98 obj = jsonObjectFindPath( cfg->config, "//%s%s", cfg->configContext, VA_BUF);
100 obj = jsonObjectFindPath( cfg->config, VA_BUF);
105 if(obj && obj->type == JSON_ARRAY ) {
108 for( i = 0; i < obj->size; i++ ) {
110 char* val = jsonObjectToSimpleString(jsonObjectGetIndex(obj, i));
113 osrfStringArrayAdd(arr, val);