mmlib/mmat: include own int parser implementation for embedded
authorMatthew Mondor <mmondor@pulsar-zone.net>
Mon, 20 May 2019 02:21:04 +0000 (02:21 +0000)
committerMatthew Mondor <mmondor@pulsar-zone.net>
Mon, 20 May 2019 02:21:04 +0000 (02:21 +0000)
mmsoftware/mmlib/mmat.c

index 107a0c4..ef6580f 100644 (file)
@@ -239,6 +239,8 @@ next:
        }
 }
 
+#ifdef USE_STRTOUL
+
 static int
 at_parse_uint(at_ctx_t *ctx, const char **cptr, bool rewind)
 {
@@ -262,6 +264,41 @@ at_parse_uint(at_ctx_t *ctx, const char **cptr, bool rewind)
        return r;
 }
 
+#else
+
+/* Custom smaller implementation for embedded */
+static int
+at_parse_uint(at_ctx_t *ctx, const char **cptr, bool rewind)
+{
+       int o = INT_MAX / 10, l = INT_MAX % 10, r = 0;
+       const char *ptr = *cptr;
+       uint_least8_t d;
+       char c;
+
+       for (; *ptr != '\0'; ptr++) {
+               c = *ptr;
+               if (c < '0' || c > '9') {
+                       if (ptr == *cptr)
+                               r = -1;
+                       break;
+               }
+               d = c - '0';
+               if (r > o || (r == o && d > l)) {
+                       r = INT_MAX;
+                       break;
+               }
+               r *= 10;
+               r += d;
+       }
+       if (rewind && ptr >= ctx->buf)
+               ptr--;
+       *cptr = ptr;
+
+       return r;
+}
+
+#endif
+
 
 #ifdef TEST