]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/libopensrf/osrfConfig.c
Fix a bug whereby, if there was only one <service> entry for the
[OpenSRF.git] / src / libopensrf / osrfConfig.c
1 /* defines the currently used bootstrap config file */
2 #include <opensrf/osrfConfig.h>
3
4 static osrfConfig* osrfConfigDefault = NULL;
5
6
7 void osrfConfigSetDefaultConfig(osrfConfig* cfg) {
8         if(cfg) {
9                 if( osrfConfigDefault )
10                         osrfConfigFree( osrfConfigDefault );
11                 osrfConfigDefault = cfg;
12         }
13 }
14
15 void osrfConfigFree(osrfConfig* cfg) {
16         if(cfg) {
17                 jsonObjectFree(cfg->config);
18                 free(cfg->configContext);
19                 free(cfg);
20         }       
21 }
22
23
24 int osrfConfigHasDefaultConfig() {
25         return ( osrfConfigDefault != NULL );
26 }
27
28
29 void osrfConfigCleanup() { 
30         osrfConfigFree(osrfConfigDefault);
31         osrfConfigDefault = NULL;
32 }
33
34
35 void osrfConfigReplaceConfig(osrfConfig* cfg, const jsonObject* obj) {
36         if(!cfg || !obj) return;
37         jsonObjectFree(cfg->config);
38         cfg->config = jsonObjectClone(obj);     
39 }
40
41 osrfConfig* osrfConfigInit(const char* configFile, const char* configContext) {
42         if(!configFile) return NULL;
43
44         // Load XML from the configuration file
45         
46         xmlDocPtr doc = xmlParseFile(configFile);
47         if(!doc) {
48                 osrfLogWarning( OSRF_LOG_MARK, "Unable to parse XML config file %s", configFile);
49                 return NULL;
50         }
51
52         // Translate it into a jsonObject
53         
54         jsonObject* json_config = xmlDocToJSON(doc);
55         xmlFreeDoc(doc);
56
57         if(!json_config ) {
58                 osrfLogWarning( OSRF_LOG_MARK, "xmlDocToJSON failed for config %s", configFile);
59                 return NULL;
60         }       
61
62         // Build an osrfConfig and return it by pointer
63         
64         osrfConfig* cfg = safe_malloc(sizeof(osrfConfig));
65
66         if(configContext) cfg->configContext = strdup(configContext);
67         else cfg->configContext = NULL;
68
69         cfg->config = json_config;
70         
71         return cfg;
72 }
73
74
75 char* osrfConfigGetValue(const osrfConfig* cfg, const char* path, ...) {
76         if(!path) return NULL;
77         if(!cfg) cfg = osrfConfigDefault;
78         if(!cfg) { 
79                 osrfLogWarning( OSRF_LOG_MARK, "No Config object in osrfConfigGetValue()"); 
80                 return NULL; 
81         }
82
83         VA_LIST_TO_STRING(path);
84         jsonObject* obj;
85
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 );
91         } else
92                 obj = jsonObjectFindPath( cfg->config, VA_BUF);
93
94         char* val = jsonObjectToSimpleString(obj);
95         jsonObjectFree(obj);
96         return val;
97 }
98
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);
105         else
106                 return jsonObjectFindPath(cfg->config, VA_BUF);
107 }
108
109 int osrfConfigGetValueList(const osrfConfig* cfg, osrfStringArray* arr,
110                 const char* path, ...) {
111
112         if(!arr || !path) return 0;
113         if(!cfg) cfg = osrfConfigDefault;
114         if(!cfg) { osrfLogWarning( OSRF_LOG_MARK, "No Config object!"); return -1;}
115
116         VA_LIST_TO_STRING(path);
117
118         jsonObject* obj;
119         if(cfg->configContext) {
120                 obj = jsonObjectFindPath( cfg->config, "//%s%s", cfg->configContext, VA_BUF);
121         } else {
122                 obj = jsonObjectFindPath( cfg->config, VA_BUF);
123         }
124
125         int count = 0;
126
127         if(obj && obj->type == JSON_ARRAY ) {
128
129                 int i;
130                 for( i = 0; i < obj->size; i++ ) {
131
132                         char* val = jsonObjectToSimpleString(jsonObjectGetIndex(obj, i));
133                         if(val) {
134                                 count++;
135                                 osrfStringArrayAdd(arr, val);
136                                 free(val);
137                         }
138                 }
139         }
140
141         jsonObjectFree(obj);
142         return count;
143 }
144