From 1e139e410744eb791486dcee0ed4b1eebdf9a515 Mon Sep 17 00:00:00 2001
From: nancy.liao <huihui.liao@greentest.com.cn>
Date: 周四, 24 4月 2025 09:32:54 +0800
Subject: [PATCH] 提交修改

---
 libscpi/src/utils.c |  224 ++++++++++++++++++++++++++++++++++++++++----------------
 1 files changed, 160 insertions(+), 64 deletions(-)

diff --git a/libscpi/src/utils.c b/libscpi/src/utils.c
index e0aa9a0..8758917 100644
--- a/libscpi/src/utils.c
+++ b/libscpi/src/utils.c
@@ -40,7 +40,6 @@
 #include <string.h>
 #include <ctype.h>
 #include <math.h>
-
 #include "utils_private.h"
 #include "scpi/utils.h"
 
@@ -91,20 +90,20 @@
     } else {
 
         switch (base) {
-            case 2:
-                x = 0x80000000L;
-                break;
-            case 8:
-                x = 0x40000000L;
-                break;
-            default:
-            case 10:
-                base = 10;
-                x = 1000000000L;
-                break;
-            case 16:
-                x = 0x10000000L;
-                break;
+        case 2:
+            x = 0x80000000L;
+            break;
+        case 8:
+            x = 0x40000000L;
+            break;
+        default:
+        case 10:
+            base = 10;
+            x = 1000000000L;
+            break;
+        case 16:
+            x = 0x10000000L;
+            break;
         }
 
         /* add sign for numbers in base 10 */
@@ -177,20 +176,20 @@
     } else {
 
         switch (base) {
-            case 2:
-                x = 0x8000000000000000ULL;
-                break;
-            case 8:
-                x = 0x8000000000000000ULL;
-                break;
-            default:
-            case 10:
-                x = 10000000000000000000ULL;
-                base = 10;
-                break;
-            case 16:
-                x = 0x1000000000000000ULL;
-                break;
+        case 2:
+            x = 0x8000000000000000ULL;
+            break;
+        case 8:
+            x = 0x8000000000000000ULL;
+            break;
+        default:
+        case 10:
+            x = 10000000000000000000ULL;
+            base = 10;
+            break;
+        case 16:
+            x = 0x1000000000000000ULL;
+            break;
         }
 
         /* add sign for numbers in base 10 */
@@ -484,13 +483,13 @@
         pattern_sep_pos_short = 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);
+               compareStrAndNum(pattern, pattern_sep_pos_short, str, str_len, num);
     } 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);
+               compareStr(pattern, pattern_sep_pos_short, str, str_len);
     }
 }
 
@@ -587,14 +586,14 @@
                 while (pattern_len) {
                     pattern_sep_pos = patternSeparatorPos(pattern_ptr, pattern_len);
                     switch (pattern_ptr[pattern_sep_pos]) {
-                        case '[':
-                            brackets++;
-                            break;
-                        case ']':
-                            brackets--;
-                            break;
-                        default:
-                            break;
+                    case '[':
+                        brackets++;
+                        break;
+                    case ']':
+                        brackets--;
+                        break;
+                    default:
+                        break;
                     }
                     SKIP_PATTERN(pattern_sep_pos + 1);
                     if (brackets == 0) {
@@ -613,29 +612,29 @@
 
             /* both command and patter contains command separator at this position */
             if ((pattern_len > 0)
-                    && ((pattern_ptr[0] == cmd_ptr[0])
+                && ((pattern_ptr[0] == cmd_ptr[0])
                     && (pattern_ptr[0] == ':'))) {
                 SKIP_PATTERN(1);
                 SKIP_CMD(1);
             } else if ((pattern_len > 1)
-                    && (pattern_ptr[1] == cmd_ptr[0])
-                    && (pattern_ptr[0] == '[')
-                    && (pattern_ptr[1] == ':')) {
+                       && (pattern_ptr[1] == cmd_ptr[0])
+                       && (pattern_ptr[0] == '[')
+                       && (pattern_ptr[1] == ':')) {
                 SKIP_PATTERN(2); /* for skip '[' in "[:" */
                 SKIP_CMD(1);
                 brackets++;
             } else if ((pattern_len > 1)
-                    && (pattern_ptr[1] == cmd_ptr[0])
-                    && (pattern_ptr[0] == ']')
-                    && (pattern_ptr[1] == ':')) {
+                       && (pattern_ptr[1] == cmd_ptr[0])
+                       && (pattern_ptr[0] == ']')
+                       && (pattern_ptr[1] == ':')) {
                 SKIP_PATTERN(2); /* for skip ']' in "]:" */
                 SKIP_CMD(1);
                 brackets--;
             } else if ((pattern_len > 2)
-                    && (pattern_ptr[2] == cmd_ptr[0])
-                    && (pattern_ptr[0] == ']')
-                    && (pattern_ptr[1] == '[')
-                    && (pattern_ptr[2] == ':')) {
+                       && (pattern_ptr[2] == cmd_ptr[0])
+                       && (pattern_ptr[0] == ']')
+                       && (pattern_ptr[1] == '[')
+                       && (pattern_ptr[2] == ':')) {
                 SKIP_PATTERN(3); /* for skip '][' in "][:" */
                 SKIP_CMD(1);
                 /* brackets++; */
@@ -650,8 +649,8 @@
                 SKIP_PATTERN(2); /* for skip ']' in "]:" , pattern_ptr continue, while cmd_ptr remain unchanged */
                 brackets--;
             } else if ((pattern_len > 2) && (pattern_ptr[0] == ']')
-                    && (pattern_ptr[1] == '[')
-                    && (pattern_ptr[2] == ':')) {
+                       && (pattern_ptr[1] == '[')
+                       && (pattern_ptr[2] == ':')) {
                 SKIP_PATTERN(3); /* for skip ']' in "][:" , pattern_ptr continue, while cmd_ptr remain unchanged */
                 /* brackets++; */
                 /* brackets--; */
@@ -676,7 +675,7 @@
  * prev and current should be in the same memory buffer
  */
 
- //缁勫悎澶嶅悎SCPI鍛戒护
+//缁勫悎澶嶅悎SCPI鍛戒护
 scpi_bool_t composeCompoundCommand(const scpi_token_t * prev, scpi_token_t * current) {
     size_t i;
 
@@ -1115,7 +1114,7 @@
  */
 uint16_t SCPI_Swap16(uint16_t val) {
     return ((val & 0x00FF) << 8) |
-            ((val & 0xFF00) >> 8);
+           ((val & 0xFF00) >> 8);
 }
 
 /**
@@ -1125,9 +1124,9 @@
  */
 uint32_t SCPI_Swap32(uint32_t val) {
     return ((val & 0x000000FFul) << 24) |
-            ((val & 0x0000FF00ul) << 8) |
-            ((val & 0x00FF0000ul) >> 8) |
-            ((val & 0xFF000000ul) >> 24);
+           ((val & 0x0000FF00ul) << 8) |
+           ((val & 0x00FF0000ul) >> 8) |
+           ((val & 0xFF000000ul) >> 24);
 }
 
 /**
@@ -1137,11 +1136,108 @@
  */
 uint64_t SCPI_Swap64(uint64_t val) {
     return ((val & 0x00000000000000FFull) << 56) |
-            ((val & 0x000000000000FF00ull) << 40) |
-            ((val & 0x0000000000FF0000ull) << 24) |
-            ((val & 0x00000000FF000000ull) << 8) |
-            ((val & 0x000000FF00000000ull) >> 8) |
-            ((val & 0x0000FF0000000000ull) >> 24) |
-            ((val & 0x00FF000000000000ull) >> 40) |
-            ((val & 0xFF00000000000000ull) >> 56);
+           ((val & 0x000000000000FF00ull) << 40) |
+           ((val & 0x0000000000FF0000ull) << 24) |
+           ((val & 0x00000000FF000000ull) << 8) |
+           ((val & 0x000000FF00000000ull) >> 8) |
+           ((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);
 }

--
Gitblit v1.9.1