OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimNmeaMessage.cpp
Go to the documentation of this file.
1 //----------------------------------------------------------------------------
2 //
3 // License: LGPL See top level LICENSE.txt file.
4 //
5 // File: ossimNmeaMessage.h
6 //
7 // Author: Garrett Potts
8 //
9 // Description: Contains a general parser for NMEA messages.
10 //
11 //
12 // $Id$
13 //----------------------------------------------------------------------------
15 #include <ossim/base/ossimCommon.h>
16 #include <iomanip>
17 
18 ossim_uint32 ossimNmeaMessage::checksum(std::string::const_iterator start, std::string::const_iterator end)
19 {
20  ossim_uint32 sum = 0;
21 
22  while(start!=end&&((*start)!='*'))
23  {
24  sum ^= ((*start)%128);
25  ++start;
26  }
27  return sum;
28 }
29 
30 void ossimNmeaMessage::setFields(std::string::const_iterator start, std::string::const_iterator end)
31 {
32  m_fields.clear();
33  ossim_uint32 idx = 0;
34  while(start != end)
35  {
36  m_fields.push_back("");
37 
38  while((start!=end)&&
39  (*start!=','))
40  {
41  m_fields[idx]+=*start;
42  ++start;
43  }
44 
45  if(start!=end)
46  {
47  ++start;
48  }
49  ++idx;
50  }
51 }
52 
54 {
55  std::string::const_iterator iter = std::find(m_startChars.begin(), m_startChars.end(), c);
56  return (iter != m_startChars.end());
57 }
58 
59 
61 {
62  ossim::skipws(in);
63  m_validCheckSum = false;
64  m_message = "";
65  if(!isValidStartChar(static_cast<char>(in.peek())))
66  {
67  throw ossimException(ossimString("Starting NMEA message indicator not found, expected one of ") +
68  m_startChars + " but found " +
69  ossimString((char)in.peek()));
70  }
71 
72  char c = static_cast<char>(in.get());
73  while(((c!='\n')&&(c!='\r'))&&
74  !in.eof()&&!in.bad())
75  {
76  m_message += c;
77  c = static_cast<char>(in.get());
78  }
79  std::string::iterator iter = std::find(m_message.begin(), m_message.end(), '*');
80 
81  if(iter != m_message.end())
82  {
83  setFields(m_message.begin()+1, m_message.end());
84  ossim_uint32 check = checksum(m_message.begin()+1, iter);
86  out << std::setw(2) << std::setfill('0') << std::hex << check;
87  std::string::iterator endChecksumIter = iter+1;
88  while((endChecksumIter!= m_message.end())&&(*endChecksumIter!=',')) ++endChecksumIter;
89  if(out.str() == ossimString(iter+1, endChecksumIter).downcase())
90  {
91  m_validCheckSum = true;
92  }
93  }
94  else
95  {
96  throw ossimException("Terminating * indicator for cbecksum not found in NMEA message format");
97  }
98 }
static ossim_uint32 checksum(std::string::const_iterator start, std::string::const_iterator end)
bool isValidStartChar(char c) const
std::basic_ostringstream< char > ostringstream
Class for char output memory streams.
Definition: ossimIosFwd.h:35
virtual void parseMessage(std::istream &in)
Parses a standard formatted NMEA message.
OSSIM_DLL std::istream & skipws(std::istream &in)
Definition: ossimCommon.cpp:38
unsigned int ossim_uint32
static ossimString downcase(const ossimString &aString)
Definition: ossimString.cpp:48
std::string m_startChars
std::basic_istream< char > istream
Base class for char input streams.
Definition: ossimIosFwd.h:20
virtual void setFields(std::string::const_iterator start, std::string::const_iterator end)
FieldListType m_fields