From f120ccfadac73945890f6dab162aff2b3140a9d6 Mon Sep 17 00:00:00 2001 From: Endlessdelete <1119229550@qq.com> Date: Sun, 26 Oct 2025 08:24:44 +0800 Subject: [PATCH 1/2] Fix, bug: CSV import program incorrectly recognizes CSV files with "\r\n"(CRLF) as line breaks, causing files to not be imported correctly --- scopeprotocols/CSVImportFilter.cpp | 107 +++++++++++++++++++---------- 1 file changed, 72 insertions(+), 35 deletions(-) diff --git a/scopeprotocols/CSVImportFilter.cpp b/scopeprotocols/CSVImportFilter.cpp index d009f2d3..66d29e45 100644 --- a/scopeprotocols/CSVImportFilter.cpp +++ b/scopeprotocols/CSVImportFilter.cpp @@ -69,7 +69,7 @@ string CSVImportFilter::GetProtocolName() void CSVImportFilter::OnFileNameChanged() { - auto fname = m_parameters[m_fpname].ToString(); +auto fname = m_parameters[m_fpname].ToString(); if(fname.empty()) return; @@ -103,6 +103,8 @@ void CSVImportFilter::OnFileNameChanged() if(flen != fread(buf, 1, flen, fp)) { LogError("file read error\n"); + delete[] buf; + fclose(fp); return; } buf[flen] = '\0'; //guarantee null termination at end of file @@ -115,15 +117,16 @@ void CSVImportFilter::OnFileNameChanged() vector names; vector< vector > vcolumns; vector timestamps; - bool digilentFormat; + bool digilentFormat = false; // 初始化变量 size_t nrow = 0; size_t ncols = 0; char* pbuf = buf; char* pend = buf + flen; bool xUnitIsFs = m_parameters[m_xunit].GetIntVal() == Unit::UNIT_FS; - while(true) + + while(pbuf < pend) // 修改循环条件 { - nrow ++; + nrow++; //Stop if at end of file char* pline = pbuf; @@ -131,13 +134,15 @@ void CSVImportFilter::OnFileNameChanged() break; //Find first non-blank character in the current line - while(isspace(*pline)) - pline ++; + while(pline < pend && isspace(*pline) && *pline != '\n' && *pline != '\r') + pline++; - //If it's a newline or nul, the line was blank - discard it - if( (*pline == '\0') || (*pline == '\n') || (*pline == '\r') ) + //If we've reached the end or the line starts with newline characters, it's a blank line - discard it + if(pline >= pend || *pline == '\0' || *pline == '\n' || *pline == '\r') { - pbuf ++; + // 跳过所有连续的换行符 + while(pbuf < pend && (*pbuf == '\n' || *pbuf == '\r')) + pbuf++; continue; } @@ -145,15 +150,26 @@ void CSVImportFilter::OnFileNameChanged() size_t slen = 0; for(; (pline + slen) < pend; slen++) { - if(pline[slen] == '\0') + char current_char = pline[slen]; + if(current_char == '\0') break; - if( (pline[slen] == '\n') || (pline[slen] == '\r') ) + if(current_char == '\n' || current_char == '\r') { - pline[slen] = '\0'; + // 处理 \r\n 序列 + if(current_char == '\r' && (pline + slen + 1) < pend && pline[slen+1] == '\n') + { + pline[slen] = '\0'; + pline[slen+1] = '\0'; + slen++; // 额外跳过 \n + } + else + { + pline[slen] = '\0'; + } break; } } - pbuf += slen; + pbuf += slen + 1; // +1 跳过换行符 //If the line starts with a #, it's a comment. Discard it, but save timestamp metadata if present if(pline[0] == '#') @@ -164,7 +180,6 @@ void CSVImportFilter::OnFileNameChanged() digilentFormat = true; LogTrace("Found Digilent metadata header\n"); } - else if(digilentFormat) { if(s.find("#Date Time: ") == 0) @@ -214,10 +229,12 @@ void CSVImportFilter::OnFileNameChanged() vector headerfields; bool headerRow = false; size_t ncol = 0; - for(size_t i=0; i<=slen; i++) + size_t line_length = strlen(pline); // 使用实际的字符串长度 + + for(size_t i=0; i<=line_length; i++) { //End of field - if( (pline[i] == ',') || (pline[i] == '\n') || (pline[i] == '\r') || (pline[i] == '\0') ) + if(pline[i] == ',' || pline[i] == '\0') { //If this is the first row, check if it's numeric if(names.empty() && timestamps.empty()) @@ -225,14 +242,14 @@ void CSVImportFilter::OnFileNameChanged() //See if it's a header row if(!headerRow) { - for(size_t j=0; pline[j] != '\0'; j++) + for(size_t j=fieldstart; j Date: Sun, 26 Oct 2025 12:17:22 +0800 Subject: [PATCH 2/2] Translate the Chinese content in the file --- scopeprotocols/CSVImportFilter.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/scopeprotocols/CSVImportFilter.cpp b/scopeprotocols/CSVImportFilter.cpp index 66d29e45..2ebcd298 100644 --- a/scopeprotocols/CSVImportFilter.cpp +++ b/scopeprotocols/CSVImportFilter.cpp @@ -117,14 +117,14 @@ auto fname = m_parameters[m_fpname].ToString(); vector names; vector< vector > vcolumns; vector timestamps; - bool digilentFormat = false; // 初始化变量 + bool digilentFormat = false; size_t nrow = 0; size_t ncols = 0; char* pbuf = buf; char* pend = buf + flen; bool xUnitIsFs = m_parameters[m_xunit].GetIntVal() == Unit::UNIT_FS; - while(pbuf < pend) // 修改循环条件 + while(pbuf < pend) // Modify loop stop conditions { nrow++; @@ -140,7 +140,7 @@ auto fname = m_parameters[m_fpname].ToString(); //If we've reached the end or the line starts with newline characters, it's a blank line - discard it if(pline >= pend || *pline == '\0' || *pline == '\n' || *pline == '\r') { - // 跳过所有连续的换行符 + // Skip all consecutive line breaks while(pbuf < pend && (*pbuf == '\n' || *pbuf == '\r')) pbuf++; continue; @@ -155,12 +155,12 @@ auto fname = m_parameters[m_fpname].ToString(); break; if(current_char == '\n' || current_char == '\r') { - // 处理 \r\n 序列 + // Process \r\n sequence if(current_char == '\r' && (pline + slen + 1) < pend && pline[slen+1] == '\n') { pline[slen] = '\0'; pline[slen+1] = '\0'; - slen++; // 额外跳过 \n + slen++; // Extra Skip \n } else { @@ -169,7 +169,7 @@ auto fname = m_parameters[m_fpname].ToString(); break; } } - pbuf += slen + 1; // +1 跳过换行符 + pbuf += slen + 1; // +1 Skip line breaks //If the line starts with a #, it's a comment. Discard it, but save timestamp metadata if present if(pline[0] == '#') @@ -229,7 +229,7 @@ auto fname = m_parameters[m_fpname].ToString(); vector headerfields; bool headerRow = false; size_t ncol = 0; - size_t line_length = strlen(pline); // 使用实际的字符串长度 + size_t line_length = strlen(pline); // Use actual string length for(size_t i=0; i<=line_length; i++) {