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(char* configFile, 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;
74 char* osrfConfigGetValue(osrfConfig* cfg, char* path, ...) {
76 if(!path) return NULL;
77 if(!cfg) cfg = osrfConfigDefault;
78 if(!cfg) { osrfLogWarning( OSRF_LOG_MARK, "No Config object in osrfConfigGetValue()"); return NULL; }
80 VA_LIST_TO_STRING(path);
85 if(cfg->configContext) {
86 obj = jsonObjectFindPath( cfg->config, "//%s%s", cfg->configContext, VA_BUF);
87 if(obj) val = jsonObjectToSimpleString(jsonObjectGetIndex(obj, 0));
90 obj = jsonObjectFindPath( cfg->config, VA_BUF);
91 if(obj) val = jsonObjectToSimpleString(obj);
99 int osrfConfigGetValueList(osrfConfig* cfg, osrfStringArray* arr, char* path, ...) {
101 if(!arr || !path) return 0;
102 if(!cfg) cfg = osrfConfigDefault;
103 if(!cfg) { osrfLogWarning( OSRF_LOG_MARK, "No Config object!"); return -1;}
105 VA_LIST_TO_STRING(path);
108 if(cfg->configContext) {
109 obj = jsonObjectFindPath( cfg->config, "//%s%s", cfg->configContext, VA_BUF);
111 obj = jsonObjectFindPath( cfg->config, VA_BUF);
116 if(obj && obj->type == JSON_ARRAY ) {
119 for( i = 0; i < obj->size; i++ ) {
121 char* val = jsonObjectToSimpleString(jsonObjectGetIndex(obj, i));
124 osrfStringArrayAdd(arr, val);