From 5061da0712c43dcd07384e5275b2bcab76e8667e Mon Sep 17 00:00:00 2001
From: nancy.liao <huihui.liao@greentest.com.cn>
Date: 周二, 22 4月 2025 18:59:59 +0800
Subject: [PATCH] 修改通用的版本 现阶段可用[:MEASure][:DC]

---
 libscpi/inc/scpi/types.h    |    2 
 libscpi/src/parser.c        |   19 ++-
 libscpi/src/utils_private.h |   19 +++
 libscpi/src/utils.c         |  232 +++++++++++++++++++++++++++++++++------------
 4 files changed, 202 insertions(+), 70 deletions(-)

diff --git a/libscpi/inc/scpi/types.h b/libscpi/inc/scpi/types.h
index f7dbcf1..45e4d53 100644
--- a/libscpi/inc/scpi/types.h
+++ b/libscpi/inc/scpi/types.h
@@ -472,6 +472,8 @@
         size_t arbitrary_remaining;
 
         int SCPIerror;
+        //澧炲姞浜嗕竴涓懡浠ゅ垪琛ㄧ殑闀垮害 闃叉闈炴硶瓒婄晫
+        int cmdlistSize;
     };
 
 
diff --git a/libscpi/src/parser.c b/libscpi/src/parser.c
index ce17163..2240d5c 100644
--- a/libscpi/src/parser.c
+++ b/libscpi/src/parser.c
@@ -182,14 +182,19 @@
  */
 static scpi_bool_t findCommandHeader(scpi_t * context, const char * header, int len) {
     int32_t i;
-    const scpi_command_t * cmd;
-
-    for (i = 0; context->cmdlist[i].pattern != NULL; i++) {
+    const scpi_command_t * cmd = NULL;
+    for (i = 0; i<context->cmdlistSize; i++) {
         cmd = &context->cmdlist[i];
-        if (matchCommand(cmd->pattern, header, len, NULL, 0, 0)) {
-            context->param_list.cmd = cmd;
+        bool result =test_match(cmd->pattern, header);
+        if(result)
+        {
+             context->param_list.cmd = &context->cmdlist[i];
             return TRUE;
         }
+        // if (matchCommand(cmd->pattern, header, len, NULL, 0, 0)) {
+        //     context->param_list.cmd = cmd;
+        //     return TRUE;
+        // }
     }
     context->SCPIerror = RETURN_NotFind;
     return FALSE;
@@ -316,6 +321,8 @@
     context->buffer.data = input_buffer;
     context->buffer.length = input_buffer_length;
     context->buffer.position = 0;
+
+
     SCPI_ErrorInit(context, error_queue_data, error_queue_size);
 }
 
@@ -355,7 +362,7 @@
  */
 scpi_bool_t SCPI_Input(scpi_t * context, const char * data, int len)
 {
-    scpi_bool_t result = FALSE;
+     scpi_bool_t result = FALSE;
     size_t totcmdlen = 0;
     int cmdlen = 0;
     //褰撻暱搴︿负0鏃�  瑙f瀽褰撳墠鐨勬暟鎹� 骞朵笖娓呯┖context鐨刡uff鏍囪浣�
diff --git a/libscpi/src/utils.c b/libscpi/src/utils.c
index e0aa9a0..bdde1ea 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,116 @@
  */
 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 parse_pattern(const char* pattern, Segment segments[], int max_segments) {
+    int seg_count = 0;
+    const char* p = pattern;
+
+    while (*p && seg_count < max_segments)
+    {
+        if (*p == '[')
+        {
+            // 澶勭悊鍙彉娈�
+            segments[seg_count].is_variable = TRUE;
+            // 璺宠繃'['
+            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
+        {
+            // 澶勭悊鍥哄畾娈�
+            segments[seg_count].is_variable = FALSE;
+
+            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';
+        }
+
+        seg_count++;
+    }
+
+    return seg_count;
+}
+
+// 鍖归厤鍛戒护涓庢ā寮�
+bool match_command(const char* command, Segment segments[], int seg_count)
+{
+    const char* cmd = command;
+    int current_seg = 0;
+
+    while (*cmd && current_seg < seg_count)
+    {
+        // 璺宠繃鍛戒护涓殑鍒嗛殧绗︼紙鍐掑彿锛�
+        if (*cmd == ':')
+        {
+            cmd++;
+            // 妯″紡涓篃搴旇鏈夊搴旂殑鍒嗛殧绗�
+            if (segments[current_seg].text[0] != ':')
+            {
+                return FALSE;
+            }
+            continue;
+        }
+
+        // 鑾峰彇褰撳墠娈甸暱搴�
+        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++;
+            }
+        } else
+        {
+            // 鍥哄畾娈� - 蹇呴』绮剧‘鍖归厤
+            if (strncasecmp(cmd, segments[current_seg].text, seg_len) != 0)
+            {
+                return FALSE;
+            }
+            cmd += seg_len;
+        }
+
+        current_seg++;
+    }
+
+    // 妫�鏌ユ槸鍚﹀鐞嗗畬鎵�鏈夊懡浠ゅ拰鎵�鏈夋
+    return (*cmd == '\0') && (current_seg == seg_count);
+}
+
+// 娴嬭瘯鍑芥暟
+bool test_match(const char* pattern, const char* command)
+{
+    Segment segments[16];
+    int seg_count = parse_pattern(pattern, segments, 16);
+    return (match_command(command, segments, seg_count));
+
+}
+
+
diff --git a/libscpi/src/utils_private.h b/libscpi/src/utils_private.h
index 165e23c..485ba16 100644
--- a/libscpi/src/utils_private.h
+++ b/libscpi/src/utils_private.h
@@ -42,6 +42,14 @@
 #include<scpi/config.h>
 #include<scpi/types.h>
 
+
+#ifdef _WIN32
+#include <string.h>
+#define strncasecmp _strnicmp
+#else
+#include <strings.h>
+#endif
+
 #ifdef	__cplusplus
 extern "C" {
 #endif
@@ -70,6 +78,17 @@
     scpi_bool_t matchCommand(const char * pattern, const char * cmd, size_t len, int32_t *numbers, size_t numbers_len, int32_t default_value) LOCAL;
     scpi_bool_t composeCompoundCommand(const scpi_token_t * prev, scpi_token_t * current) LOCAL;
 
+
+
+    typedef struct {
+        bool is_variable;  // 鏄惁涓哄彲鍙橀儴鍒嗭紙鐢╗]鎷捣鏉ョ殑锛�
+        char text[32];     // 娈靛唴瀹癸紙涓嶅寘鍚玔]锛�
+    } Segment;
+
+    int parse_pattern(const char* pattern, Segment segments[], int max_segments);
+    bool match_command(const char* command, Segment segments[], int seg_count);
+    bool test_match(const char* pattern, const char* command);
+
 #define SCPI_DTOSTRE_UPPERCASE   1
 #define SCPI_DTOSTRE_ALWAYS_SIGN 2
 #define SCPI_DTOSTRE_PLUS_SIGN   4

--
Gitblit v1.9.1