/* * Config * * Author: Andrew Salamon * Date: Tue Feb 27, 2008 * * Copyright (c) 2008 Machine Perception Laboratory * University of California San Diego. * * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * 3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */ #include "CERT_Config.h" #include "ConfigParser_XML.h" #include namespace CERT { Config::Config( const std::string &_path ) : path(_path) { // figure out which kind of config file it is and invoke the correct parser. if( path.length() > 0 ) { try { ConfigParser_XML parser( this, path ); } catch ( const ConfigParser_XML::exception &error ) { throw exception( error.what() ); } } global.setName( "global" ); } Config::~Config() { // make sure to delete all of the ConfigItems for( unsigned int i = 0; i < items.size(); ++i ) delete items[i]; } std::vector Config::getItems() const { return items; } std::vector Config::itemsOfType( const std::string &type ) const { // walk through, pulling out just 'type' plugins. return std::vector(); } void Config::addItem( ConfigItem *item ) { items.push_back( item ); } //std::vector items; //std::string path; ConfigItem::ConfigItem() : enabled(true), display(true) { } ConfigItem::~ConfigItem() { } std::string ConfigItem::getPluginID() const { return id; } void ConfigItem::setPluginID( const std::string &_id ) { id = _id; } std::string ConfigItem::getName() const { return name; } void ConfigItem::setName( const std::string &_name ) { name = _name; } std::string ConfigItem::getInternalName() const { return internalName; } void ConfigItem::setInternalName( const std::string &_name ) { internalName = _name; } std::string ConfigItem::getPluginType() const { return type; } void ConfigItem::setPluginType( const std::string &_type ) { type = _type; } bool ConfigItem::getEnabled() const { return enabled; } void ConfigItem::setEnabled( bool _enabled ) { enabled = _enabled; } bool ConfigItem::getDisplay() const { return display; } void ConfigItem::setDisplay( bool _display ) { display = _display; } std::vector ConfigItem::getValueForKey( const std::string &key ) { if( properties.find( key ) == properties.end() ) return std::vector(); return properties[ key ]; } void ConfigItem::setValueForKey( const std::string &value, const std::string &key ) { std::vector vec = properties[key]; vec.push_back(value); properties[ key ] = vec; } void ConfigItem::setValueForKey( const std::vector &value, const std::string &key ) { properties[ key ] = value; } ConfigItem::PropertyType ConfigItem::getMapForKey( const std::string &key ) { if( mapProperties.find( key ) == mapProperties.end() ) return ConfigItem::PropertyType(); return mapProperties[ key ]; } void ConfigItem::setValueForKey( const PropertyType &value, const std::string &key ) { mapProperties[ key ] = value; } bool ConfigItem::hasIntValueForKey( const std::string &key ) { std::vector intstr = getValueForKey( key ); if( 1 == intstr.size() ) { std::istringstream istr( intstr[0] ); int test; if( istr >> test ) { return true; } } return false; } int ConfigItem::getIntValueForKey( const std::string &key ) { std::vector intstr = getValueForKey( key ); int value = 0; if( 1 == intstr.size() ) { std::istringstream istr( intstr[0] ); istr >> value; } return value; } bool ConfigItem::hasFloatValueForKey( const std::string &key ) { std::vector intstr = getValueForKey( key ); if( 1 == intstr.size() ) { std::istringstream istr( intstr[0] ); float test; if( istr >> test ) { return true; } } return false; } int ConfigItem::getFloatValueForKey( const std::string &key ) { std::vector intstr = getValueForKey( key ); float value = 0.0; if( 1 == intstr.size() ) { std::istringstream istr( intstr[0] ); istr >> value; } return value; } static const std::string spacing( " " ); static const std::string spacing2( spacing + spacing ); static const std::string spacing3( spacing2 + spacing ); std::ostream& operator<<( std::ostream &os, CERT::ConfigItem::PropertyType &properties ); std::ostream& operator<<( std::ostream &os, const CERT::Config &config ) { os << "" << std::endl; os << "" << std::endl; const CERT::ConfigItem &global = config.getGlobalItem(); CERT::ConfigItem::PropertyType properties = global.getProperties(); if( properties.size() > 0 ) { os << spacing << "" << std::endl; os << properties; os << spacing << "" << std::endl; } std::vector< CERT::ConfigItem * > items = config.getItems(); for( unsigned int i = 0; i < items.size(); ++i ) os << *(items[i]); os << "" << std::endl; return os; } std::ostream& operator<<( std::ostream &os, CERT::ConfigItem::PropertyType &properties ) { for( CERT::ConfigItem::PropertyType::iterator iter = properties.begin(), lastIter = properties.end(); iter != lastIter; ++iter ) { if( iter->first == "comment" ) { for( std::vector::iterator itemIter = iter->second.begin(), lastItemIter = iter->second.end(); itemIter != lastItemIter; ++itemIter ) { os << "" << std::endl; } } else { os << spacing2 << "<" << iter->first << " type=\""; if( iter->second.size() > 1 ) { os << "array\">" << std::endl; for( std::vector::iterator itemIter = iter->second.begin(), lastItemIter = iter->second.end(); itemIter != lastItemIter; ++itemIter ) { os << spacing3; os << "" << *itemIter << "" << std::endl; } os << spacing2 << "first << ">" << std::endl; } else { os << "string\">" << iter->second[0] << "first << ">" << std::endl; } } } return os; } std::ostream& operator<<( std::ostream &os, const CERT::ConfigItem &item ) { os << spacing << "" << std::endl; os << spacing2 << "" << item.getPluginID() << "" << std::endl; os << spacing2 << "" << item.getName() << "" << std::endl; os << spacing2 << "" << item.getInternalName() << "" << std::endl; os << spacing2 << "" << item.getPluginType() << "" << std::endl; os << spacing2 << (item.getEnabled()?"":"") << std::endl; os << spacing2 << (item.getDisplay()?"":"") << std::endl; CERT::ConfigItem::PropertyType properties = item.getProperties(); os << properties; os << spacing << "" << std::endl; return os; } } // End CERT namespace