]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/libstack/osrfConfig.c
a85262584df2197628adbecd6c730a102274b296
[OpenSRF.git] / src / libstack / osrfConfig.c
1 /* defines the currently used bootstrap config file */
2 #include "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(char* configFile, char* configContext) {
42         if(!configFile) return NULL;
43
44         osrfConfig* cfg = safe_malloc(sizeof(osrfConfig));
45         if(configContext) cfg->configContext = strdup(configContext);
46         else cfg->configContext = NULL;
47
48         xmlDocPtr doc = xmlParseFile(configFile);
49         if(!doc) {
50                 osrfLogWarning( OSRF_LOG_MARK,  "Unable to parse XML config file %s", configFile);
51                 return NULL;
52         }
53
54         cfg->config = xmlDocToJSON(doc);
55         xmlFreeDoc(doc);
56
57         if(!cfg->config) {
58                 osrfLogWarning( OSRF_LOG_MARK, "xmlDocToJSON failed for config %s", configFile);
59                 return NULL;
60         }       
61
62         return cfg;
63 }
64
65 char* osrfConfigGetValue(osrfConfig* cfg, char* path, ...) {
66
67         if(!path) return NULL;
68         if(!cfg) cfg = osrfConfigDefault;
69         if(!cfg) { osrfLogWarning( OSRF_LOG_MARK, "No Config object in osrfConfigGetValue()"); return NULL; }
70
71         VA_LIST_TO_STRING(path);
72
73         jsonObject* obj;
74         char* val = NULL;
75
76         if(cfg->configContext) {
77                 obj = jsonObjectFindPath( cfg->config, "//%s%s", cfg->configContext, VA_BUF);
78                 if(obj) val = jsonObjectToSimpleString(jsonObjectGetIndex(obj, 0));
79
80         } else {
81                 obj = jsonObjectFindPath( cfg->config, VA_BUF);
82                 if(obj) val = jsonObjectToSimpleString(obj);
83         }
84
85         jsonObjectFree(obj);
86         return val;
87 }
88
89
90 int osrfConfigGetValueList(osrfConfig* cfg, osrfStringArray* arr, char* path, ...) {
91
92         if(!arr || !path) return 0;
93         if(!cfg) cfg = osrfConfigDefault;
94         if(!cfg) { osrfLogWarning( OSRF_LOG_MARK, "No Config object!"); return -1;}
95
96         VA_LIST_TO_STRING(path);
97
98         jsonObject* obj;
99         if(cfg->configContext) {
100                 obj = jsonObjectFindPath( cfg->config, "//%s%s", cfg->configContext, VA_BUF);
101         } else {
102                 obj = jsonObjectFindPath( cfg->config, VA_BUF);
103         }
104
105         int count = 0;
106
107         if(obj && obj->type == JSON_ARRAY ) {
108
109                 int i;
110                 for( i = 0; i < obj->size; i++ ) {
111
112                         char* val = jsonObjectToSimpleString(jsonObjectGetIndex(obj, i));
113                         if(val) {
114                                 count++;
115                                 osrfStringArrayAdd(arr, val);
116                                 free(val);
117                         }
118                 }
119         }
120
121         jsonObjectFree(obj);
122         return count;
123 }
124