forked from schwehr/libais
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathais.cpp
91 lines (76 loc) · 3.51 KB
/
ais.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Since Apr 2010
// g++ ais_pos.cxx -o ais_pos -g -Wall -O3 -Wimplicit -W -Wredundant-decls -pedantic -funroll-loops -fexpensive-optimizations
#include "ais.h"
#include <iostream>
#include <bitset>
#include <string>
#include <cassert>
#include <cmath>
//using namespace std;
const std::string nth_field(const std::string &str, const size_t n, const char c) {
// FIX: handle the off the end case better
size_t pos;
size_t count;
for (pos=0, count=0; count < n and pos != std::string::npos; count+=1) {
if (pos>0) pos += 1; // Skip past the current char that matched
pos = str.find(c, pos);
}
if (std::string::npos == pos) return std::string("");
const size_t start = pos;
const size_t end = str.find(c, pos+1);
if (std::string::npos == end) return str.substr(start);
return str.substr(start+1, end-start-1);
}
// for decoding str bits inside of a binary message
const std::string bits_to_char_tbl="@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^- !\"#$%&`()*+,-./0123456789:;<=>?";
const char * const AIS_STATUS_STRINGS[AIS_STATUS_NUM_CODES] = {
"AIS_OK",
"AIS_ERR_BAD_BIT_COUNT",
"AIS_ERR_WRONG_MSG_TYPE",
"AIS_ERR_BAD_NMEA_CHR",
"AIS_ERR_BAD_PTR",
"AIS_ERR_UNKNOWN_MSG_TYPE",
"AIS_ERR_MSG_NOT_IMPLEMENTED",
"AIS_ERR_BAD_MSG_CONTENT",
"AIS_ERR_EXPECTED_STRING",
"AIS_ERR_MSG_TOO_LONG",
};
std::bitset<6> nmea_ord[128];
bool nmea_ord_initialized = false;
//#ifndef NDEBUG
#if 0
#definte VERIFY_ERROR_MESSAGES
#endif
#ifdef VERIFY_ERROR_MESSAGES
static void check_error_messages() {
std::cout << "AIS_OK: " << AIS_OK << " " << AIS_STATUS_STRINGS[AIS_OK] << std::endl;
std::cout << "AIS_ERR_BAD_BIT_COUNT: " << AIS_ERR_BAD_BIT_COUNT << " " << AIS_STATUS_STRINGS[AIS_ERR_BAD_BIT_COUNT] << std::endl;
std::cout << "AIS_ERR_WRONG_MSG_TYPE: " << AIS_ERR_WRONG_MSG_TYPE << " " << AIS_STATUS_STRINGS[AIS_ERR_WRONG_MSG_TYPE] << std::endl;
std::cout << "AIS_ERR_BAD_NMEA_CHR: " << AIS_ERR_BAD_NMEA_CHR << " " << AIS_STATUS_STRINGS[AIS_ERR_BAD_NMEA_CHR] << std::endl;
std::cout << "AIS_ERR_BAD_PTR: " << AIS_ERR_BAD_PTR << " " << AIS_STATUS_STRINGS[AIS_ERR_BAD_PTR] << std::endl;
std::cout << "AIS_ERR_UNKNOWN_MSG_TYPE: " << AIS_ERR_UNKNOWN_MSG_TYPE << " " << AIS_STATUS_STRINGS[AIS_ERR_UNKNOWN_MSG_TYPE] << std::endl;
std::cout << "AIS_ERR_MSG_NOT_IMPLEMENTED: " << AIS_ERR_MSG_NOT_IMPLEMENTED << " " << AIS_STATUS_STRINGS[AIS_ERR_MSG_NOT_IMPLEMENTED] << std::endl;
std::cout << "AIS_ERR_BAD_MSG_CONTENT: " << AIS_ERR_BAD_MSG_CONTENT << " " << AIS_STATUS_STRINGS[AIS_ERR_BAD_MSG_CONTENT] << std::endl;
std::cout << "AIS_ERR_EXPECTED_STRING: " << AIS_ERR_EXPECTED_STRING << " " << AIS_STATUS_STRINGS[AIS_ERR_EXPECTED_STRING] << std::endl;
std::cout << "AIS_ERR_MSG_TOO_LONG: " << AIS_ERR_MSG_TOO_LONG << " " << AIS_STATUS_STRINGS[AIS_ERR_MSG_TOO_LONG] << std::endl;
}
#endif
void build_nmea_lookup() {
//std::cout << "building lut..." << std::endl;
for (int c=0; c < 128; c++) {
int val = c - 48;
if (val>=40) val-= 8;
if (val < 0) continue;
std::bitset<6> bits(val);
bool tmp;
tmp = bits[5]; bits[5] = bits[0]; bits[0] = tmp;
tmp = bits[4]; bits[4] = bits[1]; bits[1] = tmp;
tmp = bits[3]; bits[3] = bits[2]; bits[2] = tmp;
nmea_ord[c] = bits;
}
nmea_ord_initialized = true;
#ifdef VERIFY_ERROR_MESSAGES
std::cout << "checking error messages..." << std::endl;
check_error_messages();
#endif
}