1 /* defines the currently used bootstrap config file */
2 #include <opensrf/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(const char* configFile, const char* configContext) {
42 if(!configFile) return NULL;
44 // Load XML from the configuration file
46 xmlDocPtr doc = xmlParseFile(configFile);
48 osrfLogWarning( OSRF_LOG_MARK, "Unable to parse XML config file %s", configFile);
52 // Translate it into a jsonObject
54 jsonObject* json_config = xmlDocToJSON(doc);
58 osrfLogWarning( OSRF_LOG_MARK, "xmlDocToJSON failed for config %s", configFile);
62 // Build an osrfConfig and return it by pointer
64 osrfConfig* cfg = safe_malloc(sizeof(osrfConfig));
66 if(configContext) cfg->configContext = strdup(configContext);
67 else cfg->configContext = NULL;
69 cfg->config = json_config;
75 char* osrfConfigGetValue(const osrfConfig* cfg, const char* path, ...) {
76 if(!path) return NULL;
77 if(!cfg) cfg = osrfConfigDefault;
79 osrfLogWarning( OSRF_LOG_MARK, "No Config object in osrfConfigGetValue()");
83 VA_LIST_TO_STRING(path);
86 if(cfg->configContext) {
87 jsonObject* outer_obj =
88 jsonObjectFindPath(cfg->config, "//%s%s", cfg->configContext, VA_BUF);
89 obj = jsonObjectExtractIndex( outer_obj, 0 );
90 jsonObjectFree( outer_obj );
92 obj = jsonObjectFindPath( cfg->config, VA_BUF);
94 char* val = jsonObjectToSimpleString(obj);
99 jsonObject* osrfConfigGetValueObject(osrfConfig* cfg, char* path, ...) {
100 if(!path) return NULL;
101 if(!cfg) cfg = osrfConfigDefault;
102 VA_LIST_TO_STRING(path);
103 if(cfg->configContext)
104 return jsonObjectFindPath(cfg->config, "//%s%s", cfg->configContext, VA_BUF);
106 return jsonObjectFindPath(cfg->config, VA_BUF);
109 int osrfConfigGetValueList(const osrfConfig* cfg, osrfStringArray* arr,
110 const char* path, ...) {
112 if(!arr || !path) return 0;
113 if(!cfg) cfg = osrfConfigDefault;
114 if(!cfg) { osrfLogWarning( OSRF_LOG_MARK, "No Config object!"); return -1;}
116 VA_LIST_TO_STRING(path);
119 if(cfg->configContext) {
120 obj = jsonObjectFindPath( cfg->config, "//%s%s", cfg->configContext, VA_BUF);
122 obj = jsonObjectFindPath( cfg->config, VA_BUF);
127 if(obj && obj->type == JSON_ARRAY ) {
130 for( i = 0; i < obj->size; i++ ) {
132 char* val = jsonObjectToSimpleString(jsonObjectGetIndex(obj, i));
135 osrfStringArrayAdd(arr, val);