Message-Queues beta 1.1
A Message-Queues based Cpp
 
载入中...
搜索中...
未找到
helper.hpp
浏览该文件的文档.
1
25#pragma once
26#include <iostream>
27#include <string>
28#include <vector>
29#include <sqlite3.h>
30#include <functional>
31#include <random>
32#include <sstream>
33#include <iomanip>
34#include <atomic>
35#include <sys/stat.h>
36#include <fstream>
37#include <cstdio>
38#include <cstdlib>
39#include <cstring>
40#include <cerrno>
41#include "logger.hpp"
42
43namespace XuMQ
44{
58 {
59 public:
72 typedef int (*SqliteCallback)(void *, int, char **, char **);
79 SqliteHelper(const std::string &dbfile)
80 : _dbfile(dbfile), _handler(nullptr)
81 {
82 }
90 bool open(int safe_level = SQLITE_OPEN_FULLMUTEX)
91 {
92 int ret = sqlite3_open_v2(_dbfile.c_str(), &_handler, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | safe_level, nullptr);
93 if (ret != SQLITE_OK)
94 {
95 error(logger, "打开SQLite数据库失败: %s", sqlite3_errmsg(_handler));
96 return false;
97 }
98 return true;
99 }
109 bool exec(const std::string &sql, SqliteCallback cb, void *arg)
110 {
111 int ret = sqlite3_exec(_handler, sql.c_str(), cb, arg, nullptr);
112 if (ret != SQLITE_OK)
113 {
114 error(logger, "%s--执行语句失败: %s", sql.c_str(), sqlite3_errmsg(_handler));
115 return false;
116 }
117 return true;
118 }
124 void close()
125 {
126 sqlite3_close_v2(_handler);
127 }
128
129 private:
130 std::string _dbfile;
131 sqlite3 *_handler;
132 };
133
141 {
142 public:
152 static size_t split(const std::string &str, const std::string &sep, std::vector<std::string> &result)
153 {
154 // 从0位置查找指定字符的位置
155 // 从上次查找的位置向后继续查找指定位置
156 size_t pos, idx = 0;
157 while (idx < str.size())
158 {
159 pos = str.find(sep, idx);
160 if (pos == std::string::npos) // 没找到
161 {
162 result.push_back(str.substr(idx));
163 return result.size();
164 }
165 if (pos == idx) // 位置相同则数据无效
166 {
167 idx += sep.size();
168 continue;
169 }
170 result.push_back(str.substr(idx, pos - idx));
171 idx = pos + sep.size();
172 }
173 return result.size();
174 }
175 };
184 {
185 public:
194 static std::string uuid()
195 {
196 std::random_device rd;
197 std::mt19937_64 generator(rd()); // 生成机器随机数作为种子 使用梅森旋转算法生成伪随机数
198 std::uniform_int_distribution<int> distribution(0, 255); // 范围取到0~255
199 std::stringstream ss;
200 for (int i = 0; i < 8; i++)
201 {
202 ss << std::setw(2) << std::setfill('0') << std::hex << distribution(generator); // 转为16进制字符串 不满两位自动补0
203 if (i == 3 || i == 5 || i == 7)
204 ss << '-';
205 }
206 static std::atomic<size_t> seq(1); // 定义原子类型整数
207 size_t num = seq.fetch_add(1);
208 for (int i = 7; i >= 0; i--)
209 {
210 ss << std::setw(2) << std::setfill('0') << std::hex << ((num >> i * 8) & 0xff);
211 if (i == 6)
212 ss << '-';
213 }
214 return ss.str();
215 }
216 };
226 {
227 public:
234 FileHelper(const std::string &filename)
235 : _filename(filename)
236 {
237 }
244 bool exists()
245 {
246 struct stat st;
247 return (stat(_filename.c_str(), &st) == 0);
248 }
255 size_t size()
256 {
257 struct stat st;
258 int ret = stat(_filename.c_str(), &st);
259 if (ret < 0)
260 return 0;
261 return st.st_size;
262 }
270 bool read(std::string &body)
271 {
272 size_t fsize = size();
273 body.resize(fsize);
274 return read(&body[0], 0, fsize);
275 }
285 bool read(char *body, size_t offset, size_t len)
286 {
287 // 打开文件
288 std::ifstream ifs(_filename, std::ios::binary | std::ios::in); // 二进制形式打开
289 if (ifs.is_open() == false)
290 {
291 error(logger, "%s:文件打开失败!", _filename.c_str());
292 return false;
293 }
294 // 跳转文件读写位置
295 ifs.seekg(offset, std::ios::beg);
296 // 读取文件数据
297 ifs.read(body, len);
298 if (ifs.good() == false)
299 {
300 error(logger, "%s:文件读取数据失败!", _filename.c_str());
301 ifs.close();
302 return false;
303 }
304 // 关闭文件
305 ifs.close();
306 return true;
307 }
315 bool write(const std::string &body)
316 {
317 return write(body.c_str(), 0, body.size());
318 }
328 bool write(const char *body, size_t offset, size_t len)
329 {
330 // 打开文件
331 std::fstream fs(_filename, std::ios::binary | std::ios::in | std::ios::out); // 二进制形式打开
332 if (fs.is_open() == false)
333 {
334 error(logger, "%s:文件打开失败!", _filename.c_str());
335 return false;
336 }
337 // 跳转文件指定位置
338 fs.seekp(offset, std::ios::beg);
339 // 写入数据
340 fs.write(body, len);
341 if (fs.good() == false)
342 {
343 error(logger, "%s:文件读取数据失败!", _filename.c_str());
344 fs.close();
345 return false;
346 }
347 // 关闭文件
348 return true;
349 }
357 static std::string parentDirectory(const std::string &filename)
358 {
359 size_t pos = filename.find_last_of("/\\");
360 if (pos == std::string::npos)
361 {
362 return ".";
363 }
364 std::string path = filename.substr(0, pos);
365 return path;
366 }
374 bool rename(const std::string &nname)
375 {
376 return (::rename(_filename.c_str(), nname.c_str()) == 0);
377 }
385 static bool createFile(const std::string filename)
386 {
387 std::fstream ofs(filename, std::ios::binary | std::ios::out);
388 if (ofs.is_open() == false)
389 {
390 error(logger, "%s:文件创建失败!", filename.c_str());
391 return false;
392 }
393 ofs.close();
394 return true;
395 }
403 static bool removeFile(const std::string filename)
404 {
405 return (::remove(filename.c_str()) == 0);
406 }
414 static bool createDirectory(const std::string &pathname)
415 {
416 size_t pos = 0, idx = 0; // pos表示'/'的位置,idx表示起始位置
417 while (idx < pathname.size())
418 {
419 pos = pathname.find_first_of("/\\", idx); // 找第一个'/'
420 if (pos == std::string::npos) // 如果没有任何目录,直接创建
421 {
422 return (mkdir(pathname.c_str(), 0775) == 0);
423 }
424 std::string parent_dir = pathname.substr(0, pos + 1); // 找到父级目录
425 if (FileHelper(parent_dir).exists() == true) // 如果存在则直接找下一个
426 {
427 idx = pos + 1;
428 continue;
429 }
430 mkdir(parent_dir.c_str(), 0775);
431 idx = pos + 1;
432 }
433 return true;
434 }
442 static bool removeDirectory(const std::string &pathname)
443 {
444
445#ifdef _WIN32
446 std::string cmd = "rmdir /s /q \"" + pathname + "\"";
447#else
448 std::string cmd = "rm -rf " + pathname;
449#endif
450 return (system(cmd.c_str()) != -1);
451 }
452
453 private:
454 std::string _filename;
455 };
456}
文件操作帮助类
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