]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/libopensrf/osrfConfig.c
Merged libopensrf source directories (libtransport, libstack, and utils) into a singl...
[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                 fprintf( stderr, "osrfConfigInit: Unable to parse XML config file %s\n", configFile);
49                 osrfLogWarning( OSRF_LOG_MARK, "Unable to parse XML config file %s", configFile);
50                 return NULL;
51         }
52
53         // Translate it into a jsonObject
54         
55         jsonObject* json_config = xmlDocToJSON(doc);
56         xmlFreeDoc(doc);
57
58         if(!json_config ) {
59                 fprintf( stderr, "osrfConfigInit: xmlDocToJSON failed for config %s\n", configFile);
60                 osrfLogWarning( OSRF_LOG_MARK, "xmlDocToJSON failed for config %s", configFile);
61                 return NULL;
62         }       
63
64         // Build an osrfConfig and return it by pointer
65         
66         osrfConfig* cfg = safe_malloc(sizeof(osrfConfig));
67
68         if(configContext) cfg->configContext = strdup(configContext);
69         else cfg->configContext = NULL;
70
71         cfg->config = json_config;
72         
73         return cfg;
74 }
75
76 char* osrfConfigGetValue(osrfConfig* cfg, char* path, ...) {
77
78         if(!path) return NULL;
79         if(!cfg) cfg = osrfConfigDefault;
80         if(!cfg) { osrfLogWarning( OSRF_LOG_MARK, "No Config object in osrfConfigGetValue()"); return NULL; }
81
82         VA_LIST_TO_STRING(path);
83
84         jsonObject* obj;
85         char* val = NULL;
86
87         if(cfg->configContext) {
88                 obj = jsonObjectFindPath( cfg->config, "//%s%s", cfg->configContext, VA_BUF);
89                 if(obj) val = jsonObjectToSimpleString(jsonObjectGetIndex(obj, 0));
90
91         } else {
92                 obj = jsonObjectFindPath( cfg->config, VA_BUF);
93                 if(obj) val = jsonObjectToSimpleString(obj);
94         }
95
96         jsonObjectFree(obj);
97         return val;
98 }
99
100
101 int osrfConfigGetValueList(osrfConfig* cfg, osrfStringArray* arr, char* path, ...) {
102
103         if(!arr || !path) return 0;
104         if(!cfg) cfg = osrfConfigDefault;
105         if(!cfg) { osrfLogWarning( OSRF_LOG_MARK, "No Config object!"); return -1;}
106
107         VA_LIST_TO_STRING(path);
108
109         jsonObject* obj;
110         if(cfg->configContext) {
111                 obj = jsonObjectFindPath( cfg->config, "//%s%s", cfg->configContext, VA_BUF);
112         } else {
113                 obj = jsonObjectFindPath( cfg->config, VA_BUF);
114         }
115
116         int count = 0;
117
118         if(obj && obj->type == JSON_ARRAY ) {
119
120                 int i;
121                 for( i = 0; i < obj->size; i++ ) {
122
123                         char* val = jsonObjectToSimpleString(jsonObjectGetIndex(obj, i));
124                         if(val) {
125                                 count++;
126                                 osrfStringArrayAdd(arr, val);
127                                 free(val);
128                         }
129                 }
130         }
131
132         jsonObjectFree(obj);
133         return count;
134 }
135