Merge support for matching number suffix from master
| | |
| | | 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,}, |
| | |
| | | |
| | | {.pattern = "TEST:BOOL", .callback = TEST_Bool,}, |
| | | {.pattern = "TEST:CHOice?", .callback = TEST_ChoiceQ,}, |
| | | {.pattern = "TEST#:NUMbers#", .callback = TEST_Numbers,}, |
| | | |
| | | SCPI_CMD_LIST_END |
| | | }; |
| | |
| | | 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; |
| | | |
| | |
| | | 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); |
| | | |
| | |
| | | } |
| | | |
| | | /** |
| | | * 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 |
| | |
| | | * @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); |
| | | 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); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * Compare pattern and command |
| | |
| | | |
| | | 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; |
| | |
| | | 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; |
| | | |
| | |
| | | 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() { |
| | |
| | | || (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)) |
| | | ) { |