nancy.liao
2025-05-08 bf8143c649292042de87c0cef63e6cb3c523388f
libscpi/src/utils.c
@@ -319,7 +319,7 @@
 */
size_t strToFloat(const char * str, float * val) {
    char * endptr;
    *val = SCPIDEFINE_strtof(str, &endptr);
    *val = (float)SCPIDEFINE_strtof(str, &endptr);
    return endptr - str;
}
@@ -480,13 +480,13 @@
    if ((pattern_len > 0) && pattern[pattern_len - 1] == '#') {
        size_t new_pattern_len = pattern_len - 1;
        pattern_sep_pos_short = patternSeparatorShortPos(pattern, new_pattern_len);
        pattern_sep_pos_short = (int)patternSeparatorShortPos(pattern, new_pattern_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);
        pattern_sep_pos_short = (int)patternSeparatorShortPos(pattern, pattern_len);
        return compareStr(pattern, pattern_len, str, str_len) ||
               compareStr(pattern, pattern_sep_pos_short, str, str_len);
@@ -512,7 +512,7 @@
    int32_t *number_ptr = NULL;
    const char * pattern_ptr = pattern;
    int pattern_len = strlen(pattern);
    int pattern_len = (int)strlen(pattern);
    const char * cmd_ptr = cmd;
    size_t cmd_len = SCPIDEFINE_strnlen(cmd, len);
@@ -548,9 +548,9 @@
    }
    while (1) {
        int pattern_sep_pos = patternSeparatorPos(pattern_ptr, pattern_len);
        int pattern_sep_pos = (int)patternSeparatorPos(pattern_ptr, pattern_len);
        cmd_sep_pos = cmdSeparatorPos(cmd_ptr, cmd_len);
        cmd_sep_pos = (int)cmdSeparatorPos(cmd_ptr, cmd_len);
        if ((pattern_sep_pos > 0) && pattern_ptr[pattern_sep_pos - 1] == '#') {
            if (numbers && (numbers_idx < numbers_len)) {
@@ -584,7 +584,7 @@
            if (cmd_len == 0) {
                /* verify all subsequent pattern parts are also optional */
                while (pattern_len) {
                    pattern_sep_pos = patternSeparatorPos(pattern_ptr, pattern_len);
                    pattern_sep_pos = (int)patternSeparatorPos(pattern_ptr, pattern_len);
                    switch (pattern_ptr[pattern_sep_pos]) {
                    case '[':
                        brackets++;
@@ -677,7 +677,7 @@
//组合复合SCPI命令
scpi_bool_t composeCompoundCommand(const scpi_token_t * prev, scpi_token_t * current) {
    size_t i;
    int i;
    /* Invalid input */
    if (current == NULL || current->ptr == NULL || current->len == 0)
@@ -741,8 +741,8 @@
    unsigned char c1, c2;
    for (; n != 0; n--) {
        c1 = tolower((unsigned char) *s1++);
        c2 = tolower((unsigned char) *s2++);
        c1 = (unsigned char)tolower((unsigned char) *s1++);
        c2 = (unsigned char)tolower((unsigned char) *s2++);
        if (c1 != c2) {
            return c1 - c2;
        }
@@ -958,7 +958,7 @@
    if (fi != 0) {
        r1 = r1 * 308 / 1024 - ndigits;
        w2 = bufsize;
        w2 = (int)bufsize;
        while (r1 > 0) {
            fj = modf(fi / 10, &fi);
            r2++;
@@ -966,7 +966,7 @@
        }
        while (fi != 0) {
            fj = modf(fi / 10, &fi);
            buf[--w2] = (int) ((fj + .03) * 10) + '0';
            buf[--w2] = (char) ((fj + .03) * 10) + '0';
            r2++;
        }
        while (w2 < (int) bufsize) buf[w1++] = buf[w2++];
@@ -985,7 +985,7 @@
    while (w1 <= w2 && w1 < (int) bufsize) {
        arg *= 10;
        arg = modf(arg, &fj);
        buf[w1++] = (int) fj + '0';
        buf[w1++] = (char) fj + '0';
    }
    if (w2 >= (int) bufsize) {
        buf[bufsize - 1] = '\0';
@@ -1143,101 +1143,4 @@
           ((val & 0x0000FF0000000000ull) >> 24) |
           ((val & 0x00FF000000000000ull) >> 40) |
           ((val & 0xFF00000000000000ull) >> 56);
}
// 用于提取并处理选项的函数
int extract_required_options(const char* pattern, char options[MAX_TAGS][MAX_OPTION_LEN][MAX_OPTION_LEN], int max_tags) {
    const char* start = strchr(pattern, '<');  // 查找第一个 '<'
    const char* end = NULL;
    int tag_count = 0;
    // 处理 <...> 内的选项
    while (start != NULL && tag_count < max_tags)
    {
        end = strchr(start, '>');  // 查找对应的 '>'
        if (end == NULL) {
            break;  // 如果没有找到 '>',则退出
        }
        // 提取 <...> 中的内容
        int len = end - start - 1;
        if (len > 0 && len < MAX_OPTION_LEN) {
            char buffer[MAX_OPTION_LEN];
            strncpy(buffer, start + 1, len);  // 复制 '<' 和 '>' 之间的内容
            buffer[len] = '\0';  // 结束符
            // 处理 | 分隔符,提取多个选项
            char* token = strtok(buffer, "|");
            int option_count = 0;
            while (token && option_count < MAX_OPTION_LEN) {
                // 去除 token 中的 "[:", "]" 和空格
                char* p = token;
                while (*p == ' ' || *p == '[' || *p == ':') p++;  // 去除前导空格和 [:
                char* q = p + strlen(p) - 1;
                while (q > p && (*q == ' ' || *q == ']')) q--;  // 去除尾随空格和 ]
                *(q + 1) = '\0';  // 确保结尾是'\0'
                // 拷贝选项到 options 数组
                if (option_count < MAX_OPTION_LEN) {  // 确保不会越界
                    strncpy(options[tag_count][option_count], p, MAX_OPTION_LEN - 1);
                    options[tag_count][option_count][MAX_OPTION_LEN - 1] = '\0';  // 确保结束符
                    option_count++;
                }
                token = strtok(NULL, "|");
            }
        }
        // 移动到下一个 '<' 位置,继续查找
        start = strchr(end + 1, '<');
        tag_count++;
    }
    return tag_count;
}
// 用于检查输入是否能匹配每一组选项
int match_input_to_options(const char* input, char options[MAX_TAGS][MAX_OPTION_LEN][MAX_OPTION_LEN], int num_tags) {
    char input_copy[MAX_INPUT_LEN];
    strncpy(input_copy, input, MAX_INPUT_LEN - 1);
    input_copy[MAX_INPUT_LEN - 1] = '\0';  // 确保结尾是'\0'
    // 拆分输入字符串,按 ":" 分割
    char* token = strtok(input_copy, ":");
    int group_idx = 0;
    // 对每一组进行匹配
    while (token != NULL && group_idx < num_tags) {
        int match_found = 0;
        // 检查当前组的每个选项是否与输入的 token 匹配
        printf("Checking input token: '%s' against group %d options\n", token, group_idx + 1); // Debug info
        for (int i = 0; i < MAX_OPTION_LEN && options[group_idx][i][0] != '\0'; i++) {
            printf("  Comparing with option: '%s'\n", options[group_idx][i]); // Debug info
            if (strcmp(options[group_idx][i], token) == 0) {
                match_found = 1;
                break;
            }
        }
        // 如果当前组的某个选项没有匹配上,返回 false
        if (!match_found) {
            return 0;  // 不匹配
        }
        // 处理下一个输入部分
        token = strtok(NULL, ":");
        group_idx++;
    }
    return (group_idx == num_tags);
}
// 测试匹配函数
bool test_match(const char* pattern, const char* command)
{
    char options[MAX_TAGS][MAX_OPTION_LEN][MAX_OPTION_LEN] = { {{0}} };
    int num_tags = extract_required_options(pattern, options, MAX_TAGS);
    return match_input_to_options(command, options, num_tags);
}