update
This commit is contained in:
@@ -150,15 +150,12 @@ namespace QCL
|
|||||||
bool appendBinary(const std::vector<char> &data);
|
bool appendBinary(const std::vector<char> &data);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 计算第一个指定字节序列前的字节数(包含该字节序列本身)
|
* @brief 计算第一个指定字节序列前的字节数
|
||||||
*
|
* @param pattern 要查找的字节序列
|
||||||
* 例如:文件内容是 "ABC***--***XYZ",模式是 "***--***"
|
* @param includePattern true 表示返回值包含 pattern 自身长度,false 表示不包含
|
||||||
* 返回值应为 11("ABC"=3字节 + "***--***"=8字节)。
|
* @return size_t 字节数,如果文件没打开或 pattern 为空则返回 0
|
||||||
*
|
|
||||||
* @param pattern 要查找的字节模式(支持多字节)
|
|
||||||
* @return long 字节数(包含匹配模式本身),未找到返回 -1
|
|
||||||
*/
|
*/
|
||||||
long countBytesBeforePattern(const std::string &pattern);
|
size_t countBytesBeforePattern(const std::string &pattern, bool includePattern = false);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::string filePath_; ///< 文件路径
|
std::string filePath_; ///< 文件路径
|
||||||
@@ -259,9 +256,9 @@ namespace QCL
|
|||||||
/**
|
/**
|
||||||
* @brief 获取指定字节序列前的字节数(包含该字节序列)
|
* @brief 获取指定字节序列前的字节数(包含该字节序列)
|
||||||
* @param marker 要查找的字节序列(可能不止一个字节)
|
* @param marker 要查找的字节序列(可能不止一个字节)
|
||||||
* @return 如果找到,返回前面部分字节数;找不到返回全文字节数
|
* @return 如果找到,返回前面部分字节数;找不到返回0
|
||||||
*/
|
*/
|
||||||
size_t GetBytesBefore(const std::string &marker);
|
size_t GetBytesBefore(const std::string &marker, bool includeMarker = false);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 检查文件是否存在
|
* @brief 检查文件是否存在
|
||||||
|
@@ -306,37 +306,44 @@ namespace QCL
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
size_t WriteFile::countBytesBeforePattern(const std::string &pattern, bool includePattern)
|
||||||
* @brief 计算第一个指定字节序列前的字节数(包含该字节序列本身)
|
|
||||||
*/
|
|
||||||
long WriteFile::countBytesBeforePattern(const std::string &pattern)
|
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(writeMutex_);
|
std::lock_guard<std::mutex> lock(writeMutex_);
|
||||||
|
|
||||||
if (pattern.empty())
|
if (pattern.empty())
|
||||||
return -1;
|
return 0;
|
||||||
|
|
||||||
std::ifstream file(filePath_, std::ios::binary); // 二进制模式防止编码干扰
|
std::ifstream file(filePath_, std::ios::binary);
|
||||||
if (!file.is_open())
|
if (!file.is_open())
|
||||||
return -1;
|
return 0;
|
||||||
|
|
||||||
// 将整个文件读入内存
|
file.clear(); // 清除EOF和错误状态
|
||||||
std::vector<char> buffer((std::istreambuf_iterator<char>(file)),
|
file.seekg(0, std::ios::beg); // 回到文件开头
|
||||||
std::istreambuf_iterator<char>());
|
|
||||||
file.close();
|
|
||||||
|
|
||||||
// 在 buffer 中查找 pattern
|
const size_t chunkSize = 4096;
|
||||||
auto it = std::search(buffer.begin(), buffer.end(),
|
std::string buffer;
|
||||||
pattern.begin(), pattern.end());
|
buffer.reserve(chunkSize * 2);
|
||||||
|
|
||||||
if (it == buffer.end())
|
size_t totalRead = 0;
|
||||||
|
char chunk[chunkSize];
|
||||||
|
|
||||||
|
while (file.read(chunk, chunkSize) || file.gcount() > 0)
|
||||||
{
|
{
|
||||||
return -1; // 没找到
|
buffer.append(chunk, file.gcount());
|
||||||
|
size_t pos = buffer.find(pattern);
|
||||||
|
if (pos != std::string::npos)
|
||||||
|
{
|
||||||
|
return includePattern ? (pos + pattern.size()) : pos;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 计算从开头到 pattern 结束的字节数
|
// 保留末尾部分,避免 buffer 无限增长
|
||||||
size_t pos = std::distance(buffer.begin(), it);
|
if (buffer.size() > pattern.size())
|
||||||
return static_cast<long>(pos + pattern.size());
|
buffer.erase(0, buffer.size() - pattern.size());
|
||||||
|
|
||||||
|
totalRead += file.gcount();
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0; // 没找到 pattern,返回0
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
@@ -419,21 +426,44 @@ namespace QCL
|
|||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t ReadFile::GetBytesBefore(const std::string &marker)
|
size_t ReadFile::GetBytesBefore(const std::string &marker, bool includeMarker)
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(mtx_);
|
std::lock_guard<std::mutex> lock(mtx_);
|
||||||
|
|
||||||
if (!file_.is_open() && !Open())
|
if (!file_.is_open() && !Open())
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
std::ostringstream ss;
|
file_.clear(); // 清除EOF和错误状态
|
||||||
ss << file_.rdbuf();
|
file_.seekg(0, std::ios::beg); // 回到文件开头
|
||||||
std::string content = ss.str();
|
|
||||||
|
|
||||||
size_t pos = content.find(marker);
|
const size_t chunkSize = 4096;
|
||||||
|
std::string buffer;
|
||||||
|
buffer.reserve(chunkSize * 2);
|
||||||
|
|
||||||
|
size_t totalRead = 0;
|
||||||
|
char chunk[chunkSize];
|
||||||
|
|
||||||
|
while (file_.read(chunk, chunkSize) || file_.gcount() > 0)
|
||||||
|
{
|
||||||
|
buffer.append(chunk, file_.gcount());
|
||||||
|
size_t pos = buffer.find(marker);
|
||||||
if (pos != std::string::npos)
|
if (pos != std::string::npos)
|
||||||
|
{
|
||||||
|
// 如果 includeMarker 为 true,返回包含 marker 的长度
|
||||||
|
if (includeMarker)
|
||||||
return pos + marker.size();
|
return pos + marker.size();
|
||||||
else
|
else
|
||||||
return content.size();
|
return pos;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 保留末尾部分,避免 buffer 无限增长
|
||||||
|
if (buffer.size() > marker.size())
|
||||||
|
buffer.erase(0, buffer.size() - marker.size());
|
||||||
|
|
||||||
|
totalRead += file_.gcount();
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ReadFile::FileExists() const
|
bool ReadFile::FileExists() const
|
||||||
|
Reference in New Issue
Block a user