CanFileResolutionHelper.cpp
Go to the documentation of this file.
1 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
2 
3 // -- BEGIN LICENSE BLOCK ----------------------------------------------
4 // This file is part of FZIs ic_workspace.
5 //
6 // This program is free software licensed under the LGPL
7 // (GNU LESSER GENERAL PUBLIC LICENSE Version 3).
8 // You can find a copy of this license in LICENSE folder in the top
9 // directory of the source code.
10 //
11 // © Copyright 2016 FZI Forschungszentrum Informatik, Karlsruhe, Germany
12 //
13 // -- END LICENSE BLOCK ------------------------------------------------
14 
15 //----------------------------------------------------------------------
22 //----------------------------------------------------------------------
24 
25 #include <icl_core/BaseTypes.h>
26 #include <icl_math/MathFunctions.h>
27 
28 namespace icl_hardware {
29 namespace can {
30 
32  : m_filename(filename),
33  m_file(m_filename.c_str())
34 {
35  m_file.imbue(std::locale("C"));
36 
37  // Find the first valid line.
38  std::string line;
39  std::ifstream::pos_type current_pos = m_file.tellg();
40  while (m_file && (line.empty() || line[0] == '#'))
41  {
42  current_pos = m_file.tellg();
43  std::getline(m_file, line);
44  }
45  if (tryParse(line, current_pos))
46  {
47  m_file.seekg(current_pos);
48  m_current_position.dsin = icl_sourcesink::DSIN(current_pos);
49  }
50  else
51  {
52  // File is broken somehow, or end of file was reached. Make sure
53  // we go to the end in any case, so that subsequent good() calls
54  // return false as well.
55  m_file.seekg(0, std::ios_base::end);
56  }
57 }
58 
59 boost::posix_time::ptime CanFileResolutionHelper::timestampToPtime(double stamp)
60 {
61  unsigned long stamp_usec(icl_math::round((stamp - icl_math::floor(stamp)) * 1e6));
62  return boost::posix_time::ptime(boost::gregorian::date(1970,1,1),
63  boost::posix_time::seconds(static_cast<unsigned long>(stamp))
64  + boost::posix_time::microsec(stamp_usec));
65 }
66 
68 {
69  // We're on a valid line to begin with. Skip beyond that, then find
70  // the next valid line.
71  std::string line;
72  std::getline(m_file, line);
73  line = "";
74  std::ifstream::pos_type current_pos = m_file.tellg();
75  while (m_file && (line.empty() || line[0] == '#'))
76  {
77  current_pos = m_file.tellg();
78  std::getline(m_file, line);
79  }
80  if (tryParse(line, current_pos))
81  {
82  m_file.seekg(current_pos);
83  return true;
84  }
85  else
86  {
87  // File is broken somehow, or end of file was reached. Make sure
88  // we go to the end in any case, so that subsequent good() calls
89  // return false as well.
90  m_file.seekg(0, std::ios_base::end);
91  return false;
92  }
93 }
94 
95 bool CanFileResolutionHelper::tryParse(const std::string& line, uint64_t current_pos)
96 {
97  // FZI-CAN FILE V1.0
98  m_current_position.index = icl_sourcesink::Index(icl_sourcesink::Position::INVALID_INDEX);
99  m_current_position.timestamp = boost::posix_time::not_a_date_time;
100  m_current_position.dsin = icl_sourcesink::DSIN(icl_sourcesink::Position::INVALID_DSIN);
101  std::istringstream ss(line);
102  ss.imbue(std::locale("C"));
103  double gmtime;
104  unsigned id, dlc, msg0, msg1, msg2, msg3, msg4, msg5, msg6, msg7;
105  try
106  {
107  if (ss >> std::dec >> gmtime >> id >> dlc
108  >> std::hex >> msg0 >> msg1 >> msg2 >> msg3 >> msg4 >> msg5 >> msg6 >> msg7)
109  {
110  m_current_position.index = icl_sourcesink::Index(icl_sourcesink::Position::INVALID_INDEX);
111  m_current_position.timestamp = timestampToPtime(gmtime);
112  m_current_position.dsin = icl_sourcesink::DSIN(current_pos);
113  m_current_message.reset(
114  new CanMessageStamped(
115  tCanMessage(id, dlc),
116  icl_core::DataHeader("", m_current_position.timestamp)));
117  m_current_message->header().dsin = m_current_position.dsin;
118  (*m_current_message)->data[0] = msg0;
119  (*m_current_message)->data[1] = msg1;
120  (*m_current_message)->data[2] = msg2;
121  (*m_current_message)->data[3] = msg3;
122  (*m_current_message)->data[4] = msg4;
123  (*m_current_message)->data[5] = msg5;
124  (*m_current_message)->data[6] = msg6;
125  (*m_current_message)->data[7] = msg7;
126  return true;
127  }
128  }
129  catch (...)
130  {
131  return false;
132  }
133  return bool(ss);
134 }
135 
137 {
138  std::ifstream::pos_type current_pos = m_file.tellg();
139  std::string line;
140  std::getline(m_file, line);
141  if (tryParse(line, current_pos))
142  {
143  m_file.seekg(current_pos);
144  m_current_position.dsin = icl_sourcesink::DSIN(current_pos);
145  return true;
146  }
147  else
148  {
149  // File is broken somehow, or end of file was reached. Make sure
150  // we go to the end in any case, so that subsequent good() calls
151  // return false as well.
152  m_file.seekg(0, std::ios_base::end);
153  return false;
154  }
155 }
156 
157 }
158 }
static boost::posix_time::ptime timestampToPtime(double stamp)
unsigned __int64 uint64_t
icl_core::Stamped< tCanMessage > CanMessageStamped
Definition: tCanMessage.h:79
icl_hardware::can::tCanMessage tCanMessage
Definition: UseMCACan.h:39
bool tryParse(const std::string &line, uint64_t current_pos)


fzi_icl_can
Author(s):
autogenerated on Mon Jun 10 2019 13:17:02