From 3bab253c08a9e445b73c20b9a117c4a089d03380 Mon Sep 17 00:00:00 2001 From: erickson Date: Sun, 19 Jun 2005 18:00:10 +0000 Subject: [PATCH] took the config functions from generic_utils and put them here git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@363 9efc2488-bf62-4759-914b-345cdb29e865 --- src/libstack/osrf_config.c | 156 +++++++++++++++++++++++++++++++++++++++++++++ src/libstack/osrf_config.o | Bin 0 -> 3740 bytes 2 files changed, 156 insertions(+) create mode 100644 src/libstack/osrf_config.c create mode 100644 src/libstack/osrf_config.o diff --git a/src/libstack/osrf_config.c b/src/libstack/osrf_config.c new file mode 100644 index 0000000..a4953b1 --- /dev/null +++ b/src/libstack/osrf_config.c @@ -0,0 +1,156 @@ +#include "osrf_config.h" + +//object* config_object = NULL; + +/* +char* osrf_config_value(char* path) { +} + + +void _osrf_config_grab_config() { + if(config_object) return; +} +*/ + + +void config_reader_init( char* name, char* config_file ) { + + if( name == NULL || config_file == NULL || strlen(config_file) == 0 ) { + fatal_handler( "config_reader_init(): No config file specified" ); + return; + } + + config_reader* reader = + (config_reader*) safe_malloc(sizeof(config_reader)); + + reader->config_doc = xmlParseFile( config_file ); + reader->xpathCx = xmlXPathNewContext( reader->config_doc ); + reader->name = strdup(name); + reader->next = NULL; + + if( reader->xpathCx == NULL ) { + fprintf( stderr, "config_reader_init(): Unable to create xpath context\n"); + return; + } + + if( conf_reader == NULL ) { + conf_reader = reader; + } else { + config_reader* tmp = conf_reader; + conf_reader = reader; + reader->next = tmp; + } +} + + +char* config_value( const char* config_name, const char* xp_query, ... ) { + + if( conf_reader == NULL || xp_query == NULL ) { + fatal_handler( "config_value(): NULL conf_reader or NULL param(s)" ); + return NULL; + } + + config_reader* reader = conf_reader; + while( reader != NULL ) { + if( !strcmp(reader->name, config_name)) + break; + reader = reader->next; + } + + if( reader == NULL ) { + fprintf(stderr, "No Config file with name %s\n", config_name ); + return NULL; + } + + int slen = strlen(xp_query) + 512;/* this is unsafe ... */ + char xpath_query[ slen ]; + memset( xpath_query, 0, slen ); + va_list va_args; + va_start(va_args, xp_query); + vsprintf(xpath_query, xp_query, va_args); + va_end(va_args); + + + char* val; + int len = strlen(xpath_query) + 100; + char alert_buffer[len]; + memset( alert_buffer, 0, len ); + + // build the xpath object + xmlXPathObjectPtr xpathObj = xmlXPathEvalExpression( BAD_CAST xpath_query, reader->xpathCx ); + + if( xpathObj == NULL ) { + sprintf( alert_buffer, "Could not build xpath object: %s", xpath_query ); + fatal_handler( alert_buffer ); + return NULL; + } + + + if( xpathObj->type == XPATH_NODESET ) { + + // ---------------------------------------------------------------------------- + // Grab nodeset from xpath query, then first node, then first text node and + // finaly the text node's value + // ---------------------------------------------------------------------------- + xmlNodeSet* node_list = xpathObj->nodesetval; + if( node_list == NULL ) { + sprintf( alert_buffer, "Could not build xpath object: %s", xpath_query ); + warning_handler(alert_buffer); + return NULL; + } + + if( node_list->nodeNr == 0 ) { + sprintf( alert_buffer, "Config XPATH query returned 0 results: %s", xpath_query ); + warning_handler(alert_buffer); + return NULL; + } + + + xmlNodePtr element_node = *(node_list)->nodeTab; + if( element_node == NULL ) { + sprintf( alert_buffer, "Config XPATH query returned 0 results: %s", xpath_query ); + warning_handler(alert_buffer); + return NULL; + } + + xmlNodePtr text_node = element_node->children; + if( text_node == NULL ) { + sprintf( alert_buffer, "Config variable has no value: %s", xpath_query ); + warning_handler(alert_buffer); + return NULL; + } + + val = text_node->content; + if( val == NULL ) { + sprintf( alert_buffer, "Config variable has no value: %s", xpath_query ); + warning_handler(alert_buffer); + return NULL; + } + + + } else { + sprintf( alert_buffer, "Xpath evaluation failed: %s", xpath_query ); + warning_handler(alert_buffer); + return NULL; + } + + char* value = strdup(val); + if( value == NULL ) { warning_handler( "config_value(): Empty config value or Out of Memory!" ); } + + // Free XPATH structures + if( xpathObj != NULL ) xmlXPathFreeObject( xpathObj ); + + return value; +} + + +void config_reader_free() { + while( conf_reader != NULL ) { + xmlXPathFreeContext( conf_reader->xpathCx ); + xmlFreeDoc( conf_reader->config_doc ); + free(conf_reader->name ); + config_reader* tmp = conf_reader->next; + free( conf_reader ); + conf_reader = tmp; + } +} diff --git a/src/libstack/osrf_config.o b/src/libstack/osrf_config.o new file mode 100644 index 0000000000000000000000000000000000000000..67622aac88648964ada60360c5d1e74581aaa60d GIT binary patch literal 3740 zcmai1VQd^n6@K=u-uglzUFT4 z*qwE3DFIj4L)xxRP5<;C5~3i1_(S`nt)K-vsd8%hkrK74`hyBlQN`GmLJ4k!OpfpE z?Vk6JM0wWC_r94oZ{C}kojLzFo|tTFYa?IUC`8+gNb0Qj9CV?dy73p36ZYRr8~ygV zE$3X=p1po`t5&N$b+H{LwsoUibF7HyDsPI8EoZ0J+b;CKgWpB)0 ztV+>d^tm1O!KhuWYn{HaJtaBu@HPuO(^7RR!v4x{ihCT_v(sC?VoCPc`Y-G;`_ahy zPwUOUI|M6-?3rlTJ{pa@%K1T&n!{PkA@uXe=g~(VE^YK7pI|cDHE#+kuk<{fL4x)r zCmvz)7?aJ)Yv15t?6XnHnQR1FUJ?79g-ChR`UpK?Z$`}Rd-F3IUa4r;zyE{e>_x2B zJMipx*sGQ09?Ves>$QP5D$fzr3X-tn8>Rnr+SP@-e-+Cr>(RqRnFIL z-n{wg+j}lN^|xh+So`$C@-mUNWAz`srPX$Lpev>KLj$XoBPIf^Jd{SPh{Fxhj$d8a zi72~=Yn6wdz%oQQ*gJ&iOiC@Wl~?RbJx{-mH7<>{^^|`J#feG00S>F!da>Zd!VWe_ zJwB2-YXCTk)&D4=12%*Vc|= zc$4}1{DoqB6tIN;}_JNrSZ)1 zd{Hdr0K%0IL;V@Z|`O<0DmrR5>0rj--s?LAEttXvv13VF`TLaQfQn0r!7iQA9bj-)mJ zV`H+^qQID5CPs2Xa%-y9(9jTf}LH)pa~SY6F)NVc0!y6dcQ`l`qlbUCA91JiCK zPG9sQO-)RU%l%WwP7KH+%7}7E2GMj;D9DMdZY|au2aVzTWYa7TvdMwLk^AIgRvR2s zM)5a1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