From 31c1e390d2f7a5278e73491aee3568a6352482d1 Mon Sep 17 00:00:00 2001 From: nancy.liao <huihui.liao@greentest.com.cn> Date: 周三, 23 4月 2025 18:30:16 +0800 Subject: [PATCH] 完善了SCPI库对于<>内可选参数的解析 --- libscpi/src/utils_private.h | 17 +--- libscpi/src/utils.c | 193 +++++++++++++++++++---------------------------- 2 files changed, 84 insertions(+), 126 deletions(-) diff --git a/libscpi/src/utils.c b/libscpi/src/utils.c index afb6664..cd76fec 100644 --- a/libscpi/src/utils.c +++ b/libscpi/src/utils.c @@ -1146,139 +1146,104 @@ } -// 瑙f瀽妯″紡锛屽皢姣忎釜閮ㄥ垎淇濆瓨鍒� segments 涓� -int parse_pattern(const char* pattern, Segment segments[], int max_segments) { - int seg_count = 0; - const char* p = pattern; +// 鐢ㄤ簬鎻愬彇骞跺鐞嗛�夐」鐨勫嚱鏁� +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 (*p && seg_count < max_segments) { - if (*p == '[') { - // 澶勭悊鍙彉娈碉紙[]锛� - segments[seg_count].is_variable = TRUE; - segments[seg_count].is_required = FALSE; // []鏄彲閫夌殑 - segments[seg_count].is_option = FALSE; - p++; // 璺宠繃'[' + // 澶勭悊 <...> 鍐呯殑閫夐」 + while (start != NULL && tag_count < max_tags) + { + end = strchr(start, '>'); // 鏌ユ壘瀵瑰簲鐨� '>' - int i = 0; - while (*p && *p != ']' && i < sizeof(segments[seg_count].text) - 1) { - segments[seg_count].text[i++] = toupper(*p++); - } - segments[seg_count].text[i] = '\0'; // 缁撴潫绗� - - if (*p == ']') { - p++; // 璺宠繃']' - } - } - else if (*p == '<') { - // 澶勭悊蹇呴�夋锛�<>锛� - segments[seg_count].is_variable = TRUE; - segments[seg_count].is_required = TRUE; // <>鏄繀閫夌殑 - segments[seg_count].is_option = FALSE; - p++; // 璺宠繃'<' - - int i = 0; - while (*p && *p != '>' && i < sizeof(segments[seg_count].text) - 1) { - segments[seg_count].text[i++] = toupper(*p++); - } - segments[seg_count].text[i] = '\0'; // 缁撴潫绗� - - if (*p == '>') { - p++; // 璺宠繃'>' - } - } - else if (*p == '|') { - // 澶勭悊绔栫嚎鍒嗛殧鐨勫閫夐儴鍒嗭紙|锛� - segments[seg_count].is_variable = TRUE; - segments[seg_count].is_required = TRUE; // 閫夐」鏄繀閫夌殑 - segments[seg_count].is_option = TRUE; // 杩欒〃绀洪�夐」缁� - p++; // 璺宠繃'|' - - int i = 0; - while (*p && *p != '|' && *p != '>' && *p != '[' && *p != ']' && i < sizeof(segments[seg_count].text) - 1) { - segments[seg_count].text[i++] = toupper(*p++); - } - segments[seg_count].text[i] = '\0'; // 缁撴潫绗� - - // 璺宠繃'|'缁х画澶勭悊涓嬩竴涓�夐」 - if (*p == '|') { - p++; // 璺宠繃'|' - } - } - else { - // 澶勭悊鍥哄畾娈� - segments[seg_count].is_variable = FALSE; - segments[seg_count].is_required = TRUE; // 鍥哄畾娈靛繀閫� - segments[seg_count].is_option = FALSE; - int i = 0; - while (*p && *p != '[' && *p != '<' && *p != '|' && *p != ':' && i < sizeof(segments[seg_count].text) - 1) { - segments[seg_count].text[i++] = toupper(*p++); - } - segments[seg_count].text[i] = '\0'; // 缁撴潫绗� + if (end == NULL) { + break; // 濡傛灉娌℃湁鎵惧埌 '>'锛屽垯閫�鍑� } - seg_count++; + // 鎻愬彇 <...> 涓殑鍐呭 + 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 seg_count; + return tag_count; } -// 鍖归厤鍛戒护瀛楃涓叉槸鍚︾鍚堟ā寮� -bool match_command(const char* command, Segment segments[], int seg_count) { - const char* cmd = command; - int current_seg = 0; +// 鐢ㄤ簬妫�鏌ヨ緭鍏ユ槸鍚﹁兘鍖归厤姣忎竴缁勯�夐」 +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' - while (*cmd && current_seg < seg_count) { - // 鎸夊啋鍙峰垎闅斿懡浠� - if (*cmd == ':') { - cmd++; // 璺宠繃鍒嗛殧绗� - continue; - } + // 鎷嗗垎杈撳叆瀛楃涓诧紝鎸� ":" 鍒嗗壊 + char* token = strtok(input_copy, ":"); + int group_idx = 0; - size_t seg_len = strlen(segments[current_seg].text); - - if (segments[current_seg].is_variable) { - // 鍙彉娈� - 璺宠繃瀵瑰簲闀垮害鐨勫瓧绗� - int i = 0; - while (*cmd && *cmd != ':' && i < seg_len) { - cmd++; - i++; + // 瀵规瘡涓�缁勮繘琛屽尮閰� + 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; } } - else if (segments[current_seg].is_option) { - // 閫夐」缁� - 蹇呴』鍖归厤鍏朵腑涓�涓�夐」 - bool matched = FALSE; - const char* options[] = { segments[current_seg].text, NULL }; - for (int i = 0; options[i]; i++) { - if (strncasecmp(cmd, options[i], strlen(options[i])) == 0) { - matched = TRUE; - break; - } - } - if (!matched) { - return FALSE; - } - cmd += strlen(options[0]); - } - else { - // 鍥哄畾娈� - 蹇呴』绮剧‘鍖归厤 - if (strncasecmp(cmd, segments[current_seg].text, seg_len) != 0) { - return FALSE; - } - cmd += seg_len; // 璺宠繃鍖归厤鐨勯儴鍒� + + // 濡傛灉褰撳墠缁勭殑鏌愪釜閫夐」娌℃湁鍖归厤涓婏紝杩斿洖 false + if (!match_found) { + return 0; // 涓嶅尮閰� } - current_seg++; + // 澶勭悊涓嬩竴涓緭鍏ラ儴鍒� + token = strtok(NULL, ":"); + group_idx++; } - // 妫�鏌ユ槸鍚﹀鐞嗗畬鎵�鏈夊懡浠ゅ拰鎵�鏈夋 - return (*cmd == '\0') && (current_seg == seg_count); + return (group_idx == num_tags); } // 娴嬭瘯鍖归厤鍑芥暟 bool test_match(const char* pattern, const char* command) { - - Segment segments[MAX_SEGMENTS]; - int seg_count = parse_pattern(pattern, segments, MAX_SEGMENTS); - return match_command(command, segments, seg_count); + 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); } diff --git a/libscpi/src/utils_private.h b/libscpi/src/utils_private.h index fbc81e0..101bd71 100644 --- a/libscpi/src/utils_private.h +++ b/libscpi/src/utils_private.h @@ -79,18 +79,11 @@ scpi_bool_t composeCompoundCommand(const scpi_token_t * prev, scpi_token_t * current) LOCAL; -#define MAX_SEGMENTS 16 - -typedef struct { - bool is_variable; // 鏄惁涓哄彲鍙橀儴鍒嗭紙鐢╗]鎷捣鏉ョ殑锛� - bool is_required; // 鏄惁涓哄繀閫夐儴鍒嗭紙鐢�<>鎷捣鏉ョ殑锛� - bool is_option; // 鏄惁涓洪�夐」缁勯儴鍒嗭紙鐢▅鍒嗛殧锛� - char text[32]; // 娈靛唴瀹癸紙涓嶅寘鍚玔]鎴�<>锛� -} Segment; - -//鍖归厤[:MEASure][:VOLTage] -int parse_pattern(const char* pattern, Segment segments[], int max_segments); -bool match_command(const char* command, Segment segments[], int seg_count); +#define MAX_OPTION_LEN 256 +#define MAX_TAGS 10 +#define MAX_INPUT_LEN 256 +int extract_required_options(const char* pattern, char options[MAX_TAGS][MAX_OPTION_LEN][MAX_OPTION_LEN], int max_tags); +int match_input_to_options(const char* input, char options[MAX_TAGS][MAX_OPTION_LEN][MAX_OPTION_LEN], int num_tags); bool test_match(const char* pattern, const char* command); -- Gitblit v1.9.1