]> git.evergreen-ils.org Git - working/Evergreen.git/blob - OpenSRF/src/libstack/osrfConfig.c
825a65f230b4bee9f38b1b7e07af9d9ecaea32f1
[working/Evergreen.git] / OpenSRF / src / libstack / osrfConfig.c
1 /* defines the currently used bootstrap config file */
2 #include "osrfConfig.h"
3
4 osrfConfig* __osrfConfigDefault = NULL;
5
6
7 void osrfConfigSetDefaultConfig(osrfConfig* cfg) {
8         if(cfg) __osrfConfigDefault = cfg;
9 }
10
11 void osrfConfigFree(osrfConfig* cfg) {
12         if(cfg) {
13                 jsonObjectFree(cfg->config);
14                 free(cfg->configContext);
15                 free(cfg);
16         }       
17 }
18
19
20 int osrfConfigHasDefaultConfig() {
21         return ( __osrfConfigDefault != NULL );
22 }
23
24
25 void osrfConfigCleanup() { 
26         osrfConfigFree(__osrfConfigDefault); 
27         __osrfConfigDefault = NULL; 
28 }
29
30
31 void osrfConfigReplaceConfig(osrfConfig* cfg, const jsonObject* obj) {
32         if(!cfg || !obj) return;
33         jsonObjectFree(cfg->config);
34         cfg->config = jsonObjectClone(obj);     
35 }
36
37 osrfConfig* osrfConfigInit(char* configFile, char* configContext) {
38         if(!configFile) return NULL;
39
40         osrfConfigFree(__osrfConfigDefault);
41
42         osrfConfig* cfg = safe_malloc(sizeof(osrfConfig));
43         if(configContext) cfg->configContext = strdup(configContext);
44         else cfg->configContext = NULL;
45
46         xmlDocPtr doc = xmlParseFile(configFile);
47         if(!doc) {
48                 warning_handler( "Unable to parse XML config file %s", configFile);
49                 return NULL;
50         }
51
52         cfg->config = xmlDocToJSON(doc);
53         xmlFreeDoc(doc);
54
55         if(!cfg->config) {
56                 warning_handler("xmlDocToJSON failed for config %s", configFile);
57                 return NULL;
58         }       
59
60         return cfg;
61 }
62
63 char* osrfConfigGetValue(osrfConfig* cfg, char* path, ...) {
64
65         if(!path) return NULL;
66         if(!cfg) cfg = __osrfConfigDefault;
67         if(!cfg) { warning_handler("No Confif object!"); return NULL; }
68
69         VA_LIST_TO_STRING(path);
70
71         jsonObject* obj;
72         char* val = NULL;
73
74         if(cfg->configContext) {
75                 obj = jsonObjectFindPath( cfg->config, "//%s%s", cfg->configContext, VA_BUF);
76                 if(obj) val = jsonObjectToSimpleString(jsonObjectGetIndex(obj, 0));
77
78         } else {
79                 obj = jsonObjectFindPath( cfg->config, VA_BUF);
80                 if(obj) val = jsonObjectToSimpleString(obj);
81         }
82
83         jsonObjectFree(obj);
84         return val;
85 }
86
87
88 int osrfConfigGetValueList(osrfConfig* cfg, osrfStringArray* arr, char* path, ...) {
89
90         if(!arr || !path) return 0;
91         if(!cfg) cfg = __osrfConfigDefault;
92         if(!cfg) { return warning_handler("No Confif object!"); }
93
94         VA_LIST_TO_STRING(path);
95
96         jsonObject* obj;
97         if(cfg->configContext) {
98                 obj = jsonObjectFindPath( cfg->config, "//%s%s", cfg->configContext, VA_BUF);
99         } else {
100                 obj = jsonObjectFindPath( cfg->config, VA_BUF);
101         }
102
103         int count = 0;
104
105         if(obj && obj->type == JSON_ARRAY ) {
106
107                 int i;
108                 for( i = 0; i < obj->size; i++ ) {
109
110                         char* val = jsonObjectToSimpleString(jsonObjectGetIndex(obj, i));
111                         if(val) {
112                                 count++;
113                                 osrfStringArrayAdd(arr, val);
114                                 free(val);
115                         }
116                 }
117         }
118
119         return count;
120 }
121