Add support for matching number suffix
Add support for matching pattern
ABcd#:EFGh#
that matches
ABCD123:EFGH567
Extracting numbers from command is currently up to user. It is
possible using context->paramlist.cmd_raw structure.
| | |
| | | return SCPI_RES_OK; |
| | | } |
| | | |
| | | scpi_result_t TEST_Numbers(scpi_t * context) { |
| | | |
| | | fprintf(stderr, "RAW CMD %.*s\r\n", (int)context->paramlist.cmd_raw.length, context->paramlist.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,}, |
| | |
| | | |
| | | {.pattern = "TEST:BOOL", .callback = TEST_Bool,}, |
| | | {.pattern = "TEST:CHOice?", .callback = TEST_ChoiceQ,}, |
| | | {.pattern = "TEST#:NUMbers#", .callback = TEST_Numbers,}, |
| | | |
| | | SCPI_CMD_LIST_END |
| | | }; |
| | |
| | | |
| | | typedef struct _scpi_command_t scpi_command_t; |
| | | |
| | | struct _scpi_buffer_t { |
| | | size_t length; |
| | | size_t position; |
| | | char * data; |
| | | }; |
| | | typedef struct _scpi_buffer_t scpi_buffer_t; |
| | | |
| | | struct _scpi_param_list_t { |
| | | const scpi_command_t * cmd; |
| | | const char * parameters; |
| | | size_t length; |
| | | scpi_buffer_t cmd_raw; |
| | | }; |
| | | #define SCPI_CMD_LIST_END {NULL, NULL, } |
| | | typedef struct _scpi_param_list_t scpi_param_list_t; |
| | |
| | | /* scpi interface */ |
| | | typedef struct _scpi_t scpi_t; |
| | | typedef struct _scpi_interface_t scpi_interface_t; |
| | | |
| | | struct _scpi_buffer_t { |
| | | size_t length; |
| | | size_t position; |
| | | char * data; |
| | | }; |
| | | typedef struct _scpi_buffer_t scpi_buffer_t; |
| | | |
| | | typedef size_t(*scpi_write_t)(scpi_t * context, const char * data, size_t len); |
| | | typedef scpi_result_t(*scpi_write_control_t)(scpi_t * context, scpi_ctrl_name_t ctrl, scpi_reg_val_t val); |
| | |
| | | |
| | | 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; |
| | | size_t longToStr(int32_t val, char * str, size_t len) LOCAL; |
| | | size_t doubleToStr(double val, char * str, size_t len) LOCAL; |
| | | size_t strToLong(const char * str, int32_t * val) LOCAL; |
| | |
| | | context->paramlist.cmd = cmd; |
| | | context->paramlist.parameters = cmdline_ptr + cmd_len; |
| | | context->paramlist.length = cmdline_len - cmd_len; |
| | | context->paramlist.cmd_raw.data = cmdline_ptr; |
| | | context->paramlist.cmd_raw.length = cmd_len; |
| | | context->paramlist.cmd_raw.position = 0; |
| | | return TRUE; |
| | | } |
| | | } |
| | |
| | | return FALSE; |
| | | } |
| | | |
| | | /** |
| | | * 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; |
| | | } |
| | | |
| | | enum _locate_text_states { |
| | | STATE_FIRST_WHITESPACE, |
| | | STATE_TEXT_QUOTED, |
| | |
| | | * @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); |
| | | } |
| | | } |
| | | |
| | | /** |
| | |
| | | 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_locateText() { |
| | | |
| | | const char * v; |
| | |
| | | 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 |
| | | } |
| | | |
| | | void test_composeCompoundCommand(void) { |
| | |
| | | || (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, "locateText", test_locateText)) |
| | | || (NULL == CU_add_test(pSuite, "locateStr", test_locateStr)) |
| | | || (NULL == CU_add_test(pSuite, "matchPattern", test_matchPattern)) |