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