Ver Fonte

minmea_scan("f"): allow spaces at the start of the field

Kosma Moczek há 11 anos atrás
pai
commit
e81f908109
2 ficheiros alterados com 17 adições e 0 exclusões
  1. 5 0
      minmea.c
  2. 12 0
      tests.c

+ 5 - 0
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;
                         }

+ 12 - 0
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