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
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
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
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
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
86 {
87  try
88  {
89  std::string value = getString(key);
90  if( value.size() < 2 ) throw FieldConvertError(0);
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  if( value.size() < 2 ) throw FieldConvertError(0);
101  }
102  catch ( FieldConvertError& )
103  {
104  throw ConfigError( "Illegal value " + getString(key) + " for " + key );
105  }
106  return -1;
107 }
108 
109 void Dictionary::setString( const std::string& key, const std::string& value )
110 {
112 }
113 
114 void Dictionary::setInt( const std::string& key, int value )
115 {
117 }
118 
119 void Dictionary::setDouble( const std::string& key, double value )
120 {
122 }
123 
124 void Dictionary::setBool( const std::string& key, bool value )
125 {
127 }
128 
129 void Dictionary::setDay( const std::string& key, int value )
130 {
131  switch( value )
132  {
133  case 1:
134  setString( key, "SU" ); break;
135  case 2:
136  setString( key, "MO" ); break;
137  case 3:
138  setString( key, "TU" ); break;
139  case 4:
140  setString( key, "WE" ); break;
141  case 5:
142  setString( key, "TH" ); break;
143  case 6:
144  setString( key, "FR" ); break;
145  case 7:
146  setString( key, "SA" ); break;
147  }
148 }
149 
150 bool Dictionary::has( const std::string& key ) const
151 {
152  return m_data.find( string_toUpper(key) ) != m_data.end();
153 }
154 
155 void Dictionary::merge( const Dictionary& toMerge )
156 {
157  Data::const_iterator i = toMerge.m_data.begin();
158  for ( ; i != toMerge.m_data.end(); ++i )
159  if ( m_data.find( i->first ) == m_data.end() )
160  m_data[ i->first ] = i->second;
161 }
162 }
void setBool(const std::string &, bool)
Set a value from a bool.
Definition: Dictionary.cpp:124
Unable to convert field into its native format.
Definition: Exceptions.h:66
void merge(const Dictionary &)
Merge two dictionaries.
Definition: Dictionary.cpp:155
void setInt(const std::string &, int)
Set a value from a int.
Definition: Dictionary.cpp:114
void setDay(const std::string &, int)
Set a value from a day.
Definition: Dictionary.cpp:129
int getInt(const std::string &) const
Get a value as a int.
Definition: Dictionary.cpp:45
static std::string convert(signed_int value)
void setDouble(const std::string &, double)
Set a value from a double.
Definition: Dictionary.cpp:119
void setString(const std::string &, const std::string &)
Set a value from a string.
Definition: Dictionary.cpp:109
std::string string_toUpper(const std::string &value)
Definition: Utility.cpp:53
static std::string convert(double value, int padding=0)
Application is not configured correctly
Definition: Exceptions.h:87
std::string string_strip(const std::string &value)
Definition: Utility.cpp:67
bool has(const std::string &) const
Check if the dictionary contains a value for key.
Definition: Dictionary.cpp:150
std::string getString(const std::string &, bool capitalize=false) const
Get a value as a string.
Definition: Dictionary.cpp:32
bool getBool(const std::string &) const
Get a value as a bool.
Definition: Dictionary.cpp:71
static std::string convert(bool value)
For storage and retrieval of key/value pairs.
Definition: Dictionary.h:36
int getDay(const std::string &) const
Get a value as a day of week.
Definition: Dictionary.cpp:84
double getDouble(const std::string &) const
Get a value as a double.
Definition: Dictionary.cpp:58

Generated on Sat Mar 29 2014 15:13:31 for QuickFIX by doxygen 1.8.5 written by Dimitri van Heesch, © 1997-2001