加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
data.hpp 3.41 KB
一键复制 编辑 原始数据 按行查看 历史
刘枭雄 提交于 2022-04-02 16:05 . first version
#ifndef __MY_DATA__
#define __MY_DATA__
#include<unordered_map>
#include"util.hpp"
namespace cloud
{
typedef struct _FileInfo
{
std::string filename;
std::string url_path;
std::string real_path;
size_t file_size;
time_t back_time;
bool pack_flag;
std::string pack_path;
}FileInfo;
class DataManager
{
private:
std::string _backup_file = "./backup.dat";
std::unordered_map<std::string, std::string> _backup_map;
public:
static int Split(const std::string& body, const std::string& sep, std::vector<std::string>* arry)
{
int count = 0;
size_t idx = 0, pos = 0;
while (idx < body.size())
{
pos = body.find(sep, idx);
if (pos == std::string::npos)
{
//找不到分隔符
break;
}
if (pos == idx)
{
//防止有两个连续一起的间隔符情况,相当于检索位置起始就是间隔符
idx = pos + sep.size();
continue;
}
//hi.txt=211314\nhello.txt=432244\n
std::string val = body.substr(idx, pos - idx);
arry->push_back(val);
idx = pos + sep.size(); //让下次的检索间隔符起始位置向后偏移
count++;
}
if (idx < body.size())
{
std::string val = body.substr(idx);//从idx位置截断到末尾
arry->push_back(val);
count++;
}
return count;
}
public:
DataManager()
{
InitLoad();
}
bool Storage()
{
std::vector<std::pair<std::string, std::string>>arry;
this->SelectAll(&arry);
std::stringstream ss;
for (auto& a : arry)
{
//因为两个数据中间使用了间隔符,因此实际上需要对文件名中的特殊字符进行转义编码
ss << a.first << "=" << a.second << "\n";
}
std::cout << "into Storage" << std::endl;
std::cout <<"ss.str()=" <<ss.str() << std::endl;
FileUtil(_backup_file).Write(ss.str());
return true;
}
bool InitLoad()
{
std::string body;
if (FileUtil(_backup_file).Read(&body) == false)
{
return false;
}
//hi.txt=18-324322\nhello.txt=21-3244321\n
std::vector<std::string> arry;
Split(body, "\n", &arry);
for (auto& a : arry)
{
std::vector<std::string> tmp;
Split(a, "=", &tmp);
_backup_map[tmp[0]] = tmp[1];
}
return true;
}
public:
std::string FileEtag(const std::string& pathname)
{
size_t fsize = FileUtil(pathname).Size();
time_t mtime = FileUtil(pathname).MTime();
std::stringstream ss;
ss << fsize << "-" << mtime;
return ss.str();
}
bool Insert(const std::string& pathname)
{
std::string etag = FileEtag(pathname);
_backup_map[pathname] = etag;
Storage();
return true;
}
bool Update(const std::string& pathname)
{
std::string etag = FileEtag(pathname);
_backup_map[pathname] = etag;
Storage();
return true;
}
bool SelectAll(std::vector<std::pair<std::string,std::string>> *infos)
{
for (auto it = _backup_map.begin(); it != _backup_map.end(); it++)
{
std::pair<std::string, std::string>info;
info.first = it->first;
info.second = it->second;
infos->push_back(info);
}
return true;
}
bool SelectOne(const std::string& filename, std::string *etag)
{
auto it = _backup_map.find(filename);
if (it == _backup_map.end())
{
return false;
}
*etag = it->second;
return true;
}
bool Delete(const std::string& filename)
{
auto it = _backup_map.find(filename);
if (it == _backup_map.end())
{
return false;
}
_backup_map.erase(it);
Storage();
return true;
}
};
}
#endif
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化