Dictionary.cpp
Go to the documentation of this file.
1/****************************************************************************
2** Copyright (c) 2001-2014
3**
4** This file is part of the QuickFIX FIX Engine
5**
6** This file may be distributed under the terms of the quickfixengine.org
7** license as defined by quickfixengine.org and appearing in the file
8** LICENSE included in the packaging of this file.
9**
10** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
11** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
12**
13** See http://www.quickfixengine.org/LICENSE for licensing information.
14**
15** Contact ask@quickfixengine.org if any conditions of this licensing are
16** not clear to you.
17**
18****************************************************************************/
19
20#ifdef _MSC_VER
21#include "stdafx.h"
22#else
23#include "config.h"
24#endif
25
26#include "Dictionary.h"
27#include "FieldConvertors.h"
28#include <algorithm>
29
30namespace FIX
31{
32std::string Dictionary::getString( const std::string& key, bool capitalize ) const
34{
35 Data::const_iterator i = m_data.find( string_toUpper(key) );
36 if ( i == m_data.end() ) throw ConfigError( key + " not defined" );
37
38 std::string result = i->second;
39 if( capitalize )
40 std::transform(result.begin(), result.end(), result.begin(), toupper);
41
42 return result;
43}
44
45int Dictionary::getInt( const std::string& key ) const
47{
48 try
49 {
50 return IntConvertor::convert( getString(key) );
51 }
52 catch ( FieldConvertError& )
53 {
54 throw ConfigError( "Illegal value " + getString(key) + " for " + key );
55 }
56}
57
58double Dictionary::getDouble( const std::string& key ) const
60{
61 try
62 {
63 return DoubleConvertor::convert( getString(key) );
64 }
65 catch ( FieldConvertError& )
66 {
67 throw ConfigError( "Illegal value " + getString(key) + " for " + key );
68 }
69}
70
71bool Dictionary::getBool( const std::string& key ) const
73{
74 try
75 {
76 return BoolConvertor::convert( getString(key) );
77 }
78 catch ( FieldConvertError& )
79 {
80 throw ConfigError( "Illegal value " + getString(key) + " for " + key );
81 }
82}
83
84int Dictionary::getDay( const std::string& key ) const
86{
87 try
88 {
89 std::string value = getString(key);
90 if( value.size() < 2 ) throw FieldConvertError();
91 std::string abbr = value.substr(0, 2);
92 std::transform( abbr.begin(), abbr.end(), abbr.begin(), tolower );
93 if( abbr == "su" ) return 1;
94 if( abbr == "mo" ) return 2;
95 if( abbr == "tu" ) return 3;
96 if( abbr == "we" ) return 4;
97 if( abbr == "th" ) return 5;
98 if( abbr == "fr" ) return 6;
99 if( abbr == "sa" ) return 7;
100 }
101 catch ( FieldConvertError& )
102 {
103 throw ConfigError( "Illegal value " + getString(key) + " for " + key );
104 }
105 return -1;
106}
107
108void Dictionary::setString( const std::string& key, const std::string& value )
109{
111}
112
113void Dictionary::setInt( const std::string& key, int value )
114{
116}
117
118void Dictionary::setDouble( const std::string& key, double value )
119{
121}
122
123void Dictionary::setBool( const std::string& key, bool value )
124{
126}
127
128void Dictionary::setDay( const std::string& key, int value )
129{
130 switch( value )
131 {
132 case 1:
133 setString( key, "SU" ); break;
134 case 2:
135 setString( key, "MO" ); break;
136 case 3:
137 setString( key, "TU" ); break;
138 case 4:
139 setString( key, "WE" ); break;
140 case 5:
141 setString( key, "TH" ); break;
142 case 6:
143 setString( key, "FR" ); break;
144 case 7:
145 setString( key, "SA" ); break;
146 }
147}
148
149bool Dictionary::has( const std::string& key ) const
150{
151 return m_data.find( string_toUpper(key) ) != m_data.end();
152}
153
154void Dictionary::merge( const Dictionary& toMerge )
155{
156 Data::const_iterator i = toMerge.m_data.begin();
157 for ( ; i != toMerge.m_data.end(); ++i )
158 if ( m_data.find( i->first ) == m_data.end() )
159 m_data[ i->first ] = i->second;
160}
161}
For storage and retrieval of key/value pairs.
Definition Dictionary.h:37
void merge(const Dictionary &)
Merge two dictionaries.
int getInt(const std::string &) const
Get a value as a int.
int getDay(const std::string &) const
Get a value as a day of week.
bool getBool(const std::string &) const
Get a value as a bool.
bool has(const std::string &) const
Check if the dictionary contains a value for key.
void setInt(const std::string &, int)
Set a value from a int.
std::string getString(const std::string &, bool capitalize=false) const
Get a value as a string.
void setBool(const std::string &, bool)
Set a value from a bool.
void setDay(const std::string &, int)
Set a value from a day.
double getDouble(const std::string &) const
Get a value as a double.
void setString(const std::string &, const std::string &)
Set a value from a string.
void setDouble(const std::string &, double)
Set a value from a double.
std::string string_strip(const std::string &value)
Definition Utility.cpp:67
std::string string_toUpper(const std::string &value)
Definition Utility.cpp:53
static std::string convert(bool value)
Application is not configured correctly
Definition Exceptions.h:88
static std::string convert(double value, int padding=0)
Unable to convert field into its native format.
Definition Exceptions.h:67
static std::string convert(signed_int value)

Generated on Sun Mar 31 2024 07:07:24 for QuickFIX by doxygen 1.9.8 written by Dimitri van Heesch, © 1997-2001