update
This commit is contained in:
@@ -260,6 +260,14 @@ namespace QCL
|
||||
*/
|
||||
size_t GetBytesBefore(const std::string &marker, bool includeMarker = false);
|
||||
|
||||
/**
|
||||
* @brief 从指定位置读取指定字节数,默认读取到文件末尾
|
||||
* @param pos 起始位置(字节偏移)
|
||||
* @param count 要读取的字节数,默认为0表示读取到文件末尾
|
||||
* @return 读取到的字节数据
|
||||
*/
|
||||
std::vector<char> ReadBytesFrom(size_t pos, size_t count = 0);
|
||||
|
||||
/**
|
||||
* @brief 检查文件是否存在
|
||||
*/
|
||||
@@ -284,4 +292,14 @@ namespace QCL
|
||||
|
||||
// 屏蔽所有信号
|
||||
void blockAllSignals();
|
||||
|
||||
// 去除字符串的左空格
|
||||
std::string Ltrim(const std::string &s);
|
||||
|
||||
// 去除字符串右侧的空格
|
||||
std::string Rtrim(const std::string &s);
|
||||
|
||||
// 去除字符串左右两侧的空格
|
||||
std::string LRtrim(const std::string &s);
|
||||
|
||||
}
|
||||
|
@@ -466,15 +466,42 @@ namespace QCL
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool ReadFile::FileExists() const
|
||||
std::vector<char> ReadFile::ReadBytesFrom(size_t pos, size_t count)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mtx_);
|
||||
|
||||
if (!file_.is_open() && !Open())
|
||||
return {};
|
||||
|
||||
size_t filesize = GetFileSize();
|
||||
if (pos >= filesize)
|
||||
return {}; // 起始位置超出文件大小
|
||||
|
||||
file_.clear(); // 清除 EOF 和错误状态
|
||||
file_.seekg(pos, std::ios::beg);
|
||||
if (!file_)
|
||||
return {};
|
||||
|
||||
size_t bytes_to_read = count;
|
||||
if (count == 0 || pos + count > filesize)
|
||||
bytes_to_read = filesize - pos; // 读取到文件末尾
|
||||
|
||||
std::vector<char> buffer(bytes_to_read);
|
||||
file_.read(buffer.data(), bytes_to_read);
|
||||
|
||||
// 实际读取的字节数可能少于请求的数量
|
||||
buffer.resize(file_.gcount());
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
bool ReadFile::FileExists() const
|
||||
{
|
||||
return std::filesystem::exists(filename_);
|
||||
}
|
||||
|
||||
size_t ReadFile::GetFileSize() const
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mtx_);
|
||||
if (!FileExists())
|
||||
return 0;
|
||||
return std::filesystem::file_size(filename_);
|
||||
@@ -498,5 +525,33 @@ namespace QCL
|
||||
for (int ii = 1; ii <= 64; ii++)
|
||||
signal(ii, SIG_IGN);
|
||||
}
|
||||
|
||||
std::string Ltrim(const std::string &s)
|
||||
{
|
||||
size_t start = 0;
|
||||
while (start < s.size() && std::isspace(static_cast<unsigned char>(s[start])))
|
||||
{
|
||||
++start;
|
||||
}
|
||||
return s.substr(start);
|
||||
}
|
||||
|
||||
std::string Rtrim(const std::string &s)
|
||||
{
|
||||
if (s.empty())
|
||||
return s;
|
||||
|
||||
size_t end = s.size();
|
||||
while (end > 0 && std::isspace(static_cast<unsigned char>(s[end - 1])))
|
||||
{
|
||||
--end;
|
||||
}
|
||||
return s.substr(0, end);
|
||||
}
|
||||
|
||||
std::string LRtrim(const std::string &s)
|
||||
{
|
||||
return Ltrim(Rtrim(s));
|
||||
}
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
}
|
Reference in New Issue
Block a user