forked from quickfix/quickfix
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDictionary.cpp
More file actions
161 lines (145 loc) · 4.19 KB
/
Copy pathDictionary.cpp
File metadata and controls
161 lines (145 loc) · 4.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/****************************************************************************
** 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
EXCEPT ( 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
EXCEPT ( 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
EXCEPT ( 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
EXCEPT ( 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
EXCEPT ( ConfigError, FieldConvertError )
{
try
{
std::string value = getString(key);
if( value.size() < 2 ) throw FieldConvertError();
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;
}
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;
}
}