LogSystem beta 1.4
This is a logging system project
 
载入中...
搜索中...
未找到
util.hpp
浏览该文件的文档.
1
7#pragma once
8
9#include <iostream>
10#include <ctime>
11#include <sys/stat.h>
12#include <fstream>
13
14namespace Xulog
15{
16 namespace Util
17 {
24 class Date
25 {
31 public:
32 static size_t getTime()
33 {
34 return (size_t)time(nullptr); // 获取当前时间戳
35 }
36 };
43 class File
44 {
45 public:
53 static bool exists(const std::string &pathname)
54 {
55 struct stat st;
56 if (stat(pathname.c_str(), &st) < 0) // 判断文件是否存在
57 return false;
58 return true;
59 }
68 static std::string path(const std::string &pathname)
69 {
70 size_t pos = pathname.find_last_of("/\\"); // 查找最后一个'/'或者'\'
71 if (pos == std::string::npos)
72 return "."; // 如果没有找到路径分隔符,返回当前目录
73 return pathname.substr(0, pos + 1); // 获取文件路径的目录部分
74 }
82 static void createDirectory(const std::string &pathname)
83 {
84 size_t pos = 0, idx = 0; // pos表示'/'的位置,idx表示起始位置
85 while (idx < pathname.size())
86 {
87 pos = pathname.find_first_of("/\\", idx); // 找第一个'/'
88 if (pos == std::string::npos) // 如果没有任何目录,直接创建
89 {
90 mkdir(pathname.c_str(), 0777);
91 return;
92 }
93 std::string parent_dir = pathname.substr(0, pos + 1); // 找到父级目录
94 if (exists(parent_dir) == true) // 如果存在则直接找下一个
95 {
96 idx = pos + 1;
97 continue;
98 }
99 mkdir(parent_dir.c_str(), 0777);
100 idx = pos + 1;
101 }
102 }
110 static bool createFile(const std::string filename)
111 {
112 std::fstream ofs(filename, std::ios::binary | std::ios::out);
113 if (ofs.is_open() == false)
114 {
115 return false;
116 }
117 ofs.close();
118 return true;
119 }
120 };
121 }
122}
时间相关的实用工具类
Definition util.hpp:25
static size_t getTime()
获取当前时间戳
Definition util.hpp:32
文件操作相关的实用工具类
Definition util.hpp:44
static void createDirectory(const std::string &pathname)
创建目录及其父级目录
Definition util.hpp:82
static std::string path(const std::string &pathname)
获取文件路径
Definition util.hpp:68
static bool createFile(const std::string filename)
创建新文件
Definition util.hpp:110
static bool exists(const std::string &pathname)
判断文件是否存在
Definition util.hpp:53
Definition buffer.hpp:12