dsp3000_parser.cpp
Go to the documentation of this file.
1 // Copyright Autonomous Solutions Inc. 2016
2 
3 #include "kvh/dsp3000_parser.h"
4 #include <cstdlib>
5 
6 static bool is_important_symbol(char const c);
7 static int find_value_index(const char *buffer, const int buffer_size);
8 static int trim_buffer_up_to_rn(char *buffer, int buffer_size, int starting_index = 0);
9 static int find_mode_index(const char *buffer, const int buffer_size, int value_index);
10 
11 bool is_important_symbol(char const c)
12 {
13  return c != ' ' && c != '\r' && c != '\n';
14 }
15 
16 int find_value_index(const char *buffer, const int buffer_size)
17 {
18  int value_index = -1;
19  bool space_found = false;
20  bool found_important_symbol = false;
21  for (int i = 0; i < buffer_size && !found_important_symbol; ++i)
22  {
23  if (' ' == buffer[i])
24  {
25  space_found = true;
26  }
27  else if (buffer[i] != '\r' && buffer[i] != '\n')
28  {
29  found_important_symbol = true;
30  if (space_found)
31  {
32  value_index = i;
33  }
34  }
35  }
36  return value_index;
37 }
38 
39 int trim_buffer_up_to_rn(char *const buffer, int const buffer_size, int const starting_index)
40 {
41  int rn_index = -1;
42  for (int i = starting_index; i < buffer_size && -1 == rn_index; ++i)
43  {
44  if ('\r' == buffer[i] && i + 1 < buffer_size && '\n' == buffer[i + 1])
45  {
46  rn_index = i + 1;
47  }
48  }
49 
50  int new_buffer_size;
51  if (rn_index != -1)
52  {
53  new_buffer_size = buffer_size - rn_index - 1;
54  for (int i = 0; i < new_buffer_size; ++i)
55  {
56  buffer[i] = buffer[i + rn_index + 1];
57  }
58  }
59  else
60  {
61  new_buffer_size = buffer_size;
62  }
63 
64  return new_buffer_size;
65 }
66 
67 int find_mode_index(char const *const buffer, int const buffer_size, int const value_index)
68 {
69  int mode_index = value_index;
70  while (mode_index < buffer_size && is_important_symbol(buffer[mode_index]))
71  {
72  ++mode_index;
73  }
74  while (mode_index < buffer_size && buffer[mode_index] == ' ')
75  {
76  ++mode_index;
77  }
78  return mode_index;
79 }
80 
81 ParseDsp3000Data parse_dsp3000(char *const buffer, int const buffer_size)
82 {
83  bool parser_success = false;
84  float value = 0.0;
85  bool is_data_valid = false;
86 
87  int const value_index = find_value_index(buffer, buffer_size);
88 
89  if (-1 != value_index)
90  {
91  value = static_cast<float>(atof(&buffer[value_index]));
92 
93  int mode_index = find_mode_index(buffer, buffer_size, value_index);
94 
95  if (mode_index < buffer_size)
96  {
97  is_data_valid = '1' == buffer[mode_index];
98  parser_success = '1' == buffer[mode_index] || '0' == buffer[mode_index];
99  }
100  }
101  int const new_buffer_size = trim_buffer_up_to_rn(buffer, buffer_size);
102 
103  return ParseDsp3000Data(parser_success, new_buffer_size, value, is_data_valid);
104 }
static bool is_important_symbol(char const c)
static int find_value_index(const char *buffer, const int buffer_size)
static int find_mode_index(const char *buffer, const int buffer_size, int value_index)
Copyright Autonomous Solutions Inc. 2016.
Definition: dsp3000_parser.h:8
ParseDsp3000Data parse_dsp3000(char *const buffer, int const buffer_size)
static int trim_buffer_up_to_rn(char *buffer, int buffer_size, int starting_index=0)


kvh_drivers
Author(s): Jeff Schmidt, Geoffrey Viola
autogenerated on Mon Jun 10 2019 13:45:24