1 /* defines the currently used bootstrap config file */
2 #include "osrfConfig.h"
4 static osrfConfig* osrfConfigDefault = NULL;
7 void osrfConfigSetDefaultConfig(osrfConfig* cfg) {
9 if( osrfConfigDefault )
10 osrfConfigFree( osrfConfigDefault );
11 osrfConfigDefault = cfg;
15 void osrfConfigFree(osrfConfig* cfg) {
17 jsonObjectFree(cfg->config);
18 free(cfg->configContext);
24 int osrfConfigHasDefaultConfig() {
25 return ( osrfConfigDefault != NULL );
29 void osrfConfigCleanup() {
30 osrfConfigFree(osrfConfigDefault);
31 osrfConfigDefault = NULL;
35 void osrfConfigReplaceConfig(osrfConfig* cfg, const jsonObject* obj) {
36 if(!cfg || !obj) return;
37 jsonObjectFree(cfg->config);
38 cfg->config = jsonObjectClone(obj);
41 osrfConfig* osrfConfigInit(char* configFile, char* configContext) {
42 if(!configFile) return NULL;
44 osrfConfig* cfg = safe_malloc(sizeof(osrfConfig));
45 if(configContext) cfg->configContext = strdup(configContext);
46 else cfg->configContext = NULL;
48 xmlDocPtr doc = xmlParseFile(configFile);
50 osrfLogWarning( OSRF_LOG_MARK, "Unable to parse XML config file %s", configFile);
54 cfg->config = xmlDocToJSON(doc);
58 osrfLogWarning( OSRF_LOG_MARK, "xmlDocToJSON failed for config %s", configFile);
65 char* osrfConfigGetValue(osrfConfig* cfg, char* path, ...) {
67 if(!path) return NULL;
68 if(!cfg) cfg = osrfConfigDefault;
69 if(!cfg) { osrfLogWarning( OSRF_LOG_MARK, "No Config object in osrfConfigGetValue()"); return NULL; }
71 VA_LIST_TO_STRING(path);
76 if(cfg->configContext) {
77 obj = jsonObjectFindPath( cfg->config, "//%s%s", cfg->configContext, VA_BUF);
78 if(obj) val = jsonObjectToSimpleString(jsonObjectGetIndex(obj, 0));
81 obj = jsonObjectFindPath( cfg->config, VA_BUF);
82 if(obj) val = jsonObjectToSimpleString(obj);
90 int osrfConfigGetValueList(osrfConfig* cfg, osrfStringArray* arr, char* path, ...) {
92 if(!arr || !path) return 0;
93 if(!cfg) cfg = osrfConfigDefault;
94 if(!cfg) { osrfLogWarning( OSRF_LOG_MARK, "No Config object!"); return -1;}
96 VA_LIST_TO_STRING(path);
99 if(cfg->configContext) {
100 obj = jsonObjectFindPath( cfg->config, "//%s%s", cfg->configContext, VA_BUF);
102 obj = jsonObjectFindPath( cfg->config, VA_BUF);
107 if(obj && obj->type == JSON_ARRAY ) {
110 for( i = 0; i < obj->size; i++ ) {
112 char* val = jsonObjectToSimpleString(jsonObjectGetIndex(obj, i));
115 osrfStringArrayAdd(arr, val);