From da5745dfff362e50f02c85955162a8f1ed79bac0 Mon Sep 17 00:00:00 2001
From: nancy.liao <huihui.liao@greentest.com.cn>
Date: 周五, 25 4月 2025 18:28:58 +0800
Subject: [PATCH] 拓展的SCPI命令解析,尚未合并到工程内

---
 libscpi/inc/scpi/externinterface.h   |  253 ++++++++++++++++++++++++++++++++++++++++++++++++++
 libscpi/inc/scpi/externinterface.cpp |    2 
 2 files changed, 255 insertions(+), 0 deletions(-)

diff --git a/libscpi/inc/scpi/externinterface.cpp b/libscpi/inc/scpi/externinterface.cpp
new file mode 100644
index 0000000..1868dce
--- /dev/null
+++ b/libscpi/inc/scpi/externinterface.cpp
@@ -0,0 +1,2 @@
+#include "externinterface.h"
+
diff --git a/libscpi/inc/scpi/externinterface.h b/libscpi/inc/scpi/externinterface.h
new file mode 100644
index 0000000..6e7c4db
--- /dev/null
+++ b/libscpi/inc/scpi/externinterface.h
@@ -0,0 +1,253 @@
+#ifndef EXTERNINTERFACE_H
+#define EXTERNINTERFACE_H
+#include <iostream>
+#include<vector>
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+namespace SCPIPASER
+{
+class Segment {
+public:
+    //蹇呭~
+    bool is_required = false;
+    //鍙橀噺 [:Measure] 杩欑鐨勪负鍙橀噺 浼氱疆涓篢RUE
+    bool is_variable = false;
+    //鏄惁鏈夊祵濂楀舰寮�
+    bool is_nested = false;
+    // 鍙�夌粍鐨勬暟閲�
+    int variableSize = 0;
+    std::vector<std::string> options;
+    // std::vector<Segment> sub_segments; // 鐢ㄤ簬瀛樺偍宓屽鐨勫彲閫夌粍
+};
+
+class PatternParser
+{
+private:
+    static void trim_whitespace(std::string& str)
+    {
+        auto not_space = [](int ch) { return !std::isspace(ch); };
+        str.erase(str.begin(), std::find_if(str.begin(), str.end(), not_space));
+        str.erase(std::find_if(str.rbegin(), str.rend(), not_space).base(), str.end());
+    }
+
+    static std::vector<std::string> split_options(const std::string& input) {
+        std::vector<std::string> result;
+        size_t start = 0;
+        size_t end = input.find('|');
+
+        while (end != std::string::npos) {
+            std::string token = input.substr(start, end - start);
+            trim_whitespace(token);
+            //token = remove_colon(token); // 绉婚櫎鍐掑彿
+            if (!token.empty()) result.push_back(token);
+            start = end + 1;
+            end = input.find('|', start);
+        }
+
+        std::string last_token = input.substr(start);
+        trim_whitespace(last_token);
+        last_token = remove_colon(last_token); // 绉婚櫎鍐掑彿
+        if (!last_token.empty()) result.push_back(last_token);
+        return result;
+    }
+
+    static std::string remove_colon(const std::string& input) {
+        std::string result;
+        for (char ch : input) {
+            if (ch != ':') {
+                result += ch;
+            }
+        }
+        return result;
+    }
+public:
+    static std::vector<std::string> parse_input_data(const std::string& input)
+    {
+        std::vector<std::string> result;
+        size_t start = 0;
+        size_t end = input.find(':');
+
+        while (end != std::string::npos)
+        {
+            result.push_back(input.substr(start, end - start));
+            start = end + 1;
+            end = input.find(':', start);
+        }
+
+        result.push_back(input.substr(start));
+        return result;
+    }
+    static bool match_all_segments(std::vector<std::string> stringList, std::vector<Segment> vecSegment)
+    {
+        int currentIndex = 0;
+        for (int i =0;i<vecSegment.size();i++)
+        {
+            auto currentSegment = vecSegment[i];
+            //涓ユ牸鍖归厤
+            if (currentSegment.is_required)
+            {
+                for (auto option : currentSegment.options)
+                {
+                    if (stringList[currentIndex] != option)
+                    {
+                        //std::cout << "鍙傛暟鍖归厤澶辫触  " << stringList[currentIndex] << "   option :  " << option;
+                        return false;
+                    }
+                    else
+                    {
+                        //std::cout << "鍙傛暟鍖归厤鎴愬姛 " << stringList[currentIndex] << "   option :  " << option<<std::endl;
+                        currentIndex += 1;
+                    }
+                    break;
+                }
+            }
+            //鍙�夌粍
+            else if (currentSegment.is_variable)
+            {
+                //濡傛灉鏈夊彲閫夌粍鐨勬儏鍐典笅 涓斿弬鏁伴暱搴︿竴鐩� 鍒欏尮閰嶅彲閫夐」鍐呭 鍚﹀垯鐨勮瘽璺宠繃
+                if (stringList.size() == vecSegment.size())
+                {
+                    auto vecTempSegments = currentSegment.options;
+                    for (auto option : vecTempSegments)
+                    {
+                        if (stringList[currentIndex] != option)
+                        {
+                            //std::cout << "鍙傛暟鍖归厤澶辫触  " << stringList[currentIndex] << "   option :  " << option;
+                            return false;
+                        }
+                        else
+                        {
+                            //std::cout << "鍙傛暟鍖归厤鎴愬姛 " << stringList[currentIndex] << "   option :  " << option << std::endl;
+                            currentIndex += 1;
+                        }
+                        break;
+                    }
+                }
+            }
+            //鍙彉鍙�
+            else
+            {
+                //std::cout << "鍙彉鍙傛暟锛� "<< stringList[currentIndex] << " " << currentSegment.options[0] << std::endl;
+                currentIndex += 1;
+            }
+        }
+        return true;
+    }
+
+
+    static std::vector<Segment> extract_all_segments(const std::string& pattern)
+    {
+        std::vector<Segment> segments;
+        size_t pos = 0;
+        const size_t len = pattern.length();
+
+        while (pos < len)
+        {
+            while (pos < len && std::isspace(pattern[pos])) ++pos;
+            if (pos >= len) break;
+
+            if (pattern[pos] == '<') {
+                size_t end_pos = pattern.find('>', pos);
+                if (end_pos == std::string::npos) break;
+
+                std::string content = pattern.substr(pos + 1, end_pos - pos - 1);
+                trim_whitespace(content);
+
+                Segment segment;
+                segment.is_variable = false; // `<...>` 涓嶈瑙嗕负鍙橀噺
+                segment.options = split_options(content);
+
+                // 妫�鏌ユ槸鍚﹀寘鍚� `:`
+                if (content.find(':') != std::string::npos) {
+                    segment.is_required = true; // 濡傛灉鍖呭惈 `:`
+                }
+                else {
+                    segment.is_required = false; // 濡傛灉涓嶅寘鍚� `:`
+                }
+
+                segments.push_back(segment);
+                pos = end_pos + 1;
+            }
+            else if (pattern[pos] == '[')
+            {
+                size_t end_pos = pattern.find(']', pos);
+                if (end_pos == std::string::npos) break;
+
+                std::string content = pattern.substr(pos + 1, end_pos - pos - 1);
+                trim_whitespace(content);
+
+                Segment segment;
+                segment.variableSize += 1;
+                segment.is_required = false;
+                segment.is_variable = true;
+
+                segment.options = split_options(content);
+
+                segments.push_back(segment);
+                pos = end_pos + 1;
+            }
+            else
+            {
+                size_t next_lt = pattern.find('<', pos);
+                size_t next_lb = pattern.find('[', pos);
+                size_t next_special = std::min(next_lt, next_lb);
+
+                std::string content;
+                if (next_special != std::string::npos)
+                {
+                    content = pattern.substr(pos, next_special - pos);
+                }
+                else
+                {
+                    content = pattern.substr(pos);
+                }
+                trim_whitespace(content);
+
+                if (!content.empty())
+                {
+                    // 鐢� ':' 鎷嗗垎鍙傛暟
+                    size_t start = 0;
+                    size_t end = content.find(':');
+                    while (end != std::string::npos)
+                    {
+                        std::string token = content.substr(start, end - start);
+                        trim_whitespace(token);
+
+                        if (!token.empty())
+                        {
+                            Segment segment;
+                            segment.is_required = true; // 鏄庣‘鏍囪涓哄繀濉」
+                            segment.is_variable = false; // 鏅�氭枃鏈笉瑙嗕负鍙橀噺
+                            segment.options.push_back(token);
+                            segments.push_back(segment);
+                        }
+                        start = end + 1;
+                        end = content.find(':', start);
+                    }
+                    // 澶勭悊鏈�鍚庝竴涓儴鍒�
+                    std::string last_token = content.substr(start);
+                    trim_whitespace(last_token);
+                    if (!last_token.empty())
+                    {
+                        Segment segment;
+                        segment.is_required = true;
+                        segment.is_variable = false;
+                        segment.options.push_back(last_token);
+                        segments.push_back(segment);
+                    }
+                }
+                pos = (next_special != std::string::npos) ? next_special : len;
+            }
+        }
+
+        return segments;
+    }
+
+};
+}
+#ifdef __cplusplus
+}
+#endif
+#endif // EXTERNINTERFACE_H

--
Gitblit v1.9.1