From 4b8a5220f5c6ea965b0453f023c69a1c263b29c1 Mon Sep 17 00:00:00 2001 From: erickson Date: Wed, 31 Aug 2005 19:02:50 +0000 Subject: [PATCH] adding config code git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@514 9efc2488-bf62-4759-914b-345cdb29e865 --- src/libstack/osrfConfig.c | 121 ++++++++++++++++++++++++++++++++++++++++++++++ src/libstack/osrfConfig.h | 103 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 224 insertions(+) create mode 100644 src/libstack/osrfConfig.c create mode 100644 src/libstack/osrfConfig.h diff --git a/src/libstack/osrfConfig.c b/src/libstack/osrfConfig.c new file mode 100644 index 0000000..825a65f --- /dev/null +++ b/src/libstack/osrfConfig.c @@ -0,0 +1,121 @@ +/* defines the currently used bootstrap config file */ +#include "osrfConfig.h" + +osrfConfig* __osrfConfigDefault = NULL; + + +void osrfConfigSetDefaultConfig(osrfConfig* cfg) { + if(cfg) __osrfConfigDefault = cfg; +} + +void osrfConfigFree(osrfConfig* cfg) { + if(cfg) { + jsonObjectFree(cfg->config); + free(cfg->configContext); + free(cfg); + } +} + + +int osrfConfigHasDefaultConfig() { + return ( __osrfConfigDefault != NULL ); +} + + +void osrfConfigCleanup() { + osrfConfigFree(__osrfConfigDefault); + __osrfConfigDefault = NULL; +} + + +void osrfConfigReplaceConfig(osrfConfig* cfg, const jsonObject* obj) { + if(!cfg || !obj) return; + jsonObjectFree(cfg->config); + cfg->config = jsonObjectClone(obj); +} + +osrfConfig* osrfConfigInit(char* configFile, char* configContext) { + if(!configFile) return NULL; + + osrfConfigFree(__osrfConfigDefault); + + osrfConfig* cfg = safe_malloc(sizeof(osrfConfig)); + if(configContext) cfg->configContext = strdup(configContext); + else cfg->configContext = NULL; + + xmlDocPtr doc = xmlParseFile(configFile); + if(!doc) { + warning_handler( "Unable to parse XML config file %s", configFile); + return NULL; + } + + cfg->config = xmlDocToJSON(doc); + xmlFreeDoc(doc); + + if(!cfg->config) { + warning_handler("xmlDocToJSON failed for config %s", configFile); + return NULL; + } + + return cfg; +} + +char* osrfConfigGetValue(osrfConfig* cfg, char* path, ...) { + + if(!path) return NULL; + if(!cfg) cfg = __osrfConfigDefault; + if(!cfg) { warning_handler("No Confif object!"); return NULL; } + + VA_LIST_TO_STRING(path); + + jsonObject* obj; + char* val = NULL; + + if(cfg->configContext) { + obj = jsonObjectFindPath( cfg->config, "//%s%s", cfg->configContext, VA_BUF); + if(obj) val = jsonObjectToSimpleString(jsonObjectGetIndex(obj, 0)); + + } else { + obj = jsonObjectFindPath( cfg->config, VA_BUF); + if(obj) val = jsonObjectToSimpleString(obj); + } + + jsonObjectFree(obj); + return val; +} + + +int osrfConfigGetValueList(osrfConfig* cfg, osrfStringArray* arr, char* path, ...) { + + if(!arr || !path) return 0; + if(!cfg) cfg = __osrfConfigDefault; + if(!cfg) { return warning_handler("No Confif object!"); } + + VA_LIST_TO_STRING(path); + + jsonObject* obj; + if(cfg->configContext) { + obj = jsonObjectFindPath( cfg->config, "//%s%s", cfg->configContext, VA_BUF); + } else { + obj = jsonObjectFindPath( cfg->config, VA_BUF); + } + + int count = 0; + + if(obj && obj->type == JSON_ARRAY ) { + + int i; + for( i = 0; i < obj->size; i++ ) { + + char* val = jsonObjectToSimpleString(jsonObjectGetIndex(obj, i)); + if(val) { + count++; + osrfStringArrayAdd(arr, val); + free(val); + } + } + } + + return count; +} + diff --git a/src/libstack/osrfConfig.h b/src/libstack/osrfConfig.h new file mode 100644 index 0000000..0d184fe --- /dev/null +++ b/src/libstack/osrfConfig.h @@ -0,0 +1,103 @@ +/* +Copyright (C) 2005 Georgia Public Library Service +Bill Erickson + +This program is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License +as published by the Free Software Foundation; either version 2 +of the License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. +*/ + +#ifndef _OSRF_CONFIG_H +#define _OSRF_CONFIG_H + +#include "xml_utils.h" +#include "utils.h" +#include "string_array.h" +#include "objson/object.h" + +typedef struct { + jsonObject* config; + char* configContext; +} osrfConfig; + + +/** + Parses a new config file. Caller is responsible for freeing the returned + config object when finished. + @param configFile The XML config file to parse. + @param configContext Optional root of the subtree in the config file where + we will look for values. If it's not provided, searches will be + performed from the root of the config file + @return The config object if the file parses successfully. Otherwise + it returns NULL; +*/ +osrfConfig* osrfConfigInit(char* configFile, char* configContext); + +/** + @return True if we have a default config defined +*/ +int osrfConfigHasDefaultConfig(); + +/** + Replaces the config object's objson object. This is useful + if you have an ojbson object already and not an XML config + file to parse. + @param cfg The config object to alter + @param obj The objson objet to use when searching values +*/ +void osrfConfigReplaceConfig(osrfConfig* cfg, const jsonObject* obj); + +/** Deallocates a config object + @param cfg The config object to free +*/ +void osrfConfigFree(osrfConfig* cfg); + + +/* Assigns the default config file. This file will be used whenever + NULL is passed to config retrieval functions + @param cfg The config object to use as the default config +*/ +void osrfConfigSetDefaultConfig(osrfConfig* cfg); + +/* frees the default config if one exists */ +void osrfConfigCleanup(); + + +/** + Returns the value in the config found at 'path'. + If the value found at 'path' is a long or a double, + the value is stringified and then returned. + The caller must free the returned char* + + if there is a configContext, then it will be appended to + the front of the path like so: /// + if no configContext was provided to osfConfigSetFile, then + the path is interpreted literally. + @param cfg The config file to search or NULL if the default + config should be used + @param path The search path +*/ +char* osrfConfigGetValue(osrfConfig* cfg, char* path, ...); + +/** + Puts the list of values found at 'path' into the pre-allocated + string array. + Note that the config node found at 'path' must be an array. + @param cfg The config file to search or NULL if the default + config should be used + @param arr An allocated string_array where the values will + be stored + @param path The search path + @return the number of values added to the string array; +*/ + +int osrfConfigGetValueList(osrfConfig* cfg, osrfStringArray* arr, char* path, ...); + + +#endif -- 2.11.0