-
Notifications
You must be signed in to change notification settings - Fork 21
/
ezcod.C
134 lines (122 loc) · 3.68 KB
/
ezcod.C
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <iostream>
#include <utility>
#include <ezpwd/ezcod> // C++ implementation
#if defined( DEBUG )
# include <ezpwd/output>
#endif // DEBUG
#include <ezpwd/definitions> // must be included in one C++ compilation unit
#include "ezcod.h" // C API declarations
//
// Encode lat/lon with default worst-case 3m. accuracy: 1 part in 2^22 Latitude, 2^23 Longitude.
//
template < size_t P=1 >
int ezcod_3_encode(
double lat,
double lon,
char *enc,
size_t siz,
size_t pre = 0 ) // precision (0 --> default 9)
{
#if defined( DEBUG ) && DEBUG > 1
std::cout
<< "ezcod_3_encode<" << P << ">("
<< " lat == " << lat
<< ", lon == " << lon
<< ", w/ " << pre << " symbols precision"
#if DEBUG > 2
<< ", buf[" << siz << "] == " << std::vector<uint8_t>( (uint8_t*)enc, (uint8_t*)enc + siz )
#endif
<< std::endl;
#endif
int res;
try {
const std::string &str( ezpwd::ezcod<P>( lat, lon ).encode( pre ));
if ( str.size() + 1 > siz )
throw std::runtime_error( "insufficient buffer provided" );
std::copy( str.begin(), str.end(), enc );
enc[str.size()] = 0;
res = str.size();
} catch ( std::exception &exc ) {
ezpwd::streambuf_to_buffer sbf( enc, siz );
std::ostream( &sbf )
<< "ezcod_3_encode<" << P << ">(" << lat << ", " << lon << ") failed: "
<< exc.what();
res = -1;
}
return res;
}
//
// Decode lat/lon position in degrees and (optionally) accuracy in m, returning confidence in %.
//
template < size_t P=1 >
int ezcod_3_decode(
char *dec,
size_t siz,
double *lat = 0,
double *lon = 0,
double *acc = 0 )
{
int confidence;
try {
ezpwd::ezcod<P> decoder( dec );
confidence = decoder.confidence;
#if defined( DEBUG ) && DEBUG > 1
std::cout
<< "ezcod_3_decode<" << P << ">(\"" << dec
<< "\") == " << confidence << "% confidence: "
<< " lat (" << (void *)lat << ") == " << decoder.latitude
<< ", lon (" << (void *)lon << ") == " << decoder.longitude
<< ", acc (" << (void *)acc << ") == " << decoder.accuracy
<< std::endl;
#endif
if ( lat )
*lat = decoder.latitude;
if ( lon )
*lon = decoder.longitude;
if ( acc )
*acc = decoder.accuracy;
} catch ( std::exception &exc ) {
std::string location( dec );
ezpwd::streambuf_to_buffer sbf( dec, siz );
std::ostream( &sbf )
<< "ezcod_3_decode<" << P << ">(\"" << location << "\") failed: "
<< exc.what();
confidence = -1;
}
return confidence;
}
extern "C" {
/* ezcod 3:10 -- 9+1 Reed-Solomon parity symbol */
int ezcod_3_10_encode( double lat, double lon, char *enc, size_t siz, size_t pre )
{
return ezcod_3_encode<1>( lat, lon, enc, siz, pre );
}
int ezcod_3_10_decode( char *dec, size_t siz, double *lat, double *lon, double *acc )
{
return ezcod_3_decode<1>( dec, siz, lat, lon, acc );
}
/* ezcod 3:11 -- 9+2 Reed-Solomon parity symbols */
int ezcod_3_11_encode( double lat, double lon, char *enc, size_t siz, size_t pre )
{
return ezcod_3_encode<2>( lat, lon, enc, siz, pre );
}
int ezcod_3_11_decode( char *dec, size_t siz, double *lat, double *lon, double *acc )
{
return ezcod_3_decode<2>( dec, siz, lat, lon, acc );
}
/* ezcod 3:12 -- 9+3 Reed-Solomon parity symbols */
int ezcod_3_12_encode( double lat, double lon, char *enc, size_t siz, size_t pre )
{
return ezcod_3_encode<3>( lat, lon, enc, siz, pre );
}
int ezcod_3_12_decode( char *dec, size_t siz, double *lat, double *lon, double *acc )
{
return ezcod_3_decode<3>( dec, siz, lat, lon, acc );
}
} // extern "C"
#if defined( __cheerp )
int webMain()
{
return 0;
}
#endif