OnixS C++ Eurex T7 Market and Reference Data Interface (EMDI, RDI, EOBI) Handlers  7.4.2
API documentation
Utils.cpp
Go to the documentation of this file.
1 /*
2 * Copyright Onix Solutions Limited [OnixS]. All rights reserved.
3 *
4 * This software owned by Onix Solutions Limited [OnixS] and is protected by copyright law
5 * and international copyright treaties.
6 *
7 * Access to and use of the software is governed by the terms of the applicable OnixS Software
8 * Services Agreement (the Agreement) and Customer end user license agreements granting
9 * a non-assignable, non-transferable and non-exclusive license to use the software
10 * for it's own data processing purposes under the terms defined in the Agreement.
11 *
12 * Except as otherwise granted within the terms of the Agreement, copying or reproduction of any part
13 * of this source code or associated reference material to any other location for further reproduction
14 * or redistribution, and any amendments to this copyright notice, are expressly prohibited.
15 *
16 * Any reproduction or redistribution for sale or hiring of the Software not in accordance with
17 * the terms of the Agreement is a violation of copyright law.
18 */
19 #include <string>
20 #include <sstream>
21 
22 #include <boost/regex.hpp>
23 
24 #include <system/File.h>
25 
27 
28 #include "Utils.h"
29 #include "InternalDefines.h"
30 
31 namespace OnixS {
32 namespace Eurex {
33 namespace MarketData {
34 
35 using namespace Implementation;
36 
38  const std::string& filename,
39  std::string& detectedVersion,
40  UInt32 expectedXmlMajorVersion,
41  UInt32 expectedEmlMinorVersion)
42 {
43  const std::string xml = OnixS::System::File::readTextFile(filename);
44 
45  //! Interface version: 71.3.50.ga-71003060-83
46  const std::string parser =
47  ".*"
48  "Interface version:"
49  "\\s*"
50  "(\\d+).(\\d+).(\\d+)"
51  ".*";
52 
53  UInt32 detectedMajorVersion = 0;
54  UInt32 detectedMinorVersion = 0;
55  UInt32 detectedBuildVersion = 0;
56 
57  try
58  {
59  const boost::regex regEx(parser);
60  boost::smatch smatch;
61  const bool ok = boost::regex_match(xml, smatch, regEx);
62 
63  if (!ok)
64  return false;
65 
66  BOOST_ASSERT(smatch.size() == 4);
67 
68  {
69  std::stringstream ss;
70  ss << smatch[1] << std::endl;
71  ss >> detectedMajorVersion;
72 
73  if (!ss.good())
74  return false;
75  }
76 
77  {
78  std::stringstream ss;
79  ss << smatch[2] << std::endl;
80  ss >> detectedMinorVersion;
81 
82  if (!ss.good())
83  return false;
84  }
85 
86  {
87  std::stringstream ss;
88  ss << smatch[3] << std::endl;
89  ss >> detectedBuildVersion;
90  }
91  }
92  catch (...)
93  {
94  return false;
95  }
96 
97  if (detectedMajorVersion != expectedXmlMajorVersion || detectedMinorVersion != expectedEmlMinorVersion)
98  {
99  std::stringstream ss;
100 
101  ss
102  << filename
103  << ": "
104  << "incompatible version. "
105  << "Expected: "
106  << expectedXmlMajorVersion << "." << expectedEmlMinorVersion
107  << ", provided: "
108  << detectedMajorVersion << "." << detectedMinorVersion;
109 
110  throw OperationException(BOOST_CURRENT_FUNCTION, ss.str().c_str());
111  }
112 
113  std::stringstream ss;
114  ss << detectedMajorVersion << "." << detectedMinorVersion << "." << detectedBuildVersion;
115 
116  detectedVersion = ss.str();
117 
118  return true;
119 }
120 
121 std::string removeInvalidTemplateNodes(std::string xml, const std::string& node)
122 {
123  const std::string nodeBegin("<template "); // for a quick fix assume space is separator
124  const std::string nodeEnd("</template>");
125  const std::string nameAttr("name=\""); // for a quick fix assume double quotes are used and no spaces
126 
127  size_t nodeBeginPos = 0;
128  size_t nodeEndPos = 0;
129 
130  while ((nodeBeginPos = xml.find(nodeBegin, nodeEndPos)) != std::string::npos)
131  {
132  if ((nodeEndPos = xml.find(nodeEnd, nodeBeginPos + nodeBegin.size())) == std::string::npos)
133  {
134  return xml; // invalid template, will throw on loading
135  }
136 
137  nodeEndPos += nodeEnd.size();
138 
139  const size_t nameAttrPos = xml.find(nameAttr, nodeBeginPos + nodeBegin.size());
140 
141  if ((nameAttrPos != std::string::npos) && (nameAttrPos < nodeEndPos))
142  {
143  const size_t nameBeginPos = nameAttrPos + nameAttr.size();
144  const size_t nameEndPos = xml.find('"', nameBeginPos); // for a quick fix assume double quotes are used
145 
146  if ((nameEndPos != std::string::npos) && (nameEndPos < nodeEndPos))
147  {
148  if (xml.substr(nameBeginPos, nameEndPos - nameBeginPos) == node)
149  {
150  return xml.erase(nodeBeginPos, nodeEndPos - nodeBeginPos);
151  }
152  }
153  }
154  }
155 
156  return xml;
157 }
158 
159 DataSource::Origin feedRole2Origin(OnixS::HandlerCore::MarketData::FE::NetFeedRole::Enum value)
160 {
161  switch (value)
162  {
163  case OnixS::HandlerCore::MarketData::FE::NetFeedRole::A: return DataSource::FeedA; break;
164  case OnixS::HandlerCore::MarketData::FE::NetFeedRole::B: return DataSource::FeedB; break;
165  default: return DataSource::Undefined; break;
166  }
167 }
168 
169 Timestamp TimeManager::convert(const OnixS::Time::DateTime& time)
170 {
171  const OnixS::Time::TimeSpan& sinceEpoch = time.sinceEpoch();
172 
173  return Timestamp(sinceEpoch.totalSeconds(), sinceEpoch.nanoseconds());
174 }
175 
176 OnixS::Time::DateTime TimeManager::convert(const Timestamp& time)
177 {
178  return OnixS::Time::DateTime(
179  OnixS::Time::TimeSpan(
180  time.sinceEpoch().totalSeconds(),
181  OnixS::Time::NanosecondSubseconds(time.sinceEpoch().nanoseconds())
182  ));
183 }
184 
185 Timestamp TimeManager::createTimestamp(long long totalSeconds, int subseconds)
186 {
187  return Timestamp(totalSeconds, subseconds);
188 }
189 
190 Time::TimeSpan TimeManager::toTimeSpan(HandlerCore::MarketData::FE::TimeSpan::Ticks ticks)
191 {
192  return
193  Time::TimeSpan(
194  ticks /
195  HandlerCore::MarketData::FE::TimeTraits::nanosecondsPerSecond(),
196  OnixS::Time::NanosecondSubseconds(ticks % HandlerCore::MarketData::FE::TimeTraits::nanosecondsPerSecond())
197  );
198 }
199 
200 Timestamp TimeManager::convert(const HandlerCore::MarketData::FE::Timestamp& value)
201 {
202  static const Time::DateTime theEpoch(1970, Time::Months::January, 1);
203 
204  Time::DateTime tmp = theEpoch + toTimeSpan(value.sinceEpoch());
205 
206  return Timestamp(tmp.sinceEpoch().totalSeconds(), tmp.sinceEpoch().nanoseconds());
207 }
208 
209 
210 }}}
211 
DataSource::Origin feedRole2Origin(OnixS::HandlerCore::MarketData::FE::NetFeedRole::Enum value)
Definition: Utils.cpp:159
static Timestamp createTimestamp(long long totalSeconds, int subseconds)
Definition: Utils.cpp:185
unsigned int UInt32
Definition: Numeric.h:41
Definition: Defines.h:30
long long totalSeconds() const
Whole number of seconds in time interval.
Definition: TimeSpan.h:152
Represents timestamp without time-zone information.
Definition: Timestamp.h:80
const TimeSpan & sinceEpoch() const
Time span since Jan 1, 0001, 00:00:00.
Definition: Timestamp.h:225
bool checkXmlVersion(const std::string &filename, std::string &detectedVersion, UInt32 expectedXmlMajorVersion, UInt32 expectedEmlMinorVersion)
Definition: Utils.cpp:37
static Timestamp convert(const OnixS::Time::DateTime &time)
Definition: Utils.cpp:169
std::string removeInvalidTemplateNodes(std::string xml, const std::string &node)
Definition: Utils.cpp:121
static Time::TimeSpan toTimeSpan(HandlerCore::MarketData::FE::TimeSpan::Ticks ticks)
Definition: Utils.cpp:190