Dictionary.cpp
Go to the documentation of this file.
00001 /****************************************************************************
00002 ** Copyright (c) 2001-2014
00003 **
00004 ** This file is part of the QuickFIX FIX Engine
00005 **
00006 ** This file may be distributed under the terms of the quickfixengine.org
00007 ** license as defined by quickfixengine.org and appearing in the file
00008 ** LICENSE included in the packaging of this file.
00009 **
00010 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00011 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00012 **
00013 ** See http://www.quickfixengine.org/LICENSE for licensing information.
00014 **
00015 ** Contact ask@quickfixengine.org if any conditions of this licensing are
00016 ** not clear to you.
00017 **
00018 ****************************************************************************/
00019 
00020 #ifdef _MSC_VER
00021 #include "stdafx.h"
00022 #else
00023 #include "config.h"
00024 #endif
00025 
00026 #include "Dictionary.h"
00027 #include "FieldConvertors.h"
00028 #include <algorithm>
00029 
00030 namespace FIX
00031 {
00032 std::string Dictionary::getString( const std::string& key, bool capitalize ) const
00033 throw( ConfigError, FieldConvertError )
00034 {
00035   Data::const_iterator i = m_data.find( string_toUpper(key) );
00036   if ( i == m_data.end() ) throw ConfigError( key + " not defined" );
00037 
00038   std::string result = i->second;
00039   if( capitalize )
00040      std::transform(result.begin(), result.end(), result.begin(), toupper);
00041 
00042   return result;
00043 }
00044 
00045 int Dictionary::getInt( const std::string& key ) const
00046 throw( ConfigError, FieldConvertError )
00047 {
00048   try
00049   {
00050     return IntConvertor::convert( getString(key) );
00051   }
00052   catch ( FieldConvertError& )
00053   {
00054     throw ConfigError( "Illegal value " + getString(key) + " for " + key );
00055   }
00056 }
00057 
00058 double Dictionary::getDouble( const std::string& key ) const
00059 throw( ConfigError, FieldConvertError )
00060 {
00061   try
00062   {
00063     return DoubleConvertor::convert( getString(key) );
00064   }
00065   catch ( FieldConvertError& )
00066   {
00067     throw ConfigError( "Illegal value " + getString(key) + " for " + key );
00068   }
00069 }
00070 
00071 bool Dictionary::getBool( const std::string& key ) const
00072 throw( ConfigError, FieldConvertError )
00073 {
00074   try
00075   {
00076     return BoolConvertor::convert( getString(key) );
00077   }
00078   catch ( FieldConvertError& )
00079   {
00080     throw ConfigError( "Illegal value " + getString(key) + " for " + key );
00081   }
00082 }
00083 
00084 int Dictionary::getDay( const std::string& key ) const
00085 throw( ConfigError, FieldConvertError )
00086 {
00087   try
00088   {
00089     std::string value = getString(key);
00090     if( value.size() < 2 ) throw FieldConvertError(0);
00091     std::string abbr = value.substr(0, 2);
00092     std::transform( abbr.begin(), abbr.end(), abbr.begin(), tolower );
00093     if( abbr == "su" ) return 1;
00094     if( abbr == "mo" ) return 2;
00095     if( abbr == "tu" ) return 3;
00096     if( abbr == "we" ) return 4;
00097     if( abbr == "th" ) return 5;
00098     if( abbr == "fr" ) return 6;
00099     if( abbr == "sa" ) return 7;
00100     if( value.size() < 2 ) throw FieldConvertError(0);
00101   }
00102   catch ( FieldConvertError& )
00103   {
00104     throw ConfigError( "Illegal value " + getString(key) + " for " + key );
00105   }
00106   return -1;
00107 }
00108 
00109 void Dictionary::setString( const std::string& key, const std::string& value )
00110 {
00111   m_data[ string_strip(string_toUpper(key)) ] = string_strip(value);
00112 }
00113 
00114 void Dictionary::setInt( const std::string& key, int value )
00115 {
00116   m_data[ string_strip(string_toUpper(key)) ] = IntConvertor::convert( value );
00117 }
00118 
00119 void Dictionary::setDouble( const std::string& key, double value )
00120 {
00121   m_data[ string_strip(string_toUpper(key)) ] = DoubleConvertor::convert( value );
00122 }
00123 
00124 void Dictionary::setBool( const std::string& key, bool value )
00125 {
00126   m_data[ string_strip(string_toUpper(key)) ] = BoolConvertor::convert( value );
00127 }
00128 
00129 void Dictionary::setDay( const std::string& key, int value )
00130 {  
00131     switch( value )
00132     {
00133     case 1:
00134       setString( key, "SU" ); break;
00135     case 2:
00136       setString( key, "MO" ); break;
00137     case 3:
00138       setString( key, "TU" ); break;
00139     case 4:
00140       setString( key, "WE" ); break;
00141     case 5:
00142       setString( key, "TH" ); break;
00143     case 6:
00144       setString( key, "FR" ); break;
00145     case 7:
00146       setString( key, "SA" ); break;
00147     }
00148 }
00149 
00150 bool Dictionary::has( const std::string& key ) const
00151 {
00152   return m_data.find( string_toUpper(key) ) != m_data.end();
00153 }
00154 
00155 void Dictionary::merge( const Dictionary& toMerge )
00156 {
00157   Data::const_iterator i = toMerge.m_data.begin();
00158   for ( ; i != toMerge.m_data.end(); ++i )
00159     if ( m_data.find( i->first ) == m_data.end() )
00160       m_data[ i->first ] = i->second;
00161 }
00162 }

Generated on Mon Jun 23 2014 23:49:37 for QuickFIX by doxygen 1.7.6.1 written by Dimitri van Heesch, © 1997-2001