From 7755a1c3bfca65316d0d7ff6648d4ef8e08e1856 Mon Sep 17 00:00:00 2001 From: Jan Breuer <jan.breuer@jaybee.cz> Date: 周三, 24 9月 2014 23:21:40 +0800 Subject: [PATCH] Merge support for matching number suffix from master --- libscpi/inc/scpi/types.h | 1 libscpi/src/parser.c | 3 + libscpi/test/test_scpi_utils.c | 35 +++++++++++++++++ libscpi/src/utils_private.h | 1 libscpi/src/utils.c | 49 +++++++++++++++++++++++- examples/common/scpi-def.c | 8 ++++ 6 files changed, 94 insertions(+), 3 deletions(-) diff --git a/examples/common/scpi-def.c b/examples/common/scpi-def.c index cdfee2d..e7061bf 100644 --- a/examples/common/scpi-def.c +++ b/examples/common/scpi-def.c @@ -128,6 +128,13 @@ return SCPI_RES_OK; } +scpi_result_t TEST_Numbers(scpi_t * context) { + + fprintf(stderr, "RAW CMD %.*s\r\n", (int)context->param_list.cmd_raw.length, context->param_list.cmd_raw.data); + + return SCPI_RES_OK; +} + static const scpi_command_t scpi_commands[] = { /* IEEE Mandated Commands (SCPI std V1999.0 4.1.1) */ { .pattern = "*CLS", .callback = SCPI_CoreCls,}, @@ -178,6 +185,7 @@ {.pattern = "TEST:BOOL", .callback = TEST_Bool,}, {.pattern = "TEST:CHOice?", .callback = TEST_ChoiceQ,}, + {.pattern = "TEST#:NUMbers#", .callback = TEST_Numbers,}, SCPI_CMD_LIST_END }; diff --git a/libscpi/inc/scpi/types.h b/libscpi/inc/scpi/types.h index 2bd7c24..a5a6cf4 100644 --- a/libscpi/inc/scpi/types.h +++ b/libscpi/inc/scpi/types.h @@ -231,6 +231,7 @@ struct _scpi_param_list_t { const scpi_command_t * cmd; lex_state_t lex_state; + scpi_buffer_t cmd_raw; }; typedef struct _scpi_param_list_t scpi_param_list_t; diff --git a/libscpi/src/parser.c b/libscpi/src/parser.c index 42f0231..ba06728 100644 --- a/libscpi/src/parser.c +++ b/libscpi/src/parser.c @@ -176,6 +176,9 @@ context->param_list.lex_state.buffer = state->programData.ptr; context->param_list.lex_state.pos = context->param_list.lex_state.buffer; context->param_list.lex_state.len = state->programData.len; + context->param_list.cmd_raw.data = state->programHeader.ptr; + context->param_list.cmd_raw.position = 0; + context->param_list.cmd_raw.length = state->programHeader.len; processCommand(context); diff --git a/libscpi/src/utils.c b/libscpi/src/utils.c index 90f6ed4..922b870 100644 --- a/libscpi/src/utils.c +++ b/libscpi/src/utils.c @@ -161,6 +161,36 @@ } /** + * Compare two strings, one be longer but may contains only numbers in that section + * @param str1 + * @param len1 + * @param str2 + * @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 result = FALSE; + size_t i; + + if (len2 < len1) { + return FALSE; + } + + if (SCPI_strncasecmp(str1, str2, len1) == 0) { + result = TRUE; + } + + for (i = len1; i<len2; i++) { + if (!isdigit(str2[i])) { + result = FALSE; + break; + } + } + + return result; +} + +/** * Count white spaces from the beggining * @param cmd - command * @param len - max search length @@ -236,9 +266,22 @@ * @return */ scpi_bool_t matchPattern(const char * pattern, size_t pattern_len, const char * str, size_t str_len) { - int pattern_sep_pos_short = patternSeparatorShortPos(pattern, pattern_len); - return compareStr(pattern, pattern_len, str, str_len) || - compareStr(pattern, pattern_sep_pos_short, str, str_len); + int pattern_sep_pos_short; + + if (pattern[pattern_len - 1] == '#') { + size_t new_pattern_len = pattern_len - 1; + + 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); + } else { + + pattern_sep_pos_short = patternSeparatorShortPos(pattern, pattern_len); + + return compareStr(pattern, pattern_len, str, str_len) || + compareStr(pattern, pattern_sep_pos_short, str, str_len); + } } /** diff --git a/libscpi/src/utils_private.h b/libscpi/src/utils_private.h index 4a8234a..ed624f3 100644 --- a/libscpi/src/utils_private.h +++ b/libscpi/src/utils_private.h @@ -53,6 +53,7 @@ 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; size_t longToStr(int32_t val, char * str, size_t len, int8_t base) LOCAL; size_t doubleToStr(double val, char * str, size_t len) LOCAL; size_t strToLong(const char * str, int32_t * val, int8_t base) LOCAL; diff --git a/libscpi/test/test_scpi_utils.c b/libscpi/test/test_scpi_utils.c index 2d8f976..f1b7363 100644 --- a/libscpi/test/test_scpi_utils.c +++ b/libscpi/test/test_scpi_utils.c @@ -165,6 +165,24 @@ CU_ASSERT_FALSE(compareStr("ABCD", 4, "abcd", 3)); } +void test_compareStrAndNum() { + + 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_FALSE(compareStrAndNum("abcd", 1, "efgh", 1)); + CU_ASSERT_FALSE(compareStrAndNum("ABCD", 4, "abcd", 3)); + + 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)); + +} + void test_matchPattern() { scpi_bool_t result; @@ -289,6 +307,22 @@ TEST_MATCH_COMMAND("*IDN?", ":idn?", FALSE); // common command TEST_MATCH_COMMAND("*IDN?", ":*idn", FALSE); // common command TEST_MATCH_COMMAND("*IDN?", ":*idn?", FALSE); // common command + + TEST_MATCH_COMMAND("ABCdef#", "abc", TRUE); // test numeric parameter + TEST_MATCH_COMMAND("ABCdef#", "abc1324", TRUE); // test numeric parameter + TEST_MATCH_COMMAND("ABCdef#", "abcDef1324", TRUE); // test numeric parameter + TEST_MATCH_COMMAND("ABCdef#", "abcDef124b", FALSE); // test numeric parameter + + TEST_MATCH_COMMAND("OUTPut#:MODulation#:FM#", "abc", FALSE); // test numeric parameter + TEST_MATCH_COMMAND("OUTPut#:MODulation#:FM#", "outp1:mod10:fm", TRUE); // test numeric parameter + TEST_MATCH_COMMAND("OUTPut#:MODulation#:FM#", "output1:mod10:fm", TRUE); // test numeric parameter + TEST_MATCH_COMMAND("OUTPut#:MODulation#:FM#", "outp1:modulation:fm5", TRUE); // test numeric parameter + TEST_MATCH_COMMAND("OUTPut#:MODulation#:FM#", "output:mod:fm", TRUE); // test numeric parameter + TEST_MATCH_COMMAND("OUTPut#:MODulation#:FM#", "outp1:mod10a:fm", FALSE); // test numeric parameter + TEST_MATCH_COMMAND("OUTPut#[:MODulation#]:FM#", "outp1:fm", TRUE); // test numeric parameter + 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 } int main() { @@ -313,6 +347,7 @@ || (NULL == CU_add_test(pSuite, "strToLong", test_strToLong)) || (NULL == CU_add_test(pSuite, "strToDouble", test_strToDouble)) || (NULL == CU_add_test(pSuite, "compareStr", test_compareStr)) + || (NULL == CU_add_test(pSuite, "compareStrAndNum", test_compareStrAndNum)) || (NULL == CU_add_test(pSuite, "matchPattern", test_matchPattern)) || (NULL == CU_add_test(pSuite, "matchCommand", test_matchCommand)) ) { -- Gitblit v1.9.1