From f71f467b68ce6c8dc6c983a2963ec9b131515441 Mon Sep 17 00:00:00 2001
From: chenluhua1980 <Chenluhua@qq.com>
Date: 星期三, 10 十二月 2025 17:55:02 +0800
Subject: [PATCH] 1.实现删除变量的功能;
---
SourceCode/Bond/Servo/HsmsPassive.cpp | 102 +++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 102 insertions(+), 0 deletions(-)
diff --git a/SourceCode/Bond/Servo/HsmsPassive.cpp b/SourceCode/Bond/Servo/HsmsPassive.cpp
index e73919d..3315b32 100644
--- a/SourceCode/Bond/Servo/HsmsPassive.cpp
+++ b/SourceCode/Bond/Servo/HsmsPassive.cpp
@@ -228,6 +228,9 @@
int CHsmsPassive::loadVarialbles(const char* pszFilepath)
{
+ m_strVariableFilepath = pszFilepath;
+ m_bVariableUtf8 = false;
+ m_bVariableUtf8Bom = false;
// 鍏堣鍘熷瀛楄妭锛屽悗缁啀鎸� UTF-8/BOM 鎴栨湰鍦扮紪鐮佽浆鎹�
CFile file;
if (!file.Open(pszFilepath, CFile::modeRead | CFile::shareDenyNone)) {
@@ -251,6 +254,8 @@
// UTF-8 BOM
if (nLen >= 3 && bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF) {
offset = 3;
+ m_bVariableUtf8 = true;
+ m_bVariableUtf8Bom = true;
}
// UTF-16 LE BOM
@@ -281,6 +286,7 @@
MultiByteToWideChar(CP_UTF8, 0, buffer.data() + off,
static_cast<int>(buffer.size() - off), temp.data(), need);
content = temp.c_str();
+ m_bVariableUtf8 = true;
return true;
};
@@ -397,6 +403,102 @@
m_variabels.clear();
}
+CStringA WideToUtf8(const CStringW& ws)
+{
+ int need = WideCharToMultiByte(CP_UTF8, 0, ws, -1, nullptr, 0, nullptr, nullptr);
+ if (need <= 0) return "";
+ CStringA out;
+ LPSTR buf = out.GetBufferSetLength(need - 1);
+ WideCharToMultiByte(CP_UTF8, 0, ws, -1, buf, need, nullptr, nullptr);
+ out.ReleaseBuffer();
+ return out;
+}
+
+CStringA WideToAnsi(const CStringW& ws)
+{
+ int need = WideCharToMultiByte(CP_ACP, 0, ws, -1, nullptr, 0, nullptr, nullptr);
+ if (need <= 0) return "";
+ CStringA out;
+ LPSTR buf = out.GetBufferSetLength(need - 1);
+ WideCharToMultiByte(CP_ACP, 0, ws, -1, buf, need, nullptr, nullptr);
+ out.ReleaseBuffer();
+ return out;
+}
+
+static CStringA AnsiToUtf8(const std::string& s)
+{
+ int wlen = MultiByteToWideChar(CP_ACP, 0, s.c_str(), -1, nullptr, 0);
+ if (wlen <= 0) return "";
+ CStringW ws;
+ LPWSTR wbuf = ws.GetBufferSetLength(wlen - 1);
+ MultiByteToWideChar(CP_ACP, 0, s.c_str(), -1, wbuf, wlen);
+ ws.ReleaseBuffer();
+ return WideToUtf8(ws);
+}
+
+int CHsmsPassive::deleteVariable(int variableId)
+{
+ Lock();
+ auto it = std::find_if(m_variabels.begin(), m_variabels.end(), [=](SERVO::CVariable* v) {
+ return v != nullptr && v->getVarialbleId() == variableId;
+ });
+ if (it == m_variabels.end()) {
+ Unlock();
+ return -1;
+ }
+ delete *it;
+ m_variabels.erase(it);
+ auto filepath = m_strVariableFilepath;
+ Unlock();
+
+ if (filepath.empty()) return -2;
+
+ // 鍐欏洖鏂囦欢锛屼繚鎸佸師缂栫爜锛圲TF-8 鎴栨湰鍦扮紪鐮侊級
+ CFile file;
+ if (!file.Open(filepath.c_str(), CFile::modeCreate | CFile::modeWrite | CFile::shareDenyNone)) {
+ return -3;
+ }
+
+ // header
+ const std::string headerAnsi = "SVID,SV Name,SV Format,SV Remark\r\n";
+ if (m_bVariableUtf8) {
+ if (m_bVariableUtf8Bom) {
+ const BYTE bom[3] = { 0xEF, 0xBB, 0xBF };
+ file.Write(bom, 3);
+ }
+ CStringA header = AnsiToUtf8(headerAnsi);
+ file.Write(header.GetString(), header.GetLength());
+ }
+ else {
+ file.Write(headerAnsi.data(), (UINT)headerAnsi.size());
+ }
+
+ for (auto v : m_variabels) {
+ if (v == nullptr) continue;
+ std::string lineAnsi;
+ lineAnsi.reserve(256);
+ lineAnsi += std::to_string(v->getVarialbleId());
+ lineAnsi.push_back(',');
+ lineAnsi += v->getName();
+ lineAnsi.push_back(',');
+ lineAnsi += SERVO::CVariable::formatToString(v->getFormat());
+ lineAnsi.push_back(',');
+ lineAnsi += v->getRemark();
+ lineAnsi.append("\r\n");
+
+ if (m_bVariableUtf8) {
+ CStringA outLine = AnsiToUtf8(lineAnsi);
+ file.Write(outLine.GetString(), outLine.GetLength());
+ }
+ else {
+ file.Write(lineAnsi.data(), (UINT)lineAnsi.size());
+ }
+ }
+ file.Close();
+
+ return 0;
+}
+
void CHsmsPassive::setVariableValue(const char* pszName, __int64 value)
{
auto v = getVariable(pszName);
--
Gitblit v1.9.3