]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/libopensrf/osrfConfig.c
This update boosts the performance of the jsonFormatString function.
[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                 obj = jsonObjectGetIndex(
88             jsonObjectFindPath(cfg->config, "//%s%s", cfg->configContext, VA_BUF), 0);
89         else
90                 obj = jsonObjectFindPath( cfg->config, VA_BUF);
91
92         char* val = jsonObjectToSimpleString(obj);
93     jsonObjectFree(obj);
94     return val;
95 }
96
97 jsonObject* osrfConfigGetValueObject(osrfConfig* cfg, char* path, ...) {
98         if(!path) return NULL;
99         if(!cfg) cfg = osrfConfigDefault;
100         VA_LIST_TO_STRING(path);
101         if(cfg->configContext) 
102         return jsonObjectFindPath(cfg->config, "//%s%s", cfg->configContext, VA_BUF);
103         else
104                 return jsonObjectFindPath(cfg->config, VA_BUF);
105 }
106
107 int osrfConfigGetValueList(const osrfConfig* cfg, osrfStringArray* arr,
108                 const char* path, ...) {
109
110         if(!arr || !path) return 0;
111         if(!cfg) cfg = osrfConfigDefault;
112         if(!cfg) { osrfLogWarning( OSRF_LOG_MARK, "No Config object!"); return -1;}
113
114         VA_LIST_TO_STRING(path);
115
116         jsonObject* obj;
117         if(cfg->configContext) {
118                 obj = jsonObjectFindPath( cfg->config, "//%s%s", cfg->configContext, VA_BUF);
119         } else {
120                 obj = jsonObjectFindPath( cfg->config, VA_BUF);
121         }
122
123         int count = 0;
124
125         if(obj && obj->type == JSON_ARRAY ) {
126
127                 int i;
128                 for( i = 0; i < obj->size; i++ ) {
129
130                         char* val = jsonObjectToSimpleString(jsonObjectGetIndex(obj, i));
131                         if(val) {
132                                 count++;
133                                 osrfStringArrayAdd(arr, val);
134                                 free(val);
135                         }
136                 }
137         }
138
139         jsonObjectFree(obj);
140         return count;
141 }
142