]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/libopensrf/osrfConfig.c
40dcee01c0a9a2d2bb6868e00482a0d5d8a0d550
[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(char* configFile, 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 char* osrfConfigGetValue(osrfConfig* cfg, char* path, ...) {
75
76         if(!path) return NULL;
77         if(!cfg) cfg = osrfConfigDefault;
78         if(!cfg) { osrfLogWarning( OSRF_LOG_MARK, "No Config object in osrfConfigGetValue()"); return NULL; }
79
80         VA_LIST_TO_STRING(path);
81
82         jsonObject* obj;
83         char* val = NULL;
84
85         if(cfg->configContext) {
86                 obj = jsonObjectFindPath( cfg->config, "//%s%s", cfg->configContext, VA_BUF);
87                 if(obj) val = jsonObjectToSimpleString(jsonObjectGetIndex(obj, 0));
88
89         } else {
90                 obj = jsonObjectFindPath( cfg->config, VA_BUF);
91                 if(obj) val = jsonObjectToSimpleString(obj);
92         }
93
94         jsonObjectFree(obj);
95         return val;
96 }
97
98
99 int osrfConfigGetValueList(osrfConfig* cfg, osrfStringArray* arr, char* path, ...) {
100
101         if(!arr || !path) return 0;
102         if(!cfg) cfg = osrfConfigDefault;
103         if(!cfg) { osrfLogWarning( OSRF_LOG_MARK, "No Config object!"); return -1;}
104
105         VA_LIST_TO_STRING(path);
106
107         jsonObject* obj;
108         if(cfg->configContext) {
109                 obj = jsonObjectFindPath( cfg->config, "//%s%s", cfg->configContext, VA_BUF);
110         } else {
111                 obj = jsonObjectFindPath( cfg->config, VA_BUF);
112         }
113
114         int count = 0;
115
116         if(obj && obj->type == JSON_ARRAY ) {
117
118                 int i;
119                 for( i = 0; i < obj->size; i++ ) {
120
121                         char* val = jsonObjectToSimpleString(jsonObjectGetIndex(obj, i));
122                         if(val) {
123                                 count++;
124                                 osrfStringArrayAdd(arr, val);
125                                 free(val);
126                         }
127                 }
128         }
129
130         jsonObjectFree(obj);
131         return count;
132 }
133