Dictionary.cpp
Go to the documentation of this file.
/****************************************************************************
** Copyright (c) 2001-2014
**
** This file is part of the QuickFIX FIX Engine
**
** This file may be distributed under the terms of the quickfixengine.org
** license as defined by quickfixengine.org and appearing in the file
** LICENSE included in the packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** See http://www.quickfixengine.org/LICENSE for licensing information.
**
** Contact ask@quickfixengine.org if any conditions of this licensing are
** not clear to you.
**
****************************************************************************/
#ifdef _MSC_VER
#include "stdafx.h"
#else
#include "config.h"
#endif
#include "Dictionary.h"
#include "FieldConvertors.h"
#include <algorithm>
namespace FIX
{
std::string Dictionary::getString( const std::string& key, bool capitalize ) const
throw( ConfigError, FieldConvertError )
{
Data::const_iterator i = m_data.find( string_toUpper(key) );
if ( i == m_data.end() ) throw ConfigError( key + " not defined" );
std::string result = i->second;
if( capitalize )
std::transform(result.begin(), result.end(), result.begin(), toupper);
return result;
}
int Dictionary::getInt( const std::string& key ) const
throw( ConfigError, FieldConvertError )
{
try
{
return IntConvertor::convert( getString(key) );
}
catch ( FieldConvertError& )
{
throw ConfigError( "Illegal value " + getString(key) + " for " + key );
}
}
double Dictionary::getDouble( const std::string& key ) const
throw( ConfigError, FieldConvertError )
{
try
{
return DoubleConvertor::convert( getString(key) );
}
catch ( FieldConvertError& )
{
throw ConfigError( "Illegal value " + getString(key) + " for " + key );
}
}
bool Dictionary::getBool( const std::string& key ) const
throw( ConfigError, FieldConvertError )
{
try
{
return BoolConvertor::convert( getString(key) );
}
catch ( FieldConvertError& )
{
throw ConfigError( "Illegal value " + getString(key) + " for " + key );
}
}
int Dictionary::getDay( const std::string& key ) const
throw( ConfigError, FieldConvertError )
{
try
{
std::string value = getString(key);
if( value.size() < 2 ) throw FieldConvertError(0);
std::string abbr = value.substr(0, 2);
std::transform( abbr.begin(), abbr.end(), abbr.begin(), tolower );
if( abbr == "su" ) return 1;
if( abbr == "mo" ) return 2;
if( abbr == "tu" ) return 3;
if( abbr == "we" ) return 4;
if( abbr == "th" ) return 5;
if( abbr == "fr" ) return 6;
if( abbr == "sa" ) return 7;
if( value.size() < 2 ) throw FieldConvertError(0);
}
catch ( FieldConvertError& )
{
throw ConfigError( "Illegal value " + getString(key) + " for " + key );
}
return -1;
}
void Dictionary::setString( const std::string& key, const std::string& value )
{
m_data[ string_strip(string_toUpper(key)) ] = string_strip(value);
}
void Dictionary::setInt( const std::string& key, int value )
{
m_data[ string_strip(string_toUpper(key)) ] = IntConvertor::convert( value );
}
void Dictionary::setDouble( const std::string& key, double value )
{
m_data[ string_strip(string_toUpper(key)) ] = DoubleConvertor::convert( value );
}
void Dictionary::setBool( const std::string& key, bool value )
{
m_data[ string_strip(string_toUpper(key)) ] = BoolConvertor::convert( value );
}
void Dictionary::setDay( const std::string& key, int value )
{
switch( value )
{
case 1:
setString( key, "SU" ); break;
case 2:
setString( key, "MO" ); break;
case 3:
setString( key, "TU" ); break;
case 4:
setString( key, "WE" ); break;
case 5:
setString( key, "TH" ); break;
case 6:
setString( key, "FR" ); break;
case 7:
setString( key, "SA" ); break;
}
}
bool Dictionary::has( const std::string& key ) const
{
return m_data.find( string_toUpper(key) ) != m_data.end();
}
void Dictionary::merge( const Dictionary& toMerge )
{
Data::const_iterator i = toMerge.m_data.begin();
for ( ; i != toMerge.m_data.end(); ++i )
if ( m_data.find( i->first ) == m_data.end() )
m_data[ i->first ] = i->second;
}
}
FIX::Dictionary::setBool
void setBool(const std::string &, bool)
Set a value from a bool.
Definition: Dictionary.cpp:124
FIX::FieldConvertError
Unable to convert field into its native format.
Definition: Exceptions.h:66
FIX::Dictionary::merge
void merge(const Dictionary &)
Merge two dictionaries.
Definition: Dictionary.cpp:155
FIX::Dictionary::setInt
void setInt(const std::string &, int)
Set a value from a int.
Definition: Dictionary.cpp:114
FieldConvertors.h
FIX::Dictionary::getDouble
double getDouble(const std::string &) const
Get a value as a double.
Definition: Dictionary.cpp:58
FIX::Dictionary::setDay
void setDay(const std::string &, int)
Set a value from a day.
Definition: Dictionary.cpp:129
FIX::Dictionary::has
bool has(const std::string &) const
Check if the dictionary contains a value for key.
Definition: Dictionary.cpp:150
FIX::IntConvertor::convert
static std::string convert(signed_int value)
Definition: FieldConvertors.h:154
FIX::Dictionary::setDouble
void setDouble(const std::string &, double)
Set a value from a double.
Definition: Dictionary.cpp:119
FIX
Definition: Acceptor.cpp:34
FIX::Dictionary::setString
void setString(const std::string &, const std::string &)
Set a value from a string.
Definition: Dictionary.cpp:109
FIX::Dictionary::m_data
Data m_data
Definition: Dictionary.h:88
FIX::string_toUpper
std::string string_toUpper(const std::string &value)
Definition: Utility.cpp:53
FIX::DoubleConvertor::convert
static std::string convert(double value, int padding=0)
Definition: FieldConvertors.h:401
FIX::ConfigError
Application is not configured correctly
Definition: Exceptions.h:87
FIX::string_strip
std::string string_strip(const std::string &value)
Definition: Utility.cpp:67
FIX::Dictionary::getDay
int getDay(const std::string &) const
Get a value as a day of week.
Definition: Dictionary.cpp:84
FIX::BoolConvertor::convert
static std::string convert(bool value)
Definition: FieldConvertors.h:533
FIX::Dictionary
For storage and retrieval of key/value pairs.
Definition: Dictionary.h:36
Dictionary.h
FIX::Dictionary::getString
std::string getString(const std::string &, bool capitalize=false) const
Get a value as a string.
Definition: Dictionary.cpp:32
FIX::Dictionary::getBool
bool getBool(const std::string &) const
Get a value as a bool.
Definition: Dictionary.cpp:71
FIX::Dictionary::getInt
int getInt(const std::string &) const
Get a value as a int.
Definition: Dictionary.cpp:45