diff --git a/minmea.c b/minmea.c index f1fe3c8..3edee45 100644 --- a/minmea.c +++ b/minmea.c @@ -166,6 +166,11 @@ bool minmea_scan(const char *sentence, const char *format, ...) scale *= 10; } else if (*field == '.' && scale == 0) { scale = 1; + } else if (*field == ' ') { + /* Allow spaces at the start of the field. Not NMEA + * conformant, but some modules do this. */ + if (sign != 0 || value != -1 || scale != 0) + goto parse_error; } else { goto parse_error; } diff --git a/tests.c b/tests.c index 9bba3db..461a0c5 100644 --- a/tests.c +++ b/tests.c @@ -181,6 +181,18 @@ START_TEST(test_minmea_scan_f) ck_assert(minmea_scan("foo,12.3", "_;f", &f) == true); ck_assert_int_eq(f.value, 123); ck_assert_int_eq(f.scale, 10); + + /* accept spaces at the start of the field. some modules do this, unfortunately. */ + ck_assert(minmea_scan(" -1.23,V", "f", &f) == true); + ck_assert_int_eq(f.value, -123); + ck_assert_int_eq(f.scale, 100); + ck_assert(minmea_scan(" -4.56,V", "f", &f) == true); + ck_assert_int_eq(f.value, -456); + ck_assert_int_eq(f.scale, 100); + ck_assert(minmea_scan("-3.33 ,V", "f", &f) == false); + ck_assert(minmea_scan(" -3.33 ,V", "f", &f) == false); + ck_assert(minmea_scan("-3. 33,V", "f", &f) == false); + ck_assert(minmea_scan("0 .0,V", "f", &f) == false); } END_TEST