90 bool open(
int safe_level = SQLITE_OPEN_FULLMUTEX)
92 int ret = sqlite3_open_v2(
_dbfile.c_str(), &
_handler, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | safe_level,
nullptr);
111 int ret = sqlite3_exec(
_handler, sql.c_str(), cb, arg,
nullptr);
112 if (ret != SQLITE_OK)
114 error(
logger,
"%s--执行语句失败: %s", sql.c_str(), sqlite3_errmsg(
_handler));
152 static size_t split(
const std::string &str,
const std::string &sep, std::vector<std::string> &result)
157 while (idx < str.size())
159 pos = str.find(sep, idx);
160 if (pos == std::string::npos)
162 result.push_back(str.substr(idx));
163 return result.size();
170 result.push_back(str.substr(idx, pos - idx));
171 idx = pos + sep.size();
173 return result.size();
196 std::random_device rd;
197 std::mt19937_64 generator(rd());
198 std::uniform_int_distribution<int> distribution(0, 255);
199 std::stringstream ss;
200 for (
int i = 0; i < 8; i++)
202 ss << std::setw(2) << std::setfill(
'0') << std::hex << distribution(generator);
203 if (i == 3 || i == 5 || i == 7)
206 static std::atomic<size_t> seq(1);
207 size_t num = seq.fetch_add(1);
208 for (
int i = 7; i >= 0; i--)
210 ss << std::setw(2) << std::setfill(
'0') << std::hex << ((num >> i * 8) & 0xff);
247 return (stat(
_filename.c_str(), &st) == 0);
272 size_t fsize =
size();
274 return read(&body[0], 0, fsize);
285 bool read(
char *body,
size_t offset,
size_t len)
288 std::ifstream ifs(
_filename, std::ios::binary | std::ios::in);
289 if (ifs.is_open() ==
false)
295 ifs.seekg(offset, std::ios::beg);
298 if (ifs.good() ==
false)
317 return write(body.c_str(), 0, body.size());
328 bool write(
const char *body,
size_t offset,
size_t len)
331 std::fstream fs(
_filename, std::ios::binary | std::ios::in | std::ios::out);
332 if (fs.is_open() ==
false)
338 fs.seekp(offset, std::ios::beg);
341 if (fs.good() ==
false)
359 size_t pos = filename.find_last_of(
"/\\");
360 if (pos == std::string::npos)
364 std::string path = filename.substr(0, pos);
387 std::fstream ofs(filename, std::ios::binary | std::ios::out);
388 if (ofs.is_open() ==
false)
390 error(
logger,
"%s:文件创建失败!", filename.c_str());
405 return (::remove(filename.c_str()) == 0);
416 size_t pos = 0, idx = 0;
417 while (idx < pathname.size())
419 pos = pathname.find_first_of(
"/\\", idx);
420 if (pos == std::string::npos)
422 return (mkdir(pathname.c_str(), 0775) == 0);
424 std::string parent_dir = pathname.substr(0, pos + 1);
430 mkdir(parent_dir.c_str(), 0775);
446 std::string cmd =
"rmdir /s /q \"" + pathname +
"\"";
448 std::string cmd =
"rm -rf " + pathname;
450 return (system(cmd.c_str()) != -1);
文件操作帮助类
Definition helper.hpp:226
static bool removeFile(const std::string filename)
删除文件
Definition helper.hpp:403
static bool removeDirectory(const std::string &pathname)
删除目录
Definition helper.hpp:442
std::string _filename
操作的文件名
Definition helper.hpp:454
static bool createDirectory(const std::string &pathname)
创建目录
Definition helper.hpp:414
static std::string parentDirectory(const std::string &filename)
获取文件的父目录
Definition helper.hpp:357
static bool createFile(const std::string filename)
创建新文件
Definition helper.hpp:385
bool rename(const std::string &nname)
重命名文件
Definition helper.hpp:374
size_t size()
获取文件大小
Definition helper.hpp:255
bool exists()
检查文件是否存在
Definition helper.hpp:244
bool read(char *body, size_t offset, size_t len)
从指定位置读取文件内容
Definition helper.hpp:285
bool write(const std::string &body)
写入字符串到文件
Definition helper.hpp:315
bool write(const char *body, size_t offset, size_t len)
从指定位置写入数据到文件
Definition helper.hpp:328
bool read(std::string &body)
读取文件内容
Definition helper.hpp:270
FileHelper(const std::string &filename)
构造函数
Definition helper.hpp:234
SQLite 数据库操作助手类
Definition helper.hpp:58
sqlite3 * _handler
SQLite 数据库句柄
Definition helper.hpp:131
SqliteHelper(const std::string &dbfile)
SqliteHelper 构造函数
Definition helper.hpp:79
bool exec(const std::string &sql, SqliteCallback cb, void *arg)
执行 SQL 语句
Definition helper.hpp:109
std::string _dbfile
数据库文件路径
Definition helper.hpp:130
void close()
关闭数据库
Definition helper.hpp:124
int(* SqliteCallback)(void *, int, char **, char **)
SQLite 回调函数类型
Definition helper.hpp:72
bool open(int safe_level=SQLITE_OPEN_FULLMUTEX)
打开数据库
Definition helper.hpp:90
字符串处理助手类
Definition helper.hpp:141
static size_t split(const std::string &str, const std::string &sep, std::vector< std::string > &result)
将字符串分割为多个子字符串
Definition helper.hpp:152
提供生成 UUID 的工具类。
Definition helper.hpp:184
static std::string uuid()
生成一个随机 UUID。
Definition helper.hpp:194
Definition channel.hpp:22
Xulog::Logger::ptr logger
日志器的智能指针类型
Definition logger.hpp:24