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 
30 namespace FIX
31 {
32 std::string Dictionary::getString( const std::string& key, bool capitalize ) const
33 throw( ConfigError, FieldConvertError )
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 
45 int Dictionary::getInt( const std::string& key ) const
46 throw( ConfigError, FieldConvertError )
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 
58 double Dictionary::getDouble( const std::string& key ) const
59 throw( ConfigError, FieldConvertError )
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 
71 bool Dictionary::getBool( const std::string& key ) const
72 throw( ConfigError, FieldConvertError )
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 
84 int Dictionary::getDay( const std::string& key ) const
85 throw( ConfigError, FieldConvertError )
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 
108 void Dictionary::setString( const std::string& key, const std::string& value )
109 {
111 }
112 
113 void Dictionary::setInt( const std::string& key, int value )
114 {
116 }
117 
118 void Dictionary::setDouble( const std::string& key, double value )
119 {
121 }
122 
123 void Dictionary::setBool( const std::string& key, bool value )
124 {
126 }
127 
128 void 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 
149 bool Dictionary::has( const std::string& key ) const
150 {
151  return m_data.find( string_toUpper(key) ) != m_data.end();
152 }
153 
154 void 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 }
FIX::string_toUpper
std::string string_toUpper(const std::string &value)
Definition: Utility.cpp:70
FIX::Dictionary::getString
std::string getString(const std::string &, bool capitalize=false) const
Get a value as a string.
Definition: Dictionary.cpp:49
FIX::Dictionary::getInt
int getInt(const std::string &) const
Get a value as a int.
Definition: Dictionary.cpp:62
FIX::Dictionary::m_data
Data m_data
Definition: Dictionary.h:122
FIX::string_strip
std::string string_strip(const std::string &value)
Definition: Utility.cpp:84
FIX::Dictionary::setDouble
void setDouble(const std::string &, double)
Set a value from a double.
Definition: Dictionary.cpp:135
FIX::ConfigError
Application is not configured correctly
Definition: Exceptions.h:104
FIX::FieldConvertError
Unable to convert field into its native format.
Definition: Exceptions.h:83
FIX::Dictionary::setDay
void setDay(const std::string &, int)
Set a value from a day.
Definition: Dictionary.cpp:145
FIX::IntConvertor::convert
static std::string convert(signed_int value)
Definition: FieldConvertors.h:170
FIX::Dictionary::getDouble
double getDouble(const std::string &) const
Get a value as a double.
Definition: Dictionary.cpp:75
FieldConvertors.h
FIX::Dictionary::has
bool has(const std::string &) const
Check if the dictionary contains a value for key.
Definition: Dictionary.cpp:166
FIX::Dictionary::getDay
int getDay(const std::string &) const
Get a value as a day of week.
Definition: Dictionary.cpp:101
FIX::Dictionary::getBool
bool getBool(const std::string &) const
Get a value as a bool.
Definition: Dictionary.cpp:88
FIX::Dictionary::merge
void merge(const Dictionary &)
Merge two dictionaries.
Definition: Dictionary.cpp:171
FIX::BoolConvertor::convert
static std::string convert(bool value)
Definition: FieldConvertors.h:418
FIX
Definition: Acceptor.cpp:34
Dictionary.h
FIX::Dictionary::setString
void setString(const std::string &, const std::string &)
Set a value from a string.
Definition: Dictionary.cpp:125
FIX::Dictionary::setBool
void setBool(const std::string &, bool)
Set a value from a bool.
Definition: Dictionary.cpp:140
FIX::DoubleConvertor::convert
static std::string convert(double value, int padding=0)
Definition: FieldConvertors.h:272
FIX::Dictionary::setInt
void setInt(const std::string &, int)
Set a value from a int.
Definition: Dictionary.cpp:130

Generated on Wed Apr 29 2020 19:41:30 for QuickFIX by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2001