editable serial input line buffer

Dependents:   MAX5715BOB_Tester MAX11131BOB_Tester MAX5171BOB_Tester MAX11410BOB_Tester ... more

Revision:
9:12e37800ecdd
Parent:
7:0bda7cfee767
Child:
10:3e2ff983be1c
--- a/CmdLine.cpp	Fri Sep 27 21:14:25 2019 -0700
+++ b/CmdLine.cpp	Mon Nov 11 23:27:59 2019 +0000
@@ -312,6 +312,7 @@
  * @param[in] valueBufLen limits the size of valueBuf
  *
  * @post on successful match, the key=value substring is deleted from cmdbuf
+ * @post chSeparator is populated with the separator character '=' or '?' following key=, or null character if no separator
  *
  */
 bool CmdLine::parse_and_remove_key(const char *key, char *valueBuf, size_t valueBufLen)
@@ -327,30 +328,32 @@
         if (buf[idxSearch] != key[0]) { continue; }
         // possible match; compare buf[idxSearch..] to key[0..]
         unsigned int idxKey = idxSearch; // test whether buf[idxKey..] == key[0..]
-        unsigned int idxEqualSign = idxSearch; // test whether key=value pair
+        unsigned int idxSeparator = idxSearch; // test whether key=value pair
         unsigned int idxSpace = idxSearch; // end of key=value word
         for (unsigned int offset = 0; offset < strlen(key); offset++)
         {
             if (buf[idxKey + offset] != key[offset]) { idxKey = 0; break; }
-            idxSpace = idxKey + offset + 1;
-            idxEqualSign = idxKey + offset + 1;
-            if (buf[idxEqualSign] != '=') { idxEqualSign = 0; }
+            idxSpace = idxKey + offset + 1;  // assume next char is a word break
+            idxSeparator = idxKey + offset + 1; // assume next char is a separator
+            if ((buf[idxSeparator] != '=') && (buf[idxSeparator] != '?')) { idxSeparator = 0; }
         }
         if (idxKey == 0) continue; // no match at idxSearch but keep searching
         // ASSERT buf[idxKey..] == key[0..]
         while ((buf[idxSpace] != ' ') && idxSpace < indexOfNextEmptyCell) { idxSpace++; }
         // serial().printf("\r\n parse_and_remove_key(\"%s\") match at index %d length %d, '=' index %d, ' ' index %d",
-        //                 key, idxKey, strlen(key), idxEqualSign, idxSpace);
-        if (idxEqualSign != 0) {
-            // found key=value: copy buf[idxEqualSign+1..' '-1] into valueBuf[0..valueBufLen-1]
+        //                 key, idxKey, strlen(key), idxSeparator, idxSpace);
+        if (idxSeparator != 0) {
+            // found key=value: copy buf[idxSeparator+1..' '-1] into valueBuf[0..valueBufLen-1]
+            chSeparator = buf[idxSeparator];
             for (unsigned int offset = 0; offset < valueBufLen - 1; offset++)
             {
-                if (buf[idxEqualSign + 1 + offset] == ' ') break;
-                valueBuf[offset] = buf[idxEqualSign + 1 + offset];
+                if (buf[idxSeparator + 1 + offset] == ' ') break;
+                valueBuf[offset] = buf[idxSeparator + 1 + offset];
                 valueBuf[offset + 1] = '\0';
             }
         } else {
             // found key but no =value: valueBuf[] = ""
+            chSeparator = '\0';
             valueBuf[0] = '\0';
         }
         // on successful match, the key=value should be deleted from cmdbuf
@@ -691,6 +694,150 @@
     return false; // no match
 }
 
+/** CmdLine::parse_uint32_hex matches "key"=value
+ *
+ * @return true if keyword was found in buffer
+ * @param[in] key string value to match
+ * @param[out] uint32Var updated from value string if match "key"=value
+ *
+ * @post on successful match, the key=value substring is deleted from cmdbuf
+ *
+ * Prefix '$' or '0x' or '0X' selects radix base-16 hexadecimal.
+ * Default number conversion radix is base-16 hexadecimal.
+ */
+bool CmdLine::parse_uint32_hex(const char *key, uint32_t& uint32Var)
+{
+    char valueBuf[16];
+    // bool parse_and_remove_key(const char *key, char *valueBuf, int valueBufLen);
+    if (parse_and_remove_key(key, valueBuf, sizeof(valueBuf)))
+    {
+        // ASSERT: buf[matched_index] contains '=' followed by value
+        // parse cmdLine arg (CMD=\d)? --> g_I2C_command_regAddress
+        // TODO1: parse_byte_hex take hex prefix 0x 0X or suffix $ h H
+        if (valueBuf[0] == '$')
+        {
+            uint32Var = strtoul(valueBuf + 1, NULL, 16);
+            return true;
+        }
+        if (valueBuf[0] == '0' && (valueBuf[1] == 'X' || valueBuf[1] == 'x'))
+        {
+            uint32Var = strtoul(valueBuf + 2, NULL, 16);
+            return true;
+        }
+        uint32Var = strtoul(valueBuf, NULL, 16); // default radix hex
+        return true;
+    }
+    return false; // no match
+}
+
+/** CmdLine::parse_uint32_dec matches "key"=value
+ *
+ * @return true if keyword was found in buffer
+ * @param[in] key string value to match
+ * @param[out] uint32Var updated from value string if match "key"=value
+ *
+ * @post on successful match, the key=value substring is deleted from cmdbuf
+ *
+ * Prefix '$' or '0x' or '0X' selects radix base-16 hexadecimal.
+ * Default number conversion radix is base-10 decimal.
+ */
+bool CmdLine::parse_uint32_dec(const char *key, uint32_t& uint32Var)
+{
+    char valueBuf[16];
+    // bool parse_and_remove_key(const char *key, char *valueBuf, int valueBufLen);
+    if (parse_and_remove_key(key, valueBuf, sizeof(valueBuf)))
+    {
+        // ASSERT: buf[matched_index] contains '=' followed by value
+        // parse cmdLine arg (CMD=\d)? --> g_I2C_command_regAddress
+        // TODO1: parse_byte_hex take hex prefix 0x 0X or suffix $ h H
+        if (valueBuf[0] == '$')
+        {
+            uint32Var = strtoul(valueBuf + 1, NULL, 16);
+            return true;
+        }
+        if (valueBuf[0] == '0' && (valueBuf[1] == 'X' || valueBuf[1] == 'x'))
+        {
+            uint32Var = strtoul(valueBuf + 2, NULL, 16);
+            return true;
+        }
+        uint32Var = strtoul(valueBuf, NULL, 10); // default radix decimal
+        return true;
+    }
+    return false; // no match
+}
+
+/** CmdLine::parse_int32_hex matches "key"=value
+ *
+ * @return true if keyword was found in buffer
+ * @param[in] key string value to match
+ * @param[out] int32Var updated from value string if match "key"=value
+ *
+ * @post on successful match, the key=value substring is deleted from cmdbuf
+ *
+ * Prefix '$' or '0x' or '0X' selects radix base-16 hexadecimal.
+ * Default number conversion radix is base-16 hexadecimal.
+ */
+bool CmdLine::parse_int32_hex(const char *key, int32_t& int32Var)
+{
+    char valueBuf[16];
+    // bool parse_and_remove_key(const char *key, char *valueBuf, int valueBufLen);
+    if (parse_and_remove_key(key, valueBuf, sizeof(valueBuf)))
+    {
+        // ASSERT: buf[matched_index] contains '=' followed by value
+        // parse cmdLine arg (CMD=\d)? --> g_I2C_command_regAddress
+        // TODO1: parse_byte_hex take hex prefix 0x 0X or suffix $ h H
+        if (valueBuf[0] == '$')
+        {
+            int32Var = strtoul(valueBuf + 1, NULL, 16);
+            return true;
+        }
+        if (valueBuf[0] == '0' && (valueBuf[1] == 'X' || valueBuf[1] == 'x'))
+        {
+            int32Var = strtoul(valueBuf + 2, NULL, 16);
+            return true;
+        }
+        int32Var = strtoul(valueBuf, NULL, 16); // default radix hex
+        return true;
+    }
+    return false; // no match
+}
+
+/** CmdLine::parse_int32_dec matches "key"=value
+ *
+ * @return true if keyword was found in buffer
+ * @param[in] key string value to match
+ * @param[out] int32Var updated from value string if match "key"=value
+ *
+ * @post on successful match, the key=value substring is deleted from cmdbuf
+ *
+ * Prefix '$' or '0x' or '0X' selects radix base-16 hexadecimal.
+ * Default number conversion radix is base-10 decimal.
+ */
+bool CmdLine::parse_int32_dec(const char *key, int32_t& int32Var)
+{
+    char valueBuf[16];
+    // bool parse_and_remove_key(const char *key, char *valueBuf, int valueBufLen);
+    if (parse_and_remove_key(key, valueBuf, sizeof(valueBuf)))
+    {
+        // ASSERT: buf[matched_index] contains '=' followed by value
+        // parse cmdLine arg (CMD=\d)? --> g_I2C_command_regAddress
+        // TODO1: parse_byte_hex take hex prefix 0x 0X or suffix $ h H
+        if (valueBuf[0] == '$')
+        {
+            int32Var = strtoul(valueBuf + 1, NULL, 16);
+            return true;
+        }
+        if (valueBuf[0] == '0' && (valueBuf[1] == 'X' || valueBuf[1] == 'x'))
+        {
+            int32Var = strtoul(valueBuf + 2, NULL, 16);
+            return true;
+        }
+        int32Var = strtoul(valueBuf, NULL, 10); // default radix decimal
+        return true;
+    }
+    return false; // no match
+}
+
 /** CmdLine::parse_double matches "key"=value
  *
  * @return true if keyword was found in buffer