OnixS C++ Eurex T7 Market and Reference Data Interface (EMDI, RDI, EOBI) Handlers  8.0.0
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  const std::string parser =
46  ".*"
47  "Interface version:"
48  "\\s*"
49  "(\\d+).(\\d+).(\\d+)"
50  ".*";
51 
52  UInt32 detectedMajorVersion = 0;
53  UInt32 detectedMinorVersion = 0;
54  UInt32 detectedBuildVersion = 0;
55 
56  try
57  {
58  const boost::regex regEx(parser);
59  boost::smatch smatch;
60  const bool ok = boost::regex_match(xml, smatch, regEx);
61 
62  if (!ok)
63  return false;
64 
65  BOOST_ASSERT(smatch.size() == 4);
66 
67  {
68  std::stringstream ss;
69  ss << smatch[1] << std::endl;
70  ss >> detectedMajorVersion;
71 
72  if (!ss.good())
73  return false;
74  }
75 
76  {
77  std::stringstream ss;
78  ss << smatch[2] << std::endl;
79  ss >> detectedMinorVersion;
80 
81  if (!ss.good())
82  return false;
83  }
84 
85  {
86  std::stringstream ss;
87  ss << smatch[3] << std::endl;
88  ss >> detectedBuildVersion;
89  }
90  }
91  catch (...)
92  {
93  return false;
94  }
95 
96  if (detectedMajorVersion != expectedXmlMajorVersion || detectedMinorVersion != expectedEmlMinorVersion)
97  {
98  std::stringstream ss;
99 
100  ss
101  << filename
102  << ": "
103  << "incompatible version. "
104  << "Expected: "
105  << expectedXmlMajorVersion << "." << expectedEmlMinorVersion
106  << ", provided: "
107  << detectedMajorVersion << "." << detectedMinorVersion;
108 
109  throw OperationException(BOOST_CURRENT_FUNCTION, ss.str().c_str());
110  }
111 
112  std::stringstream ss;
113  ss << detectedMajorVersion << "." << detectedMinorVersion << "." << detectedBuildVersion;
114 
115  detectedVersion = ss.str();
116 
117  return true;
118 }
119 
120 std::string removeInvalidTemplateNodes(std::string xml, const std::string& node)
121 {
122  const std::string nodeBegin("<template "); // for a quick fix assume space is separator
123  const std::string nodeEnd("</template>");
124  const std::string nameAttr("name=\""); // for a quick fix assume double quotes are used and no spaces
125 
126  size_t nodeBeginPos = 0;
127  size_t nodeEndPos = 0;
128 
129  while ((nodeBeginPos = xml.find(nodeBegin, nodeEndPos)) != std::string::npos)
130  {
131  if ((nodeEndPos = xml.find(nodeEnd, nodeBeginPos + nodeBegin.size())) == std::string::npos)
132  {
133  return xml; // invalid template, will throw on loading
134  }
135 
136  nodeEndPos += nodeEnd.size();
137 
138  const size_t nameAttrPos = xml.find(nameAttr, nodeBeginPos + nodeBegin.size());
139 
140  if ((nameAttrPos != std::string::npos) && (nameAttrPos < nodeEndPos))
141  {
142  const size_t nameBeginPos = nameAttrPos + nameAttr.size();
143  const size_t nameEndPos = xml.find('"', nameBeginPos); // for a quick fix assume double quotes are used
144 
145  if ((nameEndPos != std::string::npos) && (nameEndPos < nodeEndPos))
146  {
147  if (xml.substr(nameBeginPos, nameEndPos - nameBeginPos) == node)
148  {
149  return xml.erase(nodeBeginPos, nodeEndPos - nodeBeginPos);
150  }
151  }
152  }
153  }
154 
155  return xml;
156 }
157 
158 DataSource::Origin feedRole2Origin(OnixS::HandlerCore::MarketData::FE::NetFeedRole::Enum value)
159 {
160  switch (value)
161  {
162  case OnixS::HandlerCore::MarketData::FE::NetFeedRole::A: return DataSource::FeedA; break;
163  case OnixS::HandlerCore::MarketData::FE::NetFeedRole::B: return DataSource::FeedB; break;
164  default: return DataSource::Undefined; break;
165  }
166 }
167 
168 Timestamp TimeManager::convert(const OnixS::Time::DateTime& time)
169 {
170  const OnixS::Time::TimeSpan& sinceEpoch = time.sinceEpoch();
171 
172  return Timestamp(sinceEpoch.totalSeconds(), sinceEpoch.nanoseconds());
173 }
174 
175 OnixS::Time::DateTime TimeManager::convert(const Timestamp& time)
176 {
177  return OnixS::Time::DateTime(
178  OnixS::Time::TimeSpan(
179  time.sinceEpoch().totalSeconds(),
180  OnixS::Time::NanosecondSubseconds(time.sinceEpoch().nanoseconds())
181  ));
182 }
183 
184 Timestamp TimeManager::createTimestamp(long long totalSeconds, int subseconds)
185 {
186  return Timestamp(totalSeconds, subseconds);
187 }
188 
189 Time::TimeSpan TimeManager::toTimeSpan(HandlerCore::MarketData::FE::TimeSpan::Ticks ticks)
190 {
191  return
192  Time::TimeSpan(
193  ticks /
194  HandlerCore::MarketData::FE::TimeTraits::nanosecondsPerSecond(),
195  OnixS::Time::NanosecondSubseconds(ticks % HandlerCore::MarketData::FE::TimeTraits::nanosecondsPerSecond())
196  );
197 }
198 
199 Timestamp TimeManager::convert(const HandlerCore::MarketData::FE::Timestamp& value)
200 {
201  static const Time::DateTime theEpoch(1970, Time::Months::January, 1);
202 
203  Time::DateTime tmp = theEpoch + toTimeSpan(value.sinceEpoch());
204 
205  return Timestamp(tmp.sinceEpoch().totalSeconds(), tmp.sinceEpoch().nanoseconds());
206 }
207 
208 
209 }}}
210 
DataSource::Origin feedRole2Origin(OnixS::HandlerCore::MarketData::FE::NetFeedRole::Enum value)
Definition: Utils.cpp:158
static Timestamp createTimestamp(long long totalSeconds, int subseconds)
Definition: Utils.cpp:184
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:168
std::string removeInvalidTemplateNodes(std::string xml, const std::string &node)
Definition: Utils.cpp:120
static Time::TimeSpan toTimeSpan(HandlerCore::MarketData::FE::TimeSpan::Ticks ticks)
Definition: Utils.cpp:189