Index Source Files Annotated Class List Alphabetical Class List Class Hierarchy Graphical Class Hierarchy

# Dictionary.cpp

Go to the documentation of this file.

    00001 /****************************************************************************
    00002 ** Copyright (c) quickfixengine.org  All rights reserved.
    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 #include "CallStack.h"
    00026
    00027 #include "Dictionary.h"
    00028 #include "FieldConvertors.h"
    00029 #include <algorithm>
    00030
    00031 namespace FIX
    00032 {
    00033 std::string Dictionary::getString( const std::string& key, bool capitalize ) const
    00034 throw( ConfigError, FieldConvertError )
    00035 { QF_STACK_PUSH(Dictionary::getString)
    00036
    00037   Data::const_iterator i = m_data.find( string_toUpper(key) );
    00038   if ( i == m_data.end() ) throw ConfigError( key + " not defined" );
    00039
    00040   std::string result = i->second;
    00041   if( capitalize )
    00042      std::transform(result.begin(), result.end(), result.begin(), toupper);
    00043
    00044   return result;
    00045
    00046   QF_STACK_POP
    00047 }
    00048
    00049 long Dictionary::getLong( const std::string& key ) const
    00050 throw( ConfigError, FieldConvertError )
    00051 { QF_STACK_PUSH(Dictionary::getLong)
    00052
    00053   Data::const_iterator i = m_data.find( string_toUpper(key) );
    00054   if ( i == m_data.end() ) throw ConfigError( key + " not defined" );
    00055   try
    00056   {
    00057     return IntConvertor::convert( i->second );
    00058   }
    00059   catch ( FieldConvertError& )
    00060   {
    00061     throw ConfigError( "Illegal value " + i->second + " for " + key );
    00062   }
    00063
    00064   QF_STACK_POP
    00065 }
    00066
    00067 double Dictionary::getDouble( const std::string& key ) const
    00068 throw( ConfigError, FieldConvertError )
    00069 { QF_STACK_PUSH(Dictionary::getDouble)
    00070
    00071   Data::const_iterator i = m_data.find( string_toUpper(key) );
    00072   if ( i == m_data.end() ) throw ConfigError( key + " not defined" );
    00073   try
    00074   {
    00075     return DoubleConvertor::convert( i->second );
    00076   }
    00077   catch ( FieldConvertError& )
    00078   {
    00079     throw ConfigError( "Illegal value " + i->second + " for " + key );
    00080   }
    00081
    00082   QF_STACK_POP
    00083 }
    00084
    00085 bool Dictionary::getBool( const std::string& key ) const
    00086 throw( ConfigError, FieldConvertError )
    00087 { QF_STACK_PUSH(Dictionary::getBool)
    00088
    00089   Data::const_iterator i = m_data.find( string_toUpper(key) );
    00090   if ( i == m_data.end() ) throw ConfigError( key + " not defined" );
    00091   try
    00092   {
    00093     return BoolConvertor::convert( i->second );
    00094   }
    00095   catch ( FieldConvertError& )
    00096   {
    00097     throw ConfigError( "Illegal value " + i->second + " for " + key );
    00098   }
    00099
    00100   QF_STACK_POP
    00101 }
    00102
    00103 int Dictionary::getDay( const std::string& key ) const
    00104 throw( ConfigError, FieldConvertError )
    00105 { QF_STACK_PUSH(Dictionary::getDay)
    00106
    00107   Data::const_iterator i = m_data.find( string_toUpper(key) );
    00108   if ( i == m_data.end() ) throw ConfigError( key + " not defined" );
    00109   try
    00110   {
    00111     std::string value = i->second;
    00112     if( value.size() < 2 ) throw FieldConvertError(0);
    00113     std::string abbr = value.substr(0, 2);
    00114     std::transform( abbr.begin(), abbr.end(), abbr.begin(), tolower );
    00115     if( abbr == "su" ) return 1;
    00116     if( abbr == "mo" ) return 2;
    00117     if( abbr == "tu" ) return 3;
    00118     if( abbr == "we" ) return 4;
    00119     if( abbr == "th" ) return 5;
    00120     if( abbr == "fr" ) return 6;
    00121     if( abbr == "sa" ) return 7;
    00122     if( value.size() < 2 ) throw FieldConvertError(0);
    00123   }
    00124   catch ( FieldConvertError& )
    00125   {
    00126     throw ConfigError( "Illegal value " + i->second + " for " + key );
    00127   }
    00128   return -1;
    00129
    00130   QF_STACK_POP
    00131 }
    00132
    00133 void Dictionary::setString( const std::string& key, const std::string& value )
    00134 { QF_STACK_PUSH(Dictionary::setString)
    00135   m_data[ string_strip(string_toUpper(key)) ] = string_strip(value);
    00136   QF_STACK_POP
    00137 }
    00138
    00139 void Dictionary::setLong( const std::string& key, long value )
    00140 { QF_STACK_PUSH(Dictionary::setString)
    00141   m_data[ string_strip(string_toUpper(key)) ] = IntConvertor::convert( value );
    00142   QF_STACK_POP
    00143 }
    00144
    00145 void Dictionary::setDouble( const std::string& key, double value )
    00146 { QF_STACK_PUSH(Dictionary::setDouble)
    00147   m_data[ string_strip(string_toUpper(key)) ] = DoubleConvertor::convert( value );
    00148   QF_STACK_POP
    00149 }
    00150
    00151 void Dictionary::setBool( const std::string& key, bool value )
    00152 { QF_STACK_PUSH(Dictionary::setBool)
    00153   m_data[ string_strip(string_toUpper(key)) ] = BoolConvertor::convert( value );
    00154   QF_STACK_POP
    00155 }
    00156
    00157 void Dictionary::setDay( const std::string& key, int value )
    00158 { QF_STACK_PUSH(Dictionary::setDay)
    00159
    00160     switch( value )
    00161     {
    00162     case 1:
    00163       setString( key, "SU" ); break;
    00164     case 2:
    00165       setString( key, "MO" ); break;
    00166     case 3:
    00167       setString( key, "TU" ); break;
    00168     case 4:
    00169       setString( key, "WE" ); break;
    00170     case 5:
    00171       setString( key, "TH" ); break;
    00172     case 6:
    00173       setString( key, "FR" ); break;
    00174     case 7:
    00175       setString( key, "SA" ); break;
    00176     }
    00177
    00178   QF_STACK_POP
    00179 }
    00180
    00181 bool Dictionary::has( const std::string& key ) const
    00182 { QF_STACK_PUSH(Dictionary::has)
    00183   return m_data.find( string_toUpper(key) ) != m_data.end();
    00184   QF_STACK_POP
    00185 }
    00186
    00187 void Dictionary::merge( const Dictionary& toMerge )
    00188 { QF_STACK_PUSH(Dictionary::merge)
    00189
    00190   Data::const_iterator i = toMerge.m_data.begin();
    00191   for ( ; i != toMerge.m_data.end(); ++i )
    00192     if ( m_data.find( i->first ) == m_data.end() )
    00193       m_data[ i->first ] = i->second;
    00194
    00195   QF_STACK_POP
    00196 }
    00197 }