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