From 94ccbc305cac38eb9c54384b9f47d3da6527299b Mon Sep 17 00:00:00 2001
From: Jan Breuer <jan.breuer@jaybee.cz>
Date: 周六, 25 4月 2015 22:29:42 +0800
Subject: [PATCH] Solve #16 Multiple Identical Capabilities

---
 libscpi/inc/scpi/utils_private.h |    6 +-
 libscpi/src/parser.c             |   16 +++--
 libscpi/src/units.c              |    2 
 libscpi/test/test_scpi_utils.c   |   88 ++++++++++++++++++++++++----
 libscpi/inc/scpi/parser.h        |    1 
 libscpi/src/utils.c              |   51 +++++++++++++---
 examples/common/scpi-def.c       |    5 +
 7 files changed, 133 insertions(+), 36 deletions(-)

diff --git a/examples/common/scpi-def.c b/examples/common/scpi-def.c
index 116f7dd..19c66df 100644
--- a/examples/common/scpi-def.c
+++ b/examples/common/scpi-def.c
@@ -155,8 +155,11 @@
 }
 
 static scpi_result_t TEST_Numbers(scpi_t * context) {
+    int32_t numbers[2];
 
-    fprintf(stderr, "RAW CMD %.*s\r\n", (int)context->paramlist.cmd_raw.length, context->paramlist.cmd_raw.data);
+    SCPI_CommandNumbers(context, numbers, 2);
+
+    fprintf(stderr, "TEST numbers %d %d\r\n", numbers[0], numbers[1]);
 
     return SCPI_RES_OK;
 }
diff --git a/libscpi/inc/scpi/parser.h b/libscpi/inc/scpi/parser.h
index 3d4d114..439fda9 100644
--- a/libscpi/inc/scpi/parser.h
+++ b/libscpi/inc/scpi/parser.h
@@ -51,6 +51,7 @@
 
     scpi_bool_t SCPI_IsCmd(scpi_t * context, const char * cmd);
     scpi_bool_t SCPI_Match(const char * pattern, const char * value, size_t len);
+    scpi_bool_t SCPI_CommandNumbers(scpi_t * context, int32_t * numbers, size_t len);
 
     size_t SCPI_ResultString(scpi_t * context, const char * data);
     size_t SCPI_ResultInt(scpi_t * context, int32_t val);
diff --git a/libscpi/inc/scpi/utils_private.h b/libscpi/inc/scpi/utils_private.h
index 0f8fdec..f1e7b81 100644
--- a/libscpi/inc/scpi/utils_private.h
+++ b/libscpi/inc/scpi/utils_private.h
@@ -53,15 +53,15 @@
 
     const char * strnpbrk(const char *str, size_t size, const char *set) LOCAL;
     scpi_bool_t compareStr(const char * str1, size_t len1, const char * str2, size_t len2) LOCAL;
-    scpi_bool_t compareStrAndNum(const char * str1, size_t len1, const char * str2, size_t len2) LOCAL;
+    scpi_bool_t compareStrAndNum(const char * str1, size_t len1, const char * str2, size_t len2, int32_t * num) LOCAL;
     size_t strToLong(const char * str, int32_t * val) LOCAL;
     size_t strToDouble(const char * str, double * val) LOCAL;
     scpi_bool_t locateText(const char * str1, size_t len1, const char ** str2, size_t * len2) LOCAL;
     scpi_bool_t locateStr(const char * str1, size_t len1, const char ** str2, size_t * len2) LOCAL;
     size_t skipWhitespace(const char * cmd, size_t len) LOCAL;
     size_t skipColon(const char * cmd, size_t len) LOCAL;
-    scpi_bool_t matchPattern(const char * pattern, size_t pattern_len, const char * str, size_t str_len) LOCAL;
-    scpi_bool_t matchCommand(const char * pattern, const char * cmd, size_t len) LOCAL;
+    scpi_bool_t matchPattern(const char * pattern, size_t pattern_len, const char * str, size_t str_len, int32_t * num) LOCAL;
+    scpi_bool_t matchCommand(const char * pattern, const char * cmd, size_t len, int32_t *numbers, size_t numbers_len) LOCAL;
     scpi_bool_t composeCompoundCommand(char * ptr_prev, size_t len_prev, char ** pptr, size_t * plen)  LOCAL;
 
 #if !HAVE_STRNLEN
diff --git a/libscpi/src/parser.c b/libscpi/src/parser.c
index f437018..4069944 100644
--- a/libscpi/src/parser.c
+++ b/libscpi/src/parser.c
@@ -247,7 +247,7 @@
 
     for (i = 0; context->cmdlist[i].pattern != NULL; i++) {
         cmd = &context->cmdlist[i];
-        if (matchCommand(cmd->pattern, cmdline_ptr, cmd_len)) {
+        if (matchCommand(cmd->pattern, cmdline_ptr, cmd_len, NULL, 0)) {
             context->paramlist.cmd = cmd;
             context->paramlist.parameters = cmdline_ptr + cmd_len;
             context->paramlist.length = cmdline_len - cmd_len;
@@ -648,9 +648,9 @@
         return FALSE;
     }
 
-    if (matchPattern("ON", 2, param, param_len)) {
+    if (matchPattern("ON", 2, param, param_len, NULL)) {
         *value = TRUE;
-    } else if (matchPattern("OFF", 3, param, param_len)) {
+    } else if (matchPattern("OFF", 3, param, param_len, NULL)) {
         *value = FALSE;
     } else {
         num_len = strToLong(param, &i);
@@ -688,7 +688,7 @@
     }
 
     for (res = 0; options[res]; ++res) {
-        if (matchPattern(options[res], strlen(options[res]), param, param_len)) {
+        if (matchPattern(options[res], strlen(options[res]), param, param_len, NULL)) {
             *value = res;
             return TRUE;
         }
@@ -704,9 +704,13 @@
     }
 
     const char * pattern = context->paramlist.cmd->pattern;
-    return matchCommand (pattern, cmd, strlen (cmd));
+    return matchCommand (pattern, cmd, strlen (cmd), NULL, 0);
 }
 
 scpi_bool_t SCPI_Match(const char * pattern, const char * value, size_t len) {
-    return matchCommand (pattern, value, len);
+    return matchCommand (pattern, value, len, NULL, 0);
+}
+
+scpi_bool_t SCPI_CommandNumbers(scpi_t * context, int32_t * numbers, size_t len) {
+    return matchCommand (context->paramlist.cmd->pattern,  context->paramlist.cmd_raw.data, context->paramlist.cmd_raw.length, numbers, len);
 }
diff --git a/libscpi/src/units.c b/libscpi/src/units.c
index a4224cd..2f1f7a7 100644
--- a/libscpi/src/units.c
+++ b/libscpi/src/units.c
@@ -136,7 +136,7 @@
     }
 
     for (i = 0; specs[i].name != NULL; i++) {
-        if (matchPattern(specs[i].name, strlen(specs[i].name), str, len)) {
+        if (matchPattern(specs[i].name, strlen(specs[i].name), str, len, NULL)) {
             value->type = specs[i].type;
             return TRUE;
         }
diff --git a/libscpi/src/utils.c b/libscpi/src/utils.c
index eb74eea..6a0ce82 100644
--- a/libscpi/src/utils.c
+++ b/libscpi/src/utils.c
@@ -170,7 +170,7 @@
  * @param len2
  * @return TRUE if strings match
  */
-scpi_bool_t compareStrAndNum(const char * str1, size_t len1, const char * str2, size_t len2) {
+scpi_bool_t compareStrAndNum(const char * str1, size_t len1, const char * str2, size_t len2, int32_t * num) {
     scpi_bool_t result = FALSE;
     size_t i;
 
@@ -180,12 +180,26 @@
 
     if (SCPIDEFINE_strncasecmp(str1, str2, len1) == 0) {
         result = TRUE;
-    }
 
-    for (i = len1; i<len2; i++) {
-        if (!isdigit((int) str2[i])) {
-            result = FALSE;
-            break;
+        if (num) {
+            if (len1 == len2) {
+                *num = 1;
+            } else {
+                int32_t tmpNum;
+                i = len1 + strToLong(str2 + len1, &tmpNum);
+                if (i != len2) {
+                    result = FALSE;
+                } else {
+                    *num = tmpNum;
+                }
+            }
+        } else {
+            for (i = len1; i<len2; i++) {
+                if (!isdigit((int) str2[i])) {
+                    result = FALSE;
+                    break;
+                }
+            }
         }
     }
 
@@ -469,7 +483,7 @@
  * @param str_len
  * @return 
  */
-scpi_bool_t matchPattern(const char * pattern, size_t pattern_len, const char * str, size_t str_len) {
+scpi_bool_t matchPattern(const char * pattern, size_t pattern_len, const char * str, size_t str_len, int32_t * num) {
     int pattern_sep_pos_short;
 
     if (pattern[pattern_len - 1] == '#') {
@@ -477,8 +491,8 @@
 
         pattern_sep_pos_short = patternSeparatorShortPos(pattern, new_pattern_len);
 
-        return compareStrAndNum(pattern, new_pattern_len, str, str_len) ||
-                compareStrAndNum(pattern, pattern_sep_pos_short, str, str_len);
+        return compareStrAndNum(pattern, new_pattern_len, str, str_len, num) ||
+                compareStrAndNum(pattern, pattern_sep_pos_short, str, str_len, num);
     } else {
 
         pattern_sep_pos_short = patternSeparatorShortPos(pattern, pattern_len);
@@ -495,11 +509,14 @@
  * @param len - max search length
  * @return TRUE if pattern matches, FALSE otherwise
  */
-scpi_bool_t matchCommand(const char * pattern, const char * cmd, size_t len) {
+scpi_bool_t matchCommand(const char * pattern, const char * cmd, size_t len, int32_t *numbers, size_t numbers_len) {
     scpi_bool_t result = FALSE;
     int leftFlag = 0; // flag for '[' on left
     int rightFlag = 0; // flag for ']' on right
     int cmd_sep_pos = 0;
+
+    size_t numbers_idx = -1;
+    int32_t *number_ptr = NULL;
 
     const char * pattern_ptr = pattern;
     int pattern_len = strlen(pattern);
@@ -538,7 +555,19 @@
             cmd_sep_pos = cmdSeparatorPos(cmd_ptr, cmd_end - cmd_ptr);
         }
 
-        if (matchPattern(pattern_ptr, pattern_sep_pos, cmd_ptr, cmd_sep_pos)) {
+        if (pattern_ptr[pattern_sep_pos - 1] == '#') {
+            numbers_idx++;
+            if (numbers && (numbers_idx < numbers_len)) {
+                number_ptr = numbers + numbers_idx;
+                *number_ptr = 1; // default value
+            } else {
+                number_ptr = NULL;
+            }
+        } else {
+            number_ptr = NULL;
+        }
+
+        if (matchPattern(pattern_ptr, pattern_sep_pos, cmd_ptr, cmd_sep_pos, number_ptr)) {
             pattern_ptr = pattern_ptr + pattern_sep_pos;
             cmd_ptr = cmd_ptr + cmd_sep_pos;
             result = TRUE;
diff --git a/libscpi/test/test_scpi_utils.c b/libscpi/test/test_scpi_utils.c
index 4d1f634..cb705a4 100644
--- a/libscpi/test/test_scpi_utils.c
+++ b/libscpi/test/test_scpi_utils.c
@@ -167,21 +167,35 @@
 }
 
 static void test_compareStrAndNum() {
+    int32_t num;
 
-    CU_ASSERT_TRUE(compareStrAndNum("abcd", 1, "afgh", 1));
-    CU_ASSERT_TRUE(compareStrAndNum("ABCD", 4, "abcd", 4));
-    CU_ASSERT_TRUE(compareStrAndNum("AbCd", 3, "AbCE", 3));
-    CU_ASSERT_TRUE(compareStrAndNum("ABCD", 1, "a", 1));
+    CU_ASSERT_TRUE(compareStrAndNum("abcd", 1, "afgh", 1, NULL));
+    CU_ASSERT_TRUE(compareStrAndNum("ABCD", 4, "abcd", 4, NULL));
+    CU_ASSERT_TRUE(compareStrAndNum("AbCd", 3, "AbCE", 3, NULL));
+    CU_ASSERT_TRUE(compareStrAndNum("ABCD", 1, "a", 1, NULL));
 
-    CU_ASSERT_FALSE(compareStrAndNum("abcd", 1, "efgh", 1));
-    CU_ASSERT_FALSE(compareStrAndNum("ABCD", 4, "abcd", 3));
+    CU_ASSERT_FALSE(compareStrAndNum("abcd", 1, "efgh", 1, NULL));
+    CU_ASSERT_FALSE(compareStrAndNum("ABCD", 4, "abcd", 3, NULL));
 
-    CU_ASSERT_TRUE(compareStrAndNum("abcd", 4, "abcd1", 5));
-    CU_ASSERT_TRUE(compareStrAndNum("abcd", 4, "abcd123", 7));
-    CU_ASSERT_FALSE(compareStrAndNum("abcd", 4, "abcd12A", 7));
-    CU_ASSERT_FALSE(compareStrAndNum("abcd", 4, "abcdB12", 7));
-    CU_ASSERT_FALSE(compareStrAndNum("abdd", 4, "abcd132", 7));
+    CU_ASSERT_TRUE(compareStrAndNum("abcd", 4, "abcd1", 5, NULL));
+    CU_ASSERT_TRUE(compareStrAndNum("abcd", 4, "abcd123", 7, NULL));
+    CU_ASSERT_FALSE(compareStrAndNum("abcd", 4, "abcd12A", 7, NULL));
+    CU_ASSERT_FALSE(compareStrAndNum("abcd", 4, "abcdB12", 7, NULL));
+    CU_ASSERT_FALSE(compareStrAndNum("abdd", 4, "abcd132", 7, NULL));
 
+#define TEST_COMPARE_STR_AND_NUM(s1, l1, s2, l2, v, r)              \
+    do {                                                            \
+        num = 0;                                                    \
+        CU_ASSERT_EQUAL(compareStrAndNum(s1, l1, s2, l2, &num),r);  \
+        CU_ASSERT_EQUAL(num, v);                                    \
+    } while(0);                                                     \
+
+    TEST_COMPARE_STR_AND_NUM("abcd", 4, "abcd", 4, 1, TRUE);
+    TEST_COMPARE_STR_AND_NUM("abcd", 4, "abcd1", 5, 1, TRUE);
+    TEST_COMPARE_STR_AND_NUM("abcd", 4, "abcd123", 7, 123, TRUE);
+    TEST_COMPARE_STR_AND_NUM("abcd", 4, "abcd12A", 7, 0, FALSE);
+    TEST_COMPARE_STR_AND_NUM("abcd", 4, "abcdB12", 7, 0, FALSE);
+    TEST_COMPARE_STR_AND_NUM("abdd", 4, "abcd132", 7, 0, FALSE);
 }
 
 static void test_locateText() {
@@ -287,7 +301,7 @@
     
 #define TEST_MATCH_PATTERN(p, s, r)                             \
     do {                                                        \
-        result = matchPattern(p, strlen(p), s, strlen(s));      \
+        result = matchPattern(p, strlen(p), s, strlen(s), NULL);\
         CU_ASSERT_EQUAL(result, r);                             \
     } while(0)                                                  \
 
@@ -296,15 +310,34 @@
     TEST_MATCH_PATTERN("Ab", "ab", TRUE);
     TEST_MATCH_PATTERN("Ab", "aB", TRUE);
     TEST_MATCH_PATTERN("AB", "a", FALSE);
+    TEST_MATCH_PATTERN("Ab#", "aB", TRUE);
+    TEST_MATCH_PATTERN("Ab#", "aB10", TRUE);
+    TEST_MATCH_PATTERN("Ab#", "a10", TRUE);
 }
 
 static void test_matchCommand() {
     scpi_bool_t result;
-    
+    int32_t values[20];
+
     #define TEST_MATCH_COMMAND(p, s, r)                         \
     do {                                                        \
-        result = matchCommand(p, s, strlen(s));                 \
+        result = matchCommand(p, s, strlen(s), NULL, 0);        \
         CU_ASSERT_EQUAL(result, r);                             \
+    } while(0)                                                  \
+
+    #define TEST_MATCH_COMMAND2(p, s, r, ...)                   \
+    do {                                                        \
+        int32_t evalues[] = {__VA_ARGS__};                      \
+        unsigned int cnt = (sizeof(evalues)/4);                 \
+        result = matchCommand(p, s, strlen(s), values, 20);     \
+        CU_ASSERT_EQUAL(result, r);                             \
+        if (cnt > 0) CU_ASSERT_EQUAL(evalues[0], values[0]);    \
+        if (cnt > 1) CU_ASSERT_EQUAL(evalues[1], values[1]);    \
+        if (cnt > 2) CU_ASSERT_EQUAL(evalues[2], values[2]);    \
+        if (cnt > 3) CU_ASSERT_EQUAL(evalues[3], values[3]);    \
+        if (cnt > 4) CU_ASSERT_EQUAL(evalues[4], values[4]);    \
+        if (cnt > 5) CU_ASSERT_EQUAL(evalues[5], values[5]);    \
+        if (cnt > 6) CU_ASSERT_EQUAL(evalues[6], values[6]);    \
     } while(0)                                                  \
 
     TEST_MATCH_COMMAND("A", "a", TRUE);
@@ -422,6 +455,33 @@
     TEST_MATCH_COMMAND("OUTPut#[:MODulation#]:FM#", "outp1:mod10:fm", TRUE); // test numeric parameter
     TEST_MATCH_COMMAND("OUTPut#[:MODulation#]:FM#", "outp1:fm2", TRUE); // test numeric parameter
     TEST_MATCH_COMMAND("OUTPut#[:MODulation#]:FM#", "output:fm", TRUE); // test numeric parameter
+
+    TEST_MATCH_COMMAND2("OUTPut#:MODulation#:FM#", "outp3:mod10:fm", TRUE, 3, 10, 1); // test numeric parameter
+    TEST_MATCH_COMMAND2("OUTPut#:MODulation#:FM#", "output3:mod10:fm", TRUE, 3, 10, 1); // test numeric parameter
+    TEST_MATCH_COMMAND2("OUTPut#:MODulation#:FM#", "outp30:modulation:fm5", TRUE, 30, 1, 5); // test numeric parameter
+    TEST_MATCH_COMMAND2("OUTPut#:MODulation#:FM#", "output:mod:fm", TRUE, 1, 1, 1); // test numeric parameter
+    TEST_MATCH_COMMAND2("OUTPut#[:MODulation#]:FM#", "outp3:fm", TRUE, 3, 1, 1); // test numeric parameter
+    TEST_MATCH_COMMAND2("OUTPut#[:MODulation#]:FM#", "outp3:mod10:fm", TRUE, 3, 10, 1); // test numeric parameter
+    TEST_MATCH_COMMAND2("OUTPut#[:MODulation#]:FM#", "outp3:fm2", TRUE, 3, 1, 2); // test numeric parameter
+    TEST_MATCH_COMMAND2("OUTPut#[:MODulation#]:FM#", "output:fm", TRUE, 1, 1, 1); // test numeric parameter
+
+    TEST_MATCH_COMMAND2("OUTPut#:MODulation:FM#", "outp3:mod:fm", TRUE, 3, 1); // test numeric parameter
+    TEST_MATCH_COMMAND2("OUTPut#:MODulation:FM#", "output3:mod:fm", TRUE, 3, 1); // test numeric parameter
+    TEST_MATCH_COMMAND2("OUTPut#:MODulation:FM#", "outp30:modulation:fm5", TRUE, 30, 5); // test numeric parameter
+    TEST_MATCH_COMMAND2("OUTPut#:MODulation:FM#", "output:mod:fm", TRUE, 1, 1); // test numeric parameter
+    TEST_MATCH_COMMAND2("OUTPut#[:MODulation]:FM#", "outp3:fm", TRUE, 3, 1); // test numeric parameter
+    TEST_MATCH_COMMAND2("OUTPut#[:MODulation]:FM#", "outp3:mod:fm", TRUE, 3, 1); // test numeric parameter
+    TEST_MATCH_COMMAND2("OUTPut#[:MODulation]:FM#", "outp3:fm2", TRUE, 3, 2); // test numeric parameter
+    TEST_MATCH_COMMAND2("OUTPut#[:MODulation]:FM#", "output:fm", TRUE, 1, 1); // test numeric parameter
+
+    TEST_MATCH_COMMAND2("OUTPut#:MODulation#:FM", "outp3:mod10:fm", TRUE, 3, 10); // test numeric parameter
+    TEST_MATCH_COMMAND2("OUTPut#:MODulation#:FM", "output3:mod10:fm", TRUE, 3, 10); // test numeric parameter
+    TEST_MATCH_COMMAND2("OUTPut#:MODulation#:FM", "outp30:modulation:fm", TRUE, 30, 1); // test numeric parameter
+    TEST_MATCH_COMMAND2("OUTPut#:MODulation#:FM", "output:mod:fm", TRUE, 1, 1); // test numeric parameter
+    TEST_MATCH_COMMAND2("OUTPut#[:MODulation#]:FM", "outp3:fm", TRUE, 3, 1); // test numeric parameter
+    TEST_MATCH_COMMAND2("OUTPut#[:MODulation#]:FM", "outp3:mod10:fm", TRUE, 3, 10); // test numeric parameter
+    TEST_MATCH_COMMAND2("OUTPut#[:MODulation#]:FM", "outp3:fm", TRUE, 3, 1); // test numeric parameter
+    TEST_MATCH_COMMAND2("OUTPut#[:MODulation#]:FM", "output:fm", TRUE, 1, 1); // test numeric parameter
 }
 
 static void test_composeCompoundCommand(void) {

--
Gitblit v1.9.1