code update
This commit is contained in:
@@ -182,13 +182,107 @@ namespace QCL
|
||||
*/
|
||||
bool writeBinary(const std::vector<char> &data, std::ios::openmode mode);
|
||||
};
|
||||
|
||||
// 读文件操作
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
/**
|
||||
* @brief ReadFile 类 - 读文件操作工具类
|
||||
*
|
||||
* 功能:
|
||||
* 1. 读取全文(文本 / 二进制)
|
||||
* 2. 按行读取
|
||||
* 3. 按字节数读取
|
||||
* 4. 获取指定字节序列前的字节数(包含该字节序列)
|
||||
* 5. 检查文件是否存在
|
||||
* 6. 获取文件大小
|
||||
*
|
||||
* 设计:
|
||||
* - 与 WriteFile 类风格保持一致
|
||||
* - 支持文本文件与二进制文件
|
||||
* - 自动关闭文件(析构时)
|
||||
*/
|
||||
class ReadFile
|
||||
{
|
||||
};
|
||||
public:
|
||||
/**
|
||||
* @brief 构造函数
|
||||
* @param filename 文件路径
|
||||
*/
|
||||
explicit ReadFile(const std::string &filename);
|
||||
|
||||
/**
|
||||
* @brief 析构函数,自动关闭文件
|
||||
*/
|
||||
~ReadFile();
|
||||
|
||||
/**
|
||||
* @brief 打开文件(以二进制方式)
|
||||
* @return true 打开成功
|
||||
* @return false 打开失败
|
||||
*/
|
||||
bool Open();
|
||||
|
||||
/**
|
||||
* @brief 关闭文件
|
||||
*/
|
||||
void Close();
|
||||
|
||||
/**
|
||||
* @brief 文件是否已经打开
|
||||
*/
|
||||
bool IsOpen() const;
|
||||
|
||||
/**
|
||||
* @brief 读取全文(文本模式)
|
||||
* @return 文件内容字符串
|
||||
*/
|
||||
std::string ReadAllText();
|
||||
|
||||
/**
|
||||
* @brief 读取全文(二进制模式)
|
||||
* @return 文件内容字节数组
|
||||
*/
|
||||
std::vector<char> ReadAllBinary();
|
||||
|
||||
/**
|
||||
* @brief 按行读取文本
|
||||
* @return 每行作为一个字符串的 vector
|
||||
*/
|
||||
std::vector<std::string> ReadLines();
|
||||
|
||||
/**
|
||||
* @brief 读取指定字节数
|
||||
* @param count 要读取的字节数
|
||||
* @return 实际读取到的字节数据
|
||||
*/
|
||||
std::vector<char> ReadBytes(size_t count);
|
||||
|
||||
/**
|
||||
* @brief 获取指定字节序列前的字节数(包含该字节序列)
|
||||
* @param marker 要查找的字节序列(可能不止一个字节)
|
||||
* @return 如果找到,返回前面部分字节数;找不到返回全文字节数
|
||||
*/
|
||||
size_t GetBytesBefore(const std::string &marker);
|
||||
|
||||
/**
|
||||
* @brief 检查文件是否存在
|
||||
*/
|
||||
bool FileExists() const;
|
||||
|
||||
/**
|
||||
* @brief 获取文件大小(字节数)
|
||||
*/
|
||||
size_t GetFileSize() const;
|
||||
|
||||
/**
|
||||
* @brief 重置读取位置到文件开头
|
||||
*/
|
||||
void Reset();
|
||||
|
||||
private:
|
||||
std::string filename_; // 文件路径
|
||||
std::ifstream file_; // 文件流对象
|
||||
};
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// 屏蔽所有信号
|
||||
void blockAllSignals();
|
||||
}
|
||||
|
105
src/Netra.cpp
105
src/Netra.cpp
@@ -340,6 +340,111 @@ namespace QCL
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
ReadFile::ReadFile(const std::string &filename) : filename_(filename) {}
|
||||
|
||||
ReadFile::~ReadFile()
|
||||
{
|
||||
Close();
|
||||
}
|
||||
|
||||
bool ReadFile::Open()
|
||||
{
|
||||
file_.open(filename_, std::ios::in | std::ios::binary);
|
||||
return file_.is_open();
|
||||
}
|
||||
|
||||
void ReadFile::Close()
|
||||
{
|
||||
if (file_.is_open())
|
||||
file_.close();
|
||||
}
|
||||
|
||||
bool ReadFile::IsOpen() const
|
||||
{
|
||||
return file_.is_open();
|
||||
}
|
||||
|
||||
std::string ReadFile::ReadAllText()
|
||||
{
|
||||
if (!file_.is_open() && !Open())
|
||||
return "";
|
||||
|
||||
std::ostringstream ss;
|
||||
ss << file_.rdbuf();
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
std::vector<char> ReadFile::ReadAllBinary()
|
||||
{
|
||||
if (!file_.is_open() && !Open())
|
||||
return {};
|
||||
|
||||
return ReadBytes(GetFileSize());
|
||||
}
|
||||
|
||||
std::vector<std::string> ReadFile::ReadLines()
|
||||
{
|
||||
if (!file_.is_open() && !Open())
|
||||
return {};
|
||||
|
||||
std::vector<std::string> lines;
|
||||
std::string line;
|
||||
while (std::getline(file_, line))
|
||||
{
|
||||
lines.push_back(line);
|
||||
}
|
||||
return lines;
|
||||
}
|
||||
|
||||
std::vector<char> ReadFile::ReadBytes(size_t count)
|
||||
{
|
||||
if (!file_.is_open() && !Open())
|
||||
return {};
|
||||
|
||||
std::vector<char> buffer(count);
|
||||
file_.read(buffer.data(), count);
|
||||
buffer.resize(file_.gcount()); // 实际读取的字节数
|
||||
return buffer;
|
||||
}
|
||||
|
||||
size_t ReadFile::GetBytesBefore(const std::string &marker)
|
||||
{
|
||||
if (!file_.is_open() && !Open())
|
||||
return 0;
|
||||
|
||||
std::ostringstream ss;
|
||||
ss << file_.rdbuf();
|
||||
std::string content = ss.str();
|
||||
|
||||
size_t pos = content.find(marker);
|
||||
if (pos != std::string::npos)
|
||||
return pos + marker.size();
|
||||
else
|
||||
return content.size();
|
||||
}
|
||||
|
||||
bool ReadFile::FileExists() const
|
||||
{
|
||||
return std::filesystem::exists(filename_);
|
||||
}
|
||||
|
||||
size_t ReadFile::GetFileSize() const
|
||||
{
|
||||
if (!FileExists())
|
||||
return 0;
|
||||
return std::filesystem::file_size(filename_);
|
||||
}
|
||||
|
||||
void ReadFile::Reset()
|
||||
{
|
||||
if (file_.is_open())
|
||||
{
|
||||
file_.clear(); // 清除 EOF 标志
|
||||
file_.seekg(0, std::ios::beg);
|
||||
}
|
||||
}
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// 屏蔽所有信号
|
||||
void blockAllSignals()
|
||||
{
|
||||
|
Reference in New Issue
Block a user